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

Unified Diff: lib/utils.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 | « lib/requestNotifier.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
*/
« no previous file with comments | « lib/requestNotifier.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld