Index: lib/child/requestNotifier.js |
=================================================================== |
--- a/lib/child/requestNotifier.js |
+++ b/lib/child/requestNotifier.js |
@@ -15,123 +15,94 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
/** |
* @fileOverview Stores Adblock Plus data to be attached to a window. |
*/ |
let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
+let {port} = require("messaging"); |
let {Utils} = require("utils"); |
let {Flasher} = require("child/flasher"); |
let nodeData = new WeakMap(); |
let windowStats = new WeakMap(); |
let windowData = new WeakMap(); |
let requestEntryMaxId = 0; |
/** |
* Active RequestNotifier instances by their ID |
* @type Map.<number,RequestNotifier> |
*/ |
let notifiers = new Map(); |
-addMessageListener("AdblockPlus:StartWindowScan", onStartScan); |
-addMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); |
-addMessageListener("AdblockPlus:FlashNodes", onFlashNodes); |
-addMessageListener("AdblockPlus:RetrieveNodeSize", onRetrieveNodeSize); |
-addMessageListener("AdblockPlus:StoreNodesForEntries", onStoreNodes); |
-addMessageListener("AdblockPlus:RetrieveWindowStats", onRetrieveWindowStats); |
-addMessageListener("AdblockPlus:StoreWindowData", onStoreWindowData); |
-addMessageListener("AdblockPlus:RetrieveWindowData", onRetrieveWindowData); |
+port.on("startWindowScan", onStartScan); |
+port.on("shutdownNotifier", onNotifierShutdown); |
+port.on("flashNodes", onFlashNodes); |
+port.on("retrieveNodeSize", onRetrieveNodeSize); |
+port.on("storeNodesForEntries", onStoreNodes); |
+port.on("retrieveWindowStats", onRetrieveWindowStats); |
+port.on("storeWindowData", onStoreWindowData); |
+port.on("retrieveWindowData", onRetrieveWindowData); |
-onShutdown.add(() => { |
- removeMessageListener("AdblockPlus:StartWindowScan", onStartScan); |
- removeMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); |
- removeMessageListener("AdblockPlus:FlashNodes", onFlashNodes); |
- removeMessageListener("AdblockPlus:RetrieveNodeSize", onRetrieveNodeSize); |
- removeMessageListener("AdblockPlus:StoreNodesForEntries", onStoreNodes); |
- removeMessageListener("AdblockPlus:RetrieveWindowStats", onRetrieveWindowStats); |
- removeMessageListener("AdblockPlus:StoreWindowData", onStoreWindowData); |
- removeMessageListener("AdblockPlus:RetrieveWindowData", onRetrieveWindowData); |
-}); |
- |
-function onStartScan(message) |
+function onStartScan({notifierID, outerWindowID}) |
{ |
- let {notifierID, outerWindowID} = message.data; |
let window = Services.wm.getOuterWindowWithId(outerWindowID); |
if (window) |
new RequestNotifier(window, notifierID); |
} |
-function onNotifierShutdown(message) |
+function onNotifierShutdown(notifierID) |
{ |
- let notifier = notifiers.get(message.data); |
+ let notifier = notifiers.get(notifierID); |
if (notifier) |
notifier.shutdown(); |
} |
-function onFlashNodes(message) |
+function onFlashNodes({notifierID, requests, scrollToItem}) |
{ |
- let {notifierID, requests, scrollToItem} = message.data; |
let notifier = notifiers.get(notifierID); |
if (notifier) |
notifier.flashNodes(requests, scrollToItem); |
} |
-function onRetrieveNodeSize(message) |
+function onRetrieveNodeSize({notifierID, requests}) |
{ |
- let {notifierID, responseID, requests} = message.data; |
let notifier = notifiers.get(notifierID); |
if (notifier) |
- notifier.retrieveNodeSize(requests, responseID); |
+ return notifier.retrieveNodeSize(requests); |
} |
-function onStoreNodes(message) |
+function onStoreNodes({notifierID, requests}) |
{ |
- let {notifierID, responseID, requests} = message.data; |
let notifier = notifiers.get(notifierID); |
if (notifier) |
- notifier.storeNodesForEntries(requests, responseID); |
+ return notifier.storeNodesForEntries(requests); |
} |
-function onRetrieveWindowStats(message) |
+function onRetrieveWindowStats(outerWindowID) |
{ |
- let {responseID, outerWindowID} = message.data; |
let window = Services.wm.getOuterWindowWithId(outerWindowID); |
if (window) |
- { |
- let stats = RequestNotifier.getWindowStatistics(window); |
- sendAsyncMessage("AdblockPlus:RetrieveWindowStatsResponse", { |
- responseID, |
- stats |
- }); |
- } |
+ return RequestNotifier.getWindowStatistics(window); |
} |
-function onStoreWindowData(message) |
+function onStoreWindowData({outerWindowID, data}) |
{ |
- let {outerWindowID, data} = message.data; |
let window = Services.wm.getOuterWindowWithId(outerWindowID); |
if (window) |
windowData.set(window.document, data); |
}; |
-function onRetrieveWindowData(message) |
+function onRetrieveWindowData(outerWindowID) |
{ |
- let {responseID, outerWindowID} = message.data; |
let window = Services.wm.getOuterWindowWithId(outerWindowID); |
if (window) |
- { |
- let data = windowData.get(window.document) || null; |
- sendAsyncMessage("AdblockPlus:RetrieveWindowDataResponse", { |
- responseID, |
- data |
- }); |
- } |
+ return windowData.get(window.document) || null; |
}; |
/** |
* 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 {Window} window window to attach the notifier to |
* @param {Integer} notifierID Parent notifier ID to be messaged |
@@ -182,25 +153,25 @@ RequestNotifier.prototype = |
* Notifies the parent about a new request. |
* @param {Node} node DOM node that the request is associated with |
* @param {Object} entry |
*/ |
notifyListener: function(node, entry) |
{ |
if (this.nodes) |
this.nodes.set(entry.id, node); |
- sendAsyncMessage("AdblockPlus:FoundNodeData", { |
+ port.emit("foundNodeData", { |
notifierID: this.id, |
data: entry |
}); |
}, |
onComplete: function() |
{ |
- sendAsyncMessage("AdblockPlus:ScanComplete", this.id); |
+ port.emit("scanComplete", this.id); |
}, |
/** |
* Number of currently posted scan events (will be 0 when the scan finishes |
* running). |
*/ |
eventsPosted: 0, |
@@ -279,23 +250,23 @@ RequestNotifier.prototype = |
stopFlashing: function() |
{ |
if (this.flasher) |
this.flasher.stop(); |
this.flasher = null; |
}, |
/** |
- * Attempts to calculate the size of the nodes associated with the requests, |
- * Sends message to parent when complete. |
+ * Attempts to calculate the size of the nodes associated with the requests. |
* @param {number[]} requests list of request IDs that were previously |
* reported by this notifier. |
- * @param {number} responseID ID to be sent with the response. |
+ * @return {number[]|null} either an array containing width and height or |
+ * null if the size could not be calculated. |
*/ |
- retrieveNodeSize: function(requests, responseID) |
+ retrieveNodeSize: function(requests) |
{ |
function getNodeSize(node) |
{ |
if (node instanceof Ci.nsIDOMHTMLImageElement && (node.naturalWidth || node.naturalHeight)) |
return [node.naturalWidth, node.naturalHeight]; |
else if (node instanceof Ci.nsIDOMHTMLElement && (node.offsetWidth || node.offsetHeight)) |
return [node.offsetWidth, node.offsetHeight]; |
else |
@@ -313,53 +284,43 @@ RequestNotifier.prototype = |
this.nodes.delete(node); |
else |
{ |
size = getNodeSize(node); |
if (size) |
break; |
} |
} |
- sendAsyncMessage("AdblockPlus:NotifierResponse", { |
- notifierID: this.id, |
- responseID, |
- response: size |
- }); |
+ return size; |
}, |
/** |
* Stores the nodes associated with the requests and generates a unique ID |
- * for them that can be used with Policy.refilterNodes(). Sends message to |
- * parent when complete. |
+ * for them that can be used with Policy.refilterNodes(). |
* @param {number[]} requests list of request IDs that were previously |
* reported by this notifier. |
- * @param {number} responseID ID to be sent with the response. |
+ * @return {string} unique identifiers associated with the nodes. |
*/ |
- storeNodesForEntries: function(requests, responseID) |
+ storeNodesForEntries: function(requests) |
{ |
let nodes = []; |
for (let id of requests) |
{ |
if (!this.nodes.has(id)) |
continue; |
let node = this.nodes.get(id); |
if (Cu.isDeadWrapper(node)) |
this.nodes.delete(node); |
else |
nodes.push(node); |
} |
let {storeNodes} = require("child/contentPolicy"); |
- let id = storeNodes(nodes); |
- sendAsyncMessage("AdblockPlus:NotifierResponse", { |
- notifierID: this.id, |
- responseID, |
- response: id |
- }); |
+ return storeNodes(nodes); |
} |
}; |
/** |
* Attaches request data to a DOM node. |
* @param {Node} node node to attach data to |
* @param {Window} topWnd top-level window the node belongs to |
* @param {Object} hitData |