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_ANDROID_DEVICE_MANAGER_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "ui/gfx/geometry/size.h"
22 class AndroidDeviceManager
: public base::NonThreadSafe
{
24 using CommandCallback
=
25 base::Callback
<void(int, const std::string
&)>;
26 using SocketCallback
=
27 base::Callback
<void(int result
, scoped_ptr
<net::StreamSocket
>)>;
28 // |body_head| should contain the body (WebSocket frame data) part that has
29 // been read during processing the header (WebSocket handshake).
30 using HttpUpgradeCallback
= base::Callback
<void(
32 const std::string
& extensions
,
33 const std::string
& body_head
,
34 scoped_ptr
<net::StreamSocket
>)>;
35 using SerialsCallback
=
36 base::Callback
<void(const std::vector
<std::string
>&)>;
47 std::string socket_name
;
48 std::string display_name
;
59 gfx::Size screen_size
;
60 std::vector
<BrowserInfo
> browser_info
;
63 typedef base::Callback
<void(const DeviceInfo
&)> DeviceInfoCallback
;
66 class AndroidWebSocket
{
70 virtual void OnSocketOpened() = 0;
71 virtual void OnFrameRead(const std::string
& message
) = 0;
72 virtual void OnSocketClosed() = 0;
75 virtual ~Delegate() {}
80 void SendFrame(const std::string
& message
);
87 scoped_refptr
<Device
> device
,
88 const std::string
& socket_name
,
89 const std::string
& path
,
90 AndroidWebSocket::Delegate
* delegate
);
91 void Connected(int result
,
92 const std::string
& extensions
,
93 const std::string
& body_head
,
94 scoped_ptr
<net::StreamSocket
> socket
);
95 void OnFrameRead(const std::string
& message
);
96 void OnSocketClosed();
100 WebSocketImpl
* socket_impl_
;
102 base::WeakPtrFactory
<AndroidWebSocket
> weak_factory_
;
103 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket
);
106 class DeviceProvider
;
108 class Device
: public base::RefCountedThreadSafe
<Device
>,
109 public base::NonThreadSafe
{
111 void QueryDeviceInfo(const DeviceInfoCallback
& callback
);
113 void OpenSocket(const std::string
& socket_name
,
114 const SocketCallback
& callback
);
116 void SendJsonRequest(const std::string
& socket_name
,
117 const std::string
& request
,
118 const CommandCallback
& callback
);
120 void HttpUpgrade(const std::string
& socket_name
,
121 const std::string
& path
,
122 const std::string
& extensions
,
123 const HttpUpgradeCallback
& callback
);
124 AndroidWebSocket
* CreateWebSocket(
125 const std::string
& socket_name
,
126 const std::string
& path
,
127 AndroidWebSocket::Delegate
* delegate
);
129 std::string
serial() { return serial_
; }
132 friend class base::RefCountedThreadSafe
<Device
>;
133 friend class AndroidDeviceManager
;
134 friend class AndroidWebSocket
;
136 Device(scoped_refptr
<base::SingleThreadTaskRunner
> device_task_runner
,
137 scoped_refptr
<DeviceProvider
> provider
,
138 const std::string
& serial
);
142 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
143 scoped_refptr
<DeviceProvider
> provider_
;
145 std::set
<AndroidWebSocket
*> sockets_
;
146 base::WeakPtrFactory
<Device
> weak_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(Device
);
151 typedef std::vector
<scoped_refptr
<Device
> > Devices
;
152 typedef base::Callback
<void(const Devices
&)> DevicesCallback
;
154 class DeviceProvider
: public base::RefCountedThreadSafe
<DeviceProvider
> {
156 typedef AndroidDeviceManager::SerialsCallback SerialsCallback
;
157 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback
;
158 typedef AndroidDeviceManager::SocketCallback SocketCallback
;
159 typedef AndroidDeviceManager::CommandCallback CommandCallback
;
161 virtual void QueryDevices(const SerialsCallback
& callback
) = 0;
163 virtual void QueryDeviceInfo(const std::string
& serial
,
164 const DeviceInfoCallback
& callback
) = 0;
166 virtual void OpenSocket(const std::string
& serial
,
167 const std::string
& socket_name
,
168 const SocketCallback
& callback
) = 0;
170 virtual void SendJsonRequest(const std::string
& serial
,
171 const std::string
& socket_name
,
172 const std::string
& request
,
173 const CommandCallback
& callback
);
175 virtual void HttpUpgrade(const std::string
& serial
,
176 const std::string
& socket_name
,
177 const std::string
& path
,
178 const std::string
& extensions
,
179 const HttpUpgradeCallback
& callback
);
181 virtual void ReleaseDevice(const std::string
& serial
);
184 friend class base::RefCountedThreadSafe
<DeviceProvider
>;
186 virtual ~DeviceProvider();
189 typedef std::vector
<scoped_refptr
<DeviceProvider
> > DeviceProviders
;
191 virtual ~AndroidDeviceManager();
193 static scoped_ptr
<AndroidDeviceManager
> Create();
195 void SetDeviceProviders(const DeviceProviders
& providers
);
197 void QueryDevices(const DevicesCallback
& callback
);
199 static std::string
GetBrowserName(const std::string
& socket
,
200 const std::string
& package
);
201 using RunCommandCallback
=
202 base::Callback
<void(const std::string
&, const CommandCallback
&)>;
204 static void QueryDeviceInfo(const RunCommandCallback
& command_callback
,
205 const DeviceInfoCallback
& callback
);
207 struct DeviceDescriptor
{
211 scoped_refptr
<DeviceProvider
> provider
;
215 typedef std::vector
<DeviceDescriptor
> DeviceDescriptors
;
218 class HandlerThread
: public base::RefCountedThreadSafe
<HandlerThread
> {
220 static scoped_refptr
<HandlerThread
> GetInstance();
221 scoped_refptr
<base::SingleThreadTaskRunner
> message_loop();
224 friend class base::RefCountedThreadSafe
<HandlerThread
>;
225 static HandlerThread
* instance_
;
226 static void StopThread(base::Thread
* thread
);
229 virtual ~HandlerThread();
230 base::Thread
* thread_
;
233 AndroidDeviceManager();
235 void UpdateDevices(const DevicesCallback
& callback
,
236 scoped_ptr
<DeviceDescriptors
> descriptors
);
238 typedef std::map
<std::string
, base::WeakPtr
<Device
> > DeviceWeakMap
;
240 scoped_refptr
<HandlerThread
> handler_thread_
;
241 DeviceProviders providers_
;
242 DeviceWeakMap devices_
;
244 base::WeakPtrFactory
<AndroidDeviceManager
> weak_factory_
;
247 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_