Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / devtools / device / android_device_manager.h
blob831b13de2cd67f306febf348ee4099913367bb9d
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_
8 #include <vector>
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"
17 namespace net {
18 class StreamSocket;
21 class AndroidDeviceManager : public base::NonThreadSafe {
22 public:
23 typedef base::Callback<void(int, const std::string&)> CommandCallback;
24 typedef base::Callback<void(int result, scoped_ptr<net::StreamSocket>)>
25 SocketCallback;
26 typedef base::Callback<void(
27 int result, const std::string& extensions, scoped_ptr<net::StreamSocket>)>
28 HttpUpgradeCallback;
29 typedef base::Callback<void(const std::vector<std::string>&)> SerialsCallback;
31 struct BrowserInfo {
32 BrowserInfo();
34 enum Type {
35 kTypeChrome,
36 kTypeWebView,
37 kTypeOther
40 std::string socket_name;
41 std::string display_name;
42 std::string user;
43 Type type;
46 struct DeviceInfo {
47 DeviceInfo();
48 ~DeviceInfo();
50 std::string model;
51 bool connected;
52 gfx::Size screen_size;
53 std::vector<BrowserInfo> browser_info;
56 typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback;
57 class Device;
59 class AndroidWebSocket {
60 public:
61 class Delegate {
62 public:
63 virtual void OnSocketOpened() = 0;
64 virtual void OnFrameRead(const std::string& message) = 0;
65 virtual void OnSocketClosed() = 0;
67 protected:
68 virtual ~Delegate() {}
71 ~AndroidWebSocket();
73 void SendFrame(const std::string& message);
75 private:
76 friend class Device;
77 class WebSocketImpl;
79 AndroidWebSocket(
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();
89 void Terminate();
91 Device* device_;
92 WebSocketImpl* socket_impl_;
93 Delegate* delegate_;
94 base::WeakPtrFactory<AndroidWebSocket> weak_factory_;
95 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket);
98 class DeviceProvider;
100 class Device : public base::RefCountedThreadSafe<Device>,
101 public base::NonThreadSafe {
102 public:
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_; }
123 private:
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);
132 virtual ~Device();
134 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
135 scoped_refptr<DeviceProvider> provider_;
136 std::string serial_;
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> {
147 public:
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);
175 protected:
176 friend class base::RefCountedThreadSafe<DeviceProvider>;
177 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 {
200 DeviceDescriptor();
201 ~DeviceDescriptor();
203 scoped_refptr<DeviceProvider> provider;
204 std::string serial;
207 typedef std::vector<DeviceDescriptor> DeviceDescriptors;
209 private:
210 class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
211 public:
212 static scoped_refptr<HandlerThread> GetInstance();
213 scoped_refptr<base::MessageLoopProxy> message_loop();
215 private:
216 friend class base::RefCountedThreadSafe<HandlerThread>;
217 static HandlerThread* instance_;
218 static void StopThread(base::Thread* thread);
220 HandlerThread();
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_