Skip to content

Instantly share code, notes, and snippets.

@hotsoft-desenv2
Created June 26, 2020 15:59
Show Gist options
  • Save hotsoft-desenv2/6b061ee278366bc5be12f0601562ceb3 to your computer and use it in GitHub Desktop.
Save hotsoft-desenv2/6b061ee278366bc5be12f0601562ceb3 to your computer and use it in GitHub Desktop.
resource://gre/modules/remotepagemanager/RemotePageManagerChild.jsm
/* 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";
var EXPORTED_SYMBOLS = ["ChildMessagePort"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { MessagePort } = ChromeUtils.import(
"resource://gre/modules/remotepagemanager/MessagePort.jsm"
);
// The content side of a message port
class ChildMessagePort extends MessagePort {
constructor(window) {
let portID =
Services.appinfo.processID + ":" + ChildMessagePort.nextPortID++;
super(window.docShell.messageManager, portID);
this.window = window;
// Add functionality to the content page
Cu.exportFunction(this.sendAsyncMessage.bind(this), window, {
defineAs: "RPMSendAsyncMessage",
});
Cu.exportFunction(this.addMessageListener.bind(this), window, {
defineAs: "RPMAddMessageListener",
allowCallbacks: true,
});
Cu.exportFunction(this.removeMessageListener.bind(this), window, {
defineAs: "RPMRemoveMessageListener",
allowCallbacks: true,
});
// The actor form only needs the functions set up above. The actor
// will send and receive messages directly.
if (!(this.messageManager instanceof Ci.nsIMessageSender)) {
return;
}
// Send a message for load events
let loadListener = () => {
this.sendAsyncMessage("RemotePage:Load");
window.removeEventListener("load", loadListener);
};
window.addEventListener("load", loadListener);
// Destroy the port when the window is unloaded
window.addEventListener("unload", () => {
try {
this.sendAsyncMessage("RemotePage:Unload");
} catch (e) {
// If the tab has been closed the frame message manager has already been
// destroyed
}
this.destroy();
});
// Tell the main process to set up its side of the message pipe.
this.messageManager.sendAsyncMessage("RemotePage:InitPort", {
portID,
url: window.document.documentURI.replace(/[\#|\?].*$/, ""),
});
}
// Called when the content process is requesting some data.
async handleRequest(name, data) {
throw new Error(`Unknown request ${name}.`);
}
// Called when a message is received from the message manager or actor.
handleMessage(messagedata) {
let message = {
name: messagedata.name,
data: messagedata.data,
};
this.listener.callListeners(Cu.cloneInto(message, this.window));
}
destroy() {
this.window = null;
super.destroy.call(this);
}
}
ChildMessagePort.nextPortID = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment