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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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/. */
"use strict";
XPCOMUtils.defineLazyGetter(this, "devtools", function() {
const { devtools } =
Cu.import("resource://devtools/shared/Loader.jsm", {});
return devtools;
});
XPCOMUtils.defineLazyGetter(this, "DebuggerServer", function() {
const { DebuggerServer } = devtools.require("devtools/server/main");
return DebuggerServer;
});
XPCOMUtils.defineLazyGetter(this, "B2GTabList", function() {
const { B2GTabList } =
devtools.require("resource://gre/modules/DebuggerActors.js");
return B2GTabList;
});
// Load the discovery module eagerly, so that it can set a device name at
// startup. This does not cause discovery to start listening for packets, as
// that only happens once DevTools is enabled.
devtools.require("devtools/shared/discovery/discovery");
var RemoteDebugger = {
_listening: false,
/**
* Prompt the user to accept or decline the incoming connection.
*
* @param session object
* The session object will contain at least the following fields:
* {
* authentication,
* client: {
* host,
* port
* },
* server: {
* host,
* port
* }
* }
* Specific authentication modes may include additional fields. Check
* the different |allowConnection| methods in
* devtools/shared/security/auth.js.
* @return An AuthenticationResult value.
* A promise that will be resolved to the above is also allowed.
*/
allowConnection(session) {
if (this._promptingForAllow) {
// Don't stack connection prompts if one is already open
return DebuggerServer.AuthenticationResult.DENY;
}
this._listen();
this._promptingForAllow = new Promise(resolve => {
this._handleAllowResult = detail => {
this._handleAllowResult = null;
this._promptingForAllow = null;
// Newer Gaia supplies |authResult|, which is one of the
// AuthenticationResult values.
if (detail.authResult) {
resolve(detail.authResult);
} else if (detail.value) {
resolve(DebuggerServer.AuthenticationResult.ALLOW);
} else {
resolve(DebuggerServer.AuthenticationResult.DENY);
}
};
shell.sendChromeEvent({
type: "remote-debugger-prompt",
session
});
});
return this._promptingForAllow;
},
/**
* During OOB_CERT authentication, the user must transfer some data through some
* out of band mechanism from the client to the server to authenticate the
* devices.
*
* This implementation instructs Gaia to continually capture images which are
* passed back here and run through a QR decoder.
*
* @return An object containing:
* * sha256: hash(ClientCert)
* * k : K(random 128-bit number)
* A promise that will be resolved to the above is also allowed.
*/
receiveOOB() {
if (this._receivingOOB) {
return this._receivingOOB;
}
this._listen();
const QR = devtools.require("devtools/shared/qrcode/index");
this._receivingOOB = new Promise((resolve, reject) => {
this._handleAuthEvent = detail => {
debug(detail.action);
if (detail.action === "abort") {
this._handleAuthEvent = null;
this._receivingOOB = null;
reject();
return;
}
if (detail.action !== "capture") {
return;
}
let url = detail.url;
QR.decodeFromURI(url).then(data => {
debug("Got auth data: " + data);
let oob = JSON.parse(data);
shell.sendChromeEvent({
type: "devtools-auth",
action: "stop"
});
this._handleAuthEvent = null;
this._receivingOOB = null;
resolve(oob);
}).catch(() => {
debug("No auth data, requesting new capture");
shell.sendChromeEvent({
type: "devtools-auth",
action: "capture"
});
});
};
// Show QR scanning dialog, get an initial capture
shell.sendChromeEvent({
type: "devtools-auth",
action: "start"
});
});
return this._receivingOOB;
},
_listen: function() {
if (this._listening) {
return;
}
this.handleEvent = this.handleEvent.bind(this);
let content = shell.contentBrowser.contentWindow;
content.addEventListener("mozContentEvent", this, false, true);
this._listening = true;
},
handleEvent: function(event) {
let detail = event.detail;
if (detail.type === "remote-debugger-prompt" && this._handleAllowResult) {
this._handleAllowResult(detail);
}
if (detail.type === "devtools-auth" && this._handleAuthEvent) {
this._handleAuthEvent(detail);
}
},
initServer: function() {
if (DebuggerServer.initialized) {
return;
}
// Ask for remote connections.
DebuggerServer.init();
// /!\ Be careful when adding a new actor, especially global actors.
// Any new global actor will be exposed and returned by the root actor.
// Add Firefox-specific actors, but prevent tab actors to be loaded in
// the parent process, unless we enable certified apps debugging.
let restrictPrivileges = Services.prefs.getBoolPref("devtools.debugger.forbid-certified-apps");
DebuggerServer.addBrowserActors("navigator:browser", restrictPrivileges);
// Allow debugging of chrome for any process
if (!restrictPrivileges) {
DebuggerServer.allowChromeProcess = true;
}
/**
* Construct a root actor appropriate for use in a server running in B2G.
* The returned root actor respects the factories registered with
* DebuggerServer.addGlobalActor only if certified apps debugging is on,
* otherwise we used an explicit limited list of global actors
*
* * @param connection DebuggerServerConnection
* The conection to the client.
*/
DebuggerServer.createRootActor = function createRootActor(connection)
{
let parameters = {
tabList: new B2GTabList(connection),
// Use an explicit global actor list to prevent exposing
// unexpected actors
globalActorFactories: restrictPrivileges ? {
webappsActor: DebuggerServer.globalActorFactories.webappsActor,
deviceActor: DebuggerServer.globalActorFactories.deviceActor,
settingsActor: DebuggerServer.globalActorFactories.settingsActor
} : DebuggerServer.globalActorFactories
};
let { RootActor } = devtools.require("devtools/server/actors/root");
let root = new RootActor(connection, parameters);
root.applicationType = "operating-system";
return root;
};
if (isGonk) {
DebuggerServer.on("connectionchange", function() {
AdbController.updateState();
});
}
}
};
RemoteDebugger.allowConnection =
RemoteDebugger.allowConnection.bind(RemoteDebugger);
RemoteDebugger.receiveOOB =
RemoteDebugger.receiveOOB.bind(RemoteDebugger);
var USBRemoteDebugger = {
get isDebugging() {
if (!this._listener) {
return false;
}
return DebuggerServer._connections &&
Object.keys(DebuggerServer._connections).length > 0;
},
start: function() {
if (this._listener) {
return;
}
RemoteDebugger.initServer();
let portOrPath =
Services.prefs.getCharPref("devtools.debugger.unix-domain-socket") ||
"/data/local/debugger-socket";
try {
debug("Starting USB debugger on " + portOrPath);
let AuthenticatorType = DebuggerServer.Authenticators.get("PROMPT");
let authenticator = new AuthenticatorType.Server();
authenticator.allowConnection = RemoteDebugger.allowConnection;
this._listener = DebuggerServer.createListener();
this._listener.portOrPath = portOrPath;
this._listener.authenticator = authenticator;
this._listener.open();
// Temporary event, until bug 942756 lands and offers a way to know
// when the server is up and running.
Services.obs.notifyObservers(null, "debugger-server-started", null);
} catch (e) {
debug("Unable to start USB debugger server: " + e);
}
},
stop: function() {
if (!this._listener) {
return;
}
try {
this._listener.close();
this._listener = null;
} catch (e) {
debug("Unable to stop USB debugger server: " + e);
}
}
};
var WiFiRemoteDebugger = {
start: function() {
if (this._listener) {
return;
}
RemoteDebugger.initServer();
try {
debug("Starting WiFi debugger");
let AuthenticatorType = DebuggerServer.Authenticators.get("OOB_CERT");
let authenticator = new AuthenticatorType.Server();
authenticator.allowConnection = RemoteDebugger.allowConnection;
authenticator.receiveOOB = RemoteDebugger.receiveOOB;
this._listener = DebuggerServer.createListener();
this._listener.portOrPath = -1 /* any available port */;
this._listener.authenticator = authenticator;
this._listener.discoverable = true;
this._listener.encryption = true;
this._listener.open();
let port = this._listener.port;
debug("Started WiFi debugger on " + port);
} catch (e) {
debug("Unable to start WiFi debugger server: " + e);
}
},
stop: function() {
if (!this._listener) {
return;
}
try {
this._listener.close();
this._listener = null;
} catch (e) {
debug("Unable to stop WiFi debugger server: " + e);
}
}
};
(function() {
// Track these separately here so we can determine the correct value for the
// pref "devtools.debugger.remote-enabled", which is true when either mode of
// using DevTools is enabled.
let devtoolsUSB = false;
let devtoolsWiFi = false;
// Keep the old setting to not break people that won't have updated
// gaia and gecko.
SettingsListener.observe("devtools.debugger.remote-enabled", false,
function(value) {
devtoolsUSB = value;
Services.prefs.setBoolPref("devtools.debugger.remote-enabled",
devtoolsUSB || devtoolsWiFi);
// This preference is consulted during startup
Services.prefs.savePrefFile(null);
try {
value ? USBRemoteDebugger.start() : USBRemoteDebugger.stop();
} catch(e) {
dump("Error while initializing USB devtools: " +
e + "\n" + e.stack + "\n");
}
});
SettingsListener.observe("debugger.remote-mode", "disabled", function(value) {
if (["disabled", "adb-only", "adb-devtools"].indexOf(value) == -1) {
dump("Illegal value for debugger.remote-mode: " + value + "\n");
return;
}
devtoolsUSB = value == "adb-devtools";
Services.prefs.setBoolPref("devtools.debugger.remote-enabled",
devtoolsUSB || devtoolsWiFi);
// This preference is consulted during startup
Services.prefs.savePrefFile(null);
try {
(value == "adb-devtools") ? USBRemoteDebugger.start()
: USBRemoteDebugger.stop();
} catch(e) {
dump("Error while initializing USB devtools: " +
e + "\n" + e.stack + "\n");
}
isGonk && AdbController.setRemoteDebuggerState(value != "disabled");
});
SettingsListener.observe("devtools.remote.wifi.enabled", false,
function(value) {
devtoolsWiFi = value;
Services.prefs.setBoolPref("devtools.debugger.remote-enabled",
devtoolsUSB || devtoolsWiFi);
// Allow remote debugging on non-local interfaces when WiFi debug is enabled
// TODO: Bug 1034411: Lock down to WiFi interface, instead of all interfaces
Services.prefs.setBoolPref("devtools.debugger.force-local", !value);
// This preference is consulted during startup
Services.prefs.savePrefFile(null);
try {
value ? WiFiRemoteDebugger.start() : WiFiRemoteDebugger.stop();
} catch(e) {
dump("Error while initializing WiFi devtools: " +
e + "\n" + e.stack + "\n");
}
});
})();
|