OLD | NEW |
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/android_device.h" |
| 15 #include "chrome/browser/devtools/refcounted_adb_thread.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; |
| 50 typedef std::vector<scoped_refptr<AndroidDeviceProvider> > DeviceProviders; |
| 51 |
52 | 52 |
53 class Wrapper : public BrowserContextKeyedService { | 53 class Wrapper : public BrowserContextKeyedService { |
54 public: | 54 public: |
55 explicit Wrapper(Profile* profile); | 55 Wrapper(); |
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 | 73 |
74 Factory(); | 74 Factory(); |
75 virtual ~Factory(); | 75 virtual ~Factory(); |
76 | 76 |
77 // BrowserContextKeyedServiceFactory overrides: | 77 // BrowserContextKeyedServiceFactory overrides: |
78 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 78 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
79 content::BrowserContext* context) const OVERRIDE; | 79 content::BrowserContext* context) const OVERRIDE; |
80 DISALLOW_COPY_AND_ASSIGN(Factory); | 80 DISALLOW_COPY_AND_ASSIGN(Factory); |
81 }; | 81 }; |
82 | 82 |
83 class AndroidDevice; | |
84 class RefCountedAdbThread; | |
85 | |
86 class RemotePage : public base::RefCounted<RemotePage> { | 83 class RemotePage : public base::RefCounted<RemotePage> { |
87 public: | 84 public: |
88 RemotePage(scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread, | 85 RemotePage(scoped_refptr<RefCountedAdbThread> adb_thread, |
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(); } |
99 | 96 |
100 bool HasDevToolsWindow(); | 97 bool HasDevToolsWindow(); |
101 | 98 |
102 void Inspect(Profile* profile); | 99 void Inspect(Profile* profile); |
103 void Activate(); | 100 void Activate(); |
104 void Close(); | 101 void Close(); |
105 void Reload(); | 102 void Reload(); |
106 | 103 |
107 void SendProtocolCommand(const std::string& method, | 104 void SendProtocolCommand(const std::string& method, |
108 base::DictionaryValue* params); | 105 base::DictionaryValue* params); |
109 | 106 |
110 private: | 107 private: |
111 friend class base::RefCounted<RemotePage>; | 108 friend class base::RefCounted<RemotePage>; |
112 virtual ~RemotePage(); | 109 virtual ~RemotePage(); |
113 | 110 |
114 void RequestActivate(const CommandCallback& callback); | 111 void RequestActivate(const AndroidDevice::CommandCallback& callback); |
115 | 112 |
116 void InspectOnHandlerThread( | 113 void InspectOnHandlerThread( |
117 Profile* profile, int result, const std::string& response); | 114 Profile* profile, int result, const std::string& response); |
118 | 115 |
119 void InspectOnUIThread(Profile* profile); | 116 void InspectOnUIThread(Profile* profile); |
120 | 117 |
121 scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread_; | 118 scoped_refptr<RefCountedAdbThread> adb_thread_; |
122 scoped_refptr<AndroidDevice> device_; | 119 scoped_refptr<AndroidDevice> device_; |
123 std::string socket_; | 120 std::string socket_; |
124 std::string id_; | 121 std::string id_; |
125 std::string url_; | 122 std::string url_; |
126 std::string title_; | 123 std::string title_; |
127 std::string description_; | 124 std::string description_; |
128 std::string favicon_url_; | 125 std::string favicon_url_; |
129 std::string debug_url_; | 126 std::string debug_url_; |
130 std::string frontend_url_; | 127 std::string frontend_url_; |
131 std::string agent_id_; | 128 std::string agent_id_; |
132 DISALLOW_COPY_AND_ASSIGN(RemotePage); | 129 DISALLOW_COPY_AND_ASSIGN(RemotePage); |
133 }; | 130 }; |
134 | 131 |
135 typedef std::vector<scoped_refptr<RemotePage> > RemotePages; | 132 typedef std::vector<scoped_refptr<RemotePage> > RemotePages; |
136 | 133 |
137 class RemoteBrowser : public base::RefCounted<RemoteBrowser> { | 134 class RemoteBrowser : public base::RefCounted<RemoteBrowser> { |
138 public: | 135 public: |
139 RemoteBrowser( | 136 RemoteBrowser( |
140 scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread, | 137 scoped_refptr<RefCountedAdbThread> adb_thread, |
141 scoped_refptr<AndroidDevice> device, | 138 scoped_refptr<AndroidDevice> device, |
142 const std::string& socket); | 139 const std::string& socket); |
143 | 140 |
144 scoped_refptr<AndroidDevice> device() { return device_; } | 141 scoped_refptr<AndroidDevice> device() { return device_; } |
145 std::string socket() { return socket_; } | 142 std::string socket() { return socket_; } |
146 | 143 |
147 std::string product() { return product_; } | 144 std::string product() { return product_; } |
148 void set_product(const std::string& product) { product_ = product; } | 145 void set_product(const std::string& product) { product_ = product; } |
149 std::string version() { return version_; } | 146 std::string version() { return version_; } |
150 void set_version(const std::string& version) { version_ = version; } | 147 void set_version(const std::string& version) { version_ = version; } |
(...skipping 10 matching lines...) Expand all Loading... |
161 private: | 158 private: |
162 friend class base::RefCounted<RemoteBrowser>; | 159 friend class base::RefCounted<RemoteBrowser>; |
163 virtual ~RemoteBrowser(); | 160 virtual ~RemoteBrowser(); |
164 | 161 |
165 void PageCreatedOnHandlerThread( | 162 void PageCreatedOnHandlerThread( |
166 const std::string& url, int result, const std::string& response); | 163 const std::string& url, int result, const std::string& response); |
167 | 164 |
168 void PageCreatedOnUIThread( | 165 void PageCreatedOnUIThread( |
169 const std::string& response, const std::string& url); | 166 const std::string& response, const std::string& url); |
170 | 167 |
171 scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread_; | 168 scoped_refptr<RefCountedAdbThread> adb_thread_; |
172 scoped_refptr<AndroidDevice> device_; | 169 scoped_refptr<AndroidDevice> device_; |
173 const std::string socket_; | 170 const std::string socket_; |
174 std::string product_; | 171 std::string product_; |
175 std::string version_; | 172 std::string version_; |
176 std::string pid_; | 173 std::string pid_; |
177 std::string package_; | 174 std::string package_; |
178 RemotePages pages_; | 175 RemotePages pages_; |
179 | 176 |
180 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser); | 177 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser); |
181 }; | 178 }; |
(...skipping 20 matching lines...) Expand all Loading... |
202 | 199 |
203 scoped_refptr<AndroidDevice> device_; | 200 scoped_refptr<AndroidDevice> device_; |
204 RemoteBrowsers browsers_; | 201 RemoteBrowsers browsers_; |
205 gfx::Size screen_size_; | 202 gfx::Size screen_size_; |
206 | 203 |
207 DISALLOW_COPY_AND_ASSIGN(RemoteDevice); | 204 DISALLOW_COPY_AND_ASSIGN(RemoteDevice); |
208 }; | 205 }; |
209 | 206 |
210 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices; | 207 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices; |
211 | 208 |
212 class AndroidDevice : public base::RefCounted<AndroidDevice> { | |
213 public: | |
214 AndroidDevice(const std::string& serial, bool is_connected); | |
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 bool is_connected() { return is_connected_; } | |
229 | |
230 std::string model() { return model_; } | |
231 void set_model(const std::string& model) { model_ = model; } | |
232 | |
233 protected: | |
234 friend class base::RefCounted<AndroidDevice>; | |
235 virtual ~AndroidDevice(); | |
236 | |
237 private: | |
238 void OnHttpSocketOpened(const std::string& request, | |
239 const CommandCallback& callback, | |
240 int result, | |
241 net::StreamSocket* socket); | |
242 void OnHttpSocketOpened2(const std::string& request, | |
243 const SocketCallback& callback, | |
244 int result, | |
245 net::StreamSocket* socket); | |
246 | |
247 std::string serial_; | |
248 bool is_connected_; | |
249 std::string model_; | |
250 | |
251 DISALLOW_COPY_AND_ASSIGN(AndroidDevice); | |
252 }; | |
253 | |
254 typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices; | 209 typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices; |
255 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; | 210 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; |
256 | 211 |
257 class Listener { | 212 class Listener { |
258 public: | 213 public: |
259 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0; | 214 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0; |
260 protected: | 215 protected: |
261 virtual ~Listener() {} | 216 virtual ~Listener() {} |
262 }; | 217 }; |
263 | 218 |
264 class RefCountedAdbThread : public base::RefCounted<RefCountedAdbThread> { | 219 DevToolsAdbBridge(); |
265 public: | |
266 static scoped_refptr<RefCountedAdbThread> GetInstance(); | |
267 base::MessageLoop* message_loop(); | |
268 | |
269 private: | |
270 friend class base::RefCounted<RefCountedAdbThread>; | |
271 static DevToolsAdbBridge::RefCountedAdbThread* instance_; | |
272 static void StopThread(base::Thread* thread); | |
273 | |
274 RefCountedAdbThread(); | |
275 virtual ~RefCountedAdbThread(); | |
276 base::Thread* thread_; | |
277 }; | |
278 | |
279 explicit DevToolsAdbBridge(Profile* profile); | |
280 void AddListener(Listener* listener); | 220 void AddListener(Listener* listener); |
281 void RemoveListener(Listener* listener); | 221 void RemoveListener(Listener* listener); |
282 void CountDevices(const base::Callback<void(int)>& callback); | 222 |
283 void set_discover_usb_devices(bool enabled) { | 223 void set_device_providers(DeviceProviders device_providers) { |
284 discover_usb_devices_ = enabled; | 224 device_providers_ = device_providers; |
285 } | 225 } |
286 | 226 |
287 private: | 227 private: |
288 friend struct content::BrowserThread::DeleteOnThread< | 228 friend struct content::BrowserThread::DeleteOnThread< |
289 content::BrowserThread::UI>; | 229 content::BrowserThread::UI>; |
290 friend class base::DeleteHelper<DevToolsAdbBridge>; | 230 friend class base::DeleteHelper<DevToolsAdbBridge>; |
291 | 231 |
292 virtual ~DevToolsAdbBridge(); | 232 virtual ~DevToolsAdbBridge(); |
293 | 233 |
294 void RequestRemoteDevices(); | 234 void RequestRemoteDevices(); |
295 void ReceivedRemoteDevices(RemoteDevices* devices); | 235 void ReceivedRemoteDevices(RemoteDevices* devices); |
296 | 236 |
297 Profile* profile_; | |
298 scoped_refptr<RefCountedAdbThread> adb_thread_; | 237 scoped_refptr<RefCountedAdbThread> adb_thread_; |
299 bool has_message_loop_; | 238 bool has_message_loop_; |
300 scoped_ptr<crypto::RSAPrivateKey> rsa_key_; | |
301 typedef std::vector<Listener*> Listeners; | 239 typedef std::vector<Listener*> Listeners; |
302 Listeners listeners_; | 240 Listeners listeners_; |
303 bool discover_usb_devices_; | 241 DeviceProviders device_providers_; |
304 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge); | 242 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge); |
305 }; | 243 }; |
306 | 244 |
307 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ | 245 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ |
OLD | NEW |