Bug 1449681 - Fix bug where opensearch API is not always successfully blocked by policy
MozReview-Commit-ID: 7ZrmiMop9IB
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -344,16 +344,21 @@ var Policies = {
"RememberPasswords": {
onBeforeUIStartup(manager, param) {
setAndLockPref("signon.rememberSignons", param);
}
},
"SearchEngines": {
+ onBeforeUIStartup(manager, param) {
+ if (param.PreventInstalls) {
+ manager.disallowFeature("installSearchEngine", true);
+ }
+ },
onAllWindowsRestored(manager, param) {
Services.search.init(() => {
if (param.Add) {
// Only rerun if the list of engine names has changed.
let engineNameList = param.Add.map(engine => engine.Name);
runOncePerModification("addSearchEngines",
JSON.stringify(engineNameList),
() => {
@@ -393,19 +398,16 @@ var Policies = {
try {
Services.search.currentEngine = defaultEngine;
} catch (ex) {
log.error("Unable to set the default search engine", ex);
}
}
});
}
- if (param.PreventInstalls) {
- manager.disallowFeature("installSearchEngine");
- }
});
}
}
};
/*
* ====================
* = HELPER FUNCTIONS =
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
@@ -67,19 +67,56 @@ add_task(async function test_install_and
// If this passes, it means that the new search engine was properly installed
// *and* was properly set as the default.
is(Services.search.currentEngine.name, "MozSearch",
"Specified search engine should be the default");
// Clean up
Services.search.removeEngine(Services.search.currentEngine);
+ EnterprisePolicyTesting.resetRunOnceState();
+});
+
+// Same as the last test, but with "PreventInstalls" set to true to make sure
+// it does not prevent search engines from being installed properly
+add_task(async function test_install_and_set_default_prevent_installs() {
+ isnot(Services.search.currentEngine.name, "MozSearch",
+ "Default search engine should not be MozSearch when test starts");
+ is(Services.search.getEngineByName("Foo"), null,
+ "Engine \"Foo\" should not be present when test starts");
+
+ await setupPolicyEngineWithJson({
+ "policies": {
+ "SearchEngines": {
+ "Add": [
+ {
+ "Name": "MozSearch",
+ "URLTemplate": "http://example.com/?q={searchTerms}"
+ }
+ ],
+ "Default": "MozSearch",
+ "PreventInstalls": true
+ }
+ }
+ });
+
+ is(Services.search.currentEngine.name, "MozSearch",
+ "Specified search engine should be the default");
+
+ // Clean up
+ Services.search.removeEngine(Services.search.currentEngine);
+ EnterprisePolicyTesting.resetRunOnceState();
});
add_task(async function test_opensearch_works() {
+ // Clear out policies so we can test with no policies applied
+ await setupPolicyEngineWithJson({
+ "policies": {
+ }
+ });
// Ensure that opensearch works before we make sure that it can be properly
// disabled
await test_opensearch(true);
});
add_task(async function setup_prevent_installs() {
await setupPolicyEngineWithJson({
"policies": {