Bug 1671196 - Implement default top site experiment filters. r=mikedeboer

Differential Revision: https://phabricator.services.mozilla.com/D93515
This commit is contained in:
Dão Gottwald 2020-10-15 13:32:14 +00:00
Родитель 0c7989277c
Коммит a9a7030edd
1 изменённых файлов: 38 добавлений и 18 удалений

Просмотреть файл

@ -111,6 +111,7 @@ const REMOTE_SETTING_MIGRATION_ID_PREF =
"browser.topsites.migratedToRemoteSetting.id";
const DEFAULT_SITES_POLICY_PREF =
"browser.newtabpage.activity-stream.default.sites";
const DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH = "browser.topsites.experiment.";
function getShortURLForCurrentSearch() {
const url = shortURL({ url: Services.search.defaultEngine.searchForm });
@ -151,6 +152,7 @@ this.TopSitesFeed = class TopSitesFeed {
}
Services.prefs.addObserver(REMOTE_SETTING_DEFAULTS_PREF, this);
Services.prefs.addObserver(DEFAULT_SITES_POLICY_PREF, this);
Services.prefs.addObserver(DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH, this);
}
uninit() {
@ -161,6 +163,7 @@ this.TopSitesFeed = class TopSitesFeed {
}
Services.prefs.removeObserver(REMOTE_SETTING_DEFAULTS_PREF, this);
Services.prefs.removeObserver(DEFAULT_SITES_POLICY_PREF, this);
Services.prefs.removeObserver(DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH, this);
}
observe(subj, topic, data) {
@ -180,7 +183,8 @@ this.TopSitesFeed = class TopSitesFeed {
case "nsPref:changed":
if (
data === REMOTE_SETTING_DEFAULTS_PREF ||
data === DEFAULT_SITES_POLICY_PREF
data === DEFAULT_SITES_POLICY_PREF ||
data.startsWith(DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH)
) {
this._readDefaults();
} else if (SEARCH_TILE_OVERRIDE_PREFS.has(data)) {
@ -322,39 +326,55 @@ this.TopSitesFeed = class TopSitesFeed {
// Sort sites based on the "order" attribute.
result.sort((a, b) => a.order - b.order);
// Filter by region.
result = result.filter(topsite => {
if (
topsite.exclude_regions &&
topsite.exclude_regions.includes(Region.home)
) {
// Filter by region.
if (topsite.exclude_regions?.includes(Region.home)) {
return false;
}
if (
topsite.include_regions &&
topsite.include_regions.length &&
topsite.include_regions?.length &&
!topsite.include_regions.includes(Region.home)
) {
return false;
}
return true;
});
// Filter by locale.
result = result.filter(topsite => {
if (
topsite.exclude_locales &&
topsite.exclude_locales.includes(Services.locale.appLocaleAsBCP47)
) {
// Filter by locale.
if (topsite.exclude_locales?.includes(Services.locale.appLocaleAsBCP47)) {
return false;
}
if (
topsite.include_locales &&
topsite.include_locales.length &&
topsite.include_locales?.length &&
!topsite.include_locales.includes(Services.locale.appLocaleAsBCP47)
) {
return false;
}
// Filter by experiment.
// Exclude this top site if any of the specified experiments are running.
if (
topsite.exclude_experiments?.some(experimentID =>
Services.prefs.getBoolPref(
DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH + experimentID,
false
)
)
) {
return false;
}
// Exclude this top site if none of the specified experiments are running.
if (
topsite.include_experiments?.length &&
topsite.include_experiments.every(
experimentID =>
!Services.prefs.getBoolPref(
DEFAULT_SITES_EXPERIMENTS_PREF_BRANCH + experimentID,
false
)
)
) {
return false;
}
return true;
});