Bug 1523272 - Use ClearDataService to clear site data in SiteDataManager.jsm. r=baku
authorJohann Hofmann <jhofmann@mozilla.com>
Thu, 14 Feb 2019 22:54:07 +0000
changeset 459345 5f30ff27430670b51e526c12023ea1236efdf8c4
parent 459344 6c289d94dcca88ecfe3893633a31370119c061c7
child 459346 4858888463e0156e398378e5e03ebce87aa626d0
push id35559
push userrmaries@mozilla.com
push dateFri, 15 Feb 2019 09:55:16 +0000
treeherdermozilla-central@fc01e86f7e80 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1523272
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1523272 - Use ClearDataService to clear site data in SiteDataManager.jsm. r=baku Differential Revision: https://phabricator.services.mozilla.com/D17870
browser/modules/SiteDataManager.jsm
--- a/browser/modules/SiteDataManager.jsm
+++ b/browser/modules/SiteDataManager.jsm
@@ -1,18 +1,13 @@
 "use strict";
 
 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
-ChromeUtils.defineModuleGetter(this, "OfflineAppCacheHelper",
-                               "resource://gre/modules/offlineAppCache.jsm");
-ChromeUtils.defineModuleGetter(this, "ServiceWorkerCleanUp",
-                               "resource://gre/modules/ServiceWorkerCleanUp.jsm");
-
 var EXPORTED_SYMBOLS = [
   "SiteDataManager",
 ];
 
 XPCOMUtils.defineLazyGetter(this, "gStringBundle", function() {
   return Services.strings.createBundle("chrome://browser/locale/siteData.properties");
 });
 
@@ -320,53 +315,63 @@ var SiteDataManager = {
   _removeCookies(site) {
     for (let cookie of site.cookies) {
       Services.cookies.remove(
         cookie.host, cookie.name, cookie.path, false, cookie.originAttributes);
     }
     site.cookies = [];
   },
 
+  // Returns a list of permissions from the permission manager that
+  // we consider part of "site data and cookies".
+  _getDeletablePermissions() {
+    let perms = [];
+    let enumerator = Services.perms.enumerator;
+
+    while (enumerator.hasMoreElements()) {
+      let permission = enumerator.getNext().QueryInterface(Ci.nsIPermission);
+      if (permission.type == "persistent-storage" ||
+          permission.type == "storage-access") {
+        perms.push(permission);
+      }
+    }
+
+    return perms;
+  },
+
   /**
    * Removes all site data for the specified list of hosts.
    *
    * @param {Array} a list of hosts to match for removal.
    * @returns a Promise that resolves when data is removed and the site data
    *          manager has been updated.
    */
   async remove(hosts) {
-    // Make sure we have up-to-date information.
-    await this._getQuotaUsage();
-    this._updateAppCache();
-
-    let unknownHost = "";
+    let perms = this._getDeletablePermissions();
     let promises = [];
     for (let host of hosts) {
-      let site = this._sites.get(host);
-      if (site) {
-        // Clear localStorage & sessionStorage
-        Services.obs.notifyObservers(null, "extension:purge-localStorage", host);
-        Services.obs.notifyObservers(null, "browser:purge-sessionStorage", host);
-        this._removePermission(site);
-        this._removeAppCache(site);
-        this._removeCookies(site);
-        promises.push(ServiceWorkerCleanUp.removeFromHost(host));
-        promises.push(this._removeQuotaUsage(site));
-      } else {
-        unknownHost = host;
-        break;
+      promises.push(new Promise(function(resolve) {
+        Services.clearData.deleteDataFromHost(host, true,
+          Ci.nsIClearDataService.CLEAR_COOKIES |
+          Ci.nsIClearDataService.CLEAR_DOM_STORAGES |
+          Ci.nsIClearDataService.CLEAR_SECURITY_SETTINGS |
+          Ci.nsIClearDataService.CLEAR_PLUGIN_DATA |
+          Ci.nsIClearDataService.CLEAR_EME |
+          Ci.nsIClearDataService.CLEAR_ALL_CACHES, resolve);
+      }));
+
+      for (let perm of perms) {
+        if (Services.eTLD.hasRootDomain(perm.principal.URI.host, host)) {
+          Services.perms.removePermission(perm);
+        }
       }
     }
 
     await Promise.all(promises);
 
-    if (unknownHost) {
-      throw `SiteDataManager: removing unknown site of ${unknownHost}`;
-    }
-
     return this.updateSites();
   },
 
   /**
    * In the specified window, shows a prompt for removing
    * all site data or the specified list of hosts, warning the
    * user that this may log them out of websites.
    *
@@ -400,59 +405,46 @@ var SiteDataManager = {
   },
 
   /**
    * Clears all site data and cache
    *
    * @returns a Promise that resolves when the data is cleared.
    */
   async removeAll() {
-    this.removeCache();
+    await this.removeCache();
     return this.removeSiteData();
   },
 
   /**
-   * Clears the entire network cache.
+   * Clears all caches.
+   *
+   * @returns a Promise that resolves when the data is cleared.
    */
   removeCache() {
-    Services.cache2.clear();
+    return new Promise(function(resolve) {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL_CACHES, resolve);
+    });
   },
 
   /**
-   * Clears all site data, which currently means
-   *   - Cookies
-   *   - AppCache
-   *   - LocalStorage
-   *   - ServiceWorkers
-   *   - Quota Managed Storage
-   *   - persistent-storage permissions
+   * Clears all site data, but not cache, because the UI offers
+   * that functionality separately.
    *
-   * @returns a Promise that resolves with the cache size on disk in bytes
+   * @returns a Promise that resolves when the data is cleared.
    */
   async removeSiteData() {
-    // LocalStorage
-    Services.obs.notifyObservers(null, "extension:purge-localStorage");
-
-    Services.cookies.removeAll();
-    OfflineAppCacheHelper.clear();
-
-    await ServiceWorkerCleanUp.removeAll();
+    await new Promise(function(resolve) {
+      Services.clearData.deleteData(
+        Ci.nsIClearDataService.CLEAR_COOKIES |
+        Ci.nsIClearDataService.CLEAR_DOM_STORAGES |
+        Ci.nsIClearDataService.CLEAR_SECURITY_SETTINGS |
+        Ci.nsIClearDataService.CLEAR_EME |
+        Ci.nsIClearDataService.CLEAR_PLUGIN_DATA, resolve);
+    });
 
-    // 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
-    // because that would clear browser data as well too,
-    // see https://bugzilla.mozilla.org/show_bug.cgi?id=1312361#c9
-    this._sites.clear();
-    await this._getQuotaUsage();
-    let promises = [];
-    for (let site of this._sites.values()) {
-      this._removePermission(site);
-      promises.push(this._removeQuotaUsage(site));
+    for (let permission of this._getDeletablePermissions()) {
+      Services.perms.removePermission(permission);
     }
-    return Promise.all(promises).then(() => this.updateSites());
+
+    return this.updateSites();
   },
 };