Bug 1630983 - Allow the Amazon search tile to point to a URL from a pref instead r=Standard8
authorDão Gottwald <dao@mozilla.com>
Sat, 23 May 2020 20:48:34 +0000
changeset 2913082 1456dc5db988c4548df454eaa2fe57b60502ffa9
parent 2913081 8912e3abc78133dbf7ffba380f3c80306780275a
child 2913083 ef3150fdf5ae71e7a98c5f1eed4fb4bdf5f15836
push id542231
push userbclary@mozilla.com
push dateSun, 24 May 2020 06:08:15 +0000
treeherdertry@5bd349510b42 [default view] [failures only]
reviewersStandard8
bugs1630983
milestone78.0a1
Bug 1630983 - Allow the Amazon search tile to point to a URL from a pref instead r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D75929
browser/components/newtab/karma.mc.config.js
browser/components/newtab/lib/TopSitesFeed.jsm
--- a/browser/components/newtab/karma.mc.config.js
+++ b/browser/components/newtab/karma.mc.config.js
@@ -75,16 +75,22 @@ module.exports = function(config) {
               branches: 83,
             },
             "lib/UTEventReporting.jsm": {
               statements: 100,
               lines: 100,
               functions: 100,
               branches: 75,
             },
+            "lib/TopSitesFeed.jsm": {
+              statements: 98,
+              lines: 98,
+              functions: 100,
+              branches: 84,
+            },
             "lib/*.jsm": {
               statements: 100,
               lines: 100,
               functions: 100,
               branches: 84,
             },
             "content-src/components/DiscoveryStreamComponents/**/*.jsx": {
               statements: 90.48,
--- a/browser/components/newtab/lib/TopSitesFeed.jsm
+++ b/browser/components/newtab/lib/TopSitesFeed.jsm
@@ -79,16 +79,18 @@ const FILTER_DEFAULT_SEARCH_PREF = "impr
 const SEARCH_FILTERS = [
   "google",
   "search.yahoo",
   "yahoo",
   "bing",
   "ask",
   "duckduckgo",
 ];
+const AMAZON_SEARCH_TILE_OVERRIDE_PREF =
+  "browser.newtabpage.amazonSearchTileOverride.url";
 
 function getShortURLForCurrentSearch() {
   const url = shortURL({ url: Services.search.defaultEngine.searchForm });
   return url;
 }
 
 this.TopSitesFeed = class TopSitesFeed {
   constructor() {
@@ -117,34 +119,41 @@ 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);
   }
 
   uninit() {
     PageThumbs.removeExpirationFilter(this);
     Services.obs.removeObserver(this, "browser-search-engine-modified");
+    Services.prefs.removeObserver(AMAZON_SEARCH_TILE_OVERRIDE_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
+    ) {
+      this.refresh({ broadcast: true });
     }
   }
 
   _dedupeKey(site) {
     return site && site.hostname;
   }
 
   refreshDefaults(sites) {
@@ -391,16 +400,21 @@ 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);
 
+    const amazonSearchTileOverrideURL = Services.prefs.getCharPref(
+      AMAZON_SEARCH_TILE_OVERRIDE_PREF,
+      ""
+    );
+
     // 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);
         } else if (link.searchTopSite && !link.isDefault) {
           this._tippyTopProvider.processSite(link);
@@ -408,16 +422,24 @@ this.TopSitesFeed = class TopSitesFeed {
           this._fetchIcon(link);
         }
 
         // 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) {
+          if (link.searchTopSite && link.label === "@amazon") {
+            delete link.searchTopSite;
+            delete link.label;
+            link.url = amazonSearchTileOverrideURL;
+          }
+        }
       }
     }
 
     return withPinned;
   }
 
   /**
    * Refresh the top sites data for content.