Bug 1000337 - Make NotificationStorage cache origin-aware. r=mhenretty, a=abillings
authorAlexandre Lissy <lissyx@lissyx.dyndns.org>
Thu, 24 Apr 2014 16:14:09 +0200
changeset 192347 d520b0344613
parent 192346 869aefb78e22
child 192348 2925e9a0a33d
push id3577
push userryanvm@gmail.com
push date2014-05-21 13:10 +0000
treeherdermozilla-beta@ff1925aa6f85 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhenretty, abillings
bugs1000337
milestone30.0
Bug 1000337 - Make NotificationStorage cache origin-aware. r=mhenretty, a=abillings
dom/src/notification/NotificationDB.jsm
dom/src/notification/NotificationStorage.js
--- a/dom/src/notification/NotificationDB.jsm
+++ b/dom/src/notification/NotificationDB.jsm
@@ -150,16 +150,17 @@ let NotificationDB = {
       }
     }
 
     switch (message.name) {
       case "Notification:GetAll":
         this.queueTask("getall", message.data, function(notifications) {
           returnMessage("Notification:GetAll:Return:OK", {
             requestID: message.data.requestID,
+            origin: message.data.origin,
             notifications: notifications
           });
         });
         break;
 
       case "Notification:Save":
         this.queueTask("save", message.data, function() {
           returnMessage("Notification:Save:Return:OK", {
--- a/dom/src/notification/NotificationStorage.js
+++ b/dom/src/notification/NotificationStorage.js
@@ -40,69 +40,74 @@ NotificationStorage.prototype = {
     if (DEBUG) { debug("PUT: " + id + ": " + title); }
     var notification = {
       id: id,
       title: title,
       dir: dir,
       lang: lang,
       body: body,
       tag: tag,
-      icon: icon
+      icon: icon,
+      origin: origin
     };
 
     this._notifications[id] = notification;
     if (tag) {
+      if (!this._byTag[origin]) {
+        this._byTag[origin] = {};
+      }
+
       // We might have existing notification with this tag,
       // if so we need to remove it from our cache.
-      if (this._byTag[tag]) {
-        var oldNotification = this._byTag[tag];
+      if (this._byTag[origin][tag]) {
+        var oldNotification = this._byTag[origin][tag];
         delete this._notifications[oldNotification.id];
       }
 
-      this._byTag[tag] = notification;
+      this._byTag[origin][tag] = notification;
     };
 
     cpmm.sendAsyncMessage("Notification:Save", {
       origin: origin,
       notification: notification
     });
   },
 
   get: function(origin, tag, callback) {
-    if (DEBUG) { debug("GET: " + tag); }
+    if (DEBUG) { debug("GET: " + origin + " " + tag); }
     if (this._cached) {
-      this._fetchFromCache(tag, callback);
+      this._fetchFromCache(origin, tag, callback);
     } else {
       this._fetchFromDB(origin, tag, callback);
     }
   },
 
   delete: function(origin, id) {
     if (DEBUG) { debug("DELETE: " + id); }
     var notification = this._notifications[id];
     if (notification) {
       if (notification.tag) {
-        delete this._byTag[notification.tag];
+        delete this._byTag[origin][notification.tag];
       }
       delete this._notifications[id];
     }
 
     cpmm.sendAsyncMessage("Notification:Delete", {
       origin: origin,
       id: id
     });
   },
 
   receiveMessage: function(message) {
     switch (message.name) {
       case "Notification:GetAll:Return:OK":
         var request = this._requests[message.data.requestID];
         delete this._requests[message.data.requestID];
         this._populateCache(message.data.notifications);
-        this._fetchFromCache(request.tag, request.callback);
+        this._fetchFromCache(request.origin, request.tag, request.callback);
         break;
 
       default:
         if (DEBUG) debug("Unrecognized message: " + message.name);
         break;
     }
   },
 
@@ -115,26 +120,28 @@ NotificationStorage.prototype = {
     var requestID = this._requestCount++;
     this._requests[requestID] = request;
     cpmm.sendAsyncMessage("Notification:GetAll", {
       origin: origin,
       requestID: requestID
     });
   },
 
-  _fetchFromCache: function(tag, callback) {
+  _fetchFromCache: function(origin, tag, callback) {
     var notifications = [];
     // If a tag was specified and we have a notification
     // with this tag, return that. If no tag was specified
     // simple return all stored notifications.
-    if (tag && this._byTag[tag]) {
-      notifications.push(this._byTag[tag]);
+    if (tag && this._byTag[origin] && this._byTag[origin][tag]) {
+      notifications.push(this._byTag[origin][tag]);
     } else if (!tag) {
       for (var id in this._notifications) {
-        notifications.push(this._notifications[id]);
+        if (this._notifications[id].origin === origin) {
+          notifications.push(this._notifications[id]);
+        }
       }
     }
 
     // Pass each notification back separately.
     notifications.forEach(function(notification) {
       try {
         callback.handle(notification.id,
                         notification.title,
@@ -152,18 +159,23 @@ NotificationStorage.prototype = {
     } catch (e) {
       if (DEBUG) { debug("Error calling callback done: " + e); }
     }
   },
 
   _populateCache: function(notifications) {
     notifications.forEach(function(notification) {
       this._notifications[notification.id] = notification;
-      if (notification.tag) {
-        this._byTag[notification.tag] = notification;
+      if (notification.tag && notification.origin) {
+        let tag = notification.tag;
+        let origin = notification.origin;
+        if (!this._byTag[origin]) {
+          this._byTag[origin] = {};
+        }
+        this._byTag[origin][tag] = notification;
       }
     }.bind(this));
     this._cached = true;
   },
 
   classID : Components.ID(NOTIFICATIONSTORAGE_CID),
   contractID : NOTIFICATIONSTORAGE_CONTRACTID,
   QueryInterface: XPCOMUtils.generateQI([Ci.nsINotificationStorage,