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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
// Copyright (c) 2012 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.
/**
* ADB Device representation. This class has static methods for querying for
* devices as well as instance methods for device manipulation.
* @param {string} deviceLine Raw device descriprion line.
* @constructor
*/
function AdbDevice(deviceLine) {
var tokens = deviceLine.split(/[ \t]/);
this.serial = tokens[0];
var modelQuery = 'host:transport:' + this.serial +
'|shell:getprop ro.product.model';
this.model = String(AdbDevice.adbQuery_(modelQuery)).trim();
}
/**
* Returns discoverable devices, establishes port forwarding for them.
* @return {Array.<AdbDevice>} Device array.
*/
AdbDevice.queryDevices = function() {
var deviceList = AdbDevice.adbQuery_('host:devices');
if (!deviceList)
return [];
var forwards = AdbDevice.collectForwards_();
var rows = deviceList.split('\n');
var devices = [];
for (var i = 0; i < rows.length; i++) {
if (!rows[i])
continue;
var device = new AdbDevice(rows[i]);
devices.push(device);
// Assign / bind TCP ports.
device.tcpPort = forwards[device.serial];
if (!device.tcpPort) {
var port = AdbDevice.nextAvailablePort_(forwards);
if (!port)
continue;
AdbDevice.adbQuery_('host-serial:' + device.serial + ':forward:tcp:' +
port + ';localabstract:chrome_devtools_remote');
forwards = AdbDevice.collectForwards_();
}
}
return devices;
};
/**
* Collects and returns port forward map for all connected devices.
* @return {Array.<Object<string, string>>} Forwarding map.
* @private
*/
AdbDevice.collectForwards_ = function() {
var response = AdbDevice.adbQuery_('host:list-forward');
if (!response)
return [];
var forwards = {};
var rows = response.split('\n');
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (!row)
continue;
var tokens = row.split(' ');
if (tokens.length != 3 ||
tokens[1].indexOf('tcp:') != 0 ||
tokens[2] != 'localabstract:chrome_devtools_remote')
continue;
var tcpPort = tokens[1].substring(4);
forwards[tokens[0]] = tcpPort;
}
return forwards;
};
/**
* Issues synchronous adb query.
* @param {string} query ADB query.
* @return {?Object} ADB query result.
* @private
*/
AdbDevice.adbQuery_ = function(query) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'adb-query/' + query, false);
xhr.send(null);
if (xhr.status !== 200)
return null;
try {
var result = JSON.parse(xhr.responseText);
return result[0] ? null : result[1];
} catch (e) {
}
return null;
};
/**
* Discovers ADB devices.
* @return {?Object} ADB query result.
* @private
*/
AdbDevice.adbPages_ = function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'adb-pages', false);
xhr.send(null);
if (xhr.status !== 200)
return null;
try {
return JSON.parse(xhr.responseText);
} catch (e) {
}
return null;
};
/**
* Returns next available (unmapped) port to use in forwarding.
* @param {Object<string,string>} forwards Forwards map.
* @return {number} Next available port.
* @private
*/
AdbDevice.nextAvailablePort_ = function(forwards) {
for (var port = 9232; port < 9252; port++) {
var taken = false;
for (var serial in forwards) {
if (forwards[serial] == port) {
taken = true;
break;
}
}
if (taken)
continue;
return port;
}
return 0;
};
/**
* Returns /json/version JSON object with target device description.
* @return {?Object} Version object.
*/
AdbDevice.prototype.version = function() {
return this.queryJson_('version');
};
/**
* Returns the list of inspectable targets in the target format suitable
* for rendering as target rows.
* @return {Array.<Object>} Target list.
*/
AdbDevice.prototype.targets = function() {
var pages = this.queryJson_('list');
var targets = [];
for (var j = 0; pages && j < pages.length; j++) {
var json = pages[j];
var target = {};
target['type'] = 'mobile';
target['name'] = json['title'];
target['url'] = json['url'];
target['attached'] = !json['webSocketDebuggerUrl'];
target['faviconUrl'] = json['faviconUrl'];
target['inspectUrl'] = json['devtoolsFrontendUrl'];
targets.push(target);
}
return targets;
};
/**
* Issues synchronous json request against target device.
* @param {string} query DevTools protocol /json query.
* @return {?Object} Result object.
* @private
*/
AdbDevice.prototype.queryJson_ = function(query) {
if (!this.tcpPort)
return null;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'local-xhr/' + this.tcpPort + '/json/' + query, false);
xhr.send(null);
if (xhr.status !== 200)
return null;
try {
var result = JSON.parse(xhr.responseText);
return result[0] ? null : JSON.parse(result[1]);
} catch (e) {
}
return null;
};
function requestData() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'targets-data.json', false);
xhr.send(null);
if (xhr.status === 200)
return JSON.parse(xhr.responseText);
return [];
}
function inspect(data) {
if (data['inspectUrl']) {
window.open(data['inspectUrl'], undefined,
'location=0,width=800,height=600');
return;
}
chrome.send('inspect',
[String(data.processId), String(data.routeId)]);
}
function terminate(data) {
chrome.send('terminate',
[String(data.processId), String(data.routeId)]);
}
function removeChildren(element_id) {
var element = document.getElementById(element_id);
element.textContent = '';
}
function onload() {
populateLists();
populateDeviceLists();
}
function populateLists() {
var data = requestData();
removeChildren('pages');
removeChildren('extensions');
removeChildren('workers');
removeChildren('others');
for (var i = 0; i < data.length; i++) {
if (data[i].type === 'page')
addToPagesList(data[i]);
else if (data[i].type === 'worker')
addToWorkersList(data[i]);
else if (data[i].type === 'extension')
addToExtensionsList(data[i]);
else
addToOthersList(data[i]);
}
}
function populateDeviceLists() {
// Clear existing entries
var deviceElements = document.querySelectorAll('.device');
for (var i = 0; i < deviceElements.length; i++)
deviceElements[i].remove();
var devices = AdbDevice.queryDevices();
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
var version = device.version();
if (!version)
continue;
var targets = device.targets();
if (!targets.length)
continue;
var sectionElement = document.createElement('div');
sectionElement.className = 'section device';
var details = version['Browser'] || version['User-Agent'];
sectionElement.textContent = device.model + ' (' + details + ')';
var listElement = document.createElement('div');
listElement.id = 'device-' + device.serial;
listElement.className = 'list device';
document.body.appendChild(sectionElement);
document.body.appendChild(listElement);
for (var j = 0; j < targets.length; j++) {
addTargetToList(targets[j], 'device-' + device.serial,
['faviconUrl', 'name', 'url']);
}
}
setTimeout(populateDeviceLists, 1000);
}
function addToPagesList(data) {
addTargetToList(data, 'pages', ['faviconUrl', 'name', 'url']);
}
function addToExtensionsList(data) {
addTargetToList(data, 'extensions', ['name', 'url']);
}
function addToWorkersList(data) {
addTargetToList(data,
'workers',
['name', 'url', 'pid'],
true);
}
function addToOthersList(data) {
addTargetToList(data, 'others', ['url']);
}
function formatValue(data, property) {
var value = data[property];
if (property == 'faviconUrl') {
var faviconElement = document.createElement('img');
if (value)
faviconElement.src = value;
return faviconElement;
}
var text = value ? String(value) : '';
if (text.length > 100)
text = text.substring(0, 100) + '\u2026';
if (property == 'pid')
text = 'Pid:' + text;
var span = document.createElement('span');
span.textContent = ' ' + text + ' ';
span.className = property;
return span;
}
function addTargetToList(data, listId, properties, canTerminate) {
var list = document.getElementById(listId);
var row = document.createElement('div');
row.className = 'row';
for (var j = 0; j < properties.length; j++)
row.appendChild(formatValue(data, properties[j]));
row.appendChild(createInspectElement(data));
if (canTerminate)
row.appendChild(createTerminateElement(data));
row.processId = data.processId;
row.routeId = data.routeId;
list.appendChild(row);
}
function createInspectElement(data) {
var link = document.createElement('a');
link.setAttribute('href', '#');
link.textContent = ' inspect ';
link.addEventListener(
'click',
inspect.bind(this, data),
true);
return link;
}
function createTerminateElement(data) {
var link = document.createElement('a');
if (data.attached)
link.disabled = true;
link.setAttribute('href', '#');
link.textContent = ' terminate ';
link.addEventListener(
'click',
terminate.bind(this, data),
true);
return link;
}
document.addEventListener('DOMContentLoaded', onload);
|