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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// 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_DEVTOOLS_ANDROID_BRIDGE_H_
#define CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/prefs/pref_change_registrar.h"
#include "chrome/browser/devtools/device/android_device_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/devtools_agent_host.h"
#include "ui/gfx/geometry/size.h"
namespace base {
template<typename T> struct DefaultSingletonTraits;
class MessageLoop;
class DictionaryValue;
class ListValue;
class Thread;
} // namespace base
namespace content {
class BrowserContext;
}
class DevToolsTargetImpl;
class PortForwardingController;
class Profile;
class DevToolsAndroidBridge : public KeyedService {
public:
class Factory : public BrowserContextKeyedServiceFactory {
public:
// Returns singleton instance of DevToolsAndroidBridge.
static Factory* GetInstance();
// Returns DevToolsAndroidBridge associated with |profile|.
static DevToolsAndroidBridge* GetForProfile(Profile* profile);
private:
friend struct base::DefaultSingletonTraits<Factory>;
Factory();
~Factory() override;
// BrowserContextKeyedServiceFactory overrides:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(Factory);
};
typedef std::pair<std::string, std::string> BrowserId;
class RemotePage : public base::RefCounted<RemotePage> {
public:
const std::string& serial() { return browser_id_.first; }
const std::string& socket() { return browser_id_.second; }
const std::string& frontend_url() { return frontend_url_; }
private:
friend class base::RefCounted<RemotePage>;
friend class DevToolsAndroidBridge;
RemotePage(const BrowserId& browser_id, const base::DictionaryValue& dict);
virtual ~RemotePage();
BrowserId browser_id_;
std::string frontend_url_;
scoped_ptr<base::DictionaryValue> dict_;
DISALLOW_COPY_AND_ASSIGN(RemotePage);
};
typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
public:
const std::string& serial() { return browser_id_.first; }
const std::string& socket() { return browser_id_.second; }
const std::string& display_name() { return display_name_; }
const std::string& user() { return user_; }
const std::string& version() { return version_; }
const RemotePages& pages() { return pages_; }
bool IsChrome();
std::string GetId();
typedef std::vector<int> ParsedVersion;
ParsedVersion GetParsedVersion();
private:
friend class base::RefCounted<RemoteBrowser>;
friend class DevToolsAndroidBridge;
RemoteBrowser(const std::string& serial,
const AndroidDeviceManager::BrowserInfo& browser_info);
virtual ~RemoteBrowser();
BrowserId browser_id_;
std::string display_name_;
std::string user_;
AndroidDeviceManager::BrowserInfo::Type type_;
std::string version_;
RemotePages pages_;
DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
};
typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
class RemoteDevice : public base::RefCounted<RemoteDevice> {
public:
std::string serial() { return serial_; }
std::string model() { return model_; }
bool is_connected() { return connected_; }
RemoteBrowsers& browsers() { return browsers_; }
gfx::Size screen_size() { return screen_size_; }
private:
friend class base::RefCounted<RemoteDevice>;
friend class DevToolsAndroidBridge;
RemoteDevice(const std::string& serial,
const AndroidDeviceManager::DeviceInfo& device_info);
virtual ~RemoteDevice();
std::string serial_;
std::string model_;
bool connected_;
RemoteBrowsers browsers_;
gfx::Size screen_size_;
DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
};
typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
class DeviceListListener {
public:
virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
protected:
virtual ~DeviceListListener() {}
};
explicit DevToolsAndroidBridge(Profile* profile);
void AddDeviceListListener(DeviceListListener* listener);
void RemoveDeviceListListener(DeviceListListener* listener);
class DeviceCountListener {
public:
virtual void DeviceCountChanged(int count) = 0;
protected:
virtual ~DeviceCountListener() {}
};
void AddDeviceCountListener(DeviceCountListener* listener);
void RemoveDeviceCountListener(DeviceCountListener* listener);
typedef int PortStatus;
typedef std::map<int, PortStatus> PortStatusMap;
typedef std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>
BrowserStatus;
typedef std::vector<BrowserStatus> ForwardingStatus;
class PortForwardingListener {
public:
typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus;
typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus;
virtual void PortStatusChanged(const ForwardingStatus&) = 0;
protected:
virtual ~PortForwardingListener() {}
};
void AddPortForwardingListener(PortForwardingListener* listener);
void RemovePortForwardingListener(PortForwardingListener* listener);
void set_device_providers_for_test(
const AndroidDeviceManager::DeviceProviders& device_providers) {
device_manager_->SetDeviceProviders(device_providers);
}
void set_task_scheduler_for_test(
base::Callback<void(const base::Closure&)> scheduler) {
task_scheduler_ = scheduler;
}
bool HasDevToolsWindow(const std::string& agent_id);
// Creates new target instance owned by caller.
DevToolsTargetImpl* CreatePageTarget(scoped_refptr<RemotePage> browser);
typedef base::Callback<void(scoped_refptr<RemotePage>)> RemotePageCallback;
void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
const std::string& url);
scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
scoped_refptr<RemoteBrowser> browser);
void SendJsonRequest(const std::string& browser_id_str,
const std::string& url,
const JsonRequestCallback& callback);
private:
friend struct content::BrowserThread::DeleteOnThread<
content::BrowserThread::UI>;
friend class base::DeleteHelper<DevToolsAndroidBridge>;
friend class PortForwardingController;
class AgentHostDelegate;
class DiscoveryRequest;
class RemotePageTarget;
~DevToolsAndroidBridge() override;
void StartDeviceListPolling();
void StopDeviceListPolling();
bool NeedsDeviceListPolling();
typedef std::pair<scoped_refptr<AndroidDeviceManager::Device>,
scoped_refptr<RemoteDevice>> CompleteDevice;
typedef std::vector<CompleteDevice> CompleteDevices;
typedef base::Callback<void(const CompleteDevices&)> DeviceListCallback;
void RequestDeviceList(const DeviceListCallback& callback);
void ReceivedDeviceList(const CompleteDevices& complete_devices);
void StartDeviceCountPolling();
void StopDeviceCountPolling();
void RequestDeviceCount(const base::Callback<void(int)>& callback);
void ReceivedDeviceCount(int count);
static void ScheduleTaskDefault(const base::Closure& task);
void CreateDeviceProviders();
void SendJsonRequest(const BrowserId& browser_id,
const std::string& url,
const JsonRequestCallback& callback);
void SendProtocolCommand(const BrowserId& browser_id,
const std::string& target_path,
const std::string& method,
scoped_ptr<base::DictionaryValue> params,
const base::Closure callback);
scoped_refptr<AndroidDeviceManager::Device> FindDevice(
const std::string& serial);
base::WeakPtr<DevToolsAndroidBridge> AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
Profile* const profile_;
const scoped_ptr<AndroidDeviceManager> device_manager_;
typedef std::map<std::string, scoped_refptr<AndroidDeviceManager::Device>>
DeviceMap;
DeviceMap device_map_;
typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates;
AgentHostDelegates host_delegates_;
typedef std::vector<DeviceListListener*> DeviceListListeners;
DeviceListListeners device_list_listeners_;
base::CancelableCallback<void(const CompleteDevices&)> device_list_callback_;
typedef std::vector<DeviceCountListener*> DeviceCountListeners;
DeviceCountListeners device_count_listeners_;
base::CancelableCallback<void(int)> device_count_callback_;
base::Callback<void(const base::Closure&)> task_scheduler_;
typedef std::vector<PortForwardingListener*> PortForwardingListeners;
PortForwardingListeners port_forwarding_listeners_;
scoped_ptr<PortForwardingController> port_forwarding_controller_;
PrefChangeRegistrar pref_change_registrar_;
base::WeakPtrFactory<DevToolsAndroidBridge> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
};
#endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
|