Bug 1433870 - Policy: Tracking protection. r=felipe, r=bytesized, a=jcristau
authorMichael Kaply <mozilla@kaply.com>
Fri, 16 Mar 2018 15:36:58 -0500
changeset 462795 117ae537ef0d0a989b9505369a28c6a58bdfeae8
parent 462794 1f86e8c381c66a232d926c62eb2f6272df21f660
child 462796 998a57bd05ada832da0cd78e2db628699afee80e
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfelipe, bytesized, jcristau
bugs1433870
milestone60.0
Bug 1433870 - Policy: Tracking protection. r=felipe, r=bytesized, a=jcristau
browser/components/enterprisepolicies/Policies.jsm
browser/components/enterprisepolicies/schemas/policies-schema.json
browser/components/enterprisepolicies/tests/browser/browser.ini
browser/components/enterprisepolicies/tests/browser/browser_policy_enable_tracking_protection.js
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -242,16 +242,28 @@ var Policies = {
   },
 
   "DontCheckDefaultBrowser": {
     onBeforeUIStartup(manager, param) {
       setAndLockPref("browser.shell.checkDefaultBrowser", false);
     }
   },
 
+  "EnableTrackingProtection": {
+    onBeforeUIStartup(manager, param) {
+      if (param.Locked) {
+        setAndLockPref("privacy.trackingprotection.enabled", param.Value);
+        setAndLockPref("privacy.trackingprotection.pbmode.enabled", param.Value);
+      } else {
+        setDefaultPref("privacy.trackingprotection.enabled", param.Value);
+        setDefaultPref("privacy.trackingprotection.pbmode.enabled", param.Value);
+      }
+    }
+  },
+
   "FlashPlugin": {
     onBeforeUIStartup(manager, param) {
       addAllowDenyPermissions("plugin:flash", param.Allow, param.Block);
     }
   },
 
   "Homepage": {
     onBeforeUIStartup(manager, param) {
@@ -317,16 +329,33 @@ var Policies = {
  * @param {boolean,number,string} prefValue
  *        The value to set and lock
  */
 function setAndLockPref(prefName, prefValue) {
   if (Services.prefs.prefIsLocked(prefName)) {
     Services.prefs.unlockPref(prefName);
   }
 
+  setDefaultPref(prefName, prefValue);
+
+  Services.prefs.lockPref(prefName);
+}
+
+/**
+ * setDefaultPref
+ *
+ * Sets the _default_ value of a pref.
+ * The value is only changed in memory, and not stored to disk.
+ *
+ * @param {string} prefName
+ *        The pref to be changed
+ * @param {boolean,number,string} prefValue
+ *        The value to set
+ */
+function setDefaultPref(prefName, prefValue) {
   let defaults = Services.prefs.getDefaultBranch("");
 
   switch (typeof(prefValue)) {
     case "boolean":
       defaults.setBoolPref(prefName, prefValue);
       break;
 
     case "number":
@@ -336,18 +365,16 @@ function setAndLockPref(prefName, prefVa
 
       defaults.setIntPref(prefName, prefValue);
       break;
 
     case "string":
       defaults.setStringPref(prefName, prefValue);
       break;
   }
-
-  Services.prefs.lockPref(prefName);
 }
 
 /**
  * addAllowDenyPermissions
  *
  * Helper function to call the permissions manager (Services.perms.add)
  * for two arrays of URLs.
  *
--- a/browser/components/enterprisepolicies/schemas/policies-schema.json
+++ b/browser/components/enterprisepolicies/schemas/policies-schema.json
@@ -187,16 +187,32 @@
 
     "DontCheckDefaultBrowser": {
       "description": "Don't check for the default browser on startup.",
       "first_available": "60.0",
 
       "type": "boolean"
     },
 
+    "EnableTrackingProtection": {
+      "description": "Enables or disables tracking protection and optionally locks it.",
+      "first_available": "60.0",
+
+      "type": "object",
+      "properties": {
+        "Value": {
+          "type": "boolean"
+        },
+        "Locked": {
+          "type": "boolean"
+        }
+      },
+      "required": ["Value"]
+    },
+
     "FlashPlugin": {
       "description": "Allow or deny flash plugin usage.",
       "first_available": "60.0",
 
       "type": "object",
       "properties": {
         "Allow": {
           "type": "array",
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -29,10 +29,11 @@ support-files =
 [browser_policy_disable_fxscreenshots.js]
 [browser_policy_disable_masterpassword.js]
 [browser_policy_disable_pdfjs.js]
 [browser_policy_disable_pocket.js]
 [browser_policy_disable_privatebrowsing.js]
 [browser_policy_disable_shield.js]
 [browser_policy_display_bookmarks.js]
 [browser_policy_display_menu.js]
+[browser_policy_enable_tracking_protection.js]
 [browser_policy_remember_passwords.js]
 [browser_policy_set_homepage.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_enable_tracking_protection.js
@@ -0,0 +1,36 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+add_task(async function test_policy_enable_tracking_protection1() {
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "EnableTrackingProtection": {
+        "Value": true
+      }
+    }
+  });
+
+  is(Services.prefs.getBoolPref("privacy.trackingprotection.enabled"), true, "Tracking protection has been enabled by default.");
+  is(Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled"), true, "Tracking protection has been enabled by default in private browsing mode.");
+  is(Services.prefs.prefIsLocked("privacy.trackingprotection.enabled"), false, "Tracking protection pref is not locked.");
+});
+
+add_task(async function test_policy_enable_tracking_protection_locked() {
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "EnableTrackingProtection": {
+        "Value": false,
+        "Locked": true
+      }
+    }
+  });
+
+  is(Services.prefs.getBoolPref("privacy.trackingprotection.enabled"), false, "Tracking protection has been disabled by default.");
+  is(Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled"), false, "Tracking protection has been disabled by default in private browsing mode.");
+  is(Services.prefs.prefIsLocked("privacy.trackingprotection.enabled"), true, "Tracking protection pref is locked.");
+
+  Services.prefs.unlockPref("privacy.trackingprotection.enabled");
+  Services.prefs.unlockPref("privacy.trackingprotection.pbmode.enabled");
+});