[Pkg-mozext-commits] [adblock-plus] 28/74: Issue 2420 - Move notification show logic to core

David Prévot taffit at moszumanska.debian.org
Tue Aug 11 12:07:07 UTC 2015


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

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

commit b3d0673edcfa880078456b89a7df213a13255c8a
Author: Felix Dahlke <felix at adblockplus.org>
Date:   Mon Jun 8 10:24:26 2015 +0200

    Issue 2420 - Move notification show logic to core
    
    This also solves issue 2367 - didn't make too much sense to tackle these in
    isolation.
---
 lib/notification.js | 89 +++++++++++++++++++++++++++++++++++++----------------
 lib/ui.js           | 39 ++++++++++-------------
 2 files changed, 79 insertions(+), 49 deletions(-)

diff --git a/lib/notification.js b/lib/notification.js
index acb41b9..cf0751c 100644
--- a/lib/notification.js
+++ b/lib/notification.js
@@ -30,13 +30,15 @@ let {Filter} = require("filterClasses");
 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE;
 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY;
+let STARTUP_SHOW_DELAY = 3 * MILLIS_IN_MINUTE;
 let TYPE = {
   information: 0,
   question: 1,
   critical: 2
 };
 
-let listeners = {};
+let showListeners = [];
+let questionListeners = {};
 
 function getNumericalSeverity(notification)
 {
@@ -81,14 +83,16 @@ let Notification = exports.Notification =
   init: function()
   {
     downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY, CHECK_INTERVAL);
-    onShutdown.add(function()
-    {
-      downloader.cancel();
-    });
-
     downloader.onExpirationChange = this._onExpirationChange.bind(this);
     downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
     downloader.onDownloadError = this._onDownloadError.bind(this);
+    onShutdown.add(() => downloader.cancel());
+
+    notificationTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+    notificationTimer.initWithCallback(Notification.showNext.bind(this),
+                                       STARTUP_SHOW_DELAY,
+                                       Ci.nsITimer.TYPE_ONE_SHOT);
+    onShutdown.add(() => notificationTimer.cancel());
   },
 
   /**
@@ -158,11 +162,33 @@ let Notification = exports.Notification =
   },
 
   /**
+   * Adds a listener for notifications to be shown.
+   * @param {Function} listener Listener to be invoked when a notification is
+   *                   to be shown
+   */
+  addShowListener: function(listener)
+  {
+    if (showListeners.indexOf(listener) == -1)
+      showListeners.push(listener);
+  },
+
+  /**
+   * Removes the supplied listener.
+   * @param {Function} listener Listener that was added via addShowListener()
+   */
+  removeShowListener: function(listener)
+  {
+    let index = showListeners.indexOf(listener);
+    if (index != -1)
+      showListeners.splice(index, 1);
+  },
+
+  /**
    * Determines which notification is to be shown next.
    * @param {String} url URL to match notifications to (optional)
    * @return {Object} notification to be shown, or null if there is none
    */
-  getNextToShow: function(url)
+  _getNextToShow: function(url)
   {
     function checkTarget(target, parameter, name, version)
     {
@@ -234,15 +260,26 @@ let Notification = exports.Notification =
         notificationToShow = notification;
     }
 
-    if (notificationToShow && "id" in notificationToShow)
-    {
-      if (notificationToShow.type !== "question")
-        this.markAsShown(notificationToShow.id);
-    }
-
     return notificationToShow;
   },
 
+  /**
+   * Invokes the listeners added via addShowListener() with the next
+   * notification to be shown.
+   * @param {String} url URL to match notifications to (optional)
+   */
+  showNext: function(url)
+  {
+    let notification = Notification._getNextToShow(url);
+    if (notification)
+      for (let showListener of showListeners)
+        showListener(notification);
+  },
+
+  /**
+   * Marks a notification as shown.
+   * @param {String} id ID of the notification to be marked as shown
+   */
   markAsShown: function(id)
   {
     if (Prefs.notificationdata.shown.indexOf(id) > -1)
@@ -303,10 +340,10 @@ let Notification = exports.Notification =
    */
   addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener)
   {
-    if (!(id in listeners))
-      listeners[id] = [];
-    if (listeners[id].indexOf(listener) === -1)
-      listeners[id].push(listener);
+    if (!(id in questionListeners))
+      questionListeners[id] = [];
+    if (questionListeners[id].indexOf(listener) === -1)
+      questionListeners[id].push(listener);
   },
 
   /**
@@ -314,26 +351,26 @@ let Notification = exports.Notification =
    */
   removeQuestionListener: function(/**string*/ id, /**function(approved)*/ listener)
   {
-    if (!(id in listeners))
+    if (!(id in questionListeners))
       return;
-    let index = listeners[id].indexOf(listener);
+    let index = questionListeners[id].indexOf(listener);
     if (index > -1)
-      listeners[id].splice(index, 1);
-    if (listeners[id].length === 0)
-      delete listeners[id];
+      questionListeners[id].splice(index, 1);
+    if (questionListeners[id].length === 0)
+      delete questionListeners[id];
   },
 
   /**
-   * Notifies listeners about interactions with a notification
+   * Notifies question listeners about interactions with a notification
    * @param {String} id notification ID
    * @param {Boolean} approved indicator whether notification has been approved or not
    */
   triggerQuestionListeners: function(id, approved)
   {
-    if (!(id in listeners))
+    if (!(id in questionListeners))
       return;
-    let questionListeners = listeners[id];
-    for (let listener of questionListeners)
+    let listeners = questionListeners[id];
+    for (let listener of listeners)
       listener(approved);
   },
   
diff --git a/lib/ui.js b/lib/ui.js
index 5f19443..47f27e9 100644
--- a/lib/ui.js
+++ b/lib/ui.js
@@ -419,10 +419,19 @@ let UI = exports.UI =
         this.updateState();
     }.bind(this));
 
-    notificationTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-    notificationTimer.initWithCallback(this.showNextNotification.bind(this),
-                                       3 * 60 * 1000, Ci.nsITimer.TYPE_ONE_SHOT);
-    onShutdown.add(() => notificationTimer.cancel());
+    Notification.addShowListener(notification =>
+    {
+      let window = this.currentWindow;
+      if (!window)
+        return;
+
+      let button = window.document.getElementById("abp-toolbarbutton")
+          || window.document.getElementById("abp-status");
+      if (!button)
+        return;
+
+      this._showNotification(window, button, notification);
+    });
 
     // Add "anti-adblock messages" notification
     initAntiAdblockNotification();
@@ -433,7 +442,7 @@ let UI = exports.UI =
         if (!(subject instanceof Ci.nsIDOMWindow))
           return;
 
-        this.showNextNotification(subject.location.href);
+        Notification.showNext(subject.location.href);
       }.bind(UI)
     };
     Services.obs.addObserver(documentCreationObserver, "content-document-global-created", false);
@@ -1849,24 +1858,6 @@ let UI = exports.UI =
     }
   },
 
-  showNextNotification: function(url)
-  {
-    let window = this.currentWindow;
-    if (!window)
-      return;
-
-    let button = window.document.getElementById("abp-toolbarbutton")
-      || window.document.getElementById("abp-status");
-    if (!button)
-      return;
-
-    let notification = Notification.getNextToShow(url);
-    if (!notification)
-      return;
-
-    this._showNotification(window, button, notification);
-  },
-
   _showNotification: function(window, button, notification)
   {
     let panel = window.document.getElementById("abp-notification");
@@ -1932,6 +1923,8 @@ let UI = exports.UI =
       window.document.getElementById("abp-notification-yes").onclick = buttonHandler.bind(null, true);
       window.document.getElementById("abp-notification-no").onclick = buttonHandler.bind(null, false);
     }
+    else
+      Notification.markAsShown(notification.id);
 
     panel.setAttribute("class", "abp-" + notification.type);
     panel.setAttribute("noautohide", notification.type === "question");

-- 
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