[Pkg-mozext-commits] [adblock-plus] 37/87: Issue 3499 - Use the new messaging API for the request notifier

David Prévot taffit at moszumanska.debian.org
Sat Apr 30 17:59:06 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 1deeb9a3a26ef0e53a63de6f207eb802db43194e
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Wed Mar 16 12:28:07 2016 +0100

    Issue 3499 - Use the new messaging API for the request notifier
---
 lib/child/requestNotifier.js | 107 ++++++++++++++----------------------------
 lib/requestNotifier.js       | 108 ++++++++-----------------------------------
 2 files changed, 52 insertions(+), 163 deletions(-)

diff --git a/lib/child/requestNotifier.js b/lib/child/requestNotifier.js
index 2e7edb6..d8eb94b 100644
--- a/lib/child/requestNotifier.js
+++ b/lib/child/requestNotifier.js
@@ -20,6 +20,7 @@
  */
 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
 
+let {port} = require("messaging");
 let {Utils} = require("utils");
 let {Flasher} = require("child/flasher");
 
@@ -34,99 +35,69 @@ let requestEntryMaxId = 0;
  */
 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);
-
-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)
+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);
+
+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;
 };
 
 /**
@@ -187,7 +158,7 @@ RequestNotifier.prototype =
   {
     if (this.nodes)
       this.nodes.set(entry.id, node);
-    sendAsyncMessage("AdblockPlus:FoundNodeData", {
+    port.emit("foundNodeData", {
       notifierID: this.id,
       data: entry
     });
@@ -195,7 +166,7 @@ RequestNotifier.prototype =
 
   onComplete: function()
   {
-    sendAsyncMessage("AdblockPlus:ScanComplete", this.id);
+    port.emit("scanComplete", this.id);
   },
 
   /**
@@ -284,13 +255,13 @@ RequestNotifier.prototype =
   },
 
   /**
-   * 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)
     {
@@ -318,22 +289,17 @@ RequestNotifier.prototype =
           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)
@@ -349,12 +315,7 @@ RequestNotifier.prototype =
     }
 
     let {storeNodes} = require("child/contentPolicy");
-    let id = storeNodes(nodes);
-    sendAsyncMessage("AdblockPlus:NotifierResponse", {
-      notifierID: this.id,
-      responseID,
-      response: id
-    });
+    return storeNodes(nodes);
   }
 };
 
diff --git a/lib/requestNotifier.js b/lib/requestNotifier.js
index a7b661f..f2f5adf 100644
--- a/lib/requestNotifier.js
+++ b/lib/requestNotifier.js
@@ -19,69 +19,29 @@
  * @fileOverview Stores Adblock Plus data to be attached to a window.
  */
 
-let {Utils} = require("utils");
+let {port} = require("messaging");
 
-let windowSelection = new WeakMap();
 let requestNotifierMaxId = 0;
 
-let windowStatsMaxResponseID = 0;
-let windowStatsCallbacks = new Map();
-
-let windowDataMaxResponseID = 0;
-let windowDataCallbacks = new Map();
-
 /**
  * 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})
+port.on("foundNodeData", ({notifierID, data}, sender) =>
 {
   let notifier = notifiers.get(notifierID);
   if (notifier)
     notifier.notifyListener(data);
-}
+});
 
-function onScanComplete(notifierID)
+port.on("scanComplete", (notifierID, sender) =>
 {
   let notifier = notifiers.get(notifierID);
   if (notifier)
     notifier.onComplete();
-}
-
-function onNotifierResponse({notifierID, responseID, response})
-{
-  let notifier = notifiers.get(notifierID);
-  if (notifier)
-    notifier.onResponse(responseID, response);
-}
-
-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
@@ -97,9 +57,8 @@ function RequestNotifier(outerWindowID, listener, listenerObj)
   this.listenerObj = listenerObj || null;
   this.id = ++requestNotifierMaxId;
   notifiers.set(this.id, this);
-  this._callbacks = new Map();
 
-  messageManager.broadcastAsyncMessage("AdblockPlus:StartWindowScan", {
+  port.emit("startWindowScan", {
     notifierID: this.id,
     outerWindowID: outerWindowID
   });
@@ -139,7 +98,7 @@ RequestNotifier.prototype =
   shutdown: function()
   {
     notifiers.delete(this.id);
-    messageManager.broadcastAsyncMessage("AdblockPlus:ShutdownNotifier", this.id);
+    port.emit("shutdownNotifier", this.id);
   },
 
   /**
@@ -168,16 +127,13 @@ RequestNotifier.prototype =
     if (!requests)
       requests = [];
 
-    messageManager.broadcastAsyncMessage("AdblockPlus:FlashNodes", {
+    port.emit("flashNodes", {
       notifierID: this.id,
       requests,
       scrollToItem
     });
   },
 
-  _maxResponseID: 0,
-  _callbacks: null,
-
   /**
    * Attempts to calculate the size of the nodes associated with the requests.
    * @param {number[]} requests  list of request IDs that were previously
@@ -189,22 +145,10 @@ RequestNotifier.prototype =
     if (!requests)
       requests = [];
 
-    let id = ++this._maxResponseID;
-    this._callbacks.set(id, callback);
-
-    messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveNodeSize", {
+    port.emitWithResponse("retrieveNodeSize", {
       notifierID: this.id,
-      responseID: id,
-      requests,
-    });
-  },
-
-  onResponse: function(responseID, response)
-  {
-    let callback = this._callbacks.get(responseID);
-    this._callbacks.delete(responseID);
-    if (typeof callback == "function")
-      callback(response);
+      requests
+    }).then(callback);
   },
 
   /**
@@ -220,14 +164,10 @@ RequestNotifier.prototype =
     if (!requests)
       requests = [];
 
-    let id = ++this._maxResponseID;
-    this._callbacks.set(id, callback);
-
-    messageManager.broadcastAsyncMessage("AdblockPlus:StoreNodesForEntries", {
+    port.emitWithResponse("storeNodesForEntries", {
       notifierID: this.id,
-      responseID: id,
-      requests,
-    });
+      requests
+    }).then(callback);
   }
 };
 
@@ -238,7 +178,7 @@ RequestNotifier.prototype =
  */
 RequestNotifier.storeWindowData = function(outerWindowID, data)
 {
-  messageManager.broadcastAsyncMessage("AdblockPlus:StoreWindowData", {
+  port.emit("storeWindowData", {
     outerWindowID,
     data
   });
@@ -253,13 +193,7 @@ RequestNotifier.storeWindowData = function(outerWindowID, data)
  */
 RequestNotifier.retrieveWindowData = function(outerWindowID, callback)
 {
-  let id = ++windowDataMaxResponseID;
-  windowDataCallbacks.set(id, callback);
-
-  messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowData", {
-    outerWindowID,
-    responseID: id
-  });
+  port.emitWithResponse("retrieveWindowData", outerWindowID).then(callback);
 };
 
 /**
@@ -271,11 +205,5 @@ RequestNotifier.retrieveWindowData = function(outerWindowID, callback)
  */
 RequestNotifier.getWindowStatistics = function(outerWindowID, callback)
 {
-  let id = ++windowStatsMaxResponseID;
-  windowStatsCallbacks.set(id, callback);
-
-  messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowStats", {
-    responseID: id,
-    outerWindowID
-  });
-}
+  port.emitWithResponse("retrieveWindowStats", outerWindowID).then(callback);
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list