Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
The Tor Project
Applications
Tor Browser
Commits
11a1a4ee
Commit
11a1a4ee
authored
Aug 02, 2020
by
Alex Catarineu
Committed by
Matthew Finkel
Mar 24, 2021
Browse files
Bug 40069: Add helpers for message passing with extensions
parent
7ce11d53
Changes
1
Hide whitespace changes
Inline
Side-by-side
toolkit/components/extensions/ExtensionParent.jsm
View file @
11a1a4ee
...
...
@@ -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
);
...
...
@@ -1878,6 +1884,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,
...
...
@@ -1889,6 +1934,8 @@ var ExtensionParent = {
promiseExtensionViewLoaded,
watchExtensionProxyContextLoad,
DebugUtils,
torSendExtensionMessage,
torWaitForExtensionMessage,
};
// browserPaintedPromise and browserStartupPromise are promises that
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment