1 // 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.
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/devtools/android_device.h"
15 #include "chrome/browser/devtools/refcounted_adb_thread.h"
16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
17 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "net/socket/tcp_client_socket.h"
20 #include "ui/gfx/size.h"
22 template<typename T
> struct DefaultSingletonTraits
;
26 class DictionaryValue
;
39 class DevToolsTargetImpl
;
42 // The format used for constructing DevTools server socket names.
43 extern const char kDevToolsChannelNameFormat
[];
45 class DevToolsAdbBridge
46 : public base::RefCountedThreadSafe
<
48 content::BrowserThread::DeleteOnUIThread
> {
50 typedef base::Callback
<void(int result
,
51 const std::string
& response
)> Callback
;
52 typedef std::vector
<scoped_refptr
<AndroidDeviceProvider
> > DeviceProviders
;
55 class Wrapper
: public BrowserContextKeyedService
{
60 DevToolsAdbBridge
* Get();
62 scoped_refptr
<DevToolsAdbBridge
> bridge_
;
65 class Factory
: public BrowserContextKeyedServiceFactory
{
67 // Returns singleton instance of DevToolsAdbBridge.
68 static Factory
* GetInstance();
70 // Returns DevToolsAdbBridge associated with |profile|.
71 static DevToolsAdbBridge
* GetForProfile(Profile
* profile
);
74 friend struct DefaultSingletonTraits
<Factory
>;
79 // BrowserContextKeyedServiceFactory overrides:
80 virtual BrowserContextKeyedService
* BuildServiceInstanceFor(
81 content::BrowserContext
* context
) const OVERRIDE
;
82 DISALLOW_COPY_AND_ASSIGN(Factory
);
85 class RemoteBrowser
: public base::RefCounted
<RemoteBrowser
> {
88 scoped_refptr
<RefCountedAdbThread
> adb_thread
,
89 scoped_refptr
<AndroidDevice
> device
,
90 const std::string
& socket
);
92 scoped_refptr
<RefCountedAdbThread
> adb_thread() { return adb_thread_
; }
94 scoped_refptr
<AndroidDevice
> device() { return device_
; }
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;
105 typedef std::vector
<int> ParsedVersion
;
106 ParsedVersion
GetParsedVersion() const;
108 std::vector
<DevToolsTargetImpl
*> CreatePageTargets();
109 void SetPageDescriptors(const base::ListValue
&);
111 void SendJsonRequest(const std::string
& request
, base::Closure callback
);
112 void SendProtocolCommand(const std::string
& debug_url
,
113 const std::string
& method
,
114 base::DictionaryValue
* params
);
116 void Open(const std::string
& url
);
119 friend class base::RefCounted
<RemoteBrowser
>;
120 virtual ~RemoteBrowser();
122 void PageCreatedOnHandlerThread(
123 const std::string
& url
, int result
, const std::string
& response
);
125 void PageCreatedOnUIThread(
126 const std::string
& response
, const std::string
& url
);
128 scoped_refptr
<RefCountedAdbThread
> adb_thread_
;
129 scoped_refptr
<AndroidDevice
> device_
;
130 const std::string socket_
;
131 std::string display_name_
;
132 std::string version_
;
133 scoped_ptr
<base::ListValue
> page_descriptors_
;
135 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser
);
138 typedef std::vector
<scoped_refptr
<RemoteBrowser
> > RemoteBrowsers
;
140 class RemoteDevice
: public base::RefCounted
<RemoteDevice
> {
142 explicit RemoteDevice(scoped_refptr
<AndroidDevice
> device
);
144 std::string
GetSerial();
145 std::string
GetModel();
147 void AddBrowser(scoped_refptr
<RemoteBrowser
> browser
);
149 scoped_refptr
<AndroidDevice
> device() { return device_
; }
150 RemoteBrowsers
& browsers() { return browsers_
; }
151 gfx::Size
screen_size() { return screen_size_
; }
152 void set_screen_size(const gfx::Size
& size
) { screen_size_
= size
; }
155 friend class base::RefCounted
<RemoteDevice
>;
156 virtual ~RemoteDevice();
158 scoped_refptr
<AndroidDevice
> device_
;
159 RemoteBrowsers browsers_
;
160 gfx::Size screen_size_
;
162 DISALLOW_COPY_AND_ASSIGN(RemoteDevice
);
165 typedef std::vector
<scoped_refptr
<RemoteDevice
> > RemoteDevices
;
167 typedef std::vector
<scoped_refptr
<AndroidDevice
> > AndroidDevices
;
168 typedef base::Callback
<void(const AndroidDevices
&)> AndroidDevicesCallback
;
172 virtual void RemoteDevicesChanged(RemoteDevices
* devices
) = 0;
174 virtual ~Listener() {}
178 void AddListener(Listener
* listener
);
179 void RemoveListener(Listener
* listener
);
181 void set_device_providers(DeviceProviders device_providers
) {
182 device_providers_
= device_providers
;
184 static bool HasDevToolsWindow(const std::string
& agent_id
);
187 friend struct content::BrowserThread::DeleteOnThread
<
188 content::BrowserThread::UI
>;
189 friend class base::DeleteHelper
<DevToolsAdbBridge
>;
191 virtual ~DevToolsAdbBridge();
193 void RequestRemoteDevices();
194 void ReceivedRemoteDevices(RemoteDevices
* devices
);
196 scoped_refptr
<RefCountedAdbThread
> adb_thread_
;
197 bool has_message_loop_
;
198 typedef std::vector
<Listener
*> Listeners
;
199 Listeners listeners_
;
200 DeviceProviders device_providers_
;
201 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge
);
204 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_