Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ |
| 6 | #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 14 | #include "chrome/browser/devtools/adb/android_usb_device.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 15 | #include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
| 16 | #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "net/socket/tcp_client_socket.h" |
| 19 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 20 | template<typename T> struct DefaultSingletonTraits; |
| 21 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | namespace base { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 23 | class MessageLoop; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 24 | class DictionaryValue; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 25 | class Thread; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 28 | namespace content { |
| 29 | class BrowserContext; |
| 30 | } |
| 31 | |
| 32 | namespace crypto { |
| 33 | class RSAPrivateKey; |
| 34 | } |
| 35 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 36 | class PortForwardingController; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 37 | class Profile; |
| 38 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 39 | // The format used for constructing DevTools server socket names. |
| 40 | extern const char kDevToolsChannelNameFormat[]; |
| 41 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 42 | typedef base::Callback<void(int, const std::string&)> CommandCallback; |
| 43 | typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback; |
| 44 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 45 | class DevToolsAdbBridge |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 46 | : public base::RefCountedThreadSafe< |
| 47 | DevToolsAdbBridge, |
| 48 | content::BrowserThread::DeleteOnUIThread> { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 49 | public: |
| 50 | typedef base::Callback<void(int result, |
| 51 | const std::string& response)> Callback; |
| 52 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 53 | class Wrapper : public BrowserContextKeyedService { |
| 54 | public: |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 55 | explicit Wrapper(Profile* profile); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 56 | virtual ~Wrapper(); |
| 57 | |
| 58 | DevToolsAdbBridge* Get(); |
| 59 | private: |
| 60 | scoped_refptr<DevToolsAdbBridge> bridge_; |
| 61 | }; |
| 62 | |
| 63 | class Factory : public BrowserContextKeyedServiceFactory { |
| 64 | public: |
| 65 | // Returns singleton instance of DevToolsAdbBridge. |
| 66 | static Factory* GetInstance(); |
| 67 | |
| 68 | // Returns DevToolsAdbBridge associated with |profile|. |
| 69 | static DevToolsAdbBridge* GetForProfile(Profile* profile); |
| 70 | |
| 71 | private: |
| 72 | friend struct DefaultSingletonTraits<Factory>; |
| 73 | friend class DevToolsAdbBridge; |
| 74 | |
| 75 | Factory(); |
| 76 | virtual ~Factory(); |
| 77 | |
| 78 | // BrowserContextKeyedServiceFactory overrides: |
| 79 | virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 80 | content::BrowserContext* context) const OVERRIDE; |
| 81 | DISALLOW_COPY_AND_ASSIGN(Factory); |
| 82 | }; |
| 83 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 84 | class AndroidDevice; |
| 85 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 86 | class RemotePage : public base::RefCounted<RemotePage> { |
| 87 | public: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 88 | RemotePage(scoped_refptr<DevToolsAdbBridge> bridge, |
| 89 | scoped_refptr<AndroidDevice> device, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 90 | const std::string& socket, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 91 | const base::DictionaryValue& value); |
| 92 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | std::string id() { return id_; } |
| 94 | std::string url() { return url_; } |
| 95 | std::string title() { return title_; } |
| 96 | std::string description() { return description_; } |
| 97 | std::string favicon_url() { return favicon_url_; } |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 98 | bool attached() { return debug_url_.empty(); } |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 99 | |
| 100 | void Inspect(Profile* profile); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 101 | void Close(); |
| 102 | void Reload(); |
| 103 | |
| 104 | void SendProtocolCommand(const std::string& method, |
| 105 | base::DictionaryValue* params); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | friend class base::RefCounted<RemotePage>; |
| 109 | virtual ~RemotePage(); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 110 | |
| 111 | scoped_refptr<DevToolsAdbBridge> bridge_; |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 112 | scoped_refptr<AndroidDevice> device_; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 113 | std::string socket_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 114 | std::string id_; |
| 115 | std::string url_; |
| 116 | std::string title_; |
| 117 | std::string description_; |
| 118 | std::string favicon_url_; |
| 119 | std::string debug_url_; |
| 120 | std::string frontend_url_; |
| 121 | DISALLOW_COPY_AND_ASSIGN(RemotePage); |
| 122 | }; |
| 123 | |
| 124 | typedef std::vector<scoped_refptr<RemotePage> > RemotePages; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 125 | |
| 126 | class RemoteBrowser : public base::RefCounted<RemoteBrowser> { |
| 127 | public: |
| 128 | RemoteBrowser(scoped_refptr<DevToolsAdbBridge> bridge, |
| 129 | scoped_refptr<AndroidDevice> device, |
| 130 | const std::string& socket, |
| 131 | const std::string& name); |
| 132 | |
| 133 | scoped_refptr<AndroidDevice> device() { return device_; } |
| 134 | std::string socket() { return socket_; } |
| 135 | std::string name() { return name_; } |
| 136 | |
| 137 | RemotePages& pages() { return pages_; } |
| 138 | void AddPage(scoped_refptr<RemotePage> page) { pages_.push_back(page); } |
| 139 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 140 | void Open(const std::string& url); |
| 141 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 142 | private: |
| 143 | friend class base::RefCounted<RemoteBrowser>; |
| 144 | virtual ~RemoteBrowser(); |
| 145 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 146 | void PageCreatedOnHandlerThread( |
| 147 | const std::string& url, int result, const std::string& response); |
| 148 | |
| 149 | void PageCreatedOnUIThread( |
| 150 | const std::string& response, const std::string& url); |
| 151 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 152 | scoped_refptr<DevToolsAdbBridge> bridge_; |
| 153 | scoped_refptr<AndroidDevice> device_; |
| 154 | const std::string socket_; |
| 155 | const std::string name_; |
| 156 | RemotePages pages_; |
| 157 | |
| 158 | DISALLOW_COPY_AND_ASSIGN(RemoteBrowser); |
| 159 | }; |
| 160 | |
| 161 | typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers; |
| 162 | |
| 163 | class RemoteDevice : public base::RefCounted<RemoteDevice> { |
| 164 | public: |
| 165 | explicit RemoteDevice(scoped_refptr<DevToolsAdbBridge> bridge, |
| 166 | scoped_refptr<AndroidDevice> device); |
| 167 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 168 | scoped_refptr<AndroidDevice> device() { return device_; } |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 169 | std::string serial() { return device_->serial(); } |
| 170 | std::string model() { return device_->model(); } |
| 171 | |
| 172 | RemoteBrowsers& browsers() { return browsers_; } |
| 173 | void AddBrowser(scoped_refptr<RemoteBrowser> browser) { |
| 174 | browsers_.push_back(browser); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | friend class base::RefCounted<RemoteDevice>; |
| 179 | virtual ~RemoteDevice(); |
| 180 | |
| 181 | scoped_refptr<DevToolsAdbBridge> bridge_; |
| 182 | scoped_refptr<AndroidDevice> device_; |
| 183 | RemoteBrowsers browsers_; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(RemoteDevice); |
| 186 | }; |
| 187 | |
| 188 | typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 189 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 190 | class AndroidDevice : public base::RefCounted<AndroidDevice> { |
| 191 | public: |
| 192 | explicit AndroidDevice(const std::string& serial); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 193 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 194 | virtual void RunCommand(const std::string& command, |
| 195 | const CommandCallback& callback) = 0; |
| 196 | virtual void OpenSocket(const std::string& socket_name, |
| 197 | const SocketCallback& callback) = 0; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 198 | void HttpQuery(const std::string& la_name, |
| 199 | const std::string& request, |
| 200 | const CommandCallback& callback); |
| 201 | void HttpUpgrade(const std::string& la_name, |
| 202 | const std::string& request, |
| 203 | const SocketCallback& callback); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 204 | |
| 205 | std::string serial() { return serial_; } |
| 206 | |
| 207 | std::string model() { return model_; } |
| 208 | void set_model(const std::string& model) { model_ = model; } |
| 209 | |
| 210 | protected: |
| 211 | friend class base::RefCounted<AndroidDevice>; |
| 212 | virtual ~AndroidDevice(); |
| 213 | |
| 214 | private: |
| 215 | void OnHttpSocketOpened(const std::string& request, |
| 216 | const CommandCallback& callback, |
| 217 | int result, |
| 218 | net::StreamSocket* socket); |
| 219 | void OnHttpSocketOpened2(const std::string& request, |
| 220 | const SocketCallback& callback, |
| 221 | int result, |
| 222 | net::StreamSocket* socket); |
| 223 | |
| 224 | std::string serial_; |
| 225 | std::string model_; |
| 226 | |
| 227 | DISALLOW_COPY_AND_ASSIGN(AndroidDevice); |
| 228 | }; |
| 229 | |
| 230 | typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices; |
| 231 | typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; |
| 232 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 233 | class Listener { |
| 234 | public: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 235 | virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0; |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 236 | protected: |
| 237 | virtual ~Listener() {} |
| 238 | }; |
| 239 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 240 | explicit DevToolsAdbBridge(Profile* profile); |
| 241 | |
| 242 | void EnumerateUsbDevices(const AndroidDevicesCallback& callback); |
| 243 | void EnumerateAdbDevices(const AndroidDevicesCallback& callback); |
| 244 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 245 | void AddListener(Listener* listener); |
| 246 | void RemoveListener(Listener* listener); |
| 247 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 248 | base::MessageLoop* GetAdbMessageLoop(); |
| 249 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 250 | private: |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 251 | friend struct content::BrowserThread::DeleteOnThread< |
| 252 | content::BrowserThread::UI>; |
| 253 | friend class base::DeleteHelper<DevToolsAdbBridge>; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 254 | |
| 255 | class RefCountedAdbThread : public base::RefCounted<RefCountedAdbThread> { |
| 256 | public: |
| 257 | static scoped_refptr<RefCountedAdbThread> GetInstance(); |
| 258 | RefCountedAdbThread(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 259 | base::MessageLoop* message_loop(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 260 | |
| 261 | private: |
| 262 | friend class base::RefCounted<RefCountedAdbThread>; |
| 263 | static DevToolsAdbBridge::RefCountedAdbThread* instance_; |
| 264 | static void StopThread(base::Thread* thread); |
| 265 | |
| 266 | virtual ~RefCountedAdbThread(); |
| 267 | base::Thread* thread_; |
| 268 | }; |
| 269 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 270 | virtual ~DevToolsAdbBridge(); |
| 271 | void ReceivedUsbDevices(const AndroidDevicesCallback& callback, |
| 272 | const AndroidUsbDevices& usb_devices); |
| 273 | void ReceivedAdbDevices(const AndroidDevicesCallback& callback, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 274 | int result, |
| 275 | const std::string& response); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 276 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 277 | void RequestRemoteDevices(); |
| 278 | void ReceivedRemoteDevices(RemoteDevices* devices); |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 279 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 280 | Profile* profile_; |
| 281 | scoped_refptr<RefCountedAdbThread> adb_thread_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 282 | bool has_message_loop_; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 283 | scoped_ptr<crypto::RSAPrivateKey> rsa_key_; |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 284 | typedef std::vector<Listener*> Listeners; |
| 285 | Listeners listeners_; |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 286 | scoped_ptr<PortForwardingController> port_forwarding_controller_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 287 | DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge); |
| 288 | }; |
| 289 | |
| 290 | #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ |