This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
scanner.js
221 lines (198 loc) · 6.96 KB
/
scanner.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { Cu } = require("chrome");
const EventEmitter =
require("./devtools-require")("devtools/shared/event-emitter");
const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
const unload = require("./unload");
const { ConnectionManager } =
require("./devtools-require")("devtools/shared/client/connection-manager");
const { Devices } =
require("./devtools-import")("resource://devtools/shared/apps/Devices.jsm");
const Runtimes =
require("./devtools-require")("devtools/client/webide/modules/runtimes");
let Scanner = {
_runtimes: [],
enable() {
this._updateRuntimes = this._updateRuntimes.bind(this);
Devices.on("register", this._updateRuntimes);
Devices.on("unregister", this._updateRuntimes);
Devices.on("addon-status-updated", this._updateRuntimes);
this._updateRuntimes();
},
disable() {
Devices.off("register", this._updateRuntimes);
Devices.off("unregister", this._updateRuntimes);
Devices.off("addon-status-updated", this._updateRuntimes);
},
_emitUpdated() {
this.emit("runtime-list-updated");
},
_updateRuntimes() {
if (this._updatingPromise) {
return this._updatingPromise;
}
this._runtimes = [];
let promises = [];
for (let id of Devices.available()) {
let device = Devices.getByName(id);
promises.push(this._detectRuntimes(device));
}
this._updatingPromise = Promise.all(promises);
this._updatingPromise.then(() => {
this._emitUpdated();
this._updatingPromise = null;
}, () => {
this._updatingPromise = null;
});
return this._updatingPromise;
},
_detectRuntimes: Task.async(function* (device) {
let model = yield device.getModel();
let detectedRuntimes = yield FirefoxOSRuntime.detect(device, model);
this._runtimes.push(...detectedRuntimes);
detectedRuntimes = yield FirefoxOnAndroidRuntime.detect(device, model);
this._runtimes.push(...detectedRuntimes);
}),
scan() {
return this._updateRuntimes();
},
listRuntimes() {
return this._runtimes;
}
};
EventEmitter.decorate(Scanner);
function Runtime(device, model, socketPath) {
this.device = device;
this._model = model;
this._socketPath = socketPath;
}
Runtime.prototype = {
type: Runtimes.RuntimeTypes.USB,
connect(connection) {
let port = ConnectionManager.getFreeTCPPort();
let local = "tcp:" + port;
let remote;
if (this._socketPath.startsWith("@")) {
remote = "localabstract:" + this._socketPath.substring(1);
} else {
remote = "localfilesystem:" + this._socketPath;
}
return this.device.forwardPort(local, remote).then(() => {
connection.host = "localhost";
connection.port = port;
connection.connect();
});
},
get id() {
return this.device.id + "|" + this._socketPath;
},
};
function FirefoxOSRuntime(device, model) {
Runtime.call(this, device, model, "/data/local/debugger-socket");
}
FirefoxOSRuntime.detect = Task.async(function* (device, model) {
let runtimes = [];
let query = "test -f /system/b2g/b2g; echo $?";
let b2gExists = yield device.shell(query);
// XXX: Sometimes we get an empty response back. Likely a bug in our shell
// code in this add-on.
// There are also some Android devices that do not have `test` installed.
for (let attempts = 3; attempts > 0; attempts--) {
b2gExists = yield device.shell(query);
if (b2gExists.length == 3) {
break;
}
}
if (b2gExists === "0\r\n") {
let runtime = new FirefoxOSRuntime(device, model);
console.log("Found " + runtime.name);
runtimes.push(runtime);
}
return runtimes;
});
FirefoxOSRuntime.prototype = Object.create(Runtime.prototype);
Object.defineProperty(FirefoxOSRuntime.prototype, "name", {
get() {
return this._model || this.device.id;
}
});
function FirefoxOnAndroidRuntime(device, model, socketPath) {
Runtime.call(this, device, model, socketPath);
}
// This requires Unix socket support from Firefox for Android (35+)
FirefoxOnAndroidRuntime.detect = Task.async(function* (device, model) {
let runtimes = [];
// A matching entry looks like:
// 00000000: 00000002 00000000 00010000 0001 01 6551588 /data/data/org.mozilla.fennec/firefox-debugger-socket
let query = "cat /proc/net/unix";
let rawSocketInfo = yield device.shell(query);
let socketInfos = rawSocketInfo.split(/\r?\n/);
// Filter to lines with "firefox-debugger-socket"
socketInfos = socketInfos.filter(l => l.includes("firefox-debugger-socket"));
// It's possible to have multiple lines with the same path, so de-dupe them
let socketPaths = new Set();
for (let socketInfo of socketInfos) {
let socketPath = socketInfo.split(" ").pop();
socketPaths.add(socketPath);
}
for (let socketPath of socketPaths) {
let runtime = new FirefoxOnAndroidRuntime(device, model, socketPath);
console.log("Found " + runtime.name);
runtimes.push(runtime);
}
return runtimes;
});
FirefoxOnAndroidRuntime.prototype = Object.create(Runtime.prototype);
Object.defineProperty(FirefoxOnAndroidRuntime.prototype, "name", {
get() {
// If using abstract socket address, it is "@org.mozilla.firefox/..."
// If using path base socket, it is "/data/data/<package>...""
// Until Fennec 62 only supports path based UNIX domain socket, but
// Fennec 63+ supports both path based and abstract socket.
const packageName = this._socketPath.startsWith("@") ?
this._socketPath.substr(1).split("/")[0] :
this._socketPath.split("/")[3];
let channel;
switch (packageName) {
case "org.mozilla.firefox":
channel = "";
break;
case "org.mozilla.firefox_beta":
channel = " Beta";
break;
case "org.mozilla.fennec_aurora":
// This package name is now the one for Firefox Nightly distributed
// through the Google Play Store since "dawn project"
// cf. https://bugzilla.mozilla.org/show_bug.cgi?id=1357351#c8
channel = " Nightly";
break;
case "org.mozilla.fennec":
channel = " Nightly";
break;
default:
channel = " Custom";
}
return "Firefox" + channel + " on Android (" +
(this._model || this.device.id) + ")";
}
});
exports.register = function() {
// Only register our |Scanner| if the API exists
if (Runtimes && Runtimes.RuntimeScanners) {
// There may be an older ADB scanner registered by default
// If so, we must disable it to avoid duplicate runtimes
if (Runtimes.DeprecatedAdbScanner) {
Runtimes.RuntimeScanners.remove(Runtimes.DeprecatedAdbScanner);
unload(() => {
Runtimes.RuntimeScanners.add(Runtimes.DeprecatedAdbScanner);
});
}
// Add our scanner
Runtimes.RuntimeScanners.add(Scanner);
unload(() => {
Runtimes.RuntimeScanners.remove(Scanner);
});
}
};