Commit 7cf173a0 authored by Mike de Boer's avatar Mike de Boer
Browse files

Bug 1587804 - Add an Enterprise Policy that allows to set the default search...

Bug 1587804 - Add an Enterprise Policy that allows to set the default search engine used in Private Browsing mode. r=mkaply

Differential Revision: https://phabricator.services.mozilla.com/D49268

--HG--
extra : moz-landing-system : lando
parent 5e1f2653
......@@ -1311,6 +1311,40 @@ var Policies = {
}
);
}
if (param.DefaultPrivate) {
await runOncePerModification(
"setDefaultPrivateSearchEngine",
param.DefaultPrivate,
async () => {
let defaultPrivateEngine;
try {
defaultPrivateEngine = Services.search.getEngineByName(
param.DefaultPrivate
);
if (!defaultPrivateEngine) {
throw new Error("No engine by that name could be found");
}
} catch (ex) {
log.error(
`Search engine lookup failed when attempting to set ` +
`the default private engine. Requested engine was ` +
`"${param.DefaultPrivate}".`,
ex
);
}
if (defaultPrivateEngine) {
try {
await Services.search.setDefaultPrivate(defaultPrivateEngine);
} catch (ex) {
log.error(
"Unable to set the default private search engine",
ex
);
}
}
}
);
}
});
},
},
......
......@@ -952,6 +952,9 @@
"Default": {
"type": "string"
},
"DefaultPrivate": {
"type": "string"
},
"PreventInstalls": {
"type": "boolean"
},
......
......@@ -12,6 +12,9 @@ registerCleanupFunction(() => {
Services.prefs.clearUserPref(
"browser.policies.runonce.setDefaultSearchEngine"
);
Services.prefs.clearUserPref(
"browser.policies.runonce.setDefaultPrivateSearchEngine"
);
Services.prefs.clearUserPref(
"browser.policies.runOncePerModification.addSearchEngines"
);
......@@ -111,6 +114,49 @@ add_task(async function test_install_and_set_default() {
EnterprisePolicyTesting.resetRunOnceState();
});
add_task(async function test_install_and_set_default_private() {
// Make sure we are starting in an expected state to avoid false positive
// test results.
isnot(
(await Services.search.getDefaultPrivate()).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}",
},
],
DefaultPrivate: "MozSearch",
},
},
});
// Get in line, because the Search policy callbacks are async.
await TestUtils.waitForTick();
// If this passes, it means that the new search engine was properly installed
// *and* was properly set as the default.
is(
(await Services.search.getDefaultPrivate()).name,
"MozSearch",
"Specified search engine should be the default private engine"
);
// Clean up
await Services.search.removeEngine(await Services.search.getDefaultPrivate());
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() {
......
Markdown is supported
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