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;
57 class AndroidWebSocket {
61 virtual void OnSocketOpened() = 0;
62 virtual void OnFrameRead(const std::string& message) = 0;
63 virtual void OnSocketClosed() = 0;
66 virtual ~Delegate() {}
69 virtual ~AndroidWebSocket() {}
71 virtual void SendFrame(const std::string& message) = 0;
76 class Device : public base::RefCountedThreadSafe<Device>,
77 public base::NonThreadSafe {
79 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
80 typedef AndroidDeviceManager::CommandCallback CommandCallback;
81 typedef AndroidDeviceManager::SocketCallback SocketCallback;
83 void QueryDeviceInfo(const DeviceInfoCallback& callback);
85 void OpenSocket(const std::string& socket_name,
86 const SocketCallback& callback);
88 void SendJsonRequest(const std::string& socket_name,
89 const std::string& request,
90 const CommandCallback& callback);
92 void HttpUpgrade(const std::string& socket_name,
93 const std::string& url,
94 const SocketCallback& callback);
96 AndroidWebSocket* CreateWebSocket(
97 const std::string& socket_name,
98 const std::string& url,
99 AndroidWebSocket::Delegate* delegate);
101 std::string serial() { return serial_; }
104 friend class AndroidDeviceManager;
105 Device(scoped_refptr<base::MessageLoopProxy> device_message_loop,
106 scoped_refptr<DeviceProvider> provider,
107 const std::string& serial);
109 friend class base::RefCountedThreadSafe<Device>;
112 scoped_refptr<base::MessageLoopProxy> device_message_loop_;
113 scoped_refptr<DeviceProvider> provider_;
115 base::WeakPtrFactory<Device> weak_factory_;
117 DISALLOW_COPY_AND_ASSIGN(Device);
120 typedef std::vector<scoped_refptr<Device> > Devices;
121 typedef base::Callback<void(const Devices&)> DevicesCallback;
123 class DeviceProvider : public base::RefCountedThreadSafe<DeviceProvider> {
125 typedef AndroidDeviceManager::SerialsCallback SerialsCallback;
126 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
127 typedef AndroidDeviceManager::SocketCallback SocketCallback;
128 typedef AndroidDeviceManager::CommandCallback CommandCallback;
130 virtual void QueryDevices(const SerialsCallback& callback) = 0;
132 virtual void QueryDeviceInfo(const std::string& serial,
133 const DeviceInfoCallback& callback) = 0;
135 virtual void OpenSocket(const std::string& serial,
136 const std::string& socket_name,
137 const SocketCallback& callback) = 0;
139 virtual void SendJsonRequest(const std::string& serial,
140 const std::string& socket_name,
141 const std::string& request,
142 const CommandCallback& callback);
144 virtual void HttpUpgrade(const std::string& serial,
145 const std::string& socket_name,
146 const std::string& url,
147 const SocketCallback& callback);
149 virtual void ReleaseDevice(const std::string& serial);
152 friend class base::RefCountedThreadSafe<DeviceProvider>;
154 virtual ~DeviceProvider();
157 typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
159 static scoped_refptr<AndroidDeviceManager> Create();
161 void SetDeviceProviders(const DeviceProviders& providers);
163 void QueryDevices(const DevicesCallback& callback);
165 struct DeviceDescriptor {
169 scoped_refptr<DeviceProvider> provider;
173 typedef std::vector<DeviceDescriptor> DeviceDescriptors;
176 class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
178 static scoped_refptr<HandlerThread> GetInstance();
179 scoped_refptr<base::MessageLoopProxy> message_loop();
182 friend class base::RefCountedThreadSafe<HandlerThread>;
183 static HandlerThread* instance_;
184 static void StopThread(base::Thread* thread);
187 virtual ~HandlerThread();
188 base::Thread* thread_;
191 friend struct content::BrowserThread::DeleteOnThread<
192 content::BrowserThread::UI>;
193 friend class base::DeleteHelper<AndroidDeviceManager>;
194 AndroidDeviceManager();
195 virtual ~AndroidDeviceManager();
197 void UpdateDevices(const DevicesCallback& callback,
198 DeviceDescriptors* descriptors);
200 typedef std::map<std::string, base::WeakPtr<Device> > DeviceWeakMap;
202 scoped_refptr<HandlerThread> handler_thread_;
203 DeviceProviders providers_;
204 DeviceWeakMap devices_;
207 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_