Bug 1466484 - Added remove to SearchEngines Policy r?felipe
MozReview-Commit-ID: 6g3qp6q605i
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -641,16 +641,33 @@ var Policies = {
"SearchEngines": {
onBeforeUIStartup(manager, param) {
if (param.PreventInstalls) {
manager.disallowFeature("installSearchEngine", true);
}
},
onAllWindowsRestored(manager, param) {
Services.search.init(() => {
+ if (param.Remove) {
+ // Only rerun if the list of engine names has changed.
+ runOncePerModification("removeSearchEngines",
+ JSON.stringify(param.Remove),
+ () => {
+ for (let engineName of param.Remove) {
+ let engine = Services.search.getEngineByName(engineName);
+ if (engine) {
+ try {
+ Services.search.removeEngine(engine);
+ } catch (ex) {
+ log.error("Unable to remove the search engine", ex);
+ }
+ }
+ }
+ });
+ }
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),
() => {
for (let newEngine of param.Add) {
let newEngineParameters = {
--- a/browser/components/enterprisepolicies/schemas/policies-schema.json
+++ b/browser/components/enterprisepolicies/schemas/policies-schema.json
@@ -596,16 +596,22 @@
}
}
},
"Default": {
"type": "string"
},
"PreventInstalls": {
"type": "boolean"
+ },
+ "Remove": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
}
}
},
"WebsiteFilter": {
"description": "Blocks websites from being visited. The parameters take an array of Match Patterns, as documented in https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Match_patterns. Only http/https accesses are supported at the moment. The arrays are limited to 1000 entries each.",
"first_available": "60.0",
"enterprise_only": "true",
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
@@ -187,8 +187,44 @@ add_task(async function test_AddSearchPr
content.window.external.AddSearchProvider(args.engineURL);
});
is(Services.search.getEngineByName("Foo"), null,
"Engine should not have been added successfully.");
is(mockPrompter.promptCount, 1,
"Should have alerted the user of an error when installing new search engine");
});
+
+add_task(async function test_install_and_remove() {
+ is(Services.search.getEngineByName("Foo"), null,
+ "Engine \"Foo\" should not be present when test starts");
+
+ await setupPolicyEngineWithJson({
+ "policies": {
+ "SearchEngines": {
+ "Add": [
+ {
+ "Name": "Foo",
+ "URLTemplate": "http://example.com/?q={searchTerms}"
+ }
+ ]
+ }
+ }
+ });
+
+ // If this passes, it means that the new search engine was properly installed
+ isnot(Services.search.getEngineByName("Foo"), null,
+ "Specified search engine should be installed");
+
+ await setupPolicyEngineWithJson({
+ "policies": {
+ "SearchEngines": {
+ "Remove": ["Foo"]
+ }
+ }
+ });
+
+ // If this passes, it means that the specified engine was properly removed
+ is(Services.search.getEngineByName("Foo"), null,
+ "Specified search engine should not be installed");
+
+ EnterprisePolicyTesting.resetRunOnceState();
+});