[tor-commits] [tor-browser/tor-browser-82.0b7-10.0-1] Bug 40069: Add helpers for message passing with extensions
gk at torproject.org
gk at torproject.org
Tue Oct 6 10:52:14 UTC 2020
commit 4165b74f5723ed05161d4f6099943874882cce23
Author: Alex Catarineu <acat at torproject.org>
Date: Sun Aug 2 19:12:25 2020 +0200
Bug 40069: Add helpers for message passing with extensions
---
toolkit/components/extensions/ExtensionParent.jsm | 47 +++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/toolkit/components/extensions/ExtensionParent.jsm b/toolkit/components/extensions/ExtensionParent.jsm
index eda752bbc153..e364db86bb6e 100644
--- a/toolkit/components/extensions/ExtensionParent.jsm
+++ b/toolkit/components/extensions/ExtensionParent.jsm
@@ -264,6 +264,8 @@ const ProxyMessenger = {
/** @type Map<number, ParentPort> */
ports: new Map(),
+ _torRuntimeMessageListeners: [],
+
init() {
this.conduit = new BroadcastConduit(ProxyMessenger, {
id: "ProxyMessenger",
@@ -326,6 +328,10 @@ const ProxyMessenger = {
},
async recvRuntimeMessage(arg, { sender }) {
+ // We need to listen to some extension messages in Tor Browser
+ for (const listener of this._torRuntimeMessageListeners) {
+ listener(arg);
+ }
arg.firstResponse = true;
let kind = await this.normalizeArgs(arg, sender);
let result = await this.conduit.castRuntimeMessage(kind, arg);
@@ -1873,6 +1879,45 @@ for (let name of StartupCache.STORE_NAMES) {
StartupCache[name] = new CacheStore(name);
}
+async function torSendExtensionMessage(extensionId, message) {
+ // This should broadcast the message to all children "conduits"
+ // listening for a "RuntimeMessage". Those children conduits
+ // will either be extension background pages or other extension
+ // pages listening to browser.runtime.onMessage.
+ const result = await ProxyMessenger.conduit.castRuntimeMessage("messenger", {
+ extensionId,
+ holder: new StructuredCloneHolder(message),
+ firstResponse: true,
+ sender: {
+ id: extensionId,
+ envType: "addon_child",
+ },
+ });
+ return result
+ ? result.value
+ : Promise.reject({ message: ERROR_NO_RECEIVERS });
+}
+
+async function torWaitForExtensionMessage(extensionId, checker) {
+ return new Promise(resolve => {
+ const msgListener = msg => {
+ try {
+ if (msg && msg.extensionId === extensionId) {
+ const deserialized = msg.holder.deserialize({});
+ if (checker(deserialized)) {
+ const idx = ProxyMessenger._torRuntimeMessageListeners.indexOf(
+ msgListener
+ );
+ ProxyMessenger._torRuntimeMessageListeners.splice(idx, 1);
+ resolve(deserialized);
+ }
+ }
+ } catch (e) {}
+ };
+ ProxyMessenger._torRuntimeMessageListeners.push(msgListener);
+ });
+}
+
var ExtensionParent = {
GlobalManager,
HiddenExtensionPage,
@@ -1884,6 +1929,8 @@ var ExtensionParent = {
promiseExtensionViewLoaded,
watchExtensionProxyContextLoad,
DebugUtils,
+ torSendExtensionMessage,
+ torWaitForExtensionMessage,
};
// browserPaintedPromise and browserStartupPromise are promises that
More information about the tor-commits
mailing list