diff --git a/browser/modules/DirectoryLinksProvider.jsm b/browser/modules/DirectoryLinksProvider.jsm index 1126d1ef8337..d8de7c074c51 100644 --- a/browser/modules/DirectoryLinksProvider.jsm +++ b/browser/modules/DirectoryLinksProvider.jsm @@ -57,8 +57,8 @@ const ALLOWED_IMAGE_SCHEMES = new Set(["https", "data"]); // The frecency of a directory link const DIRECTORY_FRECENCY = 1000; -// The frecency of a related link -const RELATED_FRECENCY = Infinity; +// The frecency of a suggested link +const SUGGESTED_FRECENCY = Infinity; // Divide frecency by this amount for pings const PING_SCORE_DIVISOR = 10000; @@ -89,14 +89,14 @@ let DirectoryLinksProvider = { _enhancedLinks: new Map(), /** - * A mapping from site to a list of related link objects + * A mapping from site to a list of suggested link objects */ - _relatedLinks: new Map(), + _suggestedLinks: new Map(), /** - * A set of top sites that we can provide related links for + * A set of top sites that we can provide suggested links for */ - _topSitesWithRelatedLinks: new Set(), + _topSitesWithSuggestedLinks: new Set(), get _observedPrefs() Object.freeze({ enhanced: PREF_NEWTAB_ENHANCED, @@ -201,11 +201,11 @@ let DirectoryLinksProvider = { } }, - _cacheRelatedLinks: function(link) { - for (let relatedSite of link.frecent_sites) { - let relatedMap = this._relatedLinks.get(relatedSite) || new Map(); - relatedMap.set(link.url, link); - this._relatedLinks.set(relatedSite, relatedMap); + _cacheSuggestedLinks: function(link) { + for (let suggestedSite of link.frecent_sites) { + let suggestedMap = this._suggestedLinks.get(suggestedSite) || new Map(); + suggestedMap.set(link.url, link); + this._suggestedLinks.set(suggestedSite, suggestedMap); } }, @@ -413,9 +413,9 @@ let DirectoryLinksProvider = { */ getLinks: function DirectoryLinksProvider_getLinks(aCallback) { this._readDirectoryLinksFile().then(rawLinks => { - // Reset the cache of related tiles and enhanced images for this new set of links + // Reset the cache of suggested tiles and enhanced images for this new set of links this._enhancedLinks.clear(); - this._relatedLinks.clear(); + this._suggestedLinks.clear(); let validityFilter = function(link) { // Make sure the link url is allowed and images too if they exist @@ -435,9 +435,9 @@ let DirectoryLinksProvider = { rawLinks.suggested.filter(validityFilter).forEach((link, position) => { setCommonProperties(link, rawLinks.suggested.length, position); - // We cache related tiles here but do not push any of them in the links list yet. - // The decision for which related tile to include will be made separately. - this._cacheRelatedLinks(link); + // We cache suggested tiles here but do not push any of them in the links list yet. + // The decision for which suggested tile to include will be made separately. + this._cacheSuggestedLinks(link); }); return rawLinks.directory.filter(validityFilter).map((link, position) => { @@ -476,32 +476,32 @@ let DirectoryLinksProvider = { }, _handleManyLinksChanged: function() { - this._topSitesWithRelatedLinks.clear(); - this._relatedLinks.forEach((relatedLinks, site) => { + this._topSitesWithSuggestedLinks.clear(); + this._suggestedLinks.forEach((suggestedLinks, site) => { if (NewTabUtils.isTopPlacesSite(site)) { - this._topSitesWithRelatedLinks.add(site); + this._topSitesWithSuggestedLinks.add(site); } }); - this._updateRelatedTile(); + this._updateSuggestedTile(); }, /** - * Updates _topSitesWithRelatedLinks based on the link that was changed. + * Updates _topSitesWithSuggestedLinks based on the link that was changed. * - * @return true if _topSitesWithRelatedLinks was modified, false otherwise. + * @return true if _topSitesWithSuggestedLinks was modified, false otherwise. */ _handleLinkChanged: function(aLink) { let changedLinkSite = NewTabUtils.extractSite(aLink.url); - let linkStored = this._topSitesWithRelatedLinks.has(changedLinkSite); + let linkStored = this._topSitesWithSuggestedLinks.has(changedLinkSite); if (!NewTabUtils.isTopPlacesSite(changedLinkSite) && linkStored) { - this._topSitesWithRelatedLinks.delete(changedLinkSite); + this._topSitesWithSuggestedLinks.delete(changedLinkSite); return true; } - if (this._relatedLinks.has(changedLinkSite) && + if (this._suggestedLinks.has(changedLinkSite) && NewTabUtils.isTopPlacesSite(changedLinkSite) && !linkStored) { - this._topSitesWithRelatedLinks.add(changedLinkSite); + this._topSitesWithSuggestedLinks.add(changedLinkSite); return true; } return false; @@ -517,7 +517,7 @@ let DirectoryLinksProvider = { // Make sure NewTabUtils.links handles the notification first. setTimeout(() => { if (this._handleLinkChanged(aLink)) { - this._updateRelatedTile(); + this._updateSuggestedTile(); } }, 0); }, @@ -530,12 +530,12 @@ let DirectoryLinksProvider = { }, /** - * Chooses and returns a related tile based on a user's top sites - * that we have an available related tile for. + * Chooses and returns a suggested tile based on a user's top sites + * that we have an available suggested tile for. * - * @return the chosen related tile, or undefined if there isn't one + * @return the chosen suggested tile, or undefined if there isn't one */ - _updateRelatedTile: function() { + _updateSuggestedTile: function() { let sortedLinks = NewTabUtils.getProviderLinks(this); if (!sortedLinks) { @@ -544,7 +544,7 @@ let DirectoryLinksProvider = { return; } - // Delete the current related tile, if one exists. + // Delete the current suggested tile, if one exists. let initialLength = sortedLinks.length; this.maxNumLinks = initialLength; if (initialLength) { @@ -559,52 +559,52 @@ let DirectoryLinksProvider = { } } - if (this._topSitesWithRelatedLinks.size == 0) { - // There are no potential related links we can show. + if (this._topSitesWithSuggestedLinks.size == 0) { + // There are no potential suggested links we can show. return; } - // Create a flat list of all possible links we can show as related. - // Note that many top sites may map to the same related links, but we only - // want to count each related link once (based on url), thus possibleLinks is a map - // from url to relatedLink. Thus, each link has an equal chance of being chosen at + // Create a flat list of all possible links we can show as suggested. + // Note that many top sites may map to the same suggested links, but we only + // want to count each suggested link once (based on url), thus possibleLinks is a map + // from url to suggestedLink. Thus, each link has an equal chance of being chosen at // random from flattenedLinks if it appears only once. let possibleLinks = new Map(); let targetedSites = new Map(); - this._topSitesWithRelatedLinks.forEach(topSiteWithRelatedLink => { - let relatedLinksMap = this._relatedLinks.get(topSiteWithRelatedLink); - relatedLinksMap.forEach((relatedLink, url) => { - possibleLinks.set(url, relatedLink); + this._topSitesWithSuggestedLinks.forEach(topSiteWithSuggestedLink => { + let suggestedLinksMap = this._suggestedLinks.get(topSiteWithSuggestedLink); + suggestedLinksMap.forEach((suggestedLink, url) => { + possibleLinks.set(url, suggestedLink); // Keep a map of URL to targeted sites. We later use this to show the user // what site they visited to trigger this suggestion. if (!targetedSites.get(url)) { targetedSites.set(url, []); } - targetedSites.get(url).push(topSiteWithRelatedLink); + targetedSites.get(url).push(topSiteWithSuggestedLink); }) }); let flattenedLinks = [...possibleLinks.values()]; - // Choose our related link at random - let relatedIndex = Math.floor(Math.random() * flattenedLinks.length); - let chosenRelatedLink = flattenedLinks[relatedIndex]; + // Choose our suggested link at random + let suggestedIndex = Math.floor(Math.random() * flattenedLinks.length); + let chosenSuggestedLink = flattenedLinks[suggestedIndex]; // Show the new directory tile. this._callObservers("onLinkChanged", { - url: chosenRelatedLink.url, - title: chosenRelatedLink.title, - frecency: RELATED_FRECENCY, - lastVisitDate: chosenRelatedLink.lastVisitDate, - type: chosenRelatedLink.type, + url: chosenSuggestedLink.url, + title: chosenSuggestedLink.title, + frecency: SUGGESTED_FRECENCY, + lastVisitDate: chosenSuggestedLink.lastVisitDate, + type: chosenSuggestedLink.type, // Choose the first site a user has visited as the target. In the future, // this should be the site with the highest frecency. However, we currently // store frecency by URL not by site. - targetedSite: targetedSites.get(chosenRelatedLink.url).length ? - targetedSites.get(chosenRelatedLink.url)[0] : null + targetedSite: targetedSites.get(chosenSuggestedLink.url).length ? + targetedSites.get(chosenSuggestedLink.url)[0] : null }); - return chosenRelatedLink; + return chosenSuggestedLink; }, /** diff --git a/browser/modules/test/xpcshell/test_DirectoryLinksProvider.js b/browser/modules/test/xpcshell/test_DirectoryLinksProvider.js index 643f28875126..5f0b504d79a4 100644 --- a/browser/modules/test/xpcshell/test_DirectoryLinksProvider.js +++ b/browser/modules/test/xpcshell/test_DirectoryLinksProvider.js @@ -61,7 +61,7 @@ const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", let gLastRequestPath; -let relatedTile1 = { +let suggestedTile1 = { url: "http://turbotax.com", type: "affiliate", lastVisitDate: 3, @@ -72,7 +72,7 @@ let relatedTile1 = { "taxslayer.com" ] }; -let relatedTile2 = { +let suggestedTile2 = { url: "http://irs.gov", type: "affiliate", lastVisitDate: 2, @@ -83,7 +83,7 @@ let relatedTile2 = { "taxslayer.com" ] }; -let relatedTile3 = { +let suggestedTile3 = { url: "http://hrblock.com", type: "affiliate", lastVisitDate: 1, @@ -94,7 +94,7 @@ let relatedTile3 = { "taxslayer.com" ] }; -let someOtherSite = {url: "http://someothersite.com", title: "Not_A_Related_Site"}; +let someOtherSite = {url: "http://someothersite.com", title: "Not_A_Suggested_Site"}; function getHttpHandler(path) { let code = 200; @@ -214,11 +214,11 @@ function run_test() { }); } -add_task(function test_updateRelatedTile() { +add_task(function test_updateSuggestedTile() { let topSites = ["site0.com", "1040.com", "site2.com", "hrblock.com", "site4.com", "freetaxusa.com", "site6.com"]; // Initial setup - let data = {"suggested": [relatedTile1, relatedTile2, relatedTile3], "directory": [someOtherSite]}; + let data = {"suggested": [suggestedTile1, suggestedTile2, suggestedTile3], "directory": [someOtherSite]}; let dataURI = 'data:application/json,' + JSON.stringify(data); let testObserver = new TestFirstRun(); @@ -237,15 +237,15 @@ add_task(function test_updateRelatedTile() { return links; } - do_check_eq(DirectoryLinksProvider._updateRelatedTile(), undefined); + do_check_eq(DirectoryLinksProvider._updateSuggestedTile(), undefined); function TestFirstRun() { this.promise = new Promise(resolve => { this.onLinkChanged = (directoryLinksProvider, link) => { links.unshift(link); - let possibleLinks = [relatedTile1.url, relatedTile2.url, relatedTile3.url]; + let possibleLinks = [suggestedTile1.url, suggestedTile2.url, suggestedTile3.url]; - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], ["hrblock.com", "1040.com", "freetaxusa.com"]); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], ["hrblock.com", "1040.com", "freetaxusa.com"]); do_check_true(possibleLinks.indexOf(link.url) > -1); do_check_eq(link.frecency, Infinity); do_check_eq(link.type, "affiliate"); @@ -254,32 +254,32 @@ add_task(function test_updateRelatedTile() { }); } - function TestChangingRelatedTile() { + function TestChangingSuggestedTile() { this.count = 0; this.promise = new Promise(resolve => { this.onLinkChanged = (directoryLinksProvider, link) => { this.count++; - let possibleLinks = [relatedTile1.url, relatedTile2.url, relatedTile3.url]; + let possibleLinks = [suggestedTile1.url, suggestedTile2.url, suggestedTile3.url]; do_check_true(possibleLinks.indexOf(link.url) > -1); do_check_eq(link.type, "affiliate"); do_check_true(this.count <= 2); if (this.count == 1) { - // The removed related link is the one we added initially. + // The removed suggested link is the one we added initially. do_check_eq(link.url, links.shift().url); do_check_eq(link.frecency, 0); } else { links.unshift(link); do_check_eq(link.frecency, Infinity); } - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], ["hrblock.com", "freetaxusa.com"]); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], ["hrblock.com", "freetaxusa.com"]); resolve(); } }); } - function TestRemovingRelatedTile() { + function TestRemovingSuggestedTile() { this.count = 0; this.promise = new Promise(resolve => { this.onLinkChanged = (directoryLinksProvider, link) => { @@ -289,30 +289,30 @@ add_task(function test_updateRelatedTile() { do_check_eq(this.count, 1); do_check_eq(link.frecency, 0); do_check_eq(link.url, links.shift().url); - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], []); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], []); resolve(); } }); } - // Test first call to '_updateRelatedTile()', called when fetching directory links. + // Test first call to '_updateSuggestedTile()', called when fetching directory links. yield testObserver.promise; DirectoryLinksProvider.removeObserver(testObserver); - // Removing a top site that doesn't have a related link should - // not change the current related tile. + // Removing a top site that doesn't have a suggested link should + // not change the current suggested tile. let removedTopsite = topSites.shift(); do_check_eq(removedTopsite, "site0.com"); do_check_false(NewTabUtils.isTopPlacesSite(removedTopsite)); - let updateRelatedTile = DirectoryLinksProvider._handleLinkChanged({ + let updateSuggestedTile = DirectoryLinksProvider._handleLinkChanged({ url: "http://" + removedTopsite, type: "history", }); - do_check_false(updateRelatedTile); + do_check_false(updateSuggestedTile); - // Removing a top site that has a related link should - // remove any current related tile and add a new one. - testObserver = new TestChangingRelatedTile(); + // Removing a top site that has a suggested link should + // remove any current suggested tile and add a new one. + testObserver = new TestChangingSuggestedTile(); DirectoryLinksProvider.addObserver(testObserver); removedTopsite = topSites.shift(); do_check_eq(removedTopsite, "1040.com"); @@ -325,10 +325,10 @@ add_task(function test_updateRelatedTile() { do_check_eq(testObserver.count, 2); DirectoryLinksProvider.removeObserver(testObserver); - // Removing all top sites with related links should remove - // the current related link and not replace it. + // Removing all top sites with suggested links should remove + // the current suggested link and not replace it. topSites = []; - testObserver = new TestRemovingRelatedTile(); + testObserver = new TestRemovingSuggestedTile(); DirectoryLinksProvider.addObserver(testObserver); DirectoryLinksProvider.onManyLinksChanged(); yield testObserver.promise; @@ -339,38 +339,38 @@ add_task(function test_updateRelatedTile() { NewTabUtils.getProviderLinks = origGetProviderLinks; }); -add_task(function test_relatedLinksMap() { - let data = {"suggested": [relatedTile1, relatedTile2, relatedTile3], "directory": [someOtherSite]}; +add_task(function test_suggestedLinksMap() { + let data = {"suggested": [suggestedTile1, suggestedTile2, suggestedTile3], "directory": [someOtherSite]}; let dataURI = 'data:application/json,' + JSON.stringify(data); yield promiseSetupDirectoryLinksProvider({linksURL: dataURI}); let links = yield fetchData(); - // Ensure the related tiles were not considered directory tiles. + // Ensure the suggested tiles were not considered directory tiles. do_check_eq(links.length, 1); - let expected_data = [{url: "http://someothersite.com", title: "Not_A_Related_Site", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; + let expected_data = [{url: "http://someothersite.com", title: "Not_A_Suggested_Site", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; isIdentical(links, expected_data); - // Check for correctly saved related tiles data. + // Check for correctly saved suggested tiles data. expected_data = { - "taxact.com": [relatedTile1, relatedTile2, relatedTile3], - "hrblock.com": [relatedTile1, relatedTile2], - "1040.com": [relatedTile1, relatedTile3], - "taxslayer.com": [relatedTile1, relatedTile2, relatedTile3], - "freetaxusa.com": [relatedTile2, relatedTile3], + "taxact.com": [suggestedTile1, suggestedTile2, suggestedTile3], + "hrblock.com": [suggestedTile1, suggestedTile2], + "1040.com": [suggestedTile1, suggestedTile3], + "taxslayer.com": [suggestedTile1, suggestedTile2, suggestedTile3], + "freetaxusa.com": [suggestedTile2, suggestedTile3], }; - DirectoryLinksProvider._relatedLinks.forEach((relatedLinks, site) => { - let relatedLinksItr = relatedLinks.values(); + DirectoryLinksProvider._suggestedLinks.forEach((suggestedLinks, site) => { + let suggestedLinksItr = suggestedLinks.values(); for (let link of expected_data[site]) { - isIdentical(relatedLinksItr.next().value, link); + isIdentical(suggestedLinksItr.next().value, link); } }) yield promiseCleanDirectoryLinksProvider(); }); -add_task(function test_topSitesWithRelatedLinks() { +add_task(function test_topSitesWithSuggestedLinks() { let topSites = ["site0.com", "1040.com", "site2.com", "hrblock.com", "site4.com", "freetaxusa.com", "site6.com"]; let origIsTopPlacesSite = NewTabUtils.isTopPlacesSite; NewTabUtils.isTopPlacesSite = function(site) { @@ -383,39 +383,39 @@ add_task(function test_topSitesWithRelatedLinks() { return []; } - // We start off with no top sites with related links. - do_check_eq(DirectoryLinksProvider._topSitesWithRelatedLinks.size, 0); + // We start off with no top sites with suggested links. + do_check_eq(DirectoryLinksProvider._topSitesWithSuggestedLinks.size, 0); - let data = {"suggested": [relatedTile1, relatedTile2, relatedTile3], "directory": [someOtherSite]}; + let data = {"suggested": [suggestedTile1, suggestedTile2, suggestedTile3], "directory": [someOtherSite]}; let dataURI = 'data:application/json,' + JSON.stringify(data); yield promiseSetupDirectoryLinksProvider({linksURL: dataURI}); let links = yield fetchData(); - // Check we've populated related links as expected. - do_check_eq(DirectoryLinksProvider._relatedLinks.size, 5); + // Check we've populated suggested links as expected. + do_check_eq(DirectoryLinksProvider._suggestedLinks.size, 5); - // When many sites change, we update _topSitesWithRelatedLinks as expected. - let expectedTopSitesWithRelatedLinks = ["hrblock.com", "1040.com", "freetaxusa.com"]; + // When many sites change, we update _topSitesWithSuggestedLinks as expected. + let expectedTopSitesWithSuggestedLinks = ["hrblock.com", "1040.com", "freetaxusa.com"]; DirectoryLinksProvider._handleManyLinksChanged(); - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], expectedTopSitesWithRelatedLinks); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], expectedTopSitesWithSuggestedLinks); - // Removing site6.com as a topsite has no impact on _topSitesWithRelatedLinks. + // Removing site6.com as a topsite has no impact on _topSitesWithSuggestedLinks. let popped = topSites.pop(); DirectoryLinksProvider._handleLinkChanged({url: "http://" + popped}); - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], expectedTopSitesWithRelatedLinks); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], expectedTopSitesWithSuggestedLinks); - // Removing freetaxusa.com as a topsite will remove it from _topSitesWithRelatedLinks. + // Removing freetaxusa.com as a topsite will remove it from _topSitesWithSuggestedLinks. popped = topSites.pop(); - expectedTopSitesWithRelatedLinks.pop(); + expectedTopSitesWithSuggestedLinks.pop(); DirectoryLinksProvider._handleLinkChanged({url: "http://" + popped}); - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], expectedTopSitesWithRelatedLinks); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], expectedTopSitesWithSuggestedLinks); - // Re-adding freetaxusa.com as a topsite will add it to _topSitesWithRelatedLinks. + // Re-adding freetaxusa.com as a topsite will add it to _topSitesWithSuggestedLinks. topSites.push(popped); - expectedTopSitesWithRelatedLinks.push(popped); + expectedTopSitesWithSuggestedLinks.push(popped); DirectoryLinksProvider._handleLinkChanged({url: "http://" + popped}); - isIdentical([...DirectoryLinksProvider._topSitesWithRelatedLinks], expectedTopSitesWithRelatedLinks); + isIdentical([...DirectoryLinksProvider._topSitesWithSuggestedLinks], expectedTopSitesWithSuggestedLinks); // Cleanup. NewTabUtils.isTopPlacesSite = origIsTopPlacesSite;