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 class Wrapper : public KeyedService {
46 explicit Wrapper(content::BrowserContext* context);
49 DevToolsAndroidBridge* Get();
51 scoped_refptr<DevToolsAndroidBridge> bridge_;
54 class Factory : public BrowserContextKeyedServiceFactory {
56 // Returns singleton instance of DevToolsAndroidBridge.
57 static Factory* GetInstance();
59 // Returns DevToolsAndroidBridge associated with |profile|.
60 static DevToolsAndroidBridge* GetForProfile(Profile* profile);
63 friend struct DefaultSingletonTraits<Factory>;
68 // BrowserContextKeyedServiceFactory overrides:
69 KeyedService* BuildServiceInstanceFor(
70 content::BrowserContext* context) const override;
71 DISALLOW_COPY_AND_ASSIGN(Factory);
74 typedef std::pair<std::string, std::string> BrowserId;
76 class RemotePage : public base::RefCounted<RemotePage> {
78 const std::string& serial() { return browser_id_.first; }
79 const std::string& socket() { return browser_id_.second; }
80 const std::string& frontend_url() { return frontend_url_; }
81 bool is_web_view() { return is_web_view_; }
84 friend class base::RefCounted<RemotePage>;
85 friend class DevToolsAndroidBridge;
87 RemotePage(const BrowserId& browser_id,
88 const base::DictionaryValue& dict,
91 virtual ~RemotePage();
93 BrowserId browser_id_;
94 std::string frontend_url_;
96 scoped_ptr<base::DictionaryValue> dict_;
98 DISALLOW_COPY_AND_ASSIGN(RemotePage);
101 typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
102 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
104 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
106 const std::string& serial() { return browser_id_.first; }
107 const std::string& socket() { return browser_id_.second; }
108 const std::string& display_name() { return display_name_; }
109 const std::string& version() { return version_; }
110 const RemotePages& pages() { return pages_; }
115 typedef std::vector<int> ParsedVersion;
116 ParsedVersion GetParsedVersion();
119 friend class base::RefCounted<RemoteBrowser>;
120 friend class DevToolsAndroidBridge;
122 RemoteBrowser(const std::string& serial,
123 const AndroidDeviceManager::BrowserInfo& browser_info);
125 virtual ~RemoteBrowser();
127 BrowserId browser_id_;
128 std::string display_name_;
129 AndroidDeviceManager::BrowserInfo::Type type_;
130 std::string version_;
133 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
136 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
138 class RemoteDevice : public base::RefCounted<RemoteDevice> {
140 std::string serial() { return serial_; }
141 std::string model() { return model_; }
142 bool is_connected() { return connected_; }
143 RemoteBrowsers& browsers() { return browsers_; }
144 gfx::Size screen_size() { return screen_size_; }
147 friend class base::RefCounted<RemoteDevice>;
148 friend class DevToolsAndroidBridge;
150 RemoteDevice(const std::string& serial,
151 const AndroidDeviceManager::DeviceInfo& device_info);
153 virtual ~RemoteDevice();
158 RemoteBrowsers browsers_;
159 gfx::Size screen_size_;
161 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
164 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
166 class DeviceListListener {
168 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
170 virtual ~DeviceListListener() {}
173 explicit DevToolsAndroidBridge(Profile* profile);
174 void AddDeviceListListener(DeviceListListener* listener);
175 void RemoveDeviceListListener(DeviceListListener* listener);
177 class DeviceCountListener {
179 virtual void DeviceCountChanged(int count) = 0;
181 virtual ~DeviceCountListener() {}
184 void AddDeviceCountListener(DeviceCountListener* listener);
185 void RemoveDeviceCountListener(DeviceCountListener* listener);
187 typedef int PortStatus;
188 typedef std::map<int, PortStatus> PortStatusMap;
189 typedef std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>
191 typedef std::vector<BrowserStatus> ForwardingStatus;
193 class PortForwardingListener {
195 typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
196 typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus;
197 typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus;
199 virtual void PortStatusChanged(const ForwardingStatus&) = 0;
201 virtual ~PortForwardingListener() {}
204 void AddPortForwardingListener(PortForwardingListener* listener);
205 void RemovePortForwardingListener(PortForwardingListener* listener);
207 void set_device_providers_for_test(
208 const AndroidDeviceManager::DeviceProviders& device_providers) {
209 device_manager_->SetDeviceProviders(device_providers);
212 void set_task_scheduler_for_test(
213 base::Callback<void(const base::Closure&)> scheduler) {
214 task_scheduler_ = scheduler;
217 bool HasDevToolsWindow(const std::string& agent_id);
219 // Creates new target instance owned by caller.
220 DevToolsTargetImpl* CreatePageTarget(scoped_refptr<RemotePage> browser);
222 typedef base::Callback<void(scoped_refptr<RemotePage>)> RemotePageCallback;
223 void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
224 const std::string& url,
225 const RemotePageCallback& callback);
227 scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
228 scoped_refptr<RemoteBrowser> browser);
230 friend struct content::BrowserThread::DeleteOnThread<
231 content::BrowserThread::UI>;
232 friend class base::DeleteHelper<DevToolsAndroidBridge>;
234 friend class PortForwardingController;
236 class AgentHostDelegate;
237 class DiscoveryRequest;
238 class RemotePageTarget;
240 virtual ~DevToolsAndroidBridge();
242 void StartDeviceListPolling();
243 void StopDeviceListPolling();
244 bool NeedsDeviceListPolling();
246 typedef std::pair<scoped_refptr<AndroidDeviceManager::Device>,
247 scoped_refptr<RemoteDevice>> CompleteDevice;
248 typedef std::vector<CompleteDevice> CompleteDevices;
249 typedef base::Callback<void(const CompleteDevices&)> DeviceListCallback;
251 void RequestDeviceList(const DeviceListCallback& callback);
252 void ReceivedDeviceList(const CompleteDevices& complete_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();
263 void SendJsonRequest(const BrowserId& browser_id,
264 const std::string& url,
265 const JsonRequestCallback& callback);
267 void SendProtocolCommand(const BrowserId& browser_id,
268 const std::string& debug_url,
269 const std::string& method,
270 base::DictionaryValue* params,
271 const base::Closure callback);
273 scoped_refptr<AndroidDeviceManager::Device> FindDevice(
274 const std::string& serial);
276 void PageCreatedOnUIThread(scoped_refptr<RemoteBrowser> browser,
277 const RemotePageCallback& callback,
278 const std::string& url,
280 const std::string& response);
282 void NavigatePageOnUIThread(scoped_refptr<RemoteBrowser> browser,
283 const RemotePageCallback& callback,
285 const std::string& response,
286 const std::string& url);
288 void RespondToOpenOnUIThread(scoped_refptr<RemoteBrowser> browser,
289 const RemotePageCallback& callback,
291 const std::string& response);
294 scoped_refptr<AndroidDeviceManager> device_manager_;
296 typedef std::map<std::string, scoped_refptr<AndroidDeviceManager::Device>>
298 DeviceMap device_map_;
300 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates;
301 AgentHostDelegates host_delegates_;
303 typedef std::vector<DeviceListListener*> DeviceListListeners;
304 DeviceListListeners device_list_listeners_;
305 base::CancelableCallback<void(const CompleteDevices&)> device_list_callback_;
307 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
308 DeviceCountListeners device_count_listeners_;
309 base::CancelableCallback<void(int)> device_count_callback_;
310 base::Callback<void(const base::Closure&)> task_scheduler_;
312 typedef std::vector<PortForwardingListener*> PortForwardingListeners;
313 PortForwardingListeners port_forwarding_listeners_;
314 scoped_ptr<PortForwardingController> port_forwarding_controller_;
316 PrefChangeRegistrar pref_change_registrar_;
317 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
320 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_