Bug 1546973 - Add download related policies. r=jaws,flod
authorMichael Kaply <mozilla@kaply.com>
Fri, 03 May 2019 17:53:32 +0000
changeset 531359 914bcc5d82716ce7ae91b90202b754ead0db06aa
parent 531358 cbf988d7f823f40af06d3124273626c6b63ee1f2
child 531360 4e998aa9c04bf1dbce2087bfcf91ce8292fb7855
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, flod
bugs1546973
milestone68.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 1546973 - Add download related policies. r=jaws,flod Differential Revision: https://phabricator.services.mozilla.com/D29277
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_downloads.js
browser/locales/en-US/browser/policies/policies-descriptions.ftl
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -12,16 +12,17 @@ XPCOMUtils.defineLazyServiceGetters(this
   gCertDB: ["@mozilla.org/security/x509certdb;1", "nsIX509CertDB"],
   gXulStore: ["@mozilla.org/xul/xulstore;1", "nsIXULStore"],
 });
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   AddonManager: "resource://gre/modules/AddonManager.jsm",
   BookmarksPolicies: "resource:///modules/policies/BookmarksPolicies.jsm",
   CustomizableUI: "resource:///modules/CustomizableUI.jsm",
+  FileUtils: "resource://gre/modules/FileUtils.jsm",
   ProxyPolicies: "resource:///modules/policies/ProxyPolicies.jsm",
   WebsiteFilter: "resource:///modules/policies/WebsiteFilter.jsm",
 });
 
 XPCOMUtils.defineLazyGlobalGetters(this, ["File", "FileReader"]);
 
 const PREF_LOGLEVEL           = "browser.policies.loglevel";
 const BROWSER_DOCUMENT_URL    = AppConstants.BROWSER_CHROME_URL;
@@ -281,16 +282,24 @@ var Policies = {
           newLifetimePolicy = KEEP_COOKIES_UNTIL_END_OF_SESSION;
         }
 
         setDefaultPref("network.cookie.lifetimePolicy", newLifetimePolicy, param.Locked);
       }
     },
   },
 
+  "DefaultDownloadDirectory": {
+    onBeforeAddons(manager, param) {
+      setDefaultPref("browser.download.dir", replacePathVariables(param));
+      // If a custom download directory is being used, just lock folder list to 2.
+      setAndLockPref("browser.download.folderList", 2);
+    },
+  },
+
   "DisableAppUpdate": {
     onBeforeAddons(manager, param) {
       if (param) {
         manager.disallowFeature("appUpdate");
       }
     },
   },
 
@@ -493,16 +502,24 @@ var Policies = {
   },
 
   "DontCheckDefaultBrowser": {
     onBeforeUIStartup(manager, param) {
       setAndLockPref("browser.shell.checkDefaultBrowser", !param);
     },
   },
 
+  "DownloadDirectory": {
+    onBeforeAddons(manager, param) {
+      setAndLockPref("browser.download.dir", replacePathVariables(param));
+      // If a custom download directory is being used, just lock folder list to 2.
+      setAndLockPref("browser.download.folderList", 2);
+    },
+  },
+
   "EnableTrackingProtection": {
     onBeforeUIStartup(manager, param) {
       if (param.Value) {
         setDefaultPref("privacy.trackingprotection.enabled", true, param.Locked);
         setDefaultPref("privacy.trackingprotection.pbmode.enabled", true, param.Locked);
       } else {
         setAndLockPref("privacy.trackingprotection.enabled", false);
         setAndLockPref("privacy.trackingprotection.pbmode.enabled", false);
@@ -817,16 +834,22 @@ var Policies = {
   "Preferences": {
     onBeforeAddons(manager, param) {
       for (let preference in param) {
         setAndLockPref(preference, param[preference]);
       }
     },
   },
 
+  "PromptForDownloadLocation": {
+    onBeforeAddons(manager, param) {
+      setAndLockPref("browser.download.useDownloadDir", !param);
+    },
+  },
+
   "Proxy": {
     onBeforeAddons(manager, param) {
       if (param.Locked) {
         manager.disallowFeature("changeProxySettings");
         ProxyPolicies.configureProxySettings(param, setAndLockPref);
       } else {
         ProxyPolicies.configureProxySettings(param, setDefaultPref);
       }
@@ -1269,16 +1292,23 @@ async function runOncePerModification(ac
  *
  * Helper function that clears a runOnce policy.
 */
 function clearRunOnceModification(actionName) {
   let prefName = `browser.policies.runOncePerModification.${actionName}`;
   Services.prefs.clearUserPref(prefName);
 }
 
+function replacePathVariables(path) {
+  if (path.includes("${home}")) {
+    return path.replace("${home}", FileUtils.getFile("Home", []).path);
+  }
+  return path;
+}
+
 let gChromeURLSBlocked = false;
 
 // If any about page is blocked, we block the loading of all
 // chrome:// URLs in the browser window.
 function blockAboutPage(manager, feature, neededOnContentProcess = false) {
   manager.disallowFeature(feature, neededOnContentProcess);
   if (!gChromeURLSBlocked) {
     blockAllChromeURLs();
--- a/browser/components/enterprisepolicies/schemas/policies-schema.json
+++ b/browser/components/enterprisepolicies/schemas/policies-schema.json
@@ -159,16 +159,20 @@
         },
 
         "Locked": {
           "type": "boolean"
         }
       }
     },
 
+    "DefaultDownloadDirectory": {
+      "type": "string"
+    },
+
     "DisableAppUpdate": {
       "type": "boolean"
     },
 
     "DisableBuiltinPDFViewer": {
       "type": "boolean"
     },
 
@@ -271,16 +275,20 @@
         }
       }
     },
 
     "DontCheckDefaultBrowser": {
       "type": "boolean"
     },
 
+    "DownloadDirectory": {
+      "type": "string"
+    },
+
     "EnableTrackingProtection": {
       "type": "object",
       "properties": {
         "Value": {
           "type": "boolean"
         },
         "Locked": {
           "type": "boolean"
@@ -619,16 +627,20 @@
           "type": "boolean"
         },
         "browser.urlbar.suggest.bookmark": {
           "type": "boolean"
         }
       }
     },
 
+    "PromptForDownloadLocation": {
+      "type": "boolean"
+    },
+
     "Proxy": {
       "type": "object",
       "properties": {
         "Mode": {
           "type": "string",
           "enum": ["none", "system", "manual", "autoDetect", "autoConfig"]
         },
 
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -44,16 +44,17 @@ skip-if = (verify && debug && (os == 'ma
 [browser_policy_disable_profile_reset.js]
 [browser_policy_disable_profile_import.js]
 [browser_policy_disable_safemode.js]
 [browser_policy_disable_shield.js]
 [browser_policy_disable_telemetry.js]
 [browser_policy_display_bookmarks.js]
 [browser_policy_display_menu.js]
 [browser_policy_extensions.js]
+[browser_policy_downloads.js]
 [browser_policy_extensionsettings.js]
 [browser_policy_locale.js]
 [browser_policy_firefoxhome.js]
 [browser_policy_override_postupdatepage.js]
 [browser_policy_permissions.js]
 [browser_policy_proxy.js]
 [browser_policy_preferences.js]
 [browser_policy_search_engine.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_downloads.js
@@ -0,0 +1,26 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+ChromeUtils.defineModuleGetter(this, "FileUtils",
+                               "resource://gre/modules/FileUtils.jsm");
+
+add_task(async function test_notice_in_aboutprefences() {
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "DefaultDownloadDirectory": "${home}/Downloads",
+      "PromptForDownloadLocation": false,
+     },
+  });
+
+  await BrowserTestUtils.withNewTab("about:preferences", async browser => {
+    is(browser.contentDocument.getElementById("alwaysAsk").disabled, true, "alwaysAsk should be disabled.");
+    is(browser.contentDocument.getElementById("saveTo").selected, true, "saveTo should be selected.");
+    is(browser.contentDocument.getElementById("saveTo").disabled, true, "saveTo should be disabled.");
+    let home = FileUtils.getFile("Home", []).path;
+    is(Services.prefs.getStringPref("browser.download.dir"), home + "/Downloads", "browser.download.dir should be ${home}/Downloads.");
+    is(Services.prefs.getBoolPref("browser.download.useDownloadDir"), true, "browser.download.useDownloadDir should be true.");
+    is(Services.prefs.prefIsLocked("browser.download.useDownloadDir"), true, "browser.download.useDownloadDir should be locked.");
+  });
+});
--- a/browser/locales/en-US/browser/policies/policies-descriptions.ftl
+++ b/browser/locales/en-US/browser/policies/policies-descriptions.ftl
@@ -26,16 +26,18 @@ policy-BlockAboutSupport = Block access 
 policy-Bookmarks = Create bookmarks in the Bookmarks toolbar, Bookmarks menu, or a specified folder inside them.
 
 policy-CaptivePortal = Enable or disable captive portal support.
 
 policy-CertificatesDescription = Add certificates or use built-in certificates.
 
 policy-Cookies = Allow or deny websites to set cookies.
 
+policy-DefaultDownloadDirectory = Set the default download directory.
+
 policy-DisableAppUpdate = Prevent the browser from updating.
 
 policy-DisableBuiltinPDFViewer = Disable PDF.js, the built-in PDF viewer in { -brand-short-name }.
 
 policy-DisableDeveloperTools = Block access to the developer tools.
 
 policy-DisableFeedbackCommands = Disable commands to send feedback from the Help menu (Submit Feedback and Report Deceptive Site).
 
@@ -73,16 +75,18 @@ policy-DisableTelemetry = Turn off Telem
 policy-DisplayBookmarksToolbar = Display the Bookmarks Toolbar by default.
 
 policy-DisplayMenuBar = Display the Menu Bar by default.
 
 policy-DNSOverHTTPS = Configure DNS over HTTPS.
 
 policy-DontCheckDefaultBrowser = Disable check for default browser on startup.
 
+policy-DownloadDirectory = Set and lock the download directory.
+
 # “lock” means that the user won’t be able to change this setting
 policy-EnableTrackingProtection = Enable or disable Content Blocking and optionally lock it.
 
 # A “locked” extension can’t be disabled or removed by the user. This policy
 # takes 3 keys (“Install”, ”Uninstall”, ”Locked”), you can either keep them in
 # English or translate them as verbs.
 policy-Extensions = Install, uninstall or lock extensions. The Install option takes URLs or paths as parameters. The Uninstall and Locked options take extension IDs.
 
@@ -114,16 +118,18 @@ policy-OverrideFirstRunPage = Override t
 policy-OverridePostUpdatePage = Override the post-update “What’s New” page. Set this policy to blank if you want to disable the post-update page.
 
 policy-Permissions = Configure permissions for camera, microphone, location and notifications.
 
 policy-PopupBlocking = Allow certain websites to display popups by default.
 
 policy-Preferences = Set and lock the value for a subset of preferences.
 
+policy-PromptForDownloadLocation = Ask where to save files when downloading.
+
 policy-Proxy = Configure proxy settings.
 
 policy-RequestedLocales = Set the list of requested locales for the application in order of preference.
 
 policy-SanitizeOnShutdown2 = Clear navigation data on shutdown.
 
 policy-SearchBar = Set the default location of the search bar. The user is still allowed to customize it.