Bug 1653937 - Read top site defaults from remote settings. r=mikedeboer

Differential Revision: https://phabricator.services.mozilla.com/D88041
This commit is contained in:
Dão Gottwald 2020-08-24 17:55:40 +00:00
Родитель bbe5591583
Коммит 1800cee263
3 изменённых файлов: 49 добавлений и 25 удалений

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

@ -61,6 +61,11 @@ ChromeUtils.defineModuleGetter(
"PageThumbs",
"resource://gre/modules/PageThumbs.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"RemoteSettings",
"resource://services-settings/remote-settings.js"
);
const DEFAULT_SITES_PREF = "default.sites";
const SHOWN_ON_NEWTAB_PREF = "feeds.topsites";
@ -130,7 +135,6 @@ this.TopSitesFeed = class TopSitesFeed {
// If the feed was previously disabled PREFS_INITIAL_VALUES was never received
this._readDefaults();
this._storage = this.store.dbStorage.getDbTable("sectionPrefs");
this.refresh({ broadcast: true, isStartup: true });
Services.obs.addObserver(this, "browser-search-engine-modified");
for (let [pref] of SEARCH_TILE_OVERRIDE_PREFS) {
Services.prefs.addObserver(pref, this);
@ -169,7 +173,6 @@ this.TopSitesFeed = class TopSitesFeed {
data === REMOTE_SETTING_OVERRIDE_PREF
) {
this._readDefaults();
this.refresh({ broadcast: true });
} else if (SEARCH_TILE_OVERRIDE_PREFS.has(data)) {
this.refresh({ broadcast: true });
}
@ -184,14 +187,15 @@ this.TopSitesFeed = class TopSitesFeed {
/**
* _readDefaults - sets DEFAULT_TOP_SITES
*/
_readDefaults() {
async _readDefaults({ isStartup = false } = {}) {
this._useRemoteSetting = Services.prefs.getBoolPref(
REMOTE_SETTING_DEFAULTS_PREF
);
if (!this._useRemoteSetting) {
this.refreshDefaults(
this.store.getState().Prefs.values[DEFAULT_SITES_PREF]
this.store.getState().Prefs.values[DEFAULT_SITES_PREF],
{ isStartup }
);
return;
}
@ -202,27 +206,12 @@ this.TopSitesFeed = class TopSitesFeed {
sites = Services.prefs.getStringPref(REMOTE_SETTING_OVERRIDE_PREF);
} catch (e) {}
if (sites) {
this.refreshDefaults(sites);
this.refreshDefaults(sites, { isStartup });
return;
}
// Read defaults from remote settings.
// Placeholder for the actual remote setting (bug 1653937).
let remoteSettingData = [
{
title: "Mozilla!",
url: "https://mozilla.org/#%YYYYMMDDHH%",
send_attribution_request: true,
},
{
url: "https://firefox.com",
url_urlbar_override: "https://firefox.com/#urlbar",
},
{
url: "https://foobar.com",
keyword: "@foobar",
},
];
let remoteSettingData = await this._getRemoteConfig();
// Clear out the array of any previous defaults.
DEFAULT_TOP_SITES.length = 0;
@ -246,9 +235,11 @@ this.TopSitesFeed = class TopSitesFeed {
}
DEFAULT_TOP_SITES.push(link);
}
this.refresh({ broadcast: true, isStartup });
}
refreshDefaults(sites) {
refreshDefaults(sites, { isStartup = false } = {}) {
// Clear out the array of any previous defaults
DEFAULT_TOP_SITES.length = 0;
@ -263,6 +254,36 @@ this.TopSitesFeed = class TopSitesFeed {
DEFAULT_TOP_SITES.push(site);
}
}
this.refresh({ broadcast: true, isStartup });
}
async _getRemoteConfig(firstTime = true) {
if (!this._remoteConfig) {
this._remoteConfig = await RemoteSettings("top-sites");
}
let result = [];
let failed = false;
try {
result = await this._remoteConfig.get();
} catch (ex) {
Cu.reportError(ex);
failed = true;
}
if (!result.length) {
Cu.reportError("Received empty search configuration!");
failed = true;
}
// If we failed, or the result is empty, try loading from the local dump.
if (firstTime && failed) {
await this._remoteConfig.db.clear();
// Now call this again.
return this._getRemoteConfig(false);
}
// Sort sites based on the "order" attribute.
result.sort((a, b) => a.order - b.order);
return result;
}
filterForThumbnailExpiration(callback) {
@ -286,6 +307,7 @@ this.TopSitesFeed = class TopSitesFeed {
*/
shouldFilterSearchTile(hostname) {
if (
!this._useRemoteSetting &&
this.store.getState().Prefs.values[FILTER_DEFAULT_SEARCH_PREF] &&
(SEARCH_FILTERS.includes(hostname) ||
hostname === this._currentSearchHostname)
@ -379,9 +401,9 @@ this.TopSitesFeed = class TopSitesFeed {
const numItems =
this.store.getState().Prefs.values[ROWS_PREF] *
TOP_SITES_MAX_SITES_PER_ROW;
const searchShortcutsExperiment = this.store.getState().Prefs.values[
SEARCH_SHORTCUTS_EXPERIMENT
];
const searchShortcutsExperiment =
!this._useRemoteSetting &&
this.store.getState().Prefs.values[SEARCH_SHORTCUTS_EXPERIMENT];
// We must wait for search services to initialize in order to access default
// search engine properties without triggering a synchronous initialization
await Services.search.init();

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

@ -11,6 +11,7 @@ FINAL_TARGET_FILES.defaults.settings.main += [
'search-config.json',
'search-default-override-allowlist.json',
'sites-classification.json',
'top-sites.json',
'url-classifier-skip-urls.json',
]

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

@ -0,0 +1 @@
{"data":[{"url":"https://www.amazon.com/","order":4,"title":"Amazon","schema":1598289482863,"send_attribution_request":true,"id":"08f43769-116f-4450-b57d-5108042b5fe5","last_modified":1598289691049},{"url":"https://www.facebook.com/","order":1,"title":"Facebook","schema":1598289454075,"send_attribution_request":false,"id":"b64b8279-6996-4e35-9176-c5ae5964c422","last_modified":1598289691047},{"url":"https://www.youtube.com/","order":3,"title":"Youtube","schema":1598289470275,"send_attribution_request":false,"id":"fc5d5d35-5637-4268-bb5f-f262818fa7e9","last_modified":1598289691044},{"url":"https://www.wikipedia.org/","order":5,"title":"Wikipedia","schema":1598289491094,"send_attribution_request":false,"id":"7105fa42-4066-41df-9af3-885a7b2e9682","last_modified":1598289691041},{"url":"https://twitter.com/","order":2,"title":"Twitter","schema":1598289462557,"send_attribution_request":false,"id":"6bb2647b-31d6-4185-9052-6d680bf2d777","last_modified":1598289691039},{"url":"https://google.com/","order":0,"title":"Google","schema":1598289436719,"keyword":"@google","send_attribution_request":false,"id":"cb6417cc-21d1-4a4d-9103-5094a5277342","last_modified":1598289691036}]}