| Index: lib/utils.js |
| =================================================================== |
| --- a/lib/utils.js |
| +++ b/lib/utils.js |
| @@ -524,16 +524,54 @@ let Utils = exports.Utils = |
| if (accesskey != "") |
| element.setAttribute("accesskey", accesskey); |
| // Labels forward changes of the accessKey property to their control, only |
| // set it for actual controls. |
| if (element.localName != "label") |
| element.accessKey = accesskey; |
| } |
| + }, |
| + |
| + /** |
| + * Adds a message handler that will respond to both synchronous and |
| + * asynchonous messages. |
| + * @param {string} messageName name of the message to listen to |
| + * @param {Function} handler handler to be called with the message data. The |
| + * return value will be sent back to the child. |
| + */ |
| + addChildMessageListener: function(messageName, handler) |
| + { |
| + let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
| + .getService(Ci.nsIMessageListenerManager); |
| + let wrapper = (message) => { |
| + let {callbackID, data} = message.data; |
| + let response = undefined; |
| + try |
| + { |
| + response = handler(data); |
| + } |
| + catch (e) |
| + { |
| + Cu.reportError(e); |
| + } |
| + |
| + if (callbackID) |
| + { |
| + let target = message.target.QueryInterface(Ci.nsIMessageSender); |
| + target.sendAsyncMessage("AdblockPlus:Response", { |
| + callbackID, |
| + response |
| + }); |
| + } |
| + else |
| + return response; |
| + }; |
| + messageManager.addMessageListener(messageName, wrapper); |
| + onShutdown.add(() => messageManager.removeMessageListener(messageName, wrapper)); |
| } |
| }; |
| /** |
| * A cache with a fixed capacity, newer entries replace entries that have been |
| * stored first. |
| * @constructor |
| */ |