blob: cfd348242ea4cecc3c7615bba4075f9fc07ea450 [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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 Murdochca12bfa2013-07-23 11:17:05 +010014#include "chrome/browser/devtools/adb/android_usb_device.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010015#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 Murdoch558790d2013-07-30 15:19:42 +010017#include "content/public/browser/browser_thread.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "net/socket/tcp_client_socket.h"
19
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010020template<typename T> struct DefaultSingletonTraits;
21
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022namespace base {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010023class MessageLoop;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000024class DictionaryValue;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010025class Thread;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000026}
27
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010028namespace content {
29class BrowserContext;
30}
31
32namespace crypto {
33class RSAPrivateKey;
34}
35
Ben Murdoch2385ea32013-08-06 11:01:04 +010036class PortForwardingController;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000037class Profile;
38
Ben Murdocheb525c52013-07-10 11:40:50 +010039// The format used for constructing DevTools server socket names.
40extern const char kDevToolsChannelNameFormat[];
41
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010042typedef base::Callback<void(int, const std::string&)> CommandCallback;
43typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
44
Ben Murdoch9ab55632013-07-18 11:57:30 +010045class DevToolsAdbBridge
Ben Murdoch558790d2013-07-30 15:19:42 +010046 : public base::RefCountedThreadSafe<
47 DevToolsAdbBridge,
48 content::BrowserThread::DeleteOnUIThread> {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000049 public:
50 typedef base::Callback<void(int result,
51 const std::string& response)> Callback;
52
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010053 class Wrapper : public BrowserContextKeyedService {
54 public:
Ben Murdoch9ab55632013-07-18 11:57:30 +010055 explicit Wrapper(Profile* profile);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010056 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 Murdoch558790d2013-07-30 15:19:42 +010084 class AndroidDevice;
85
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000086 class RemotePage : public base::RefCounted<RemotePage> {
87 public:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010088 RemotePage(scoped_refptr<DevToolsAdbBridge> bridge,
89 scoped_refptr<AndroidDevice> device,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010090 const std::string& socket,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000091 const base::DictionaryValue& value);
92
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093 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 Murdochbb1529c2013-08-08 10:24:53 +010098 bool attached() { return debug_url_.empty(); }
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010099
100 void Inspect(Profile* profile);
Ben Murdochbb1529c2013-08-08 10:24:53 +0100101 void Close();
102 void Reload();
103
104 void SendProtocolCommand(const std::string& method,
105 base::DictionaryValue* params);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106
107 private:
108 friend class base::RefCounted<RemotePage>;
109 virtual ~RemotePage();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100110
111 scoped_refptr<DevToolsAdbBridge> bridge_;
Ben Murdoch558790d2013-07-30 15:19:42 +0100112 scoped_refptr<AndroidDevice> device_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100113 std::string socket_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000114 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)a36e5922013-08-05 13:57:33 +0100125
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 Murdochbb1529c2013-08-08 10:24:53 +0100140 void Open(const std::string& url);
141
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100142 private:
143 friend class base::RefCounted<RemoteBrowser>;
144 virtual ~RemoteBrowser();
145
Ben Murdochbb1529c2013-08-08 10:24:53 +0100146 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)a36e5922013-08-05 13:57:33 +0100152 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 Murdoch2385ea32013-08-06 11:01:04 +0100168 scoped_refptr<AndroidDevice> device() { return device_; }
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100169 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)2a99a7e2013-03-28 15:31:22 +0000189
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100190 class AndroidDevice : public base::RefCounted<AndroidDevice> {
191 public:
192 explicit AndroidDevice(const std::string& serial);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000193
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100194 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 Murdochbb1529c2013-08-08 10:24:53 +0100198 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 Murdoch7dbb3d52013-07-17 14:55:54 +0100204
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 Murdoch9ab55632013-07-18 11:57:30 +0100233 class Listener {
234 public:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100235 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0;
Ben Murdoch9ab55632013-07-18 11:57:30 +0100236 protected:
237 virtual ~Listener() {}
238 };
239
Ben Murdoch558790d2013-07-30 15:19:42 +0100240 explicit DevToolsAdbBridge(Profile* profile);
241
242 void EnumerateUsbDevices(const AndroidDevicesCallback& callback);
243 void EnumerateAdbDevices(const AndroidDevicesCallback& callback);
244
Ben Murdoch9ab55632013-07-18 11:57:30 +0100245 void AddListener(Listener* listener);
246 void RemoveListener(Listener* listener);
247
Ben Murdoch558790d2013-07-30 15:19:42 +0100248 base::MessageLoop* GetAdbMessageLoop();
249
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000250 private:
Ben Murdoch558790d2013-07-30 15:19:42 +0100251 friend struct content::BrowserThread::DeleteOnThread<
252 content::BrowserThread::UI>;
253 friend class base::DeleteHelper<DevToolsAdbBridge>;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000254
255 class RefCountedAdbThread : public base::RefCounted<RefCountedAdbThread> {
256 public:
257 static scoped_refptr<RefCountedAdbThread> GetInstance();
258 RefCountedAdbThread();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100259 base::MessageLoop* message_loop();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000260
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 Murdochca12bfa2013-07-23 11:17:05 +0100270 virtual ~DevToolsAdbBridge();
271 void ReceivedUsbDevices(const AndroidDevicesCallback& callback,
272 const AndroidUsbDevices& usb_devices);
273 void ReceivedAdbDevices(const AndroidDevicesCallback& callback,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100274 int result,
275 const std::string& response);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100276
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100277 void RequestRemoteDevices();
278 void ReceivedRemoteDevices(RemoteDevices* devices);
Ben Murdoch9ab55632013-07-18 11:57:30 +0100279
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000280 Profile* profile_;
281 scoped_refptr<RefCountedAdbThread> adb_thread_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000282 bool has_message_loop_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100283 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
Ben Murdoch9ab55632013-07-18 11:57:30 +0100284 typedef std::vector<Listener*> Listeners;
285 Listeners listeners_;
Ben Murdoch2385ea32013-08-06 11:01:04 +0100286 scoped_ptr<PortForwardingController> port_forwarding_controller_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000287 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge);
288};
289
290#endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_