| Index: lib/requestNotifier.js |
| =================================================================== |
| --- a/lib/requestNotifier.js |
| +++ b/lib/requestNotifier.js |
| @@ -22,30 +22,34 @@ |
| let {Utils} = require("utils"); |
| let windowSelection = new WeakMap(); |
| let requestNotifierMaxId = 0; |
| let windowStatsMaxResponseID = 0; |
| let windowStatsCallbacks = new Map(); |
| +let windowDataMaxResponseID = 0; |
| +let windowDataCallbacks = new Map(); |
|
Thomas Greiner
2016/01/05 15:43:58
I noticed that most of the message logic (windowDa
Wladimir Palant
2016/01/06 12:31:58
The messaging logic is very suboptimal right now,
Thomas Greiner
2016/01/06 13:16:22
Sounds great. In that case I don't mind implementi
|
| + |
| /** |
| * Active RequestNotifier instances by their ID |
| * @type Map.<number,RequestNotifier> |
| */ |
| let notifiers = new Map(); |
| let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
| .getService(Ci.nsIMessageListenerManager) |
| .QueryInterface(Ci.nsIMessageBroadcaster); |
| Utils.addChildMessageListener("AdblockPlus:FoundNodeData", onNodeData); |
| Utils.addChildMessageListener("AdblockPlus:ScanComplete", onScanComplete); |
| Utils.addChildMessageListener("AdblockPlus:NotifierResponse", onNotifierResponse); |
| Utils.addChildMessageListener("AdblockPlus:RetrieveWindowStatsResponse", onWindowStatsReceived); |
| +Utils.addChildMessageListener("AdblockPlus:RetrieveWindowDataResponse", onWindowDataReceived); |
| function onNodeData({notifierID, data}) |
| { |
| let notifier = notifiers.get(notifierID); |
| if (notifier) |
| notifier.notifyListener(data); |
| } |
| @@ -66,16 +70,24 @@ function onNotifierResponse({notifierID, |
| function onWindowStatsReceived({responseID, stats}) |
| { |
| let callback = windowStatsCallbacks.get(responseID); |
| windowStatsCallbacks.delete(responseID); |
| if (typeof callback == "function") |
| callback(stats); |
| } |
| +function onWindowDataReceived({responseID, data}) |
| +{ |
| + let callback = windowDataCallbacks.get(responseID); |
| + windowDataCallbacks.delete(responseID); |
| + if (typeof callback == "function") |
| + callback(data); |
| +} |
| + |
| /** |
| * Creates a notifier object for a particular window. After creation the window |
| * will first be scanned for previously saved requests. Once that scan is |
| * complete only new requests for this window will be reported. |
| * @param {Integer} outerWindowID ID of the window to attach the notifier to |
| * @param {Function} listener listener to be called whenever a new request is found |
| * @param {Object} [listenerObj] "this" pointer to be used when calling the listener |
| */ |
| @@ -214,26 +226,45 @@ RequestNotifier.prototype = |
| messageManager.broadcastAsyncMessage("AdblockPlus:StoreNodesForEntries", { |
| notifierID: this.id, |
| responseID: id, |
| requests, |
| }); |
| } |
| }; |
| -RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection) |
| +/** |
| + * Associates a piece of data with a particular window. |
| + * @param {number} outerWindowID the ID of the window |
| + * @static |
| + */ |
| +RequestNotifier.storeWindowData = function(outerWindowID, data) |
| { |
| - windowSelection.set(wnd.document, selection); |
| + messageManager.broadcastAsyncMessage("AdblockPlus:StoreWindowData", { |
| + outerWindowID, |
| + data |
| + }); |
| }; |
| -RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/ |
| + |
| +/** |
| + * Retrieves a piece of data previously associated with the window by calling |
| + * storeWindowData. |
| + * @param {number} outerWindowID the ID of the window |
| + * @param {Function} callback function to be called with the data. |
| + * @static |
| + */ |
| +RequestNotifier.retrieveWindowData = function(outerWindowID, callback) |
| { |
| - if (windowSelection.has(wnd.document)) |
| - return windowSelection.get(wnd.document); |
| - else |
| - return null; |
| + let id = ++windowDataMaxResponseID; |
| + windowDataCallbacks.set(id, callback); |
| + |
| + messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowData", { |
| + outerWindowID, |
| + responseID: id |
| + }); |
| }; |
| /** |
| * Retrieves the statistics for a window. |
| * @param {number} outerWindowID the ID of the window |
| * @param {Function} callback the callback to be called with the resulting |
| * object (object properties will be items, blocked, |
| * whitelisted, hidden, filters) or null. |