Bug 1487300 - Restore the state of the tracking protection menu when All Detected Trackers is checked after being previously unchecked; r=johannh
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 04 Sep 2018 19:41:59 -0400
changeset 435060 fd88ac1e5391f66cfd31b5484cd4574b1a34d144
parent 435059 dce5fe50d17a49193cf57df9b06c67f2b8526322
child 435061 2766bfc77116ee3a53426a4111ccddb7abb92aa8
child 435113 7569b575ea252d924640f6663b1516dd11533ad9
push id107545
push usereakhgari@mozilla.com
push dateThu, 06 Sep 2018 14:28:44 +0000
treeherdermozilla-inbound@fd88ac1e5391 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1487300
milestone64.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 1487300 - Restore the state of the tracking protection menu when All Detected Trackers is checked after being previously unchecked; r=johannh Differential Revision: https://phabricator.services.mozilla.com/D4993
browser/components/preferences/in-content/privacy.js
browser/components/preferences/in-content/tests/browser_contentblocking.js
--- a/browser/components/preferences/in-content/privacy.js
+++ b/browser/components/preferences/in-content/privacy.js
@@ -71,16 +71,21 @@ Preferences.addAll([
   { id: "browser.contentblocking.enabled", type: "bool" },
 
   // FastBlock
   { id: "browser.fastblock.enabled", type: "bool" },
 
   // Tracking Protection
   { id: "privacy.trackingprotection.enabled", type: "bool" },
   { id: "privacy.trackingprotection.pbmode.enabled", type: "bool" },
+  // This isn't a configuration pref, rather it's for saving the previous state
+  // of the UI when we turn off the TP controls when the user checks off the
+  // All Detected Trackers under Content Blocking.  This pref isn't listed in
+  // all.js/firefox.js to make sure it doesn't appear in about:config by default.
+  { id: "browser.privacy.trackingprotection.menu", type: "string" },
 
   // Button prefs
   { id: "pref.privacy.disable_button.cookie_exceptions", type: "bool" },
   { id: "pref.privacy.disable_button.view_cookies", type: "bool" },
   { id: "pref.privacy.disable_button.change_blocklist", type: "bool" },
   { id: "pref.privacy.disable_button.tracking_protection_exceptions", type: "bool" },
 
   // Location Bar
@@ -698,41 +703,55 @@ var gPrivacyPane = {
   // TRACKING PROTECTION MODE
 
   /**
    * Selects the right item of the Tracking Protection radiogroup.
    */
   trackingProtectionReadPrefs() {
     let enabledPref = Preferences.get("privacy.trackingprotection.enabled");
     let pbmPref = Preferences.get("privacy.trackingprotection.pbmode.enabled");
+    let btpmPref = Preferences.get("browser.privacy.trackingprotection.menu");
     let tpControl,
         tpCheckbox;
     if (contentBlockingUiEnabled) {
       tpControl = document.getElementById("trackingProtectionMenu");
       tpCheckbox = document.getElementById("contentBlockingTrackingProtectionCheckbox");
     } else {
       tpControl = document.getElementById("trackingProtectionRadioGroup");
     }
 
+    let savedMenuValue;
+    if (contentBlockingUiEnabled) {
+      // Only look at the backup pref when restoring the checkbox next to
+      // "All Detected Trackers".
+      if (["always", "private"].includes(btpmPref.value) &&
+          tpCheckbox.checked) {
+        savedMenuValue = btpmPref.value;
+      }
+    }
+
     this._updateTrackingProtectionUI();
 
     // Global enable takes precedence over enabled in Private Browsing.
     if (enabledPref.value) {
       tpControl.value = "always";
       if (tpCheckbox) {
         tpCheckbox.checked = true;
       }
     } else if (pbmPref.value) {
       tpControl.value = "private";
       if (tpCheckbox) {
         tpCheckbox.checked = true;
       }
     } else if (!tpCheckbox) {
       tpControl.value = "never";
     } else {
+      if (savedMenuValue) {
+        tpControl.value = savedMenuValue;
+      }
       tpCheckbox.checked = false;
     }
   },
 
   /**
    * Selects the right items of the new Cookies & Site Data UI.
    */
   networkCookieBehaviorReadPrefs() {
@@ -780,29 +799,31 @@ var gPrivacyPane = {
   },
 
   /**
    * Sets the pref values based on the selected item of the radiogroup.
    */
   trackingProtectionWritePrefs() {
     let enabledPref = Preferences.get("privacy.trackingprotection.enabled");
     let pbmPref = Preferences.get("privacy.trackingprotection.pbmode.enabled");
+    let btpmPref = Preferences.get("browser.privacy.trackingprotection.menu");
     let tpControl,
         tpCheckbox;
     if (contentBlockingUiEnabled) {
       tpControl = document.getElementById("trackingProtectionMenu");
       tpCheckbox = document.getElementById("contentBlockingTrackingProtectionCheckbox");
     } else {
       tpControl = document.getElementById("trackingProtectionRadioGroup");
     }
 
     let value;
     if (tpCheckbox) {
       if (tpCheckbox.checked) {
         value = tpControl.value;
+        btpmPref.value = value;
       } else {
         value = "never";
       }
     } else {
       value = tpControl.value;
     }
 
     switch (value) {
--- a/browser/components/preferences/in-content/tests/browser_contentblocking.js
+++ b/browser/components/preferences/in-content/tests/browser_contentblocking.js
@@ -106,16 +106,23 @@ add_task(async function testContentBlock
   let alwaysEnabledControls = [
     "#trackingProtectionMenuDesc",
     ".content-blocking-category-name",
     "#changeBlockListLink",
   ];
 
   tpCheckbox.checked = true;
 
+  // Select "Always" under "All Detected Trackers".
+  let always = doc.querySelector("#trackingProtectionMenu > radio[value=always]");
+  let private = doc.querySelector("#trackingProtectionMenu > radio[value=private]");
+  always.radioGroup.selectedItem = always;
+  ok(!private.selected, "The Only in private windows item should not be selected");
+  ok(always.selected, "The Always item should be selected");
+
   // The first time, privacy-pane-tp-ui-updated won't be dispatched since the
   // assignment above is a no-op.
 
   // Ensure the dependent controls are enabled
   checkControlStateWorker(doc, dependentControls, true);
   checkControlStateWorker(doc, alwaysEnabledControls, true);
 
   let promise = TestUtils.topicObserved("privacy-pane-tp-ui-updated");
@@ -123,16 +130,31 @@ add_task(async function testContentBlock
 
   await promise;
   ok(!tpCheckbox.checked, "The checkbox should now be unchecked");
 
   // Ensure the dependent controls are disabled
   checkControlStateWorker(doc, dependentControls, false);
   checkControlStateWorker(doc, alwaysEnabledControls, true);
 
+  // Make sure the selection in the tracking protection submenu persists after
+  // a few times of checking and unchecking All Detected Trackers.
+  // Doing this in a loop in order to avoid typing in the unrolled version manually.
+  // We need to go from the checked state of the checkbox to unchecked back to
+  // checked again...
+  for (let i = 0; i < 3; ++i) {
+    promise = TestUtils.topicObserved("privacy-pane-tp-ui-updated");
+    EventUtils.synthesizeMouseAtCenter(tpCheckbox, {}, doc.defaultView);
+
+    await promise;
+    is(tpCheckbox.checked, i % 2 == 0, "The checkbox should now be unchecked");
+    ok(!private.selected, "The Only in private windows item should still not be selected");
+    ok(always.selected, "The Always item should still be selected");
+  }
+
   gBrowser.removeCurrentTab();
 
   for (let pref of prefs) {
     SpecialPowers.clearUserPref(pref[0]);
   }
 });
 
 // Tests that the content blocking "Restore Defaults" button does what it's supposed to.