Skip to content

Commit

Permalink
Bug 1536888 - Move the remote agent to a JSM. r=ato
Browse files Browse the repository at this point in the history
This will later help register the component statically in bug 1536862.
And already ease using the remote agent from other JSM files.

Differential Revision: https://phabricator.services.mozilla.com/D24227
  • Loading branch information
ochameau committed Mar 22, 2019
1 parent 845698d commit b1a10cc
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 47 deletions.
52 changes: 14 additions & 38 deletions remote/RemoteAgent.js → remote/RemoteAgent.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"use strict";

var EXPORTED_SYMBOLS = ["RemoteAgent"];

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");

Expand All @@ -28,8 +30,15 @@ const DEFAULT_HOST = "localhost";
const DEFAULT_PORT = 9222;
const LOOPBACKS = ["localhost", "127.0.0.1", "[::1]"];

class ParentRemoteAgent {
constructor() {
class RemoteAgentClass {
init() {
if (!Preferences.get(ENABLED, false)) {
throw new Error("Remote agent is disabled by its preference");
}
if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
throw new Error("Remote agent can only be instantiated from the parent process");
}

this.server = new HttpServer();
this.targets = new Targets();

Expand All @@ -52,10 +61,6 @@ class ParentRemoteAgent {
this.targets.on("disconnect", (eventName, target) => {
// TODO(ato): removing a handler is currently not possible
});

// This allows getting access to the underlying JS object
// of the @mozilla.org/remote/agent XPCOM components
this.wrappedJSObject = this;
}

get listening() {
Expand Down Expand Up @@ -173,6 +178,8 @@ class ParentRemoteAgent {
return;
}

this.init();

await Observer.once("sessionstore-windows-restored");
await this.tabs.start();

Expand All @@ -198,35 +205,4 @@ class ParentRemoteAgent {
}
}

const RemoteAgentFactory = {
instance_: null,

createInstance(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
if (!Preferences.get(ENABLED, false)) {
return null;
}

if (Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
}

if (!this.instance_) {
this.instance_ = new ParentRemoteAgent();
}
return this.instance_.QueryInterface(iid);
},
};

function RemoteAgent() {}

RemoteAgent.prototype = {
classDescription: "Remote Agent",
classID: Components.ID("{8f685a9d-8181-46d6-a71d-869289099c6d}"),
contractID: "@mozilla.org/remote/agent",
_xpcom_factory: RemoteAgentFactory, /* eslint-disable-line */
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RemoteAgent]);
var RemoteAgent = new RemoteAgentClass();
2 changes: 1 addition & 1 deletion remote/RemoteAgent.manifest
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
component {8f685a9d-8181-46d6-a71d-869289099c6d} RemoteAgent.js
component {8f685a9d-8181-46d6-a71d-869289099c6d} command-line-handler.js
contract @mozilla.org/remote/agent {8f685a9d-8181-46d6-a71d-869289099c6d}
category command-line-handler m-remote @mozilla.org/remote/agent
31 changes: 31 additions & 0 deletions remote/command-line-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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";

// Stopgap module until we can land bug 1536862 and remove this temporary file

const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");

const RemoteAgentFactory = {
createInstance(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}

return RemoteAgent.QueryInterface(iid);
},
};

function RemoteAgentComponent() {}

RemoteAgentComponent.prototype = {
classDescription: "Remote Agent",
classID: Components.ID("{8f685a9d-8181-46d6-a71d-869289099c6d}"),
contractID: "@mozilla.org/remote/agent",
_xpcom_factory: RemoteAgentFactory, /* eslint-disable-line */
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RemoteAgentComponent]);
2 changes: 2 additions & 0 deletions remote/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

remote.jar:
% content remote %content/
content/RemoteAgent.jsm (RemoteAgent.jsm)

content/Connection.jsm (Connection.jsm)
content/Error.jsm (Error.jsm)
content/JSONHandler.jsm (JSONHandler.jsm)
Expand Down
2 changes: 1 addition & 1 deletion remote/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DIRS += [
]

EXTRA_COMPONENTS += [
"RemoteAgent.js",
"command-line-handler.js",
"RemoteAgent.manifest",
]

Expand Down
3 changes: 1 addition & 2 deletions remote/targets/MainProcessTarget.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var EXPORTED_SYMBOLS = ["MainProcessTarget"];

const {Target} = ChromeUtils.import("chrome://remote/content/targets/Target.jsm");
const {Session} = ChromeUtils.import("chrome://remote/content/sessions/Session.jsm");
const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");

/**
* The main process Target.
Expand All @@ -29,8 +30,6 @@ class MainProcessTarget extends Target {
}

get wsDebuggerURL() {
const RemoteAgent = Cc["@mozilla.org/remote/agent"]
.getService(Ci.nsISupports).wrappedJSObject;
const {host, port} = RemoteAgent;
return `ws://${host}:${port}${this.path}`;
}
Expand Down
3 changes: 1 addition & 2 deletions remote/targets/TabTarget.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {Target} = ChromeUtils.import("chrome://remote/content/targets/Target.jsm"
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {TabSession} = ChromeUtils.import("chrome://remote/content/sessions/TabSession.jsm");
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "Favicons",
"@mozilla.org/browser/favicon-service;1", "nsIFaviconService");
Expand Down Expand Up @@ -101,8 +102,6 @@ class TabTarget extends Target {
}

get wsDebuggerURL() {
const RemoteAgent = Cc["@mozilla.org/remote/agent"]
.getService(Ci.nsISupports).wrappedJSObject;
const {host, port} = RemoteAgent;
return `ws://${host}:${port}${this.path}`;
}
Expand Down
4 changes: 3 additions & 1 deletion remote/test/browser/browser_cdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/* global getCDP */

const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");

// Test very basic CDP features.

const TEST_URI = "data:text/html;charset=utf-8,default-test-page";
Expand All @@ -28,7 +30,7 @@ async function testCDP() {
const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI);

// Start the CDP server
const RemoteAgent = Cc["@mozilla.org/remote/agent"].getService(Ci.nsISupports).wrappedJSObject;
RemoteAgent.init();
RemoteAgent.tabs.start();
RemoteAgent.listen(Services.io.newURI("http://localhost:9222"));

Expand Down
4 changes: 3 additions & 1 deletion remote/test/browser/browser_tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

// Test very basic CDP features.

const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");

const TEST_URI = "data:text/html;charset=utf-8,default-test-page";

add_task(async function() {
// Start the CDP server
const RemoteAgent = Cc["@mozilla.org/remote/agent"].getService(Ci.nsISupports).wrappedJSObject;
RemoteAgent.init();
RemoteAgent.tabs.start();
RemoteAgent.listen(Services.io.newURI("http://localhost:9222"));

Expand Down
4 changes: 3 additions & 1 deletion remote/test/browser/browser_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/* global getCDP */

const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");

// Test the Target domain

add_task(async function() {
Expand All @@ -23,7 +25,7 @@ add_task(async function() {

async function testCDP() {
// Start the CDP server
const RemoteAgent = Cc["@mozilla.org/remote/agent"].getService(Ci.nsISupports).wrappedJSObject;
RemoteAgent.init();
RemoteAgent.tabs.start();
RemoteAgent.listen(Services.io.newURI("http://localhost:9222"));

Expand Down

0 comments on commit b1a10cc

Please sign in to comment.