gecko-dev/devtools/server/actors/targets/webextension-proxy.js
Julian Descottes ef047a50ac Bug 1497264 - Split addon/webextension.js into webextension.js and webextension-proxy.js;r=ochameau,yulia
Depends on D15384

Differential Revision: https://phabricator.services.mozilla.com/D15385

--HG--
rename : devtools/server/actors/addon/webextension.js => devtools/server/actors/targets/webextension-proxy.js
extra : moz-landing-system : lando
2019-01-08 18:18:14 +00:00

94 lines
2.5 KiB
JavaScript

/* 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";
const {DebuggerServer} = require("devtools/server/main");
loader.lazyImporter(this, "ExtensionParent", "resource://gre/modules/ExtensionParent.jsm");
function WebExtensionTargetActorProxy(connection, parentActor) {
this._conn = connection;
this._parentActor = parentActor;
this.addonId = parentActor.addonId;
this._onChildExit = this._onChildExit.bind(this);
this._form = null;
this._browser = null;
this._childActorID = null;
}
WebExtensionTargetActorProxy.prototype = {
/**
* Connect the webextension child actor.
*/
async connect() {
if (this._browser) {
throw new Error("This actor is already connected to the extension process");
}
// Called when the debug browser element has been destroyed
// (no actor is using it anymore to connect the child extension process).
const onDestroy = this.destroy.bind(this);
this._browser = await ExtensionParent.DebugUtils.getExtensionProcessBrowser(this);
this._form = await DebuggerServer.connectToFrame(this._conn, this._browser, onDestroy,
{addonId: this.addonId});
this._childActorID = this._form.actor;
// Exit the proxy child actor if the child actor has been destroyed.
this._mm.addMessageListener("debug:webext_child_exit", this._onChildExit);
return this._form;
},
get isOOP() {
return this._browser ? this._browser.isRemoteBrowser : undefined;
},
get _mm() {
return this._browser && (
this._browser.messageManager ||
this._browser.frameLoader.messageManager);
},
destroy() {
if (this._mm) {
this._mm.removeMessageListener("debug:webext_child_exit", this._onChildExit);
this._mm.sendAsyncMessage("debug:webext_parent_exit", {
actor: this._childActorID,
});
ExtensionParent.DebugUtils.releaseExtensionProcessBrowser(this);
}
if (this._parentActor) {
this._parentActor.onProxyDestroy();
}
this._parentActor = null;
this._browser = null;
this._childActorID = null;
this._form = null;
},
/**
* Handle the child actor exit.
*/
_onChildExit(msg) {
if (msg.json.actor !== this._childActorID) {
return;
}
this.destroy();
},
};
exports.WebExtensionTargetActorProxy = WebExtensionTargetActorProxy;