Bug 1401878 - Remove ServiceWorkers in SiteDataManager.jsm, r=bkelly
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 21 Sep 2017 21:32:00 +0200
changeset 668891 e2b1cd9fd4a66fc932d8f74cfe2b38c55b9a7a1e
parent 668890 a33c563d5428ddc49e06d802ea04fbea69999e40
child 668892 10cfc563ec9dc126f4667d1d6952fbd2a703ab9a
push id81146
push userbmo:topwu.tw@gmail.com
push dateFri, 22 Sep 2017 05:24:51 +0000
reviewersbkelly
bugs1401878
milestone58.0a1
Bug 1401878 - Remove ServiceWorkers in SiteDataManager.jsm, r=bkelly
browser/components/preferences/SiteDataManager.jsm
--- a/browser/components/preferences/SiteDataManager.jsm
+++ b/browser/components/preferences/SiteDataManager.jsm
@@ -4,16 +4,19 @@ const { classes: Cc, interfaces: Ci, uti
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "OfflineAppCacheHelper",
                                   "resource:///modules/offlineAppCache.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "ContextualIdentityService",
                                   "resource://gre/modules/ContextualIdentityService.jsm");
+XPCOMUtils.defineLazyServiceGetter(this, "serviceWorkerManager",
+                                   "@mozilla.org/serviceworkers/manager;1",
+                                   "nsIServiceWorkerManager");
 
 this.EXPORTED_SYMBOLS = [
   "SiteDataManager"
 ];
 
 this.SiteDataManager = {
 
   _qms: Services.qms,
@@ -212,25 +215,39 @@ this.SiteDataManager = {
           }
           Services.cookies.remove(
             cookie.host, cookie.name, cookie.path, false, cookie.originAttributes);
         }
       }
     }
   },
 
+  _removeServiceWorkers(site) {
+    let serviceWorkers = serviceWorkerManager.getAllRegistrations();
+    for (let i = 0; i < serviceWorkers.length; i++) {
+      let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
+      for (let principal of site.principals) {
+        if (sw.principal.equals(principal)) {
+          serviceWorkerManager.removeAndPropagate(sw.principal.URI.host);
+          break;
+        }
+      }
+    }
+  },
+
   remove(hosts) {
     let promises = [];
     let unknownHost = "";
     for (let host of hosts) {
       let site = this._sites.get(host);
       if (site) {
         this._removePermission(site);
         this._removeAppCache(site);
         this._removeCookie(site);
+        this._removeServiceWorkers(site);
         promises.push(this._removeQuotaUsage(site));
       } else {
         unknownHost = host;
         break;
       }
     }
     if (promises.length > 0) {
       Promise.all(promises).then(() => this.updateSites());
@@ -239,16 +256,25 @@ this.SiteDataManager = {
       throw `SiteDataManager: removing unknown site of ${unknownHost}`;
     }
   },
 
   async removeAll() {
     Services.cache2.clear();
     Services.cookies.removeAll();
     OfflineAppCacheHelper.clear();
+
+    // Iterate through the service workers and remove them.
+    let serviceWorkers = serviceWorkerManager.getAllRegistrations();
+    for (let i = 0; i < serviceWorkers.length; i++) {
+      let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
+      let host = sw.principal.URI.host;
+      serviceWorkerManager.removeAndPropagate(host);
+    }
+
     // Refresh sites using quota usage again.
     // This is for the case:
     //   1. User goes to the about:preferences Site Data section.
     //   2. With the about:preferences opened, user visits another website.
     //   3. The website saves to quota usage, like indexedDB.
     //   4. User goes back to the Site Data section and commands to clear all site data.
     // For this case, we should refresh the site list so not to miss the website in the step 3.
     // We don't do "Clear All" on the quota manager like the cookie, appcache, http cache above