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
This commit is contained in:
Mike de Boer 2019-10-15 14:31:05 +00:00
parent 5e1f2653cf
commit 7cf173a0ec
3 changed files with 83 additions and 0 deletions

View File

@ -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
);
}
}
}
);
}
});
},
},

View File

@ -952,6 +952,9 @@
"Default": {
"type": "string"
},
"DefaultPrivate": {
"type": "string"
},
"PreventInstalls": {
"type": "boolean"
},

View File

@ -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() {