Bug 1396104 - Use rich icons when getting top sites
MozReview-Commit-ID: IMv0osslTS3
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -921,50 +921,55 @@ var ActivityStreamProvider = {
},
/**
* Computes favicon data for each url in a set of links
*
* @param {Array} links
* an array containing objects without favicon data or mimeTypes yet
*
- * @returns {Promise} Returns a promise with the array of links with favicon data,
- * mimeType, and byte array length
+ * @returns {Promise} Returns a promise with the array of links with the largest
+ * favicon available (as a byte array), mimeType, byte array
+ * length, and favicon size (width)
*/
async _addFavicons(aLinks) {
if (aLinks.length) {
// Each link in the array needs a favicon for it's page - so we fire off
// a promise for each link to compute the favicon data and attach it back
// to the original link object. We must wait until all favicons for
// the array of links are computed before returning
await Promise.all(aLinks.map(link => new Promise(resolve => {
return PlacesUtils.favicons.getFaviconDataForPage(
Services.io.newURI(link.url),
- (iconuri, len, data, mime) => {
+ (iconuri, len, data, mime, size) => {
// Due to the asynchronous behaviour of inserting a favicon into
// moz_favicons, the data may not be available to us just yet,
// since we listen on a history entry being inserted. As a result,
// we don't want to throw if the icon uri is not here yet, we
// just want to resolve on an empty favicon. Activity Stream
// knows how to handle null favicons
if (!iconuri) {
link.favicon = null;
link.mimeType = null;
+ link.faviconSize = null;
} else {
link.favicon = data;
link.mimeType = mime;
link.faviconLength = len;
+ link.faviconSize = size;
}
return resolve(link);
- });
+ },
+ 0); // preferredWidth: get the biggest width available
}).catch(() => {
// If something goes wrong - that's ok - just return a null favicon
// without rejecting the entire Promise.all
link.favicon = null;
link.mimeType = null;
+ link.faviconSize = null;
return link;
})
));
}
return aLinks;
},
/**
--- a/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
+++ b/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
@@ -338,18 +338,20 @@ add_task(async function addFavicons() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamProvider;
// start by passing in a bad uri and check that we get a null favicon back
let links = [{url: "mozilla.com"}];
await provider._addFavicons(links);
Assert.equal(links[0].favicon, null, "Got a null favicon because we passed in a bad url");
Assert.equal(links[0].mimeType, null, "Got a null mime type because we passed in a bad url");
+ Assert.equal(links[0].faviconSize, null, "Got a null favicon size because we passed in a bad url");
// now fix the url and try again - this time we get good favicon data back
+ // a 1x1 favicon as a data URI of mime type image/png
links[0].url = "https://mozilla.com";
let base64URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAA" +
"AAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==";
let visit = [
{uri: links[0].url, visitDate: timeDaysAgo(0), transition: PlacesUtils.history.TRANSITION_TYPED}
];
await PlacesTestUtils.addVisits(visit);
@@ -357,16 +359,17 @@ add_task(async function addFavicons() {
let faviconData = new Map();
faviconData.set("https://mozilla.com", base64URL);
await PlacesTestUtils.addFavicons(faviconData);
await provider._addFavicons(links);
Assert.equal(links[0].mimeType, "image/png", "Got the right mime type before deleting it");
Assert.equal(links[0].faviconLength, links[0].favicon.length, "Got the right length for the byte array");
Assert.equal(provider._faviconBytesToDataURI(links)[0].favicon, base64URL, "Got the right favicon");
+ Assert.equal(links[0].faviconSize, 1, "Got the right favicon size (width and height of favicon)");
});
add_task(async function getHighlights() {
const addMetadata = url => PlacesUtils.history.update({
description: "desc",
previewImageURL: "https://image/",
url
});