1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "chrome/browser/devtools/device/android_device_manager.h"
17 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "content/public/browser/devtools_agent_host.h"
20 #include "ui/gfx/size.h"
22 template<typename T> struct DefaultSingletonTraits;
26 class DictionaryValue;
35 class DevToolsTargetImpl;
36 class PortForwardingController;
39 class DevToolsAndroidBridge
40 : public base::RefCountedThreadSafe<
41 DevToolsAndroidBridge,
42 content::BrowserThread::DeleteOnUIThread> {
44 typedef base::Callback<void(int result,
45 const std::string& response)> Callback;
47 class Wrapper : public KeyedService {
49 explicit Wrapper(content::BrowserContext* context);
52 DevToolsAndroidBridge* Get();
54 scoped_refptr<DevToolsAndroidBridge> bridge_;
57 class Factory : public BrowserContextKeyedServiceFactory {
59 // Returns singleton instance of DevToolsAndroidBridge.
60 static Factory* GetInstance();
62 // Returns DevToolsAndroidBridge associated with |profile|.
63 static DevToolsAndroidBridge* GetForProfile(Profile* profile);
66 friend struct DefaultSingletonTraits<Factory>;
71 // BrowserContextKeyedServiceFactory overrides:
72 virtual KeyedService* BuildServiceInstanceFor(
73 content::BrowserContext* context) const OVERRIDE;
74 DISALLOW_COPY_AND_ASSIGN(Factory);
79 virtual ~RemotePage() {}
80 virtual DevToolsTargetImpl* GetTarget() = 0;
81 virtual std::string GetFrontendURL() = 0;
84 typedef base::Callback<void(RemotePage*)> RemotePageCallback;
85 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
86 typedef AndroidDeviceManager::Device Device;
87 typedef AndroidDeviceManager::AndroidWebSocket AndroidWebSocket;
89 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
91 RemoteBrowser(scoped_refptr<Device> device,
92 const AndroidDeviceManager::BrowserInfo& browser_info);
94 std::string serial() { return device_->serial(); }
95 std::string socket() { return socket_; }
97 std::string display_name() { return display_name_; }
98 void set_display_name(const std::string& name) { display_name_ = name; }
100 std::string version() { return version_; }
101 void set_version(const std::string& version) { version_ = version; }
103 bool IsChrome() const;
104 bool IsWebView() const;
106 typedef std::vector<int> ParsedVersion;
107 ParsedVersion GetParsedVersion() const;
109 std::vector<RemotePage*> CreatePages();
110 void SetPageDescriptors(const base::ListValue&);
112 void SendJsonRequest(const std::string& request,
113 const JsonRequestCallback& callback);
115 void SendProtocolCommand(const std::string& debug_url,
116 const std::string& method,
117 base::DictionaryValue* params,
118 const base::Closure callback);
120 void Open(const std::string& url,
121 const RemotePageCallback& callback);
123 scoped_refptr<content::DevToolsAgentHost> GetAgentHost();
125 AndroidWebSocket* CreateWebSocket(
126 const std::string& url,
127 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate);
130 friend class base::RefCounted<RemoteBrowser>;
131 virtual ~RemoteBrowser();
133 void InnerOpen(const std::string& url,
134 const JsonRequestCallback& callback);
136 void PageCreatedOnUIThread(
137 const JsonRequestCallback& callback,
138 const std::string& url, int result, const std::string& response);
140 void NavigatePageOnUIThread(const JsonRequestCallback& callback,
142 const std::string& response,
143 const std::string& url);
145 void RespondToOpenOnUIThread(
146 const DevToolsAndroidBridge::RemotePageCallback& callback,
148 const std::string& response);
150 scoped_refptr<Device> device_;
151 const std::string socket_;
152 std::string display_name_;
153 const AndroidDeviceManager::BrowserInfo::Type type_;
154 std::string version_;
155 scoped_ptr<base::ListValue> page_descriptors_;
157 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
160 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
162 class RemoteDevice : public base::RefCounted<RemoteDevice> {
164 RemoteDevice(scoped_refptr<Device> device,
165 const AndroidDeviceManager::DeviceInfo& device_info);
167 std::string serial() { return device_->serial(); }
168 std::string model() { return model_; }
169 bool is_connected() { return connected_; }
170 RemoteBrowsers& browsers() { return browsers_; }
171 gfx::Size screen_size() { return screen_size_; }
173 void OpenSocket(const std::string& socket_name,
174 const AndroidDeviceManager::SocketCallback& callback);
177 friend class base::RefCounted<RemoteDevice>;
178 virtual ~RemoteDevice();
180 scoped_refptr<Device> device_;
183 RemoteBrowsers browsers_;
184 gfx::Size screen_size_;
186 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
189 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
191 class DeviceListListener {
193 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
195 virtual ~DeviceListListener() {}
198 explicit DevToolsAndroidBridge(Profile* profile);
199 void AddDeviceListListener(DeviceListListener* listener);
200 void RemoveDeviceListListener(DeviceListListener* listener);
202 class DeviceCountListener {
204 virtual void DeviceCountChanged(int count) = 0;
206 virtual ~DeviceCountListener() {}
209 void AddDeviceCountListener(DeviceCountListener* listener);
210 void RemoveDeviceCountListener(DeviceCountListener* listener);
212 typedef int PortStatus;
213 typedef std::map<int, PortStatus> PortStatusMap;
214 typedef std::map<std::string, PortStatusMap> DevicesStatus;
216 class PortForwardingListener {
218 typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
219 typedef DevToolsAndroidBridge::DevicesStatus DevicesStatus;
221 virtual void PortStatusChanged(const DevicesStatus&) = 0;
223 virtual ~PortForwardingListener() {}
226 void AddPortForwardingListener(PortForwardingListener* listener);
227 void RemovePortForwardingListener(PortForwardingListener* listener);
229 void set_device_providers_for_test(
230 const AndroidDeviceManager::DeviceProviders& device_providers) {
231 device_manager_->SetDeviceProviders(device_providers);
234 void set_task_scheduler_for_test(
235 base::Callback<void(const base::Closure&)> scheduler) {
236 task_scheduler_ = scheduler;
239 static bool HasDevToolsWindow(const std::string& agent_id);
242 friend struct content::BrowserThread::DeleteOnThread<
243 content::BrowserThread::UI>;
244 friend class base::DeleteHelper<DevToolsAndroidBridge>;
246 virtual ~DevToolsAndroidBridge();
248 void StartDeviceListPolling();
249 void StopDeviceListPolling();
250 bool NeedsDeviceListPolling();
251 void RequestDeviceList(
252 const base::Callback<void(const RemoteDevices&)>& callback);
253 void ReceivedDeviceList(const RemoteDevices& devices);
254 void StartDeviceCountPolling();
255 void StopDeviceCountPolling();
256 void RequestDeviceCount(const base::Callback<void(int)>& callback);
257 void ReceivedDeviceCount(int count);
259 static void ScheduleTaskDefault(const base::Closure& task);
261 void CreateDeviceProviders();
264 scoped_refptr<AndroidDeviceManager> device_manager_;
265 RemoteDevices devices_;
267 typedef std::vector<DeviceListListener*> DeviceListListeners;
268 DeviceListListeners device_list_listeners_;
269 base::CancelableCallback<void(const RemoteDevices&)> device_list_callback_;
271 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
272 DeviceCountListeners device_count_listeners_;
273 base::CancelableCallback<void(int)> device_count_callback_;
274 base::Callback<void(const base::Closure&)> task_scheduler_;
276 typedef std::vector<PortForwardingListener*> PortForwardingListeners;
277 PortForwardingListeners port_forwarding_listeners_;
278 scoped_ptr<PortForwardingController> port_forwarding_controller_;
280 PrefChangeRegistrar pref_change_registrar_;
281 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
284 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_