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,61 @@ |
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) |
+ { |
+ let response = sendSyncMessage(messageName, {data})[0]; |
+ return typeof response == "string" ? JSON.parse(response) : response; |
+ } |
+ |
+ function sendAsyncMessageWithResponse(messageName, data, callback) |
Wladimir Palant
2015/11/04 15:04:09
Note that normally the third parameter for both se
|
+ { |
+ 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"); |
} |