Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
feat(tippytop): Use default top icons if a site lacks a rich icon (#3890
Browse files Browse the repository at this point in the history
)

Fix Bug 1421903 - Allow default packaged top site rich icons to be used for non-root pages
  • Loading branch information
Mardak committed Dec 2, 2017
1 parent 6fcebac commit ce92c43
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
18 changes: 4 additions & 14 deletions system-addon/lib/TippyTopProvider.jsm
Expand Up @@ -10,18 +10,17 @@ const TIPPYTOP_JSON_PATH = "resource://activity-stream/data/content/tippytop/top
const TIPPYTOP_URL_PREFIX = "resource://activity-stream/data/content/tippytop/images/";

function getDomain(url) {
let domain = new URL(url).hostname;
let domain;
try {
domain = new URL(url).hostname;
} catch (ex) {}
if (domain && domain.startsWith("www.")) {
domain = domain.slice(4);
}
return domain;
}
this.getDomain = getDomain;

function getPath(url) {
return new URL(url).pathname;
}

this.TippyTopProvider = class TippyTopProvider {
constructor() {
this._sitesByDomain = new Map();
Expand All @@ -43,15 +42,6 @@ this.TippyTopProvider = class TippyTopProvider {
}
}
processSite(site) {
// Skip URLs with a path that isn't the root path /
let path;
try {
path = getPath(site.url);
} catch (e) {}
if (path !== "/") {
return site;
}

const tippyTop = this._sitesByDomain.get(getDomain(site.url));
if (tippyTop) {
site.tippyTopIcon = TIPPYTOP_URL_PREFIX + tippyTop.image_url;
Expand Down
21 changes: 13 additions & 8 deletions system-addon/lib/TopSitesFeed.jsm
Expand Up @@ -165,17 +165,22 @@ this.TopSitesFeed = class TopSitesFeed {
* Get an image for the link preferring tippy top, rich favicon, screenshots.
*/
async _fetchIcon(link) {
// Check for tippy top icon and rich icon.
this._tippyTopProvider.processSite(link);
let hasTippyTop = !!link.tippyTopIcon;
let hasRichIcon = link.favicon && link.faviconSize >= MIN_FAVICON_SIZE;
// Nothing to do if we already have a rich icon from the page
if (link.favicon && link.faviconSize >= MIN_FAVICON_SIZE) {
return;
}

if (!hasTippyTop && !hasRichIcon) {
this._requestRichIcon(link.url);
// Nothing more to do if we can use a default tippy top icon
this._tippyTopProvider.processSite(link);
if (link.tippyTopIcon) {
return;
}

// Request a screenshot if needed.
if (!hasTippyTop && !hasRichIcon && !link.screenshot) {
// Make a request for a better icon
this._requestRichIcon(link.url);

// Also request a screenshot if we don't have one yet
if (!link.screenshot) {
const {url} = link;
await Screenshots.maybeCacheScreenshot(link, url, "screenshot",
screenshot => this.store.dispatch(ac.BroadcastToContent({
Expand Down
8 changes: 4 additions & 4 deletions system-addon/test/unit/lib/TippyTopProvider.test.js
Expand Up @@ -39,10 +39,10 @@ describe("TippyTopProvider", () => {
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/facebook-com.png");
assert.equal(site.backgroundColor, "#3b5998");
});
it("should not provide an icon for facebook.com/foobar", () => {
it("should provide an icon for facebook.com/foobar", () => {
const site = instance.processSite({url: "https://facebook.com/foobar"});
assert.isUndefined(site.tippyTopIcon);
assert.isUndefined(site.backgroundColor);
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/facebook-com.png");
assert.equal(site.backgroundColor, "#3b5998");
});
it("should provide an icon for gmail.com", () => {
const site = instance.processSite({url: "https://gmail.com"});
Expand All @@ -66,6 +66,6 @@ describe("TippyTopProvider", () => {
fetchStub.rejects("whaaaa");
instance = new TippyTopProvider();
await instance.init();
instance.processSite("https://facebook.com");
instance.processSite({url: "https://facebook.com"});
});
});
14 changes: 14 additions & 0 deletions system-addon/test/unit/lib/TopSitesFeed.test.js
Expand Up @@ -488,6 +488,20 @@ describe("Top Sites Feed", () => {
assert.notProperty(link, "screenshot");
assert.notCalled(fakeScreenshot.getScreenshotForURL);
});
it("should use the link's rich icon even if there's a tippy top", () => {
feed._tippyTopProvider.processSite = site => {
site.tippyTopIcon = "icon.png";
site.backgroundColor = "#fff";
return site;
};
const link = {
url: "foo.com",
favicon: "data:foo",
faviconSize: 196
};
feed._fetchIcon(link);
assert.notProperty(link, "tippyTopIcon");
});
});
describe("#onAction", () => {
it("should refresh on SYSTEM_TICK", async () => {
Expand Down

0 comments on commit ce92c43

Please sign in to comment.