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, {
|
|
|
|
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",
|
|
|
|
Preferences: "resource://gre/modules/Preferences.jsm",
|
2019-03-05 12:05:16 +03:00
|
|
|
RecommendedPreferences: "chrome://remote/content/RecommendedPreferences.jsm",
|
2020-01-06 12:32:09 +03:00
|
|
|
TargetList: "chrome://remote/content/targets/TargetList.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-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-22 11:02:56 +03:00
|
|
|
get listening() {
|
|
|
|
return !!this.server && !this.server.isStopped();
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:57:13 +03:00
|
|
|
listen(url) {
|
2019-03-22 12:46:50 +03:00
|
|
|
if (!Preferences.get(ENABLED, false)) {
|
2019-12-04 21:26:36 +03:00
|
|
|
throw Components.Exception(
|
|
|
|
"Disabled by preference",
|
|
|
|
Cr.NS_ERROR_NOT_AVAILABLE
|
|
|
|
);
|
2019-03-22 12:46:50 +03:00
|
|
|
}
|
|
|
|
if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
|
2019-12-04 21:26:36 +03:00
|
|
|
throw Components.Exception(
|
|
|
|
"May only be instantiated in parent process",
|
|
|
|
Cr.NS_ERROR_LAUNCHED_CHILD_PROCESS
|
2019-03-22 12:46:50 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:03:10 +03:00
|
|
|
if (!(url instanceof Ci.nsIURI)) {
|
|
|
|
url = Services.io.newURI(url);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:03:10 +03:00
|
|
|
let { host, port } = url;
|
2019-01-29 18:18:42 +03:00
|
|
|
if (Preferences.get(FORCE_LOCAL) && !LOOPBACKS.includes(host)) {
|
2019-12-04 21:26:36 +03:00
|
|
|
throw Components.Exception(
|
|
|
|
"Restricted to loopback devices",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE
|
|
|
|
);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIServerSocket uses -1 for atomic port allocation
|
|
|
|
if (port === 0) {
|
|
|
|
port = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.listening) {
|
2019-12-04 14:57:13 +03:00
|
|
|
return Promise.resolve();
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:03:05 +03:00
|
|
|
Preferences.set(RecommendedPreferences);
|
|
|
|
|
2019-11-22 11:02:56 +03:00
|
|
|
this.server = new HttpServer();
|
|
|
|
this.server.registerPrefixHandler("/json/", new JSONHandler(this));
|
2019-11-19 15:32:41 +03:00
|
|
|
|
2020-01-06 12:32:09 +03:00
|
|
|
this.targets = new TargetList();
|
2019-11-22 11:02:56 +03:00
|
|
|
this.targets.on("target-created", (eventName, target) => {
|
|
|
|
this.server.registerPathHandler(target.path, target);
|
|
|
|
});
|
|
|
|
this.targets.on("target-destroyed", (eventName, target) => {
|
2020-01-03 19:19:33 +03:00
|
|
|
this.server.registerPathHandler(target.path, null);
|
2019-11-22 11:02:56 +03:00
|
|
|
});
|
2019-07-18 12:48:18 +03:00
|
|
|
|
2019-12-04 14:57:13 +03:00
|
|
|
return this.asyncListen(host, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
async asyncListen(host, port) {
|
2019-01-29 18:18:42 +03:00
|
|
|
try {
|
2019-11-22 11:02:56 +03:00
|
|
|
await this.targets.watchForTargets();
|
|
|
|
|
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-22 11:03:08 +03:00
|
|
|
Services.obs.notifyObservers(
|
|
|
|
null,
|
|
|
|
"remote-listening",
|
|
|
|
mainTarget.wsDebuggerURL
|
|
|
|
);
|
2019-01-29 18:18:42 +03:00
|
|
|
} catch (e) {
|
2019-11-22 11:03:03 +03:00
|
|
|
await this.close();
|
2019-12-04 21:26:36 +03:00
|
|
|
log.error(`Unable to start remote agent: ${e.message}`, e);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:57:06 +03:00
|
|
|
close() {
|
2019-11-22 11:03:03 +03:00
|
|
|
try {
|
|
|
|
// if called early at startup, preferences may not be available
|
2019-11-19 16:54:15 +03:00
|
|
|
try {
|
2019-11-22 11:03:03 +03:00
|
|
|
Preferences.reset(Object.keys(RecommendedPreferences));
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
// destroy targets before stopping server,
|
|
|
|
// otherwise the HTTP will fail to stop
|
|
|
|
if (this.targets) {
|
2019-07-10 19:32:35 +03:00
|
|
|
this.targets.destructor();
|
2019-11-22 11:03:03 +03:00
|
|
|
}
|
2019-03-21 17:18:03 +03:00
|
|
|
|
2019-11-22 11:03:03 +03:00
|
|
|
if (this.listening) {
|
2019-12-04 14:57:06 +03:00
|
|
|
return this.server.stop();
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
2019-11-22 11:03:03 +03:00
|
|
|
} catch (e) {
|
|
|
|
// this function must never fail
|
|
|
|
log.error("unable to stop listener", e);
|
|
|
|
} finally {
|
|
|
|
this.server = null;
|
|
|
|
this.targets = null;
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
2019-12-04 14:57:06 +03:00
|
|
|
|
|
|
|
return Promise.resolve();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XPCOM
|
|
|
|
|
|
|
|
get QueryInterface() {
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
return ChromeUtils.generateQI(["nsIRemoteAgent"]);
|
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;
|
|
|
|
};
|