| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * @fileOverview Stores Adblock Plus data to be attached to a window. | 19 * @fileOverview Stores Adblock Plus data to be attached to a window. |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 let {Utils} = require("utils"); | 22 let {Utils} = require("utils"); |
| 23 | 23 |
| 24 let windowSelection = new WeakMap(); | 24 let windowSelection = new WeakMap(); |
| 25 let requestNotifierMaxId = 0; | 25 let requestNotifierMaxId = 0; |
| 26 | 26 |
| 27 let windowStatsMaxResponseID = 0; | 27 let windowStatsMaxResponseID = 0; |
| 28 let windowStatsCallbacks = new Map(); | 28 let windowStatsCallbacks = new Map(); |
| 29 | 29 |
| 30 let windowDataMaxResponseID = 0; | |
| 31 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
| |
| 32 | |
| 30 /** | 33 /** |
| 31 * Active RequestNotifier instances by their ID | 34 * Active RequestNotifier instances by their ID |
| 32 * @type Map.<number,RequestNotifier> | 35 * @type Map.<number,RequestNotifier> |
| 33 */ | 36 */ |
| 34 let notifiers = new Map(); | 37 let notifiers = new Map(); |
| 35 | 38 |
| 36 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] | 39 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
| 37 .getService(Ci.nsIMessageListenerManager) | 40 .getService(Ci.nsIMessageListenerManager) |
| 38 .QueryInterface(Ci.nsIMessageBroadcaster); | 41 .QueryInterface(Ci.nsIMessageBroadcaster); |
| 39 | 42 |
| 40 Utils.addChildMessageListener("AdblockPlus:FoundNodeData", onNodeData); | 43 Utils.addChildMessageListener("AdblockPlus:FoundNodeData", onNodeData); |
| 41 Utils.addChildMessageListener("AdblockPlus:ScanComplete", onScanComplete); | 44 Utils.addChildMessageListener("AdblockPlus:ScanComplete", onScanComplete); |
| 42 Utils.addChildMessageListener("AdblockPlus:NotifierResponse", onNotifierResponse ); | 45 Utils.addChildMessageListener("AdblockPlus:NotifierResponse", onNotifierResponse ); |
| 43 Utils.addChildMessageListener("AdblockPlus:RetrieveWindowStatsResponse", onWindo wStatsReceived); | 46 Utils.addChildMessageListener("AdblockPlus:RetrieveWindowStatsResponse", onWindo wStatsReceived); |
| 47 Utils.addChildMessageListener("AdblockPlus:RetrieveWindowDataResponse", onWindow DataReceived); | |
| 44 | 48 |
| 45 function onNodeData({notifierID, data}) | 49 function onNodeData({notifierID, data}) |
| 46 { | 50 { |
| 47 let notifier = notifiers.get(notifierID); | 51 let notifier = notifiers.get(notifierID); |
| 48 if (notifier) | 52 if (notifier) |
| 49 notifier.notifyListener(data); | 53 notifier.notifyListener(data); |
| 50 } | 54 } |
| 51 | 55 |
| 52 function onScanComplete(notifierID) | 56 function onScanComplete(notifierID) |
| 53 { | 57 { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 64 } | 68 } |
| 65 | 69 |
| 66 function onWindowStatsReceived({responseID, stats}) | 70 function onWindowStatsReceived({responseID, stats}) |
| 67 { | 71 { |
| 68 let callback = windowStatsCallbacks.get(responseID); | 72 let callback = windowStatsCallbacks.get(responseID); |
| 69 windowStatsCallbacks.delete(responseID); | 73 windowStatsCallbacks.delete(responseID); |
| 70 if (typeof callback == "function") | 74 if (typeof callback == "function") |
| 71 callback(stats); | 75 callback(stats); |
| 72 } | 76 } |
| 73 | 77 |
| 78 function onWindowDataReceived({responseID, data}) | |
| 79 { | |
| 80 let callback = windowDataCallbacks.get(responseID); | |
| 81 windowDataCallbacks.delete(responseID); | |
| 82 if (typeof callback == "function") | |
| 83 callback(data); | |
| 84 } | |
| 85 | |
| 74 /** | 86 /** |
| 75 * Creates a notifier object for a particular window. After creation the window | 87 * Creates a notifier object for a particular window. After creation the window |
| 76 * will first be scanned for previously saved requests. Once that scan is | 88 * will first be scanned for previously saved requests. Once that scan is |
| 77 * complete only new requests for this window will be reported. | 89 * complete only new requests for this window will be reported. |
| 78 * @param {Integer} outerWindowID ID of the window to attach the notifier to | 90 * @param {Integer} outerWindowID ID of the window to attach the notifier to |
| 79 * @param {Function} listener listener to be called whenever a new request is f ound | 91 * @param {Function} listener listener to be called whenever a new request is f ound |
| 80 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener | 92 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener |
| 81 */ | 93 */ |
| 82 function RequestNotifier(outerWindowID, listener, listenerObj) | 94 function RequestNotifier(outerWindowID, listener, listenerObj) |
| 83 { | 95 { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 this._callbacks.set(id, callback); | 224 this._callbacks.set(id, callback); |
| 213 | 225 |
| 214 messageManager.broadcastAsyncMessage("AdblockPlus:StoreNodesForEntries", { | 226 messageManager.broadcastAsyncMessage("AdblockPlus:StoreNodesForEntries", { |
| 215 notifierID: this.id, | 227 notifierID: this.id, |
| 216 responseID: id, | 228 responseID: id, |
| 217 requests, | 229 requests, |
| 218 }); | 230 }); |
| 219 } | 231 } |
| 220 }; | 232 }; |
| 221 | 233 |
| 222 RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection ) | 234 /** |
| 235 * Associates a piece of data with a particular window. | |
| 236 * @param {number} outerWindowID the ID of the window | |
| 237 * @static | |
| 238 */ | |
| 239 RequestNotifier.storeWindowData = function(outerWindowID, data) | |
| 223 { | 240 { |
| 224 windowSelection.set(wnd.document, selection); | 241 messageManager.broadcastAsyncMessage("AdblockPlus:StoreWindowData", { |
| 242 outerWindowID, | |
| 243 data | |
| 244 }); | |
| 225 }; | 245 }; |
| 226 RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/ | 246 |
| 247 /** | |
| 248 * Retrieves a piece of data previously associated with the window by calling | |
| 249 * storeWindowData. | |
| 250 * @param {number} outerWindowID the ID of the window | |
| 251 * @param {Function} callback function to be called with the data. | |
| 252 * @static | |
| 253 */ | |
| 254 RequestNotifier.retrieveWindowData = function(outerWindowID, callback) | |
| 227 { | 255 { |
| 228 if (windowSelection.has(wnd.document)) | 256 let id = ++windowDataMaxResponseID; |
| 229 return windowSelection.get(wnd.document); | 257 windowDataCallbacks.set(id, callback); |
| 230 else | 258 |
| 231 return null; | 259 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowData", { |
| 260 outerWindowID, | |
| 261 responseID: id | |
| 262 }); | |
| 232 }; | 263 }; |
| 233 | 264 |
| 234 /** | 265 /** |
| 235 * Retrieves the statistics for a window. | 266 * Retrieves the statistics for a window. |
| 236 * @param {number} outerWindowID the ID of the window | 267 * @param {number} outerWindowID the ID of the window |
| 237 * @param {Function} callback the callback to be called with the resulting | 268 * @param {Function} callback the callback to be called with the resulting |
| 238 * object (object properties will be items, blocked, | 269 * object (object properties will be items, blocked, |
| 239 * whitelisted, hidden, filters) or null. | 270 * whitelisted, hidden, filters) or null. |
| 240 */ | 271 */ |
| 241 RequestNotifier.getWindowStatistics = function(outerWindowID, callback) | 272 RequestNotifier.getWindowStatistics = function(outerWindowID, callback) |
| 242 { | 273 { |
| 243 let id = ++windowStatsMaxResponseID; | 274 let id = ++windowStatsMaxResponseID; |
| 244 windowStatsCallbacks.set(id, callback); | 275 windowStatsCallbacks.set(id, callback); |
| 245 | 276 |
| 246 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowStats", { | 277 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowStats", { |
| 247 responseID: id, | 278 responseID: id, |
| 248 outerWindowID | 279 outerWindowID |
| 249 }); | 280 }); |
| 250 } | 281 } |
| OLD | NEW |