blob: f0913659e0f74d8575ce09a3790103c464be1f68 [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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "net/socket/tcp_client_socket.h"
18
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010019template<typename T> struct DefaultSingletonTraits;
20
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000021namespace base {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010022class MessageLoop;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000023class DictionaryValue;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010024class Thread;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025}
26
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010027namespace content {
28class BrowserContext;
29}
30
31namespace crypto {
32class RSAPrivateKey;
33}
34
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035class Profile;
36
Ben Murdocheb525c52013-07-10 11:40:50 +010037// The format used for constructing DevTools server socket names.
38extern const char kDevToolsChannelNameFormat[];
39
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010040typedef base::Callback<void(int, const std::string&)> CommandCallback;
41typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
42
Ben Murdoch9ab55632013-07-18 11:57:30 +010043class DevToolsAdbBridge
44 : public base::RefCountedThreadSafe<DevToolsAdbBridge> {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000045 public:
46 typedef base::Callback<void(int result,
47 const std::string& response)> Callback;
48
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010049 class Wrapper : public BrowserContextKeyedService {
50 public:
Ben Murdoch9ab55632013-07-18 11:57:30 +010051 explicit Wrapper(Profile* profile);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010052 virtual ~Wrapper();
53
54 DevToolsAdbBridge* Get();
55 private:
56 scoped_refptr<DevToolsAdbBridge> bridge_;
57 };
58
59 class Factory : public BrowserContextKeyedServiceFactory {
60 public:
61 // Returns singleton instance of DevToolsAdbBridge.
62 static Factory* GetInstance();
63
64 // Returns DevToolsAdbBridge associated with |profile|.
65 static DevToolsAdbBridge* GetForProfile(Profile* profile);
66
67 private:
68 friend struct DefaultSingletonTraits<Factory>;
69 friend class DevToolsAdbBridge;
70
71 Factory();
72 virtual ~Factory();
73
74 // BrowserContextKeyedServiceFactory overrides:
75 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
76 content::BrowserContext* context) const OVERRIDE;
77 DISALLOW_COPY_AND_ASSIGN(Factory);
78 };
79
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080 class RemotePage : public base::RefCounted<RemotePage> {
81 public:
82 RemotePage(const std::string& serial,
83 const std::string& model,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010084 const std::string& package,
85 const std::string& socket,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000086 const base::DictionaryValue& value);
87
88 std::string serial() { return serial_; }
89 std::string model() { return model_; }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010090 std::string package() { return package_; }
91 std::string socket() { return socket_; }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000092 std::string id() { return id_; }
93 std::string url() { return url_; }
94 std::string title() { return title_; }
95 std::string description() { return description_; }
96 std::string favicon_url() { return favicon_url_; }
97 std::string debug_url() { return debug_url_; }
98 std::string frontend_url() { return frontend_url_; }
99
100 private:
101 friend class base::RefCounted<RemotePage>;
102 virtual ~RemotePage();
103 std::string serial_;
104 std::string model_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100105 std::string package_;
106 std::string socket_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107 std::string id_;
108 std::string url_;
109 std::string title_;
110 std::string description_;
111 std::string favicon_url_;
112 std::string debug_url_;
113 std::string frontend_url_;
114 DISALLOW_COPY_AND_ASSIGN(RemotePage);
115 };
116
117 typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
118 typedef base::Callback<void(int, RemotePages*)> PagesCallback;
119
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100120 class AndroidDevice : public base::RefCounted<AndroidDevice> {
121 public:
122 explicit AndroidDevice(const std::string& serial);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000123
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100124 virtual void RunCommand(const std::string& command,
125 const CommandCallback& callback) = 0;
126 virtual void OpenSocket(const std::string& socket_name,
127 const SocketCallback& callback) = 0;
128 virtual void HttpQuery(const std::string& la_name,
129 const std::string& request,
130 const CommandCallback& callback);
131 virtual void HttpQuery(const std::string& la_name,
132 const std::string& request,
133 const SocketCallback& callback);
134
135 std::string serial() { return serial_; }
136
137 std::string model() { return model_; }
138 void set_model(const std::string& model) { model_ = model; }
139
140 protected:
141 friend class base::RefCounted<AndroidDevice>;
142 virtual ~AndroidDevice();
143
144 private:
145 void OnHttpSocketOpened(const std::string& request,
146 const CommandCallback& callback,
147 int result,
148 net::StreamSocket* socket);
149 void OnHttpSocketOpened2(const std::string& request,
150 const SocketCallback& callback,
151 int result,
152 net::StreamSocket* socket);
153
154 std::string serial_;
155 std::string model_;
156
157 DISALLOW_COPY_AND_ASSIGN(AndroidDevice);
158 };
159
160 typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices;
161 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback;
162
163 explicit DevToolsAdbBridge(Profile* profile);
164
165 void EnumerateDevices(const AndroidDevicesCallback& callback);
Ben Murdochca12bfa2013-07-23 11:17:05 +0100166
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100167 void Attach(const std::string& serial,
168 const std::string& socket,
169 const std::string& debug_url,
170 const std::string& frontend_url);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000171
Ben Murdoch9ab55632013-07-18 11:57:30 +0100172 class Listener {
173 public:
174 virtual void RemotePagesChanged(RemotePages* pages) = 0;
175 protected:
176 virtual ~Listener() {}
177 };
178
179 void AddListener(Listener* listener);
180 void RemoveListener(Listener* listener);
181
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000182 private:
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100183 friend class base::RefCountedThreadSafe<DevToolsAdbBridge>;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100184 friend class AdbAttachCommand;
Ben Murdoch9ab55632013-07-18 11:57:30 +0100185 friend class AdbWebSocket;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100186 friend class AgentHostDelegate;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000187
188 class RefCountedAdbThread : public base::RefCounted<RefCountedAdbThread> {
189 public:
190 static scoped_refptr<RefCountedAdbThread> GetInstance();
191 RefCountedAdbThread();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100192 base::MessageLoop* message_loop();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000193
194 private:
195 friend class base::RefCounted<RefCountedAdbThread>;
196 static DevToolsAdbBridge::RefCountedAdbThread* instance_;
197 static void StopThread(base::Thread* thread);
198
199 virtual ~RefCountedAdbThread();
200 base::Thread* thread_;
201 };
202
Ben Murdochca12bfa2013-07-23 11:17:05 +0100203 virtual ~DevToolsAdbBridge();
204 void ReceivedUsbDevices(const AndroidDevicesCallback& callback,
205 const AndroidUsbDevices& usb_devices);
206 void ReceivedAdbDevices(const AndroidDevicesCallback& callback,
207 AndroidDevices devices,
208 int result,
209 const std::string& response);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100210
Ben Murdoch9ab55632013-07-18 11:57:30 +0100211 void RequestPages();
212 void ReceivedPages(int result, RemotePages* pages);
213
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000214 Profile* profile_;
215 scoped_refptr<RefCountedAdbThread> adb_thread_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000216 bool has_message_loop_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100217 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
Ben Murdoch9ab55632013-07-18 11:57:30 +0100218 typedef std::vector<Listener*> Listeners;
219 Listeners listeners_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000220 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge);
221};
222
223#endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_