Bug 1591079 - Add application name and version restrictions. r=Standard8

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dale Harvey 2020-01-29 21:01:50 +00:00
parent 154885fc6d
commit 959b36b578
3 changed files with 178 additions and 4 deletions

View File

@ -6,6 +6,7 @@
var EXPORTED_SYMBOLS = ["SearchEngineSelector"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
@ -27,6 +28,37 @@ function log(str) {
SearchUtils.log("SearchEngineSelector " + str + "\n");
}
function getAppInfo(key) {
let value = null;
try {
// Services.appinfo is often null in tests.
value = Services.appinfo[key].toLowerCase();
} catch (e) {}
return value;
}
function hasAppKey(config, key) {
return "application" in config && key in config.application;
}
function sectionExcludes(config, key, value) {
return hasAppKey(config, key) && !config.application[key].includes(value);
}
function belowMinVersion(config, version) {
return (
hasAppKey(config, "minVersion") &&
Services.vc.compare(version, config.application.minVersion) < 0
);
}
function aboveMaxVersion(config, version) {
return (
hasAppKey(config, "maxVersion") &&
Services.vc.compare(version, config.application.maxVersion) > 0
);
}
/**
* SearchEngineSelector parses the JSON configuration for
* search engines and returns the applicable engines depending
@ -59,8 +91,12 @@ class SearchEngineSelector {
* details for the engine which should be the default in Private Browsing mode.
*/
fetchEngineConfiguration(locale, region, channel) {
log(`fetchEngineConfiguration ${region}:${locale}:${channel}`);
let cohort = Services.prefs.getCharPref("browser.search.cohort", null);
let name = getAppInfo("name");
let version = getAppInfo("version");
log(
`fetchEngineConfiguration ${region}:${locale}:${channel}:${cohort}:${name}:${version}`
);
let engines = [];
const lcLocale = locale.toLowerCase();
const lcRegion = region.toLowerCase();
@ -71,9 +107,10 @@ class SearchEngineSelector {
return false;
}
if (
"application" in section &&
"channel" in section.application &&
!section.application.channel.includes(channel)
sectionExcludes(section, "channel", channel) ||
sectionExcludes(section, "name", name) ||
belowMinVersion(section, version) ||
aboveMaxVersion(section, version)
) {
return false;
}

View File

@ -0,0 +1,136 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
XPCOMUtils.defineLazyModuleGetters(this, {
SearchEngineSelector: "resource://gre/modules/SearchEngineSelector.jsm",
});
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const CONFIG = {
data: [
{
webExtension: {
id: "aol@example.com",
},
appliesTo: [
{
included: { everywhere: true },
},
],
default: "yes-if-no-other",
},
{
webExtension: {
id: "lycos@example.com",
},
appliesTo: [
{
included: { everywhere: true },
application: {
name: ["firefox"],
},
},
],
default: "yes",
},
{
webExtension: {
id: "altavista@example.com",
},
appliesTo: [
{
included: { everywhere: true },
application: {
name: ["fenix"],
},
},
],
},
{
webExtension: {
id: "excite@example.com",
},
appliesTo: [
{
included: { everywhere: true },
application: {
name: ["firefox"],
minVersion: "10",
maxVersion: "30",
},
default: "yes",
},
],
},
],
};
function fetchWithConfig(name, version) {
Services.appinfo = { name, version };
return engineSelector.fetchEngineConfiguration("default", "default");
}
const engineSelector = new SearchEngineSelector();
const tests = [
{
name: "Firefox",
version: "1",
expected: ["lycos@example.com", "aol@example.com"],
},
{
name: "Firefox",
version: "20",
expected: ["lycos@example.com", "aol@example.com", "excite@example.com"],
},
{
name: "Fenix",
version: "20",
expected: ["aol@example.com", "altavista@example.com"],
},
{
name: "Firefox",
version: "31",
expected: ["lycos@example.com", "aol@example.com"],
},
{
name: "Firefox",
version: "30",
expected: ["lycos@example.com", "aol@example.com", "excite@example.com"],
},
{
name: "Firefox",
version: "10",
expected: ["lycos@example.com", "aol@example.com", "excite@example.com"],
},
];
add_task(async function setup() {
await useTestEngines();
await AddonTestUtils.promiseStartupManager();
let confUrl = `data:application/json,${JSON.stringify(CONFIG)}`;
Services.prefs.setStringPref("search.config.url", confUrl);
await engineSelector.init();
});
add_task(async function test_application_name() {
for (const { name, version, expected } of tests) {
Services.appinfo = { name, version };
let { engines } = engineSelector.fetchEngineConfiguration(
"default",
"default"
);
const engineIds = engines.map(obj => obj.webExtension.id);
Assert.deepEqual(
engineIds,
expected,
`Should have the expected engines for app: "${name}"
and version: "${version}"`
);
}
});

View File

@ -55,3 +55,4 @@ support-files =
[test_reload_engines.js]
[test_location_timeout.js]
[test_engine_selector_application_name.js]