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/memory/weak_ptr.h"
16 #include "base/prefs/pref_change_registrar.h"
17 #include "chrome/browser/devtools/device/android_device_manager.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/devtools_agent_host.h"
21 #include "ui/gfx/geometry/size.h"
23 template<typename T
> struct DefaultSingletonTraits
;
27 class DictionaryValue
;
36 class DevToolsTargetImpl
;
37 class PortForwardingController
;
39 class WebRTCDeviceProvider
;
40 class SigninManagerBase
;
41 class ProfileOAuth2TokenService
;
43 class DevToolsAndroidBridge
: public KeyedService
{
45 class Factory
: public BrowserContextKeyedServiceFactory
{
47 // Returns singleton instance of DevToolsAndroidBridge.
48 static Factory
* GetInstance();
50 // Returns DevToolsAndroidBridge associated with |profile|.
51 static DevToolsAndroidBridge
* GetForProfile(Profile
* profile
);
54 friend struct DefaultSingletonTraits
<Factory
>;
59 // BrowserContextKeyedServiceFactory overrides:
60 KeyedService
* BuildServiceInstanceFor(
61 content::BrowserContext
* context
) const override
;
62 DISALLOW_COPY_AND_ASSIGN(Factory
);
65 typedef std::pair
<std::string
, std::string
> BrowserId
;
67 class RemotePage
: public base::RefCounted
<RemotePage
> {
69 const std::string
& serial() { return browser_id_
.first
; }
70 const std::string
& socket() { return browser_id_
.second
; }
71 const std::string
& frontend_url() { return frontend_url_
; }
74 friend class base::RefCounted
<RemotePage
>;
75 friend class DevToolsAndroidBridge
;
77 RemotePage(const BrowserId
& browser_id
, const base::DictionaryValue
& dict
);
79 virtual ~RemotePage();
81 BrowserId browser_id_
;
82 std::string frontend_url_
;
83 scoped_ptr
<base::DictionaryValue
> dict_
;
85 DISALLOW_COPY_AND_ASSIGN(RemotePage
);
88 typedef std::vector
<scoped_refptr
<RemotePage
> > RemotePages
;
89 typedef base::Callback
<void(int, const std::string
&)> JsonRequestCallback
;
91 class RemoteBrowser
: public base::RefCounted
<RemoteBrowser
> {
93 const std::string
& serial() { return browser_id_
.first
; }
94 const std::string
& socket() { return browser_id_
.second
; }
95 const std::string
& display_name() { return display_name_
; }
96 const std::string
& user() { return user_
; }
97 const std::string
& version() { return version_
; }
98 const RemotePages
& pages() { return pages_
; }
103 typedef std::vector
<int> ParsedVersion
;
104 ParsedVersion
GetParsedVersion();
107 friend class base::RefCounted
<RemoteBrowser
>;
108 friend class DevToolsAndroidBridge
;
110 RemoteBrowser(const std::string
& serial
,
111 const AndroidDeviceManager::BrowserInfo
& browser_info
);
113 virtual ~RemoteBrowser();
115 BrowserId browser_id_
;
116 std::string display_name_
;
118 AndroidDeviceManager::BrowserInfo::Type type_
;
119 std::string version_
;
122 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser
);
125 typedef std::vector
<scoped_refptr
<RemoteBrowser
> > RemoteBrowsers
;
127 class RemoteDevice
: public base::RefCounted
<RemoteDevice
> {
129 std::string
serial() { return serial_
; }
130 std::string
model() { return model_
; }
131 bool is_connected() { return connected_
; }
132 RemoteBrowsers
& browsers() { return browsers_
; }
133 gfx::Size
screen_size() { return screen_size_
; }
136 friend class base::RefCounted
<RemoteDevice
>;
137 friend class DevToolsAndroidBridge
;
139 RemoteDevice(const std::string
& serial
,
140 const AndroidDeviceManager::DeviceInfo
& device_info
);
142 virtual ~RemoteDevice();
147 RemoteBrowsers browsers_
;
148 gfx::Size screen_size_
;
150 DISALLOW_COPY_AND_ASSIGN(RemoteDevice
);
153 typedef std::vector
<scoped_refptr
<RemoteDevice
> > RemoteDevices
;
155 class DeviceListListener
{
157 virtual void DeviceListChanged(const RemoteDevices
& devices
) = 0;
159 virtual ~DeviceListListener() {}
162 DevToolsAndroidBridge(Profile
* profile
,
163 SigninManagerBase
* signin_manager
,
164 ProfileOAuth2TokenService
* token_service
);
165 void AddDeviceListListener(DeviceListListener
* listener
);
166 void RemoveDeviceListListener(DeviceListListener
* listener
);
168 class DeviceCountListener
{
170 virtual void DeviceCountChanged(int count
) = 0;
172 virtual ~DeviceCountListener() {}
175 void AddDeviceCountListener(DeviceCountListener
* listener
);
176 void RemoveDeviceCountListener(DeviceCountListener
* listener
);
178 typedef int PortStatus
;
179 typedef std::map
<int, PortStatus
> PortStatusMap
;
180 typedef std::pair
<scoped_refptr
<RemoteBrowser
>, PortStatusMap
>
182 typedef std::vector
<BrowserStatus
> ForwardingStatus
;
184 class PortForwardingListener
{
186 typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap
;
187 typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus
;
188 typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus
;
190 virtual void PortStatusChanged(const ForwardingStatus
&) = 0;
192 virtual ~PortForwardingListener() {}
195 void AddPortForwardingListener(PortForwardingListener
* listener
);
196 void RemovePortForwardingListener(PortForwardingListener
* listener
);
198 void set_device_providers_for_test(
199 const AndroidDeviceManager::DeviceProviders
& device_providers
) {
200 device_manager_
->SetDeviceProviders(device_providers
);
203 void set_task_scheduler_for_test(
204 base::Callback
<void(const base::Closure
&)> scheduler
) {
205 task_scheduler_
= scheduler
;
208 bool HasDevToolsWindow(const std::string
& agent_id
);
210 // Creates new target instance owned by caller.
211 DevToolsTargetImpl
* CreatePageTarget(scoped_refptr
<RemotePage
> browser
);
213 typedef base::Callback
<void(scoped_refptr
<RemotePage
>)> RemotePageCallback
;
214 void OpenRemotePage(scoped_refptr
<RemoteBrowser
> browser
,
215 const std::string
& url
,
216 const RemotePageCallback
& callback
);
218 scoped_refptr
<content::DevToolsAgentHost
> GetBrowserAgentHost(
219 scoped_refptr
<RemoteBrowser
> browser
);
221 void SendJsonRequest(const std::string
& browser_id_str
,
222 const std::string
& url
,
223 const JsonRequestCallback
& callback
);
226 friend struct content::BrowserThread::DeleteOnThread
<
227 content::BrowserThread::UI
>;
228 friend class base::DeleteHelper
<DevToolsAndroidBridge
>;
230 friend class PortForwardingController
;
232 class AgentHostDelegate
;
233 class DiscoveryRequest
;
234 class RemotePageTarget
;
236 ~DevToolsAndroidBridge() override
;
238 void StartDeviceListPolling();
239 void StopDeviceListPolling();
240 bool NeedsDeviceListPolling();
242 typedef std::pair
<scoped_refptr
<AndroidDeviceManager::Device
>,
243 scoped_refptr
<RemoteDevice
>> CompleteDevice
;
244 typedef std::vector
<CompleteDevice
> CompleteDevices
;
245 typedef base::Callback
<void(const CompleteDevices
&)> DeviceListCallback
;
247 void RequestDeviceList(const DeviceListCallback
& callback
);
248 void ReceivedDeviceList(const CompleteDevices
& complete_devices
);
250 void StartDeviceCountPolling();
251 void StopDeviceCountPolling();
252 void RequestDeviceCount(const base::Callback
<void(int)>& callback
);
253 void ReceivedDeviceCount(int count
);
255 static void ScheduleTaskDefault(const base::Closure
& task
);
257 void CreateDeviceProviders();
259 void SendJsonRequest(const BrowserId
& browser_id
,
260 const std::string
& url
,
261 const JsonRequestCallback
& callback
);
263 void SendProtocolCommand(const BrowserId
& browser_id
,
264 const std::string
& debug_url
,
265 const std::string
& method
,
266 scoped_ptr
<base::DictionaryValue
> params
,
267 const base::Closure callback
);
269 scoped_refptr
<AndroidDeviceManager::Device
> FindDevice(
270 const std::string
& serial
);
272 void RespondToOpenOnUIThread(scoped_refptr
<RemoteBrowser
> browser
,
273 const RemotePageCallback
& callback
,
275 const std::string
& response
);
277 base::WeakPtr
<DevToolsAndroidBridge
> AsWeakPtr() {
278 return weak_factory_
.GetWeakPtr();
281 Profile
* const profile_
;
282 SigninManagerBase
* const signin_manager_
;
283 ProfileOAuth2TokenService
* const token_service_
;
284 const scoped_ptr
<AndroidDeviceManager
> device_manager_
;
286 typedef std::map
<std::string
, scoped_refptr
<AndroidDeviceManager::Device
>>
288 DeviceMap device_map_
;
290 typedef std::map
<std::string
, AgentHostDelegate
*> AgentHostDelegates
;
291 AgentHostDelegates host_delegates_
;
293 typedef std::vector
<DeviceListListener
*> DeviceListListeners
;
294 DeviceListListeners device_list_listeners_
;
295 base::CancelableCallback
<void(const CompleteDevices
&)> device_list_callback_
;
297 typedef std::vector
<DeviceCountListener
*> DeviceCountListeners
;
298 DeviceCountListeners device_count_listeners_
;
299 base::CancelableCallback
<void(int)> device_count_callback_
;
300 base::Callback
<void(const base::Closure
&)> task_scheduler_
;
302 typedef std::vector
<PortForwardingListener
*> PortForwardingListeners
;
303 PortForwardingListeners port_forwarding_listeners_
;
304 scoped_ptr
<PortForwardingController
> port_forwarding_controller_
;
306 PrefChangeRegistrar pref_change_registrar_
;
308 base::WeakPtrFactory
<DevToolsAndroidBridge
> weak_factory_
;
310 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge
);
313 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_