mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-28 15:23:51 +00:00
Bug 1126183 - Provide a way to check if a site is part of the top 100 PlacesProvider links. r=adw
This commit is contained in:
parent
5655f5bf86
commit
e22847194c
@ -720,9 +720,10 @@ let Links = {
|
||||
_providers: new Set(),
|
||||
|
||||
/**
|
||||
* A mapping from each provider to an object { sortedLinks, linkMap }.
|
||||
* sortedLinks is the cached, sorted array of links for the provider. linkMap
|
||||
* is a Map from link URLs to link objects.
|
||||
* A mapping from each provider to an object { sortedLinks, siteMap, linkMap }.
|
||||
* sortedLinks is the cached, sorted array of links for the provider.
|
||||
* siteMap is a mapping from base domains to URL count associated with the domain.
|
||||
* linkMap is a Map from link URLs to link objects.
|
||||
*/
|
||||
_providerLinks: new Map(),
|
||||
|
||||
@ -862,6 +863,21 @@ let Links = {
|
||||
aLink1.url.localeCompare(aLink2.url);
|
||||
},
|
||||
|
||||
_incrementSiteMap: function(map, link) {
|
||||
let site = NewTabUtils.extractSite(link.url);
|
||||
map.set(site, (map.get(site) || 0) + 1);
|
||||
},
|
||||
|
||||
_decrementSiteMap: function(map, link) {
|
||||
let site = NewTabUtils.extractSite(link.url);
|
||||
let previousURLCount = map.get(site);
|
||||
if (previousURLCount === 1) {
|
||||
map.delete(site);
|
||||
} else {
|
||||
map.set(site, previousURLCount - 1);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Calls getLinks on the given provider and populates our cache for it.
|
||||
* @param aProvider The provider whose cache will be populated.
|
||||
@ -879,6 +895,10 @@ let Links = {
|
||||
links = links.filter((link) => !!link);
|
||||
this._providerLinks.set(aProvider, {
|
||||
sortedLinks: links,
|
||||
siteMap: links.reduce((map, link) => {
|
||||
this._incrementSiteMap(map, link);
|
||||
return map;
|
||||
}, new Map()),
|
||||
linkMap: links.reduce((map, link) => {
|
||||
map.set(link.url, link);
|
||||
return map;
|
||||
@ -938,7 +958,7 @@ let Links = {
|
||||
// a change.
|
||||
return;
|
||||
|
||||
let { sortedLinks, linkMap } = links;
|
||||
let { sortedLinks, siteMap, linkMap } = links;
|
||||
let existingLink = linkMap.get(aLink.url);
|
||||
let insertionLink = null;
|
||||
let updatePages = false;
|
||||
@ -983,6 +1003,7 @@ let Links = {
|
||||
insertionLink[prop] = aLink[prop];
|
||||
}
|
||||
linkMap.set(aLink.url, insertionLink);
|
||||
this._incrementSiteMap(siteMap, aLink);
|
||||
}
|
||||
|
||||
if (insertionLink) {
|
||||
@ -991,6 +1012,7 @@ let Links = {
|
||||
if (sortedLinks.length > aProvider.maxNumLinks) {
|
||||
let lastLink = sortedLinks.pop();
|
||||
linkMap.delete(lastLink.url);
|
||||
this._decrementSiteMap(siteMap, lastLink);
|
||||
}
|
||||
updatePages = true;
|
||||
}
|
||||
@ -1187,6 +1209,14 @@ this.NewTabUtils = {
|
||||
return false;
|
||||
},
|
||||
|
||||
isTopSiteGivenProvider: function(aSite, aProvider) {
|
||||
return Links._providerLinks.get(aProvider).siteMap.has(aSite);
|
||||
},
|
||||
|
||||
isTopPlacesSite: function(aSite) {
|
||||
return this.isTopSiteGivenProvider(aSite, PlacesProvider);
|
||||
},
|
||||
|
||||
/**
|
||||
* Restores all sites that have been removed from the grid.
|
||||
*/
|
||||
|
@ -11,7 +11,46 @@ function run_test() {
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
add_test(function multipleProviders() {
|
||||
add_task(function isTopSiteGivenProvider() {
|
||||
let expectedLinks = makeLinks(0, 10, 2);
|
||||
|
||||
// The lowest 2 frecencies have the same base domain.
|
||||
expectedLinks[expectedLinks.length - 2].url = expectedLinks[expectedLinks.length - 1].url + "Test";
|
||||
|
||||
let provider = new TestProvider(done => done(expectedLinks));
|
||||
provider.maxNumLinks = expectedLinks.length;
|
||||
|
||||
NewTabUtils.initWithoutProviders();
|
||||
NewTabUtils.links.addProvider(provider);
|
||||
NewTabUtils.links.populateCache(function () {}, false);
|
||||
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example2.com", provider), true);
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example1.com", provider), false);
|
||||
|
||||
// Push out frecency 2 because the maxNumLinks is reached when adding frecency 3
|
||||
let newLink = makeLink(3);
|
||||
provider.notifyLinkChanged(newLink);
|
||||
|
||||
// There is still a frecent url with example2 domain, so it's still frecent.
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example3.com", provider), true);
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example2.com", provider), true);
|
||||
|
||||
// Push out frecency 3
|
||||
newLink = makeLink(5);
|
||||
provider.notifyLinkChanged(newLink);
|
||||
|
||||
// Push out frecency 4
|
||||
newLink = makeLink(9);
|
||||
provider.notifyLinkChanged(newLink);
|
||||
|
||||
// Our count reached 0 for the example2.com domain so it's no longer a frecent site.
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example5.com", provider), true);
|
||||
do_check_eq(NewTabUtils.isTopSiteGivenProvider("example2.com", provider), false);
|
||||
|
||||
NewTabUtils.links.removeProvider(provider);
|
||||
});
|
||||
|
||||
add_task(function multipleProviders() {
|
||||
// Make each provider generate NewTabUtils.links.maxNumLinks links to check
|
||||
// that no more than maxNumLinks are actually returned in the merged list.
|
||||
let evenLinks = makeLinks(0, 2 * NewTabUtils.links.maxNumLinks, 2);
|
||||
@ -35,10 +74,9 @@ add_test(function multipleProviders() {
|
||||
|
||||
NewTabUtils.links.removeProvider(evenProvider);
|
||||
NewTabUtils.links.removeProvider(oddProvider);
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_test(function changeLinks() {
|
||||
add_task(function changeLinks() {
|
||||
let expectedLinks = makeLinks(0, 20, 2);
|
||||
let provider = new TestProvider(done => done(expectedLinks));
|
||||
|
||||
@ -91,7 +129,6 @@ add_test(function changeLinks() {
|
||||
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
|
||||
|
||||
NewTabUtils.links.removeProvider(provider);
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
add_task(function oneProviderAlreadyCached() {
|
||||
|
Loading…
Reference in New Issue
Block a user