Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/child/bootstrap.js

Issue 29329742: Issue 3251 - Simplify messaging from child scripts to parent (Closed)
Patch Set: Rebased, no return value stringification Created Nov. 9, 2015, 11:01 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/child/contentPolicy.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/child/bootstrap.js
===================================================================
--- a/lib/child/bootstrap.js
+++ b/lib/child/bootstrap.js
@@ -18,16 +18,17 @@
(function()
{
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
let {Loader, main, unload} = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {});
+ let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
let loader = null;
let shutdownHandlers = [];
let onShutdown =
{
done: false,
add: function(handler)
@@ -45,24 +46,60 @@
addMessageListener("AdblockPlus:Info", init);
addMessageListener("AdblockPlus:Shutdown", shutdown);
function init(message)
{
removeMessageListener("AdblockPlus:Info", init);
+ let callbackPrefix = Services.appinfo.processID + " ";
+ let maxCallbackID = 0;
+ let callbacks = new Map();
+
+ function sendSyncMessageSingleResponse(messageName, data)
+ {
+ return sendSyncMessage(messageName, {data})[0];
+ }
+
+ function sendAsyncMessageWithResponse(messageName, data, callback)
+ {
+ data = {data};
+ if (callback)
+ {
+ let callbackID = callbackPrefix + (++maxCallbackID);
+ callbacks.set(callbackID, callback);
+ data.callbackID = callbackID;
+ }
+ sendAsyncMessage(messageName, data);
+ }
+
+ function onResponse(message)
+ {
+ let {callbackID, response} = message.data;
+ if (callbacks.has(callbackID))
+ {
+ let callback = callbacks.get(callbackID);
+ callbacks.delete(callbackID);
+ callback(response);
+ }
+ }
+ addMessageListener("AdblockPlus:Response", onResponse);
+ onShutdown.add(() => removeMessageListener("AdblockPlus:Response", onResponse));
+
let info = message.data;
loader = Loader({
paths: {
"": info.addonRoot + "lib/"
},
globals: {
Components, Cc, Ci, Cu, Cr, atob, btoa, onShutdown,
- addMessageListener, removeMessageListener, sendAsyncMessage, sendSyncMessage
+ addMessageListener, removeMessageListener,
+ sendAsyncMessage: sendAsyncMessageWithResponse,
+ sendSyncMessage: sendSyncMessageSingleResponse
},
modules: {"info": info},
id: info.addonID
});
onShutdown.add(() => unload(loader, "disable"))
main(loader, "child/main");
}
« no previous file with comments | « no previous file | lib/child/contentPolicy.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld