Bug 1512485 - Introduce nsIClearDataService.deleteDataFromOriginAttributesPattern, r=johannh
authorAndrea Marchesini <amarchesini@mozilla.com>
Sun, 09 Dec 2018 13:19:04 +0100
changeset 449675 ff2282b876b65e7a9b1b7e2404c50c18300f0dfa
parent 449674 a24d661d8362f388f002a47b3a0d18a265590b65
child 449676 53fd96ca5aa4298054f581ca846ea2cccbe76085
push idunknown
push userunknown
push dateunknown
reviewersjohannh
bugs1512485
milestone65.0a1
Bug 1512485 - Introduce nsIClearDataService.deleteDataFromOriginAttributesPattern, r=johannh
browser/modules/Sanitizer.jsm
netwerk/test/unit/test_auth_jar.js
netwerk/test/unit/test_cache_jar.js
toolkit/components/cleardata/ClearDataService.js
toolkit/components/cleardata/nsIClearDataService.idl
toolkit/components/contextualidentity/ContextualIdentityService.jsm
toolkit/components/thumbnails/BackgroundPageThumbs.jsm
--- a/browser/modules/Sanitizer.jsm
+++ b/browser/modules/Sanitizer.jsm
@@ -837,18 +837,17 @@ async function sanitizeSessionPrincipal(
                                                Ci.nsIClearDataService.CLEAR_COOKIES,
                                                resolve);
   });
 }
 
 function sanitizeNewTabSegregation() {
   let identity = ContextualIdentityService.getPrivateIdentity("userContextIdInternal.thumbnail");
   if (identity) {
-    Services.obs.notifyObservers(null, "clear-origin-attributes-data",
-                                 JSON.stringify({ userContextId: identity.userContextId }));
+    Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId: identity.userContextId });
   }
 }
 
 /**
  * Gets an array of items to clear from the given pref branch.
  * @param branch The pref branch to fetch.
  * @return Array of items to clear
  */
--- a/netwerk/test/unit/test_auth_jar.js
+++ b/netwerk/test/unit/test_auth_jar.js
@@ -18,18 +18,17 @@ function run_test() {
   var app1browser = secMan.createCodebasePrincipal(createURI(kURI1), {appId: 1, inIsolatedMozBrowser: true});
 
   var am = Cc["@mozilla.org/network/http-auth-manager;1"].
            getService(Ci.nsIHttpAuthManager);
   am.setAuthIdentity("http", "a.example.com", -1, "basic", "realm", "", "example.com", "user", "pass", false, app1);
   am.setAuthIdentity("http", "a.example.com", -1, "basic", "realm", "", "example.com", "user3", "pass3", false, app1browser);
   am.setAuthIdentity("http", "a.example.com", -1, "basic", "realm", "", "example.com", "user2", "pass2", false, app10);
 
-  let attrs_inBrowser = JSON.stringify({ appId:1, inIsolatedMozBrowser:true });
-  Services.obs.notifyObservers(null, "clear-origin-attributes-data", attrs_inBrowser);
+  Services.clearData.deleteDataFromOriginAttributesPattern({ appId:1, inIsolatedMozBrowser:true });
   
   var domain = {value: ""}, user = {value: ""}, pass = {value: ""};
   try {
     am.getAuthIdentity("http", "a.example.com", -1, "basic", "realm", "", domain, user, pass, false, app1browser);
     Assert.equal(false, true); // no identity should be present
   } catch (x) {
     Assert.equal(domain.value, "");
     Assert.equal(user.value, "");
--- a/netwerk/test/unit/test_cache_jar.js
+++ b/netwerk/test/unit/test_cache_jar.js
@@ -55,36 +55,35 @@ async function run_all_tests() {
   }
 
   // We can't easily cause webapp data to be cleared from the child process, so skip
   // the rest of these tests.
   let procType = Cc["@mozilla.org/xre/runtime;1"].getService(Ci.nsIXULRuntime).processType;
   if (procType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT)
     return;
 
-  let attrs_inBrowser = JSON.stringify({ appId:1, inIsolatedMozBrowser:true });
-  let attrs_notInBrowser = JSON.stringify({ appId:1 });
+  let attrs_inBrowser = { appId:1, inIsolatedMozBrowser:true };
+  let attrs_notInBrowser = { appId:1 };
 
-  Services.obs.notifyObservers(null, "clear-origin-attributes-data", attrs_inBrowser);
+  Services.clearData.deleteDataFromOriginAttributesPattern(attrs_inBrowser);
 
   for (let test of secondTests) {
     handlers_called = 0;
     await test_channel(...test);
   }
 
-  Services.obs.notifyObservers(null, "clear-origin-attributes-data", attrs_notInBrowser);
-  Services.obs.notifyObservers(null, "clear-origin-attributes-data", attrs_inBrowser);
+  Services.clearData.deleteDataFromOriginAttributesPattern(attrs_notInBrowser);
+  Services.clearData.deleteDataFromOriginAttributesPattern(attrs_inBrowser);
 
   for (let test of thirdTests) {
     handlers_called = 0;
     await test_channel(...test);
   }
 
-  let attrs_userContextId = JSON.stringify({ userContextId: 1 });
-  Services.obs.notifyObservers(null, "clear-origin-attributes-data", attrs_userContextId);
+  Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId: 1 });
 
   for (let test of fourthTests) {
     handlers_called = 0;
     await test_channel(...test);
   }
 }
 
 function run_test() {
--- a/toolkit/components/cleardata/ClearDataService.js
+++ b/toolkit/components/cleardata/ClearDataService.js
@@ -852,16 +852,21 @@ ClearDataService.prototype = Object.free
       return Cr.NS_ERROR_INVALID_ARG;
     }
 
     return this._deleteInternal(aFlags, aCallback, aCleaner => {
       return aCleaner.deleteAll();
     });
   },
 
+  deleteDataFromOriginAttributesPattern(aPattern) {
+    Services.obs.notifyObservers(null, "clear-origin-attributes-data",
+      JSON.stringify(aPattern));
+  },
+
   // This internal method uses aFlags against FLAGS_MAP in order to retrieve a
   // list of 'Cleaners'. For each of them, the aHelper callback retrieves a
   // promise object. All these promise objects are resolved before calling
   // onDataDeleted.
   _deleteInternal(aFlags, aCallback, aHelper) {
     let resultFlags = 0;
     let promises = FLAGS_MAP.filter(c => aFlags & c.flag).map(c => {
       // Let's collect the failure in resultFlags.
--- a/toolkit/components/cleardata/nsIClearDataService.idl
+++ b/toolkit/components/cleardata/nsIClearDataService.idl
@@ -74,16 +74,22 @@ interface nsIClearDataService : nsISuppo
    * Delete all data from any host, in any time range.
    * @param aFlags List of flags. See below the accepted values.
    * @param aCallback ths callback will be executed when the operation is
    *                  completed.
    */
   void deleteData(in uint32_t aFlags,
                   in nsIClearDataCallback aCallback);
 
+  /**
+   * Delete all data from an OriginAttributesPatternDictionary.
+   * @param aOriginAttributesPattern the originAttributes dictionary.
+   */
+  void deleteDataFromOriginAttributesPattern(in jsval aOriginAttributesPattern);
+
   /**************************************************************************
    * Listed below are the various flags which may be or'd together.
    */
 
   /**
    * Delete cookies.
    */
   const uint32_t CLEAR_COOKIES = 1 << 0;
--- a/toolkit/components/contextualidentity/ContextualIdentityService.jsm
+++ b/toolkit/components/contextualidentity/ContextualIdentityService.jsm
@@ -290,18 +290,17 @@ function _ContextualIdentityService(path
   remove(userContextId) {
     this.ensureDataReady();
 
     let index = this._identities.findIndex(i => i.userContextId == userContextId && i.public);
     if (index == -1) {
       return false;
     }
 
-    Services.obs.notifyObservers(null, "clear-origin-attributes-data",
-                                 JSON.stringify({ userContextId }));
+    Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId });
 
     let deletedOutput = this.getIdentityObserverOutput(this.getPublicIdentityFromId(userContextId));
     this._identities.splice(index, 1);
     this._openedIdentities.delete(userContextId);
     this.saveSoon();
     Services.obs.notifyObservers(deletedOutput, "contextual-identity-deleted");
 
     return true;
@@ -462,18 +461,17 @@ function _ContextualIdentityService(path
 
   notifyAllContainersCleared() {
     for (let identity of this._identities) {
       // Don't clear the data related to private identities (e.g. the one used internally
       // for the thumbnails and the one used for the storage.local IndexedDB backend).
       if (!identity.public) {
         continue;
       }
-      Services.obs.notifyObservers(null, "clear-origin-attributes-data",
-                                   JSON.stringify({ userContextId: identity.userContextId }));
+      Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId: identity.userContextId });
     }
   },
 
   _forEachContainerTab(callback, userContextId = 0) {
     for (let win of Services.wm.getEnumerator("navigator:browser")) {
       if (win.closed || !win.gBrowser) {
         continue;
       }
@@ -531,18 +529,17 @@ function _ContextualIdentityService(path
       // Skip any userContextIds that should not be cleared.
       if (cookie.originAttributes.userContextId >= minUserContextId &&
           !keepDataContextIds.includes(cookie.originAttributes.userContextId)) {
         cookiesUserContextIds.add(cookie.originAttributes.userContextId);
       }
     }
 
     for (let userContextId of cookiesUserContextIds) {
-      Services.obs.notifyObservers(null, "clear-origin-attributes-data",
-                                   JSON.stringify({ userContextId }));
+      Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId });
     }
   },
 
   migrate2to3(data) {
     // migrating from 2 to 3 is basically just increasing the version id.
     // This migration was needed for bug 1419591. See bug 1419591 to know more.
     data.version = 3;
 
--- a/toolkit/components/thumbnails/BackgroundPageThumbs.jsm
+++ b/toolkit/components/thumbnails/BackgroundPageThumbs.jsm
@@ -507,18 +507,19 @@ Capture.prototype = {
           Cu.reportError(err);
         }
       }
 
       if (Services.prefs.getBoolPref(ABOUT_NEWTAB_SEGREGATION_PREF)) {
         // Clear the data in the private container for thumbnails.
         let privateIdentity =
           ContextualIdentityService.getPrivateIdentity("userContextIdInternal.thumbnail");
-        Services.obs.notifyObservers(null, "clear-origin-attributes-data",
-          JSON.stringify({ userContextId: privateIdentity.userContextId }));
+        if (privateIdentity) {
+          Services.clearData.deleteDataFromOriginAttributesPattern({ userContextId: privateIdentity.userContextId });
+        }
       }
     };
 
     if (!data) {
       done();
       return;
     }