Commit c5b3bb6c authored by Felipe Gomes's avatar Felipe Gomes
Browse files

Bug 1440932 - Implement a runOnce helper function for policies. r=bytesized

MozReview-Commit-ID: HOTrRIfknmn

--HG--
extra : rebase_source : bf3c1295c93d57e2e0191c13e5e1d0327c075ccc
parent 26acd17d
......@@ -15,9 +15,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
});
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", {});
......@@ -165,13 +163,9 @@ var Policies = {
// 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.");
}
});
}
}
},
......@@ -182,13 +176,9 @@ var Policies = {
// 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.");
}
});
}
}
},
......@@ -304,3 +294,23 @@ function addAllowDenyPermissions(permissionName, allowList, blockList) {
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);
}
......@@ -8,6 +8,7 @@ support-files =
[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]
......
/* 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");
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment