Skip to content

Commit

Permalink
Bug 1693805 - [remote] Reorganize RemoteAgent component. r=webdriver-…
Browse files Browse the repository at this point in the history
…reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D119265
  • Loading branch information
whimboo committed Jul 9, 2021
1 parent 295cad8 commit 82582b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 47 deletions.
9 changes: 6 additions & 3 deletions remote/cdp/CDP.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class CDP {
*/
constructor(agent) {
this.agent = agent;
this.targetList = new TargetList();

RecommendedPreferences.applyPreferences(RECOMMENDED_PREFS);
this.targetList = null;
}

get address() {
Expand All @@ -63,8 +61,11 @@ class CDP {
* Starts the CDP support.
*/
async start() {
RecommendedPreferences.applyPreferences(RECOMMENDED_PREFS);

this.agent.server.registerPrefixHandler("/json/", new JSONHandler(this));

this.targetList = new TargetList();
this.targetList.on("target-created", (eventName, target) => {
this.agent.server.registerPathHandler(target.path, target);
});
Expand All @@ -89,6 +90,8 @@ class CDP {
*/
stop() {
this.targetList.destructor();
this.targetList = null;

RecommendedPreferences.restorePreferences(RECOMMENDED_PREFS);
}
}
80 changes: 36 additions & 44 deletions remote/components/RemoteAgent.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,42 @@ const { XPCOMUtils } = ChromeUtils.import(
);

XPCOMUtils.defineLazyModuleGetters(this, {
Preferences: "resource://gre/modules/Preferences.jsm",
Services: "resource://gre/modules/Services.jsm",

CDP: "chrome://remote/content/cdp/CDP.jsm",
HttpServer: "chrome://remote/content/server/HTTPD.jsm",
Log: "chrome://remote/content/shared/Log.jsm",
Preferences: "resource://gre/modules/Preferences.jsm",
});

XPCOMUtils.defineLazyGetter(this, "logger", () => Log.get());

const PREF_ACTIVE_PROTOCOLS = "remote.active-protocols";
const PREF_FORCE_LOCAL = "remote.force-local";
XPCOMUtils.defineLazyGetter(this, "activeProtocols", () => {
const protocols = Services.prefs.getIntPref("remote.active-protocols");
if (protocols < 1 || protocols > 3) {
throw Error(`Invalid remote protocol identifier: ${protocols}`);
}

return protocols;
});

// const BIDI_ACTIVE = 0x1;
const CDP_ACTIVE = 0x2;

// By default force local connections only
const LOOPBACKS = ["localhost", "127.0.0.1", "[::1]"];
const PREF_FORCE_LOCAL = "remote.force-local";

class RemoteAgentClass {
constructor() {
this.cdp = null;
this.server = null;

const protocols = Services.prefs.getIntPref(PREF_ACTIVE_PROTOCOLS);
if (protocols < 1 || protocols > 3) {
throw Error(`Invalid remote protocol identifier: ${protocols}`);
if ((activeProtocols & CDP_ACTIVE) === CDP_ACTIVE) {
this.cdp = new CDP(this);
logger.debug("CDP enabled");
} else {
this.cdp = null;
}
this.activeProtocols = protocols;
}

get listening() {
return !!this.server && !this.server.isStopped();
}

get debuggerAddress() {
Expand All @@ -53,6 +57,26 @@ class RemoteAgentClass {
return `${this.host}:${this.port}`;
}

get host() {
// Bug 1675471: When using the nsIRemoteAgent interface the HTTPd server's
// primary identity ("this.server.identity.primaryHost") is lazily set.
return this.server?._host;
}

get listening() {
return !!this.server && !this.server.isStopped();
}

get port() {
// Bug 1675471: When using the nsIRemoteAgent interface the HTTPd server's
// primary identity ("this.server.identity.primaryPort") is lazily set.
return this.server?._port;
}

get scheme() {
return this.server?.identity.primaryScheme;
}

listen(url) {
if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
throw Components.Exception(
Expand Down Expand Up @@ -84,10 +108,6 @@ class RemoteAgentClass {

this.server = new HttpServer();

if ((this.activeProtocols & CDP_ACTIVE) === CDP_ACTIVE) {
this.cdp = new CDP(this);
}

return this.asyncListen(host, port);
}

Expand Down Expand Up @@ -115,40 +135,12 @@ class RemoteAgentClass {
// this function must never fail
logger.error("unable to stop listener", e);
} finally {
this.cdp = null;
this.server = null;
}

return Promise.resolve();
}

get scheme() {
if (!this.server) {
return null;
}
return this.server.identity.primaryScheme;
}

get host() {
if (!this.server) {
return null;
}

// Bug 1675471: When using the nsIRemoteAgent interface the HTTPd server's
// primary identity ("this.server.identity.primaryHost") is lazily set.
return this.server._host;
}

get port() {
if (!this.server) {
return null;
}

// Bug 1675471: When using the nsIRemoteAgent interface the HTTPd server's
// primary identity ("this.server.identity.primaryPort") is lazily set.
return this.server._port;
}

// XPCOM

get QueryInterface() {
Expand Down

0 comments on commit 82582b3

Please sign in to comment.