Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/devtools/devtools_adb_bridge.h

Issue 26568004: Introduced AndroidDeviceProvider to simplify testing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added files to changelist. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ 5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ 6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/devtools/adb/android_usb_device.h" 14 #include "chrome/browser/devtools/adb/android_usb_device.h"
15 #include "chrome/browser/devtools/devtools_device_provider.h"
15 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" 16 #include "components/browser_context_keyed_service/browser_context_keyed_service .h"
16 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" 17 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "net/socket/tcp_client_socket.h" 19 #include "net/socket/tcp_client_socket.h"
19 #include "ui/gfx/size.h" 20 #include "ui/gfx/size.h"
20 21
21 template<typename T> struct DefaultSingletonTraits; 22 template<typename T> struct DefaultSingletonTraits;
22 23
23 namespace base { 24 namespace base {
24 class MessageLoop; 25 class MessageLoop;
25 class DictionaryValue; 26 class DictionaryValue;
26 class Thread; 27 class Thread;
27 } 28 }
28 29
29 namespace content { 30 namespace content {
30 class BrowserContext; 31 class BrowserContext;
31 } 32 }
32 33
33 namespace crypto { 34 namespace crypto {
34 class RSAPrivateKey; 35 class RSAPrivateKey;
35 } 36 }
36 37
37 class Profile; 38 class Profile;
38 39
39 // The format used for constructing DevTools server socket names. 40 // The format used for constructing DevTools server socket names.
40 extern const char kDevToolsChannelNameFormat[]; 41 extern const char kDevToolsChannelNameFormat[];
41 42
42 typedef base::Callback<void(int, const std::string&)> CommandCallback;
43 typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
44
45 class DevToolsAdbBridge 43 class DevToolsAdbBridge
46 : public base::RefCountedThreadSafe< 44 : public base::RefCountedThreadSafe<
47 DevToolsAdbBridge, 45 DevToolsAdbBridge,
48 content::BrowserThread::DeleteOnUIThread> { 46 content::BrowserThread::DeleteOnUIThread> {
49 public: 47 public:
50 typedef base::Callback<void(int result, 48 typedef base::Callback<void(int result,
51 const std::string& response)> Callback; 49 const std::string& response)> Callback;
52 50
51 typedef std::vector<scoped_refptr<DevToolsDeviceProvider>> DeviceProviders;
52
53 class Wrapper : public BrowserContextKeyedService { 53 class Wrapper : public BrowserContextKeyedService {
54 public: 54 public:
55 explicit Wrapper(Profile* profile); 55 explicit Wrapper(Profile* profile, const DeviceProviders& device_providers);
56 virtual ~Wrapper(); 56 virtual ~Wrapper();
57 57
58 DevToolsAdbBridge* Get(); 58 DevToolsAdbBridge* Get();
59 private: 59 private:
60 scoped_refptr<DevToolsAdbBridge> bridge_; 60 scoped_refptr<DevToolsAdbBridge> bridge_;
61 }; 61 };
62 62
63 class Factory : public BrowserContextKeyedServiceFactory { 63 class Factory : public BrowserContextKeyedServiceFactory {
64 public: 64 public:
65 // Returns singleton instance of DevToolsAdbBridge. 65 // Returns singleton instance of DevToolsAdbBridge.
66 static Factory* GetInstance(); 66 static Factory* GetInstance();
67 67
68 // Returns DevToolsAdbBridge associated with |profile|. 68 // Returns DevToolsAdbBridge associated with |profile|.
69 static DevToolsAdbBridge* GetForProfile(Profile* profile); 69 static DevToolsAdbBridge* GetForProfile(Profile* profile);
70 70
71 private: 71 private:
72 friend struct DefaultSingletonTraits<Factory>; 72 friend struct DefaultSingletonTraits<Factory>;
73 friend class DevToolsAdbBridge;
74 73
75 Factory(); 74 Factory();
76 virtual ~Factory(); 75 virtual ~Factory();
77 76
78 // BrowserContextKeyedServiceFactory overrides: 77 // BrowserContextKeyedServiceFactory overrides:
79 virtual BrowserContextKeyedService* BuildServiceInstanceFor( 78 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
80 content::BrowserContext* context) const OVERRIDE; 79 content::BrowserContext* context) const OVERRIDE;
81 DISALLOW_COPY_AND_ASSIGN(Factory); 80 DISALLOW_COPY_AND_ASSIGN(Factory);
82 }; 81 };
83 82
84 class AndroidDevice;
85
86 class RemotePage : public base::RefCounted<RemotePage> { 83 class RemotePage : public base::RefCounted<RemotePage> {
87 public: 84 public:
88 RemotePage(scoped_refptr<DevToolsAdbBridge> bridge, 85 RemotePage(scoped_refptr<RefCountedAdbThread> adb_thread,
Vladislav Kaznacheev 2013/10/16 11:27:04 This patch is obviously not rebased on the latest
Dmitry Zvorygin 2013/10/17 16:19:37 Done.
89 scoped_refptr<AndroidDevice> device, 86 scoped_refptr<AndroidDevice> device,
90 const std::string& socket, 87 const std::string& socket,
91 const base::DictionaryValue& value); 88 const base::DictionaryValue& value);
92 89
93 std::string id() { return id_; } 90 std::string id() { return id_; }
94 std::string url() { return url_; } 91 std::string url() { return url_; }
95 std::string title() { return title_; } 92 std::string title() { return title_; }
96 std::string description() { return description_; } 93 std::string description() { return description_; }
97 std::string favicon_url() { return favicon_url_; } 94 std::string favicon_url() { return favicon_url_; }
98 bool attached() { return debug_url_.empty(); } 95 bool attached() { return debug_url_.empty(); }
(...skipping 10 matching lines...) Expand all
109 friend class base::RefCounted<RemotePage>; 106 friend class base::RefCounted<RemotePage>;
110 virtual ~RemotePage(); 107 virtual ~RemotePage();
111 108
112 void RequestActivate(const CommandCallback& callback); 109 void RequestActivate(const CommandCallback& callback);
113 110
114 void InspectOnHandlerThread( 111 void InspectOnHandlerThread(
115 Profile* profile, int result, const std::string& response); 112 Profile* profile, int result, const std::string& response);
116 113
117 void InspectOnUIThread(Profile* profile); 114 void InspectOnUIThread(Profile* profile);
118 115
119 scoped_refptr<DevToolsAdbBridge> bridge_; 116 scoped_refptr<RefCountedAdbThread> adb_thread_;
120 scoped_refptr<AndroidDevice> device_; 117 scoped_refptr<AndroidDevice> device_;
121 std::string socket_; 118 std::string socket_;
122 std::string id_; 119 std::string id_;
123 std::string url_; 120 std::string url_;
124 std::string title_; 121 std::string title_;
125 std::string description_; 122 std::string description_;
126 std::string favicon_url_; 123 std::string favicon_url_;
127 std::string debug_url_; 124 std::string debug_url_;
128 std::string frontend_url_; 125 std::string frontend_url_;
129 DISALLOW_COPY_AND_ASSIGN(RemotePage); 126 DISALLOW_COPY_AND_ASSIGN(RemotePage);
130 }; 127 };
131 128
132 typedef std::vector<scoped_refptr<RemotePage> > RemotePages; 129 typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
133 130
134 class RemoteBrowser : public base::RefCounted<RemoteBrowser> { 131 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
135 public: 132 public:
136 RemoteBrowser(scoped_refptr<DevToolsAdbBridge> bridge, 133 RemoteBrowser(
137 scoped_refptr<AndroidDevice> device, 134 scoped_refptr<RefCountedAdbThread> adb_thread,
138 const std::string& socket); 135 scoped_refptr<AndroidDevice> device,
136 const std::string& socket);
139 137
140 scoped_refptr<AndroidDevice> device() { return device_; } 138 scoped_refptr<AndroidDevice> device() { return device_; }
141 std::string socket() { return socket_; } 139 std::string socket() { return socket_; }
142 140
143 std::string product() { return product_; } 141 std::string product() { return product_; }
144 void set_product(const std::string& product) { product_ = product; } 142 void set_product(const std::string& product) { product_ = product; }
145 std::string version() { return version_; } 143 std::string version() { return version_; }
146 void set_version(const std::string& version) { version_ = version; } 144 void set_version(const std::string& version) { version_ = version; }
147 std::string pid() { return pid_; } 145 std::string pid() { return pid_; }
148 void set_pid(const std::string& pid) { pid_ = pid; } 146 void set_pid(const std::string& pid) { pid_ = pid; }
149 std::string package() { return package_; } 147 std::string package() { return package_; }
150 void set_package(const std::string& package) { package_ = package; } 148 void set_package(const std::string& package) { package_ = package; }
151 149
152 RemotePages& pages() { return pages_; } 150 RemotePages& pages() { return pages_; }
153 void AddPage(scoped_refptr<RemotePage> page) { pages_.push_back(page); } 151 void AddPage(scoped_refptr<RemotePage> page) { pages_.push_back(page); }
154 152
155 void Open(const std::string& url); 153 void Open(const std::string& url);
156 154
157 private: 155 private:
158 friend class base::RefCounted<RemoteBrowser>; 156 friend class base::RefCounted<RemoteBrowser>;
159 virtual ~RemoteBrowser(); 157 virtual ~RemoteBrowser();
160 158
161 void PageCreatedOnHandlerThread( 159 void PageCreatedOnHandlerThread(
162 const std::string& url, int result, const std::string& response); 160 const std::string& url, int result, const std::string& response);
163 161
164 void PageCreatedOnUIThread( 162 void PageCreatedOnUIThread(
165 const std::string& response, const std::string& url); 163 const std::string& response, const std::string& url);
166 164
167 scoped_refptr<DevToolsAdbBridge> bridge_; 165 scoped_refptr<RefCountedAdbThread> adb_thread_;
168 scoped_refptr<AndroidDevice> device_; 166 scoped_refptr<AndroidDevice> device_;
169 const std::string socket_; 167 const std::string socket_;
170 std::string product_; 168 std::string product_;
171 std::string version_; 169 std::string version_;
172 std::string pid_; 170 std::string pid_;
173 std::string package_; 171 std::string package_;
174 RemotePages pages_; 172 RemotePages pages_;
175 173
176 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser); 174 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
177 }; 175 };
178 176
179 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers; 177 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
180 178
181 class RemoteDevice : public base::RefCounted<RemoteDevice> { 179 class RemoteDevice : public base::RefCounted<RemoteDevice> {
182 public: 180 public:
183 explicit RemoteDevice(scoped_refptr<DevToolsAdbBridge> bridge, 181 explicit RemoteDevice(scoped_refptr<AndroidDevice> device);
184 scoped_refptr<AndroidDevice> device);
185 182
186 scoped_refptr<AndroidDevice> device() { return device_; } 183 scoped_refptr<AndroidDevice> device() { return device_; }
187 std::string serial() { return device_->serial(); } 184 std::string serial() { return device_->serial(); }
188 std::string model() { return device_->model(); } 185 std::string model() { return device_->model(); }
189 186
190 RemoteBrowsers& browsers() { return browsers_; } 187 RemoteBrowsers& browsers() { return browsers_; }
191 void AddBrowser(scoped_refptr<RemoteBrowser> browser) { 188 void AddBrowser(scoped_refptr<RemoteBrowser> browser) {
192 browsers_.push_back(browser); 189 browsers_.push_back(browser);
193 } 190 }
194 191
195 gfx::Size GetScreenSize() { return screen_size_; } 192 gfx::Size GetScreenSize() { return screen_size_; }
196 void SetScreenSize(const gfx::Size& size) { screen_size_ = size; } 193 void SetScreenSize(const gfx::Size& size) { screen_size_ = size; }
197 194
198 private: 195 private:
199 friend class base::RefCounted<RemoteDevice>; 196 friend class base::RefCounted<RemoteDevice>;
200 virtual ~RemoteDevice(); 197 virtual ~RemoteDevice();
201 198
202 scoped_refptr<DevToolsAdbBridge> bridge_;
203 scoped_refptr<AndroidDevice> device_; 199 scoped_refptr<AndroidDevice> device_;
204 RemoteBrowsers browsers_; 200 RemoteBrowsers browsers_;
205 gfx::Size screen_size_; 201 gfx::Size screen_size_;
206 202
207 DISALLOW_COPY_AND_ASSIGN(RemoteDevice); 203 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
208 }; 204 };
209 205
210 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices; 206 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
211 207
212 class AndroidDevice : public base::RefCounted<AndroidDevice> {
213 public:
214 explicit AndroidDevice(const std::string& serial);
215
216 virtual void RunCommand(const std::string& command,
217 const CommandCallback& callback) = 0;
218 virtual void OpenSocket(const std::string& socket_name,
219 const SocketCallback& callback) = 0;
220 void HttpQuery(const std::string& la_name,
221 const std::string& request,
222 const CommandCallback& callback);
223 void HttpUpgrade(const std::string& la_name,
224 const std::string& request,
225 const SocketCallback& callback);
226
227 std::string serial() { return serial_; }
228
229 std::string model() { return model_; }
230 void set_model(const std::string& model) { model_ = model; }
231
232 protected:
233 friend class base::RefCounted<AndroidDevice>;
234 virtual ~AndroidDevice();
235
236 private:
237 void OnHttpSocketOpened(const std::string& request,
238 const CommandCallback& callback,
239 int result,
240 net::StreamSocket* socket);
241 void OnHttpSocketOpened2(const std::string& request,
242 const SocketCallback& callback,
243 int result,
244 net::StreamSocket* socket);
245
246 std::string serial_;
247 std::string model_;
248
249 DISALLOW_COPY_AND_ASSIGN(AndroidDevice);
250 };
251
252 typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices;
253 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; 208 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback;
254 209
255 class Listener { 210 class Listener {
256 public: 211 public:
257 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0; 212 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0;
258 protected: 213 protected:
259 virtual ~Listener() {} 214 virtual ~Listener() {}
260 }; 215 };
261 216
262 explicit DevToolsAdbBridge(Profile* profile); 217 explicit DevToolsAdbBridge(Profile* profile,
Vladislav Kaznacheev 2013/10/16 11:27:04 explicit is no longer needed
Dmitry Zvorygin 2013/10/17 16:19:37 Done.
218 const DeviceProviders& device_providers_);
Vladislav Kaznacheev 2013/10/16 11:27:04 indent
Dmitry Zvorygin 2013/10/17 16:19:37 Done.
263 219
264 void AddListener(Listener* listener); 220 void AddListener(Listener* listener);
265 void RemoveListener(Listener* listener); 221 void RemoveListener(Listener* listener);
266 222
267 base::MessageLoop* GetAdbMessageLoop();
268 223
269 private: 224 private:
270 friend struct content::BrowserThread::DeleteOnThread< 225 friend struct content::BrowserThread::DeleteOnThread<
271 content::BrowserThread::UI>; 226 content::BrowserThread::UI>;
272 friend class base::DeleteHelper<DevToolsAdbBridge>; 227 friend class base::DeleteHelper<DevToolsAdbBridge>;
273 228
274 class RefCountedAdbThread : public base::RefCounted<RefCountedAdbThread> {
275 public:
276 static scoped_refptr<RefCountedAdbThread> GetInstance();
277 RefCountedAdbThread();
278 base::MessageLoop* message_loop();
279
280 private:
281 friend class base::RefCounted<RefCountedAdbThread>;
282 static DevToolsAdbBridge::RefCountedAdbThread* instance_;
283 static void StopThread(base::Thread* thread);
284
285 virtual ~RefCountedAdbThread();
286 base::Thread* thread_;
287 };
288
289 virtual ~DevToolsAdbBridge(); 229 virtual ~DevToolsAdbBridge();
290 230
291 void RequestRemoteDevices(); 231 void RequestRemoteDevices();
292 void ReceivedRemoteDevices(RemoteDevices* devices); 232 void ReceivedRemoteDevices(RemoteDevices* devices);
293 233
294 Profile* profile_; 234 Profile* profile_;
295 scoped_refptr<RefCountedAdbThread> adb_thread_; 235 scoped_refptr<RefCountedAdbThread> adb_thread_;
236 DeviceProviders device_providers_;
296 bool has_message_loop_; 237 bool has_message_loop_;
297 scoped_ptr<crypto::RSAPrivateKey> rsa_key_; 238 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
298 typedef std::vector<Listener*> Listeners; 239 typedef std::vector<Listener*> Listeners;
299 Listeners listeners_; 240 Listeners listeners_;
300 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge); 241 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge);
301 }; 242 };
302 243
303 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ 244 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698