Bug 1484876 - Part 1: Refactor the PrivateBrowsingTrackingProtectionWhitelist service on top of the permission manager; r=johannh
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 20 Aug 2018 19:32:32 -0400
changeset 481157 bddf9e4f6f3ab2c4ed2b9c4d64250319b957c5c0
parent 481156 a77c322a5422ace5a0763f3beb45d93baa79536b
child 481158 66519e95427ef0744b84e3dd292867ccb8c3a854
push id232
push userfmarier@mozilla.com
push dateWed, 05 Sep 2018 20:45:54 +0000
reviewersjohannh
bugs1484876
milestone63.0a1
Bug 1484876 - Part 1: Refactor the PrivateBrowsingTrackingProtectionWhitelist service on top of the permission manager; r=johannh Session permissions aren't persisted to disk, so we can easily add a new permission type and only store session permissions in the permission manager database, and drop the in-memory allow list that this service maintains. This has the advantage that the permission manager already has the IPC machinery to make this information available in the content processes, so we can also check this allow list in the content process.
toolkit/components/privatebrowsing/PrivateBrowsingTrackingProtectionWhitelist.js
--- a/toolkit/components/privatebrowsing/PrivateBrowsingTrackingProtectionWhitelist.js
+++ b/toolkit/components/privatebrowsing/PrivateBrowsingTrackingProtectionWhitelist.js
@@ -1,62 +1,56 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 function PrivateBrowsingTrackingProtectionWhitelist() {
-  // The list of URIs explicitly excluded from tracking protection.
-  this._allowlist = [];
-
   Services.obs.addObserver(this, "last-pb-context-exited", true);
 }
 
 PrivateBrowsingTrackingProtectionWhitelist.prototype = {
   classID: Components.ID("{a319b616-c45d-4037-8d86-01c592b5a9af}"),
   QueryInterface: ChromeUtils.generateQI([Ci.nsIPrivateBrowsingTrackingProtectionWhitelist, Ci.nsIObserver, Ci.nsISupportsWeakReference]),
   _xpcom_factory: XPCOMUtils.generateSingletonFactory(PrivateBrowsingTrackingProtectionWhitelist),
 
   /**
    * Add the provided URI to the list of allowed tracking sites.
    *
    * @param uri nsIURI
    *        The URI to add to the list.
    */
   addToAllowList(uri) {
-    if (!this._allowlist.includes(uri.spec)) {
-      this._allowlist.push(uri.spec);
-    }
+    Services.perms.add(uri, "trackingprotection-pb", Ci.nsIPermissionManager.ALLOW_ACTION,
+                       Ci.nsIPermissionManager.EXPIRE_SESSION);
   },
 
   /**
    * Remove the provided URI from the list of allowed tracking sites.
    *
    * @param uri nsIURI
    *        The URI to add to the list.
    */
   removeFromAllowList(uri) {
-    let index = this._allowlist.indexOf(uri.spec);
-    if (index !== -1) {
-      this._allowlist.splice(index, 1);
-    }
+    Services.perms.remove(uri, "trackingprotection-pb");
   },
 
   /**
    * Check if the provided URI exists in the list of allowed tracking sites.
    *
    * @param uri nsIURI
    *        The URI to add to the list.
    */
   existsInAllowList(uri) {
-    return this._allowlist.includes(uri.spec);
+    return Services.perms.testPermission(uri, "trackingprotection-pb") ==
+           Ci.nsIPermissionManager.ALLOW_ACTION;
   },
 
   observe(subject, topic, data) {
     if (topic == "last-pb-context-exited") {
-      this._allowlist = [];
+      Services.perms.removeByType("trackingprotection-pb");
     }
   }
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PrivateBrowsingTrackingProtectionWhitelist]);