Bug 1581409 - ensure `SocialTracking` enabled status is correct; r=johannh
authorLiang-Heng Chen <xeonchen@gmail.com>
Tue, 24 Sep 2019 18:45:11 +0000
changeset 494805 f94173bcd08dcb7059333c7e01ac8ab5c53de63e
parent 494804 5eca1b56929069654b5851c9dd054e821570fd78
child 494806 56c3918b5c21ec54e3e51c5ba9a51d063946aaca
push id114131
push userdluca@mozilla.com
push dateThu, 26 Sep 2019 09:47:34 +0000
treeherdermozilla-inbound@1dc1a755079a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1581409
milestone71.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 1581409 - ensure `SocialTracking` enabled status is correct; r=johannh - to check the enabled status, we must have: - TP or ETP is enabled - block social tracking cookies is enabled - Hide category item while not blocking social tracking cookies - sync behavior/tests from fingerprinter/cryptominer - check isAllowing/isBlocking without enabled status - add `testCategoryItem()` Differential Revision: https://phabricator.services.mozilla.com/D46161
browser/base/content/browser-siteProtections.js
browser/base/content/test/trackingUI/browser_trackingUI_categories.js
browser/base/content/test/trackingUI/browser_trackingUI_socialtracking.js
--- a/browser/base/content/browser-siteProtections.js
+++ b/browser/base/content/browser-siteProtections.js
@@ -946,17 +946,20 @@ var ThirdPartyCookies = {
       listItem.append(removeException);
     }
 
     return listItem;
   },
 };
 
 var SocialTracking = {
-  PREF_ENABLED: "privacy.socialtracking.block_cookies.enabled",
+  PREF_STP_TP_ENABLED: "privacy.trackingprotection.socialtracking.enabled",
+  PREF_STP_COOKIE_ENABLED: "privacy.socialtracking.block_cookies.enabled",
+  PREF_COOKIE_BEHAVIOR: "network.cookie.cookieBehavior",
+  reportBreakageLabel: "socialtracking",
 
   strings: {
     get subViewBlocked() {
       delete this.subViewBlocked;
       return (this.subViewBlocked = gNavigatorBundle.getString(
         "contentBlocking.fingerprintersView.blocked.label"
       ));
     },
@@ -974,50 +977,75 @@ var SocialTracking = {
         "protections.notBlocking.socialMediaTrackers.title"
       ));
     },
   },
 
   init() {
     XPCOMUtils.defineLazyPreferenceGetter(
       this,
-      "enabled",
-      this.PREF_ENABLED,
+      "socialTrackingProtectionEnabled",
+      this.PREF_STP_TP_ENABLED,
+      false,
+      this.updateCategoryItem.bind(this)
+    );
+    XPCOMUtils.defineLazyPreferenceGetter(
+      this,
+      "rejectTrackingCookies",
+      this.PREF_COOKIE_BEHAVIOR,
+      false,
+      this.updateCategoryItem.bind(this),
+      val => val == Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
+    );
+    XPCOMUtils.defineLazyPreferenceGetter(
+      this,
+      "blockSocialTrackingCookies",
+      this.PREF_STP_COOKIE_ENABLED,
       false,
       this.updateCategoryItem.bind(this)
     );
     this.updateCategoryItem();
   },
 
+  get enabled() {
+    return this.blockSocialTrackingCookies;
+  },
+
+  get blockingEnabled() {
+    return (
+      (this.socialTrackingProtectionEnabled || this.rejectTrackingCookies) &&
+      this.blockSocialTrackingCookies
+    );
+  },
+
   updateCategoryItem() {
-    this.categoryItem.classList.toggle("blocked", this.enabled);
+    this.categoryItem.hidden = !this.enabled;
+    this.categoryItem.classList.toggle("blocked", this.blockingEnabled);
   },
 
   isBlocking(state) {
     let cookieTrackerBlocked =
       (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) != 0;
     let socialtrackingContentBlocked =
       (state &
         Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT) !=
       0;
     let socialtrackingContentLoaded =
       (state & Ci.nsIWebProgressListener.STATE_LOADED_SOCIALTRACKING_CONTENT) !=
       0;
     return (
-      this.enabled &&
-      ((socialtrackingContentLoaded && cookieTrackerBlocked) ||
-        socialtrackingContentBlocked)
+      (socialtrackingContentLoaded && cookieTrackerBlocked) ||
+      socialtrackingContentBlocked
     );
   },
 
   isAllowing(state) {
     return (
-      this.enabled &&
       (state & Ci.nsIWebProgressListener.STATE_LOADED_SOCIALTRACKING_CONTENT) !=
-        0
+      0
     );
   },
 
   isDetected(state) {
     return this.isBlocking(state) || this.isAllowing(state);
   },
 
   get categoryItem() {
@@ -1052,17 +1080,17 @@ var SocialTracking = {
         fragment.appendChild(listItem);
       }
     }
 
     this.subViewList.textContent = "";
     this.subViewList.append(fragment);
     this.subView.setAttribute(
       "title",
-      this.enabled && !gProtectionsHandler.hasException
+      this.blockingEnabled && !gProtectionsHandler.hasException
         ? this.strings.subViewTitleBlocking
         : this.strings.subViewTitleNotBlocking
     );
   },
 
   _createListItem(origin, actions) {
     let isAllowed = actions.some(([state]) => this.isAllowing(state));
     let isDetected =
@@ -1071,16 +1099,17 @@ var SocialTracking = {
     if (!isDetected) {
       return null;
     }
 
     let uri = Services.io.newURI(origin);
 
     let listItem = document.createXULElement("hbox");
     listItem.className = "protections-popup-list-item";
+    listItem.classList.toggle("allowed", isAllowed);
     // Repeat the host in the tooltip in case it's too long
     // and overflows in our panel.
     listItem.tooltipText = uri.host;
 
     let label = document.createXULElement("label");
     label.value = uri.host;
     label.className = "protections-popup-list-host-label";
     label.setAttribute("crop", "end");
--- a/browser/base/content/test/trackingUI/browser_trackingUI_categories.js
+++ b/browser/base/content/test/trackingUI/browser_trackingUI_categories.js
@@ -1,29 +1,31 @@
 const CAT_PREF = "browser.contentblocking.category";
 const TP_PREF = "privacy.trackingprotection.enabled";
 const TP_PB_PREF = "privacy.trackingprotection.pbmode.enabled";
 const TPC_PREF = "network.cookie.cookieBehavior";
 const CM_PREF = "privacy.trackingprotection.cryptomining.enabled";
 const FP_PREF = "privacy.trackingprotection.fingerprinting.enabled";
-const ST_PREF = "privacy.socialtracking.block_cookies.enabled";
+const ST_PREF = "privacy.trackingprotection.socialtracking.enabled";
+const STC_PREF = "privacy.socialtracking.block_cookies.enabled";
 
 ChromeUtils.import(
   "resource://testing-common/CustomizableUITestUtils.jsm",
   this
 );
 
 registerCleanupFunction(function() {
+  Services.prefs.clearUserPref(CAT_PREF);
   Services.prefs.clearUserPref(TP_PREF);
   Services.prefs.clearUserPref(TP_PB_PREF);
   Services.prefs.clearUserPref(TPC_PREF);
-  Services.prefs.clearUserPref(CAT_PREF);
   Services.prefs.clearUserPref(CM_PREF);
   Services.prefs.clearUserPref(FP_PREF);
   Services.prefs.clearUserPref(ST_PREF);
+  Services.prefs.clearUserPref(STC_PREF);
 });
 
 add_task(async function testCookieCategoryLabels() {
   await BrowserTestUtils.withNewTab("http://www.example.com", async function() {
     let categoryItem = document.getElementById(
       "protections-popup-category-cookies"
     );
     let categoryLabelDisabled = document.getElementById(
@@ -157,17 +159,17 @@ add_task(async function testCookieCatego
 let categoryItems = [
   "protections-popup-category-tracking-protection",
   "protections-popup-category-socialblock",
   "protections-popup-category-cookies",
   "protections-popup-category-cryptominers",
   "protections-popup-category-fingerprinters",
 ].map(id => document.getElementById(id));
 
-let categoryEnabledPrefs = [TP_PREF, ST_PREF, TPC_PREF, CM_PREF, FP_PREF];
+let categoryEnabledPrefs = [TP_PREF, STC_PREF, TPC_PREF, CM_PREF, FP_PREF];
 
 let detectedStateFlags = [
   Ci.nsIWebProgressListener.STATE_BLOCKED_TRACKING_CONTENT,
   Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT,
   Ci.nsIWebProgressListener.STATE_COOKIES_LOADED,
   Ci.nsIWebProgressListener.STATE_BLOCKED_CRYPTOMINING_CONTENT,
   Ci.nsIWebProgressListener.STATE_BLOCKED_FINGERPRINTING_CONTENT,
 ];
@@ -179,16 +181,18 @@ async function waitForClass(item, classN
 
   ok(
     item.classList.contains(className) == shouldBePresent,
     `item.classList.contains(${className}) is ${shouldBePresent} for ${item.id}`
   );
 }
 
 add_task(async function testCategorySections() {
+  Services.prefs.setBoolPref(ST_PREF, true);
+
   for (let pref of categoryEnabledPrefs) {
     if (pref == TPC_PREF) {
       Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
     } else {
       Services.prefs.setBoolPref(pref, false);
     }
   }
 
--- a/browser/base/content/test/trackingUI/browser_trackingUI_socialtracking.js
+++ b/browser/base/content/test/trackingUI/browser_trackingUI_socialtracking.js
@@ -1,21 +1,24 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 const TRACKING_PAGE =
   "http://example.org/browser/browser/base/content/test/trackingUI/trackingPage.html";
 
+const ST_PROTECTION_PREF = "privacy.trackingprotection.socialtracking.enabled";
+const ST_BLOCK_COOKIES_PREF = "privacy.socialtracking.block_cookies.enabled";
+
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
-      ["privacy.socialtracking.block_cookies.enabled", true],
-      ["privacy.trackingprotection.socialtracking.enabled", true],
+      [ST_PROTECTION_PREF, true],
+      [ST_BLOCK_COOKIES_PREF, true],
       [
         "urlclassifier.features.socialtracking.blacklistHosts",
         "socialtracking.example.com",
       ],
       [
         "urlclassifier.features.socialtracking.annotate.blacklistHosts",
         "socialtracking.example.com",
       ],
@@ -157,17 +160,91 @@ async function testSubview(hasException)
     );
     gProtectionsHandler.enableForCurrentPage();
     await loaded;
   }
 
   BrowserTestUtils.removeTab(tab);
 }
 
+async function testCategoryItem() {
+  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, false);
+
+  let promise = BrowserTestUtils.openNewForegroundTab({
+    url: TRACKING_PAGE,
+    gBrowser,
+  });
+  let [tab] = await Promise.all([promise, waitForContentBlockingEvent()]);
+
+  let categoryItem = document.getElementById(
+    "protections-popup-category-socialblock"
+  );
+
+  ok(
+    !categoryItem.classList.contains("blocked"),
+    "Category not marked as blocked"
+  );
+  ok(
+    categoryItem.classList.contains("notFound"),
+    "Category marked as not found"
+  );
+  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, true);
+  ok(categoryItem.classList.contains("blocked"), "Category marked as blocked");
+  ok(
+    categoryItem.classList.contains("notFound"),
+    "Category marked as not found"
+  );
+  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, false);
+  ok(
+    !categoryItem.classList.contains("blocked"),
+    "Category not marked as blocked"
+  );
+  ok(
+    categoryItem.classList.contains("notFound"),
+    "Category marked as not found"
+  );
+
+  promise = waitForContentBlockingEvent();
+
+  await ContentTask.spawn(tab.linkedBrowser, {}, function() {
+    content.postMessage("socialtracking", "*");
+  });
+
+  await promise;
+
+  ok(
+    !categoryItem.classList.contains("blocked"),
+    "Category not marked as blocked"
+  );
+  ok(
+    !categoryItem.classList.contains("notFound"),
+    "Category not marked as not found"
+  );
+  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, true);
+  ok(categoryItem.classList.contains("blocked"), "Category marked as blocked");
+  ok(
+    !categoryItem.classList.contains("notFound"),
+    "Category not marked as not found"
+  );
+  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, false);
+  ok(
+    !categoryItem.classList.contains("blocked"),
+    "Category not marked as blocked"
+  );
+  ok(
+    !categoryItem.classList.contains("notFound"),
+    "Category not marked as not found"
+  );
+
+  BrowserTestUtils.removeTab(tab);
+}
+
 add_task(async function testIdentityUI() {
   requestLongerTimeout(2);
 
   await testIdentityState(false);
   await testIdentityState(true);
 
   await testSubview(false);
   await testSubview(true);
+
+  await testCategoryItem();
 });