1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
#define CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "ui/gfx/geometry/size.h"
namespace net {
class StreamSocket;
}
class AndroidDeviceManager : public base::NonThreadSafe {
public:
typedef base::Callback<void(int, const std::string&)> CommandCallback;
typedef base::Callback<void(int result, scoped_ptr<net::StreamSocket>)>
SocketCallback;
typedef base::Callback<void(
int result, const std::string& extensions, scoped_ptr<net::StreamSocket>)>
HttpUpgradeCallback;
typedef base::Callback<void(const std::vector<std::string>&)> SerialsCallback;
struct BrowserInfo {
BrowserInfo();
enum Type {
kTypeChrome,
kTypeWebView,
kTypeOther
};
std::string socket_name;
std::string display_name;
std::string user;
Type type;
};
struct DeviceInfo {
DeviceInfo();
~DeviceInfo();
std::string model;
bool connected;
gfx::Size screen_size;
std::vector<BrowserInfo> browser_info;
};
typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback;
class Device;
class AndroidWebSocket {
public:
class Delegate {
public:
virtual void OnSocketOpened() = 0;
virtual void OnFrameRead(const std::string& message) = 0;
virtual void OnSocketClosed() = 0;
protected:
virtual ~Delegate() {}
};
~AndroidWebSocket();
void SendFrame(const std::string& message);
private:
friend class Device;
class WebSocketImpl;
AndroidWebSocket(
scoped_refptr<Device> device,
const std::string& socket_name,
const std::string& url,
AndroidWebSocket::Delegate* delegate);
void Connected(int result,
const std::string& extensions,
scoped_ptr<net::StreamSocket> socket);
void OnFrameRead(const std::string& message);
void OnSocketClosed();
void Terminate();
Device* device_;
WebSocketImpl* socket_impl_;
Delegate* delegate_;
base::WeakPtrFactory<AndroidWebSocket> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket);
};
class DeviceProvider;
class Device : public base::RefCountedThreadSafe<Device>,
public base::NonThreadSafe {
public:
void QueryDeviceInfo(const DeviceInfoCallback& callback);
void OpenSocket(const std::string& socket_name,
const SocketCallback& callback);
void SendJsonRequest(const std::string& socket_name,
const std::string& request,
const CommandCallback& callback);
void HttpUpgrade(const std::string& socket_name,
const std::string& url,
const std::string& extensions,
const HttpUpgradeCallback& callback);
AndroidWebSocket* CreateWebSocket(
const std::string& socket_name,
const std::string& url,
AndroidWebSocket::Delegate* delegate);
std::string serial() { return serial_; }
private:
friend class base::RefCountedThreadSafe<Device>;
friend class AndroidDeviceManager;
friend class AndroidWebSocket;
Device(scoped_refptr<base::MessageLoopProxy> device_message_loop,
scoped_refptr<DeviceProvider> provider,
const std::string& serial);
virtual ~Device();
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
scoped_refptr<DeviceProvider> provider_;
std::string serial_;
std::set<AndroidWebSocket*> sockets_;
base::WeakPtrFactory<Device> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Device);
};
typedef std::vector<scoped_refptr<Device> > Devices;
typedef base::Callback<void(const Devices&)> DevicesCallback;
class DeviceProvider : public base::RefCountedThreadSafe<DeviceProvider> {
public:
typedef AndroidDeviceManager::SerialsCallback SerialsCallback;
typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
typedef AndroidDeviceManager::SocketCallback SocketCallback;
typedef AndroidDeviceManager::CommandCallback CommandCallback;
virtual void QueryDevices(const SerialsCallback& callback) = 0;
virtual void QueryDeviceInfo(const std::string& serial,
const DeviceInfoCallback& callback) = 0;
virtual void OpenSocket(const std::string& serial,
const std::string& socket_name,
const SocketCallback& callback) = 0;
virtual void SendJsonRequest(const std::string& serial,
const std::string& socket_name,
const std::string& request,
const CommandCallback& callback);
virtual void HttpUpgrade(const std::string& serial,
const std::string& socket_name,
const std::string& url,
const std::string& extensions,
const HttpUpgradeCallback& callback);
virtual void ReleaseDevice(const std::string& serial);
protected:
friend class base::RefCountedThreadSafe<DeviceProvider>;
DeviceProvider();
virtual ~DeviceProvider();
};
typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
virtual ~AndroidDeviceManager();
static scoped_ptr<AndroidDeviceManager> Create();
void SetDeviceProviders(const DeviceProviders& providers);
void QueryDevices(const DevicesCallback& callback);
static std::string GetBrowserName(const std::string& socket,
const std::string& package);
using RunCommandCallback =
base::Callback<void(const std::string&, const CommandCallback&)>;
static void QueryDeviceInfo(const RunCommandCallback& command_callback,
const DeviceInfoCallback& callback);
struct DeviceDescriptor {
DeviceDescriptor();
~DeviceDescriptor();
scoped_refptr<DeviceProvider> provider;
std::string serial;
};
typedef std::vector<DeviceDescriptor> DeviceDescriptors;
private:
class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
public:
static scoped_refptr<HandlerThread> GetInstance();
scoped_refptr<base::MessageLoopProxy> message_loop();
private:
friend class base::RefCountedThreadSafe<HandlerThread>;
static HandlerThread* instance_;
static void StopThread(base::Thread* thread);
HandlerThread();
virtual ~HandlerThread();
base::Thread* thread_;
};
AndroidDeviceManager();
void UpdateDevices(const DevicesCallback& callback,
scoped_ptr<DeviceDescriptors> descriptors);
typedef std::map<std::string, base::WeakPtr<Device> > DeviceWeakMap;
scoped_refptr<HandlerThread> handler_thread_;
DeviceProviders providers_;
DeviceWeakMap devices_;
base::WeakPtrFactory<AndroidDeviceManager> weak_factory_;
};
#endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
|