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"); |
} |