Bug 1433870 - Tracking protection policy. r?felipe,bytesized draft
authorMichael Kaply <mozilla@kaply.com>
Fri, 16 Mar 2018 15:36:58 -0500
changeset 768780 bd0e08074968cda4d28c0666a5972064dbfd07ab
parent 768720 238bbfbb56d1e68c6e66c5de365f23872bc860ac
push id102978
push usermozilla@kaply.com
push dateFri, 16 Mar 2018 20:37:20 +0000
reviewersfelipe, bytesized
bugs1433870
milestone61.0a1
Bug 1433870 - Tracking protection policy. r?felipe,bytesized MozReview-Commit-ID: 2U9v7F7fggN
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
@@ -258,16 +258,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) {
@@ -333,16 +345,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":
@@ -352,18 +381,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
@@ -202,16 +202,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
@@ -31,10 +31,11 @@ support-files =
 [browser_policy_disable_masterpassword.js]
 [browser_policy_disable_pdfjs.js]
 [browser_policy_disable_pocket.js]
 [browser_policy_disable_privatebrowsing.js]
 [browser_policy_disable_safemode.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,33 @@
+/* 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_protection1() {
+  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.");
+});