2019-01-29 18:18:42 +03:00
|
|
|
/* 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";
|
|
|
|
|
2019-06-26 23:25:43 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["RemoteAgent", "RemoteAgentFactory"];
|
2019-03-22 12:46:50 +03:00
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2019-11-19 16:54:15 +03:00
|
|
|
FatalError: "chrome://remote/content/Error.jsm",
|
2019-01-29 18:18:42 +03:00
|
|
|
HttpServer: "chrome://remote/content/server/HTTPD.jsm",
|
2019-03-08 01:18:45 +03:00
|
|
|
JSONHandler: "chrome://remote/content/JSONHandler.jsm",
|
2019-01-29 18:18:42 +03:00
|
|
|
Log: "chrome://remote/content/Log.jsm",
|
2019-11-19 16:54:15 +03:00
|
|
|
NetUtil: "resource://gre/modules/NetUtil.jsm",
|
|
|
|
Observer: "chrome://remote/content/Observer.jsm",
|
2019-01-29 18:18:42 +03:00
|
|
|
Preferences: "resource://gre/modules/Preferences.jsm",
|
2019-03-05 12:05:16 +03:00
|
|
|
RecommendedPreferences: "chrome://remote/content/RecommendedPreferences.jsm",
|
2019-03-10 15:50:57 +03:00
|
|
|
Targets: "chrome://remote/content/targets/Targets.jsm",
|
2019-01-29 18:18:42 +03:00
|
|
|
});
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "log", Log.get);
|
|
|
|
|
|
|
|
const ENABLED = "remote.enabled";
|
|
|
|
const FORCE_LOCAL = "remote.force-local";
|
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
const DEFAULT_HOST = "localhost";
|
|
|
|
const DEFAULT_PORT = 9222;
|
2019-02-17 20:33:59 +03:00
|
|
|
const LOOPBACKS = ["localhost", "127.0.0.1", "[::1]"];
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-03-22 12:46:50 +03:00
|
|
|
class RemoteAgentClass {
|
2019-11-19 16:54:15 +03:00
|
|
|
async init() {
|
2019-03-22 12:46:50 +03:00
|
|
|
if (!Preferences.get(ENABLED, false)) {
|
2019-11-19 16:54:15 +03:00
|
|
|
throw new Error("Remote agent is disabled by its preference");
|
2019-03-22 12:46:50 +03:00
|
|
|
}
|
|
|
|
if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
|
|
|
|
throw new Error(
|
|
|
|
"Remote agent can only be instantiated from the parent process"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
if (this.server) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.server = new HttpServer();
|
|
|
|
this.targets = new Targets();
|
|
|
|
|
|
|
|
// Register the static HTTP endpoints like /json/version or /json/list
|
|
|
|
this.server.registerPrefixHandler("/json/", new JSONHandler(this));
|
|
|
|
|
|
|
|
// Register the dynamic HTTP endpoint of each target.
|
|
|
|
// These are WebSocket URL where the HTTP request will be morphed
|
|
|
|
// into a WebSocket connectiong after an handshake.
|
|
|
|
this.targets.on("target-created", (eventName, target) => {
|
|
|
|
if (!target.path) {
|
|
|
|
throw new Error(`Target is missing 'path' attribute: ${target}`);
|
|
|
|
}
|
|
|
|
this.server.registerPathHandler(target.path, target);
|
|
|
|
});
|
|
|
|
this.targets.on("target-destroyed", (eventName, target) => {
|
|
|
|
// TODO: This removes the entry added by registerPathHandler, should rather expose
|
|
|
|
// an unregisterPathHandler method on nsHttpServer.
|
|
|
|
delete this.server._handler._overridePaths[target.path];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get listening() {
|
|
|
|
return !!this.server && !this.server._socketClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async listen(address) {
|
|
|
|
if (!(address instanceof Ci.nsIURI)) {
|
|
|
|
throw new TypeError(`Expected nsIURI: ${address}`);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
let { host, port } = address;
|
2019-01-29 18:18:42 +03:00
|
|
|
if (Preferences.get(FORCE_LOCAL) && !LOOPBACKS.includes(host)) {
|
|
|
|
throw new Error("Restricted to loopback devices");
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIServerSocket uses -1 for atomic port allocation
|
|
|
|
if (port === 0) {
|
|
|
|
port = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.listening) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
await this.init();
|
2019-11-19 15:32:41 +03:00
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
// Start watching for targets *after* registering the target listeners
|
|
|
|
// as this will fire event for already-existing targets.
|
|
|
|
await this.targets.watchForTargets();
|
2019-07-18 12:48:18 +03:00
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
try {
|
2019-04-04 14:16:27 +03:00
|
|
|
// Immediatly instantiate the main process target in order
|
|
|
|
// to be accessible via HTTP endpoint on startup
|
|
|
|
const mainTarget = this.targets.getMainProcessTarget();
|
2019-05-13 17:43:21 +03:00
|
|
|
|
|
|
|
this.server._start(port, host);
|
2019-11-19 16:54:15 +03:00
|
|
|
dump(`DevTools listening on ${mainTarget.wsDebuggerURL}\n`);
|
2019-01-29 18:18:42 +03:00
|
|
|
} catch (e) {
|
2019-03-08 01:14:15 +03:00
|
|
|
throw new Error(`Unable to start remote agent: ${e.message}`, e);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
2019-11-19 16:54:15 +03:00
|
|
|
|
|
|
|
Preferences.set(RecommendedPreferences);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2019-11-19 16:54:15 +03:00
|
|
|
if (this.listening) {
|
|
|
|
try {
|
|
|
|
// Destroy all the targets first in order to ensure closing all pending
|
|
|
|
// connections first. Otherwise Httpd's stop is not going to resolve.
|
2019-07-10 19:32:35 +03:00
|
|
|
this.targets.destructor();
|
2019-03-21 17:18:03 +03:00
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
await this.server.stop();
|
2019-11-19 16:54:15 +03:00
|
|
|
|
|
|
|
Preferences.reset(Object.keys(RecommendedPreferences));
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Unable to stop agent: ${e.message}`, e);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
2019-11-19 16:54:15 +03:00
|
|
|
|
|
|
|
log.info("Stopped listening");
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get scheme() {
|
|
|
|
if (!this.server) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.server.identity.primaryScheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
get host() {
|
|
|
|
if (!this.server) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.server.identity.primaryHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
get port() {
|
|
|
|
if (!this.server) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.server.identity.primaryPort;
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:54:15 +03:00
|
|
|
// nsICommandLineHandler
|
|
|
|
|
|
|
|
async handle(cmdLine) {
|
|
|
|
function flag(name) {
|
|
|
|
const caseSensitive = true;
|
|
|
|
try {
|
|
|
|
return cmdLine.handleFlagWithParam(name, caseSensitive);
|
|
|
|
} catch (e) {
|
|
|
|
return cmdLine.handleFlag(name, caseSensitive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteDebugger = flag("remote-debugger");
|
|
|
|
const remoteDebuggingPort = flag("remote-debugging-port");
|
|
|
|
|
|
|
|
if (remoteDebugger && remoteDebuggingPort) {
|
|
|
|
log.fatal(
|
|
|
|
"Conflicting flags --remote-debugger and --remote-debugging-port"
|
|
|
|
);
|
|
|
|
cmdLine.preventDefault = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!remoteDebugger && !remoteDebuggingPort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let host, port;
|
|
|
|
if (typeof remoteDebugger == "string") {
|
|
|
|
[host, port] = remoteDebugger.split(":");
|
|
|
|
} else if (typeof remoteDebuggingPort == "string") {
|
|
|
|
port = remoteDebuggingPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
let addr;
|
|
|
|
try {
|
|
|
|
addr = NetUtil.newURI(
|
|
|
|
`http://${host || DEFAULT_HOST}:${port || DEFAULT_PORT}/`
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
log.fatal(
|
|
|
|
`Expected address syntax [<host>]:<port>: ${remoteDebugger ||
|
|
|
|
remoteDebuggingPort}`
|
|
|
|
);
|
|
|
|
cmdLine.preventDefault = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Observer.once("sessionstore-windows-restored");
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.listen(addr);
|
|
|
|
} catch (e) {
|
|
|
|
this.close();
|
|
|
|
throw new FatalError(
|
|
|
|
`Unable to start remote agent on ${addr.spec}: ${e.message}`,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get helpInfo() {
|
|
|
|
return (
|
|
|
|
" --remote-debugger [<host>][:<port>]\n" +
|
|
|
|
" --remote-debugging-port <port> Start the Firefox remote agent, which is \n" +
|
|
|
|
" a low-level debugging interface based on the CDP protocol.\n" +
|
|
|
|
" Defaults to listen on localhost:9222.\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-29 18:18:42 +03:00
|
|
|
// XPCOM
|
|
|
|
|
|
|
|
get QueryInterface() {
|
2019-11-19 16:54:15 +03:00
|
|
|
return ChromeUtils.generateQI([Ci.nsICommandLineHandler]);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 12:46:50 +03:00
|
|
|
var RemoteAgent = new RemoteAgentClass();
|
2019-06-26 23:25:43 +03:00
|
|
|
|
|
|
|
// This is used by the XPCOM codepath which expects a constructor
|
|
|
|
var RemoteAgentFactory = function() {
|
|
|
|
return RemoteAgent;
|
|
|
|
};
|