Bug 1440932 - Implement a runOnce helper function for policies. r=bytesized
authorFelipe Gomes <felipc@gmail.com>
Mon, 26 Feb 2018 15:42:40 -0300
changeset 405322 5d7a2c906875e26e2e09f874af703775d7559349
parent 405321 b82d04b3bd6f886d1be3d55474f955800c3ffb68
child 405323 1c46e9a24298cd10c3ecc19d05129f3d6bead7dd
push id60196
push userfelipc@gmail.com
push dateMon, 26 Feb 2018 18:57:52 +0000
treeherderautoland@5d7a2c906875 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbytesized
bugs1440932
milestone60.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 1440932 - Implement a runOnce helper function for policies. r=bytesized MozReview-Commit-ID: HOTrRIfknmn
browser/components/enterprisepolicies/Policies.jsm
browser/components/enterprisepolicies/tests/browser/browser.ini
browser/components/enterprisepolicies/tests/browser/browser_policies_runOnce_helper.js
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -10,19 +10,17 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "@mozilla.org/xul/xulstore;1",
                                    "nsIXULStore");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   BookmarksPolicies: "resource:///modules/policies/BookmarksPolicies.jsm",
 });
 
 const PREF_LOGLEVEL           = "browser.policies.loglevel";
-const PREF_MENU_ALREADY_DISPLAYED = "browser.policies.menuBarWasDisplayed";
-const BROWSER_DOCUMENT_URL        = "chrome://browser/content/browser.xul";
-const PREF_BOOKMARKS_ALREADY_DISPLAYED = "browser.policies.bookmarkBarWasDisplayed";
+const BROWSER_DOCUMENT_URL    = "chrome://browser/content/browser.xul";
 
 XPCOMUtils.defineLazyGetter(this, "log", () => {
   let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm", {});
   return new ConsoleAPI({
     prefix: "Policies.jsm",
     // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
     // messages during development. See LOG_LEVELS in Console.jsm for details.
     maxLogLevel: "error",
@@ -160,40 +158,32 @@ var Policies = {
   },
 
   "DisplayBookmarksToolbar": {
     onBeforeUIStartup(manager, param) {
       if (param) {
         // This policy is meant to change the default behavior, not to force it.
         // If this policy was alreay applied and the user chose to re-hide the
         // bookmarks toolbar, do not show it again.
-        if (!Services.prefs.getBoolPref(PREF_BOOKMARKS_ALREADY_DISPLAYED, false)) {
-          log.debug("Showing the bookmarks toolbar");
+        runOnce("displayBookmarksToolbar", () => {
           gXulStore.setValue(BROWSER_DOCUMENT_URL, "PersonalToolbar", "collapsed", "false");
-          Services.prefs.setBoolPref(PREF_BOOKMARKS_ALREADY_DISPLAYED, true);
-        } else {
-          log.debug("Not showing the bookmarks toolbar because it has already been shown.");
-        }
+        });
       }
     }
   },
 
   "DisplayMenuBar": {
     onBeforeUIStartup(manager, param) {
       if (param) {
         // This policy is meant to change the default behavior, not to force it.
         // If this policy was alreay applied and the user chose to re-hide the
         // menu bar, do not show it again.
-        if (!Services.prefs.getBoolPref(PREF_MENU_ALREADY_DISPLAYED, false)) {
-          log.debug("Showing the menu bar");
+        runOnce("displayMenuBar", () => {
           gXulStore.setValue(BROWSER_DOCUMENT_URL, "toolbar-menubar", "autohide", "false");
-          Services.prefs.setBoolPref(PREF_MENU_ALREADY_DISPLAYED, true);
-        } else {
-          log.debug("Not showing the menu bar because it has already been shown.");
-        }
+        });
       }
     }
   },
 
   "DontCheckDefaultBrowser": {
     onBeforeUIStartup(manager, param) {
       setAndLockPref("browser.shell.checkDefaultBrowser", false);
     }
@@ -299,8 +289,28 @@ function addAllowDenyPermissions(permiss
 
   for (let origin of blockList) {
     Services.perms.add(origin,
                        permissionName,
                        Ci.nsIPermissionManager.DENY_ACTION,
                        Ci.nsIPermissionManager.EXPIRE_POLICY);
   }
 }
+
+/**
+ * runOnce
+ *
+ * Helper function to run a callback only once per policy.
+ *
+ * @param {string} actionName
+ *        A given name which will be used to track if this callback has run.
+ * @param {Functon} callback
+ *        The callback to run only once.
+ */
+function runOnce(actionName, callback) {
+  let prefName = `browser.policies.runonce.${actionName}`;
+  if (Services.prefs.getBoolPref(prefName, false)) {
+    log.debug(`Not running action ${actionName} again because it has already run.`);
+    return;
+  }
+  callback();
+  Services.prefs.setBoolPref(prefName, true);
+}
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -3,16 +3,17 @@ prefs =
   browser.policies.enabled=true
 support-files =
   head.js
   config_popups_cookies_addons_flash.json
   config_broken_json.json
 
 [browser_policies_broken_json.js]
 [browser_policies_popups_cookies_addons_flash.js]
+[browser_policies_runOnce_helper.js]
 [browser_policies_setAndLockPref_API.js]
 [browser_policies_simple_policies.js]
 [browser_policies_sorted_alphabetically.js]
 [browser_policies_validate_and_parse_API.js]
 [browser_policy_app_update.js]
 [browser_policy_block_about_addons.js]
 [browser_policy_block_about_config.js]
 [browser_policy_block_about_profiles.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_runOnce_helper.js
@@ -0,0 +1,22 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+let { runOnce } = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
+
+let runCount = 0;
+function callback() {
+  runCount++;
+}
+
+add_task(async function test_runonce_helper() {
+  runOnce("test_action", callback);
+  is(runCount, 1, "Callback ran for the first time.");
+
+  runOnce("test_action", callback);
+  is(runCount, 1, "Callback didn't run again.");
+
+  // clean-up
+  Services.prefs.clearUserPref("browser.policies.runonce.test_action");
+});