Bug 1630983 - Allow the Amazon search tile to point to a URL from a pref instead r=Standard8
☠☠ backed out by 3b2e18ed6a65 ☠ ☠
authorDão Gottwald <dao@mozilla.com>
Fri, 22 May 2020 10:21:34 +0000
changeset 531629 fa1babb9fbdabbfe550000788aa58020e43d9374
parent 531628 1981a4af9b8ef9382cc47ef3c24e83530591540e
child 531630 34389f9c86e41f0701991dd2d02729bbbedb8546
push id37441
push userapavel@mozilla.com
push dateFri, 22 May 2020 21:38:53 +0000
treeherdermozilla-central@d6abd35b54ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1630983
milestone78.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 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/lib/TopSitesFeed.jsm
browser/components/newtab/test/browser/browser_topsites_section.js
--- 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.getStringPref(
+      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.
--- a/browser/components/newtab/test/browser/browser_topsites_section.js
+++ b/browser/components/newtab/test/browser/browser_topsites_section.js
@@ -194,8 +194,26 @@ test_newtab({
       "We clicked a search topsite the focus should be in location bar"
     );
     ok(
       gURLBar.value.includes(searchTopSiteTag),
       "Should contain the tag of the search topsite clicked"
     );
   },
 });
+
+test_newtab({
+  async before() {
+    await setDefaultTopSites();
+    Services.prefs.setStringPref(
+      "browser.newtabpage.amazonSearchTileOverride.url",
+      "https://imaginarydomain.org/"
+    );
+  },
+  test: async function test_search_topsite_override() {
+    // Stub, expand in bug 1637219.
+  },
+  after() {
+    Services.prefs.clearUserPref(
+      "browser.newtabpage.amazonSearchTileOverride.url"
+    );
+  },
+});