Bug 851368 - Remove SimplePush registrations on app uninstall. r=dougt, a=akeybl
authorNikhil Marathe <nsm.nikhil@gmail.com>
Thu, 04 Apr 2013 13:42:35 -0700
changeset 119031 b644771c03f5aa28d01e798b3d5d29b22c9f04d5
parent 119030 317c9fa4879085f81c1a47d13e225fffc0e2a6ff
child 119032 423f7851bdb56a218da985b5b6ac503839d29e0b
push id649
push userryanvm@gmail.com
push dateWed, 10 Apr 2013 12:34:26 +0000
reviewersdougt, akeybl
bugs851368
milestone18.0
Bug 851368 - Remove SimplePush registrations on app uninstall. r=dougt, a=akeybl
dom/push/src/PushService.js
--- a/dom/push/src/PushService.js
+++ b/dom/push/src/PushService.js
@@ -279,16 +279,17 @@ PushService.prototype = {
   observe: function observe(aSubject, aTopic, aData) {
     switch (aTopic) {
       case "app-startup":
         Services.obs.addObserver(this, "final-ui-startup", false);
         Services.obs.addObserver(this, "profile-change-teardown", false);
         Services.obs.addObserver(this,
                                  "network-interface-state-changed",
                                  false);
+        Services.obs.addObserver(this, "webapps-uninstall", false);
         break;
       case "final-ui-startup":
         Services.obs.removeObserver(this, "final-ui-startup");
         this.init();
         break;
       case "profile-change-teardown":
         Services.obs.removeObserver(this, "profile-change-teardown");
         this._shutdown();
@@ -333,16 +334,44 @@ PushService.prototype = {
                 if (this._requestQueue[i].channelID == channelID)
                   this._requestQueue.splice(i, 1);
             }
           }
         }
         else if (aSubject == this._retryTimeoutTimer) {
           this._beginWSSetup();
         }
+        break;
+      case "webapps-uninstall":
+        debug("webapps-uninstall");
+        let appsService = Cc["@mozilla.org/AppsService;1"]
+                            .getService(Ci.nsIAppsService);
+        var app = appsService.getAppFromObserverMessage(aData);
+        if (!app) {
+          debug("webapps-uninstall: No app found " + aData.origin);
+          return;
+        }
+
+        this._db.getAllByManifestURL(app.manifestURL, function(records) {
+          debug("Got " + records.length);
+          for (var i = 0; i < records.length; i++) {
+            this._db.delete(records[i].channelID, null, function() {
+              debug("app uninstall: " + app.manifestURL + " Could not delete entry " + records[i].channelID);
+            });
+            // courtesy, but don't establish a connection
+            // just for it
+            if (this._ws) {
+              debug("Had a connection, so telling the server");
+              this._request("unregister", {channelID: records[i].channelID});
+            }
+          }
+        }.bind(this), function() {
+          debug("Error in getAllByManifestURL: url " + app.manifestURL);
+        });
+
     }
   },
 
   _prefs : new Preferences("services.push."),
 
   get _UAID() {
     return this._prefs.get("userAgentID");
   },