Bug 1640701 - Allow the Google search tile to point to a URL from a pref instead. r=mikedeboer
authorDão Gottwald <dao@mozilla.com>
Fri, 05 Jun 2020 10:53:21 +0000
changeset 598245 33847bf1cfbb5c7203b42c397b10da217d367bc7
parent 598244 1e1c9d28eb43d98c61d95a642785caf0567dcaf7
child 598246 431b35a5b06ce4e543a38d35a38e2625a9ae2314
push id13310
push userffxbld-merge
push dateMon, 29 Jun 2020 14:50:06 +0000
treeherdermozilla-beta@15a59a0afa5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmikedeboer
bugs1640701
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1640701 - Allow the Google search tile to point to a URL from a pref instead. r=mikedeboer Differential Revision: https://phabricator.services.mozilla.com/D78288
browser/components/newtab/lib/TopSitesFeed.jsm
--- a/browser/components/newtab/lib/TopSitesFeed.jsm
+++ b/browser/components/newtab/lib/TopSitesFeed.jsm
@@ -80,18 +80,23 @@ const FILTER_DEFAULT_SEARCH_PREF = "impr
 const SEARCH_FILTERS = [
   "google",
   "search.yahoo",
   "yahoo",
   "bing",
   "ask",
   "duckduckgo",
 ];
-const AMAZON_SEARCH_TILE_OVERRIDE_PREF =
-  "browser.newtabpage.searchTileOverride.amazon.url";
+let SEARCH_TILE_OVERRIDE_PREFS = new Map();
+for (let searchProvider of ["amazon", "google"]) {
+  SEARCH_TILE_OVERRIDE_PREFS.set(
+    `browser.newtabpage.searchTileOverride.${searchProvider}.url`,
+    searchProvider
+  );
+}
 
 function getShortURLForCurrentSearch() {
   const url = shortURL({ url: Services.search.defaultEngine.searchForm });
   return url;
 }
 
 this.TopSitesFeed = class TopSitesFeed {
   constructor() {
@@ -120,39 +125,43 @@ this.TopSitesFeed = class TopSitesFeed {
   init() {
     // If the feed was previously disabled PREFS_INITIAL_VALUES was never received
     this.refreshDefaults(
       this.store.getState().Prefs.values[DEFAULT_SITES_PREF]
     );
     this._storage = this.store.dbStorage.getDbTable("sectionPrefs");
     this.refresh({ broadcast: true });
     Services.obs.addObserver(this, "browser-search-engine-modified");
-    Services.prefs.addObserver(AMAZON_SEARCH_TILE_OVERRIDE_PREF, this);
+    for (let [pref] of SEARCH_TILE_OVERRIDE_PREFS) {
+      Services.prefs.addObserver(pref, this);
+    }
   }
 
   uninit() {
     PageThumbs.removeExpirationFilter(this);
     Services.obs.removeObserver(this, "browser-search-engine-modified");
-    Services.prefs.removeObserver(AMAZON_SEARCH_TILE_OVERRIDE_PREF, this);
+    for (let [pref] of SEARCH_TILE_OVERRIDE_PREFS) {
+      Services.prefs.removeObserver(pref, this);
+    }
   }
 
   observe(subj, topic, data) {
     // We should update the current top sites if the search engine has been changed since
     // the search engine that gets filtered out of top sites has changed.
     if (
       topic === "browser-search-engine-modified" &&
       data === "engine-default" &&
       this.store.getState().Prefs.values[FILTER_DEFAULT_SEARCH_PREF]
     ) {
       delete this._currentSearchHostname;
       this._currentSearchHostname = getShortURLForCurrentSearch();
       this.refresh({ broadcast: true });
     } else if (
       topic === "nsPref:changed" &&
-      data === AMAZON_SEARCH_TILE_OVERRIDE_PREF
+      SEARCH_TILE_OVERRIDE_PREFS.has(data)
     ) {
       this.refresh({ broadcast: true });
     }
   }
 
   _dedupeKey(site) {
     return site && site.hostname;
   }
@@ -401,29 +410,30 @@ this.TopSitesFeed = class TopSitesFeed {
     // Remove adult sites if we need to
     const checkedAdult = this.store.getState().Prefs.values.filterAdult
       ? filterAdult(dedupedUnpinned)
       : dedupedUnpinned;
 
     // Insert the original pinned sites into the deduped frecent and defaults
     const withPinned = insertPinned(checkedAdult, pinned).slice(0, numItems);
 
-    let amazonSearchTileOverrideURL = Services.prefs.getStringPref(
-      AMAZON_SEARCH_TILE_OVERRIDE_PREF,
-      ""
-    );
-    if (amazonSearchTileOverrideURL) {
-      let date = new Date();
-      let pad = number => number.toString().padStart(2, "0");
-      amazonSearchTileOverrideURL = amazonSearchTileOverrideURL.replace(
-        "%YYYYMMDD%",
-        String(date.getFullYear()) +
-          pad(date.getMonth() + 1) +
-          pad(date.getDate())
-      );
+    let searchTileOverrideURLs = new Map();
+    for (let [pref, hostname] of SEARCH_TILE_OVERRIDE_PREFS) {
+      let url = Services.prefs.getStringPref(pref, "");
+      if (url) {
+        let date = new Date();
+        let pad = number => number.toString().padStart(2, "0");
+        url = url.replace(
+          "%YYYYMMDD%",
+          String(date.getFullYear()) +
+            pad(date.getMonth() + 1) +
+            pad(date.getDate())
+        );
+        searchTileOverrideURLs.set(hostname, url);
+      }
     }
 
     // Now, get a tippy top icon, a rich icon, or screenshot for every item
     for (const link of withPinned) {
       if (link) {
         // If there is a custom screenshot this is the only image we display
         if (link.customScreenshotURL) {
           this._fetchScreenshot(link, link.customScreenshotURL);
@@ -434,26 +444,26 @@ this.TopSitesFeed = class TopSitesFeed {
         }
 
         // Remove internal properties that might be updated after dispatch
         delete link.__sharedCache;
 
         // Indicate that these links should get a frecency bonus when clicked
         link.typedBonus = true;
 
-        if (amazonSearchTileOverrideURL) {
+        for (let [hostname, url] of searchTileOverrideURLs) {
           // The `searchVendor` property is set if the engine was re-added manually.
           if (
             link.searchTopSite &&
             !link.searchVendor &&
-            link.label === "@amazon"
+            link.hostname === hostname
           ) {
             delete link.searchTopSite;
             delete link.label;
-            link.url = amazonSearchTileOverrideURL;
+            link.url = url;
             link.overriddenSearchTopSite = true;
           }
         }
       }
     }
 
     return withPinned;
   }