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/threading/non_thread_safe.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "ui/gfx/geometry/size.h"
21 class AndroidDeviceManager
: public base::NonThreadSafe
{
23 typedef base::Callback
<void(int, const std::string
&)> CommandCallback
;
24 typedef base::Callback
<void(int result
, scoped_ptr
<net::StreamSocket
>)>
26 typedef base::Callback
<void(
27 int result
, const std::string
& extensions
, scoped_ptr
<net::StreamSocket
>)>
29 typedef base::Callback
<void(const std::vector
<std::string
>&)> SerialsCallback
;
40 std::string socket_name
;
41 std::string display_name
;
52 gfx::Size screen_size
;
53 std::vector
<BrowserInfo
> browser_info
;
56 typedef base::Callback
<void(const DeviceInfo
&)> DeviceInfoCallback
;
59 class AndroidWebSocket
{
63 virtual void OnSocketOpened() = 0;
64 virtual void OnFrameRead(const std::string
& message
) = 0;
65 virtual void OnSocketClosed() = 0;
68 virtual ~Delegate() {}
73 void SendFrame(const std::string
& message
);
80 scoped_refptr
<Device
> device
,
81 const std::string
& socket_name
,
82 const std::string
& url
,
83 AndroidWebSocket::Delegate
* delegate
);
84 void Connected(int result
,
85 const std::string
& extensions
,
86 scoped_ptr
<net::StreamSocket
> socket
);
87 void OnFrameRead(const std::string
& message
);
88 void OnSocketClosed();
92 WebSocketImpl
* socket_impl_
;
94 base::WeakPtrFactory
<AndroidWebSocket
> weak_factory_
;
95 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket
);
100 class Device
: public base::RefCountedThreadSafe
<Device
>,
101 public base::NonThreadSafe
{
103 void QueryDeviceInfo(const DeviceInfoCallback
& callback
);
105 void OpenSocket(const std::string
& socket_name
,
106 const SocketCallback
& callback
);
108 void SendJsonRequest(const std::string
& socket_name
,
109 const std::string
& request
,
110 const CommandCallback
& callback
);
112 void HttpUpgrade(const std::string
& socket_name
,
113 const std::string
& url
,
114 const std::string
& extensions
,
115 const HttpUpgradeCallback
& callback
);
116 AndroidWebSocket
* CreateWebSocket(
117 const std::string
& socket_name
,
118 const std::string
& url
,
119 AndroidWebSocket::Delegate
* delegate
);
121 std::string
serial() { return serial_
; }
124 friend class base::RefCountedThreadSafe
<Device
>;
125 friend class AndroidDeviceManager
;
126 friend class AndroidWebSocket
;
128 Device(scoped_refptr
<base::MessageLoopProxy
> device_message_loop
,
129 scoped_refptr
<DeviceProvider
> provider
,
130 const std::string
& serial
);
134 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy_
;
135 scoped_refptr
<DeviceProvider
> provider_
;
137 std::set
<AndroidWebSocket
*> sockets_
;
138 base::WeakPtrFactory
<Device
> weak_factory_
;
140 DISALLOW_COPY_AND_ASSIGN(Device
);
143 typedef std::vector
<scoped_refptr
<Device
> > Devices
;
144 typedef base::Callback
<void(const Devices
&)> DevicesCallback
;
146 class DeviceProvider
: public base::RefCountedThreadSafe
<DeviceProvider
> {
148 typedef AndroidDeviceManager::SerialsCallback SerialsCallback
;
149 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback
;
150 typedef AndroidDeviceManager::SocketCallback SocketCallback
;
151 typedef AndroidDeviceManager::CommandCallback CommandCallback
;
153 virtual void QueryDevices(const SerialsCallback
& callback
) = 0;
155 virtual void QueryDeviceInfo(const std::string
& serial
,
156 const DeviceInfoCallback
& callback
) = 0;
158 virtual void OpenSocket(const std::string
& serial
,
159 const std::string
& socket_name
,
160 const SocketCallback
& callback
) = 0;
162 virtual void SendJsonRequest(const std::string
& serial
,
163 const std::string
& socket_name
,
164 const std::string
& request
,
165 const CommandCallback
& callback
);
167 virtual void HttpUpgrade(const std::string
& serial
,
168 const std::string
& socket_name
,
169 const std::string
& url
,
170 const std::string
& extensions
,
171 const HttpUpgradeCallback
& callback
);
173 virtual void ReleaseDevice(const std::string
& serial
);
176 friend class base::RefCountedThreadSafe
<DeviceProvider
>;
178 virtual ~DeviceProvider();
181 typedef std::vector
<scoped_refptr
<DeviceProvider
> > DeviceProviders
;
183 virtual ~AndroidDeviceManager();
185 static scoped_ptr
<AndroidDeviceManager
> Create();
187 void SetDeviceProviders(const DeviceProviders
& providers
);
189 void QueryDevices(const DevicesCallback
& callback
);
191 static std::string
GetBrowserName(const std::string
& socket
,
192 const std::string
& package
);
193 using RunCommandCallback
=
194 base::Callback
<void(const std::string
&, const CommandCallback
&)>;
196 static void QueryDeviceInfo(const RunCommandCallback
& command_callback
,
197 const DeviceInfoCallback
& callback
);
199 struct DeviceDescriptor
{
203 scoped_refptr
<DeviceProvider
> provider
;
207 typedef std::vector
<DeviceDescriptor
> DeviceDescriptors
;
210 class HandlerThread
: public base::RefCountedThreadSafe
<HandlerThread
> {
212 static scoped_refptr
<HandlerThread
> GetInstance();
213 scoped_refptr
<base::MessageLoopProxy
> message_loop();
216 friend class base::RefCountedThreadSafe
<HandlerThread
>;
217 static HandlerThread
* instance_
;
218 static void StopThread(base::Thread
* thread
);
221 virtual ~HandlerThread();
222 base::Thread
* thread_
;
225 AndroidDeviceManager();
227 void UpdateDevices(const DevicesCallback
& callback
,
228 scoped_ptr
<DeviceDescriptors
> descriptors
);
230 typedef std::map
<std::string
, base::WeakPtr
<Device
> > DeviceWeakMap
;
232 scoped_refptr
<HandlerThread
> handler_thread_
;
233 DeviceProviders providers_
;
234 DeviceWeakMap devices_
;
236 base::WeakPtrFactory
<AndroidDeviceManager
> weak_factory_
;
239 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_