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/threading/non_thread_safe.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/gfx/size.h"
20 class AndroidDeviceManager
21 : public base::RefCountedThreadSafe<
23 content::BrowserThread::DeleteOnUIThread>,
24 public base::NonThreadSafe {
26 typedef base::Callback<void(int, const std::string&)> CommandCallback;
27 typedef base::Callback<void(int result, 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;
51 gfx::Size screen_size;
52 std::vector<BrowserInfo> browser_info;
55 typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback;
58 class AndroidWebSocket {
62 virtual void OnSocketOpened() = 0;
63 virtual void OnFrameRead(const std::string& message) = 0;
64 virtual void OnSocketClosed() = 0;
67 virtual ~Delegate() {}
72 void SendFrame(const std::string& message);
79 scoped_refptr<Device> device,
80 const std::string& socket_name,
81 const std::string& url,
82 AndroidWebSocket::Delegate* delegate);
83 void Connected(int result, scoped_ptr<net::StreamSocket> socket);
84 void OnFrameRead(const std::string& message);
85 void OnSocketClosed();
89 WebSocketImpl* socket_impl_;
91 base::WeakPtrFactory<AndroidWebSocket> weak_factory_;
92 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket);
97 class Device : public base::RefCountedThreadSafe<Device>,
98 public base::NonThreadSafe {
100 void QueryDeviceInfo(const DeviceInfoCallback& callback);
102 void OpenSocket(const std::string& socket_name,
103 const SocketCallback& callback);
105 void SendJsonRequest(const std::string& socket_name,
106 const std::string& request,
107 const CommandCallback& callback);
109 void HttpUpgrade(const std::string& socket_name,
110 const std::string& url,
111 const SocketCallback& callback);
112 AndroidWebSocket* CreateWebSocket(
113 const std::string& socket_name,
114 const std::string& url,
115 AndroidWebSocket::Delegate* delegate);
117 std::string serial() { return serial_; }
120 friend class base::RefCountedThreadSafe<Device>;
121 friend class AndroidDeviceManager;
122 friend class AndroidWebSocket;
124 Device(scoped_refptr<base::MessageLoopProxy> device_message_loop,
125 scoped_refptr<DeviceProvider> provider,
126 const std::string& serial);
130 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
131 scoped_refptr<DeviceProvider> provider_;
133 std::set<AndroidWebSocket*> sockets_;
134 base::WeakPtrFactory<Device> weak_factory_;
136 DISALLOW_COPY_AND_ASSIGN(Device);
139 typedef std::vector<scoped_refptr<Device> > Devices;
140 typedef base::Callback<void(const Devices&)> DevicesCallback;
142 class DeviceProvider : public base::RefCountedThreadSafe<DeviceProvider> {
144 typedef AndroidDeviceManager::SerialsCallback SerialsCallback;
145 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
146 typedef AndroidDeviceManager::SocketCallback SocketCallback;
147 typedef AndroidDeviceManager::CommandCallback CommandCallback;
149 virtual void QueryDevices(const SerialsCallback& callback) = 0;
151 virtual void QueryDeviceInfo(const std::string& serial,
152 const DeviceInfoCallback& callback) = 0;
154 virtual void OpenSocket(const std::string& serial,
155 const std::string& socket_name,
156 const SocketCallback& callback) = 0;
158 virtual void SendJsonRequest(const std::string& serial,
159 const std::string& socket_name,
160 const std::string& request,
161 const CommandCallback& callback);
163 virtual void HttpUpgrade(const std::string& serial,
164 const std::string& socket_name,
165 const std::string& url,
166 const SocketCallback& callback);
168 virtual void ReleaseDevice(const std::string& serial);
171 friend class base::RefCountedThreadSafe<DeviceProvider>;
173 virtual ~DeviceProvider();
176 typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
178 static scoped_refptr<AndroidDeviceManager> Create();
180 void SetDeviceProviders(const DeviceProviders& providers);
182 void QueryDevices(const DevicesCallback& callback);
184 struct DeviceDescriptor {
188 scoped_refptr<DeviceProvider> provider;
192 typedef std::vector<DeviceDescriptor> DeviceDescriptors;
195 class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
197 static scoped_refptr<HandlerThread> GetInstance();
198 scoped_refptr<base::MessageLoopProxy> message_loop();
201 friend class base::RefCountedThreadSafe<HandlerThread>;
202 static HandlerThread* instance_;
203 static void StopThread(base::Thread* thread);
206 virtual ~HandlerThread();
207 base::Thread* thread_;
210 friend struct content::BrowserThread::DeleteOnThread<
211 content::BrowserThread::UI>;
212 friend class base::DeleteHelper<AndroidDeviceManager>;
213 AndroidDeviceManager();
214 virtual ~AndroidDeviceManager();
216 void UpdateDevices(const DevicesCallback& callback,
217 DeviceDescriptors* descriptors);
219 typedef std::map<std::string, base::WeakPtr<Device> > DeviceWeakMap;
221 scoped_refptr<HandlerThread> handler_thread_;
222 DeviceProviders providers_;
223 DeviceWeakMap devices_;
226 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_