This repository was archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathicons_helper.js
459 lines (407 loc) · 13.2 KB
/
icons_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* globals Icon */
/* exported IconsHelper */
'use strict';
/**
* Utility library that will help us to work with icons coming from
* different sources.
*/
(function IconsHelper(exports) {
const ICON_CACHE_PERIOD = 24 * 60 * 60 * 1000; // 1 day
const FETCH_XHR_TIMEOUT = 10000;
const DEBUG = false;
var dataStore = null;
/**
* Return default size in px based in devicePixelRatio
*
* Sized based on current homescreen selected icons for apps
* in a configuration of 3 icons per row. See:
* https://github.com/mozilla-b2g/gaia/blob/master/
* shared/elements/gaia_grid/js/grid_layout.js#L15
* @returns {Number}
*/
function getDefaultIconSize() {
var dpr = window.devicePixelRatio;
return (dpr && dpr > 1) ? 142 : 84;
}
function sizeIsNearer(size1, size2, targetSize) {
// TODO: weight for a larger vs. smaller icon?
var delta1 = Math.abs(targetSize - size1);
var delta2 = Math.abs(targetSize - size2);
return (delta1 <= delta2);
}
/**
* Return a promise that resolves to the URL of the best icon for a web page
* given its meta data and web manifest.
*
* @param uri {string}
* @param iconTargetSize {number}
* @param placeObj {Object}
* @param siteObj {Object}
* @returns {Promise}
*/
function getIcon(uri, iconTargetSize, placeObj = {}, siteObj = {}) {
var iconUrl = null;
iconTargetSize = iconTargetSize * window.devicePixelRatio;
// First look for an icon in the Webmanifest.
if (siteObj.webManifestUrl && siteObj.webManifest) {
iconUrl = getBestIconFromWebManifest(siteObj.webManifest, iconTargetSize);
if (DEBUG && iconUrl) {
console.log('Icon from Web Manifest');
}
}
// Then look for an icon in the Firefox manifest.
if (!iconUrl && siteObj.manifest && siteObj.manifest.icons) {
iconUrl = getBestIconFromWebManifest({
icons: _convertToWebManifestIcons(siteObj.manifest,
siteObj.origin || siteObj.manifest.origin)
}, iconTargetSize);
if (DEBUG && iconUrl) {
console.log('Icon from Firefox App Manifest');
}
}
// Otherwise, look into the meta tags.
if (!iconUrl && placeObj && placeObj.icons) {
iconUrl = getBestIconFromMetaTags(placeObj.icons, iconTargetSize);
if (DEBUG && iconUrl) {
console.log('Icon from Meta tags');
}
}
// Last resort, we look for a favicon.ico file.
if (!iconUrl) {
var a = document.createElement('a');
a.href = uri;
iconUrl = a.origin + '/favicon.ico';
if (iconTargetSize) {
iconUrl += '#-moz-resolution=' + iconTargetSize + ',' + iconTargetSize;
}
DEBUG && console.log('Icon from favicon.ico');
}
return new Promise(resolve => {
resolve(iconUrl);
});
}
function processIconBlob(iconBlob, iconUrl, uri, iconTargetSize) {
return new Promise((resolve, reject) => {
var img = document.createElement('img');
var icon = new Icon(img, uri);
icon.renderBlob(iconBlob, {
size: iconTargetSize,
onLoad: function(blob) {
var iconObj = {
blob: blob,
originalUrl: iconUrl.toString(),
timestamp: Date.now()
};
resolve(iconObj);
},
onerror: function(e) {
reject(`Failed to fetch icon ${iconUrl}`);
}
});
});
}
/**
* Same as above except the promise resolves as an object containing the blob
* of the icon and its size in pixels.
*
* @param uri {string}
* @param iconTargetSize {number}
* @param placeObj {Object}
* @param siteObj {Object}
* @returns {Promise}
*/
function getIconBlob(uri, iconTargetSize, placeObj = {}, siteObj = {}) {
return new Promise((resolve, reject) => {
getIcon(uri, iconTargetSize, placeObj, siteObj)
.then(iconUrl => {
// @todo Need a better syntax.
getStore().then(iconStore => {
iconStore.get(iconUrl).then(iconObj => {
if (!iconObj || !iconObj.timestamp ||
Date.now() - iconObj.timestamp >= ICON_CACHE_PERIOD) {
return fetchIconBlob(iconUrl)
.then(iconBlob => {
processIconBlob(iconBlob, iconUrl, uri, iconTargetSize)
.then(
iconObj => {
// We resolve here to avoid I/O blocking on
// dataStore and quicker display.
// Persisting to the dataStore takes place after.
resolve(iconObj);
iconStore.add(iconObj, iconUrl);
});
}).catch(err => {
reject(`Failed to fetch icon ${iconUrl}: ${err}`);
});
}
return resolve(iconObj);
}).catch(err => {
reject(`Failed to get icon from dataStore: ${err}`);
});
}).catch(err => {
console.error(`Error opening the dataStore: ${err}`);
// We should fetch the icon and resolve the promise here, anyhow.
fetchIconBlob(iconUrl).then(iconBlob => {
processIconBlob(iconBlob, iconUrl, uri, iconTargetSize)
.then(iconObj => {
console.log('Successfully fetched icon');
resolve(iconObj);
});
}).catch(err => {
reject(`Failed to fetch icon ${iconUrl}: ${err}`);
});
});
});
});
}
/**
* Same as above but set the image as the icon property of a gaia-app-icon
* element.
*
* @param icon {Object?}
* @param targetSize {number}
* @returns {Promise}
*/
function setElementIcon(icon, targetSize) {
return getIconBlob(icon.bookmark.url, targetSize,
icon.bookmark, icon.bookmark)
.then(iconObj => {
if (iconObj.blob) {
icon.icon = iconObj.blob;
return Promise.resolve();
} else if (icon.bookmark.icon) {
// We fallback to the bookmark.icon property if no icons were found.
return fetchIconBlob(icon.bookmark.icon)
.then(iconBlob => {
icon.icon = iconBlob;
return Promise.resolve();
}, Promise.reject.bind(Promise)); // XXXbz Why second arg?
}
return Promise.reject('No icon data found');
}, Promise.reject.bind(Promise)); // XXXbz Why bother with second arg?
}
function getBestIconFromWebManifest(webManifest, iconSize) {
var icons = webManifest.icons;
if (!icons) {
return null;
}
var maxSize = 10000;
var bestSize = maxSize;
var iconURL = null;
iconSize = iconSize || getDefaultIconSize();
icons.forEach((potentialIcon) => {
if (!iconURL) {
iconURL = potentialIcon.src;
}
var sizes = Array.from(potentialIcon.sizes);
var nearestSize = getNearestSize(sizes, iconSize, bestSize);
if (nearestSize !== bestSize &&
sizeIsNearer(nearestSize, bestSize, iconSize)) {
iconURL = potentialIcon.src;
bestSize = nearestSize;
}
});
return iconURL ? iconURL : null;
}
function _convertToWebManifestIcons(manifest, origin) {
return Object.keys(manifest.icons).map(function(size) {
var url = manifest.icons[size];
var sizes = [size + 'x' + size];
url = url.indexOf('http') > -1 ? url : origin + url;
return {
src: new URL(url),
sizes: sizes
};
});
}
// See bug 1041482, we will need to support better
// icons for different part of the system application.
// A web page have different ways to defining icons
// based on size, 'touch' capabilities and so on.
// From gecko we will receive all the rel='icon'
// defined which will contain as well the sizes
// supported in that file.
// This function will help to deliver the best suitable
// icon based on that definition list.
// The expected format is the following one:
//
// {
// '[uri 1]': {
// sizes: ['16x16 32x32 48x48', '60x60']
// },
// '[uri 2]': {
// sizes: ['16x16']
// }
// }
//
// iconSize is an additional parameter to specify a concrete
// size or the closest icon.
function getBestIconFromMetaTags(icons, iconSize) {
if (!icons) {
return null;
}
iconSize = iconSize || getDefaultIconSize();
var iconURL = null;
var bestSize = 10000;
Object.keys(icons).forEach((uri) => {
var potentialIcon = icons[uri];
if (!iconURL) {
// Handle the case of no size info in the whole list
iconURL = uri;
}
var sizes = Array.from(potentialIcon.sizes);
var nearestSize = getNearestSize(sizes, iconSize, bestSize);
if (nearestSize !== bestSize &&
sizeIsNearer(nearestSize, bestSize, iconSize)) {
iconURL = uri;
bestSize = nearestSize;
}
if (potentialIcon.rel === 'apple-touch-icon' ||
potentialIcon.rel === 'apple-touch-icon-precomposed') {
var moreInfoUrl = 'https://developer.mozilla.org/en-US/' +
'Apps/Build/Icon_implementation_for_apps#General_icons_for_web_apps';
console.warn('Warning: The apple-touch icons are being used ' +
'as a fallback only. They will be deprecated in ' +
'the future. See ' + moreInfoUrl);
}
});
return iconURL || null;
}
// Given an array of size strings e.g. (64x64), a target iconSize
// and optionally a best-so-far-size,
// return the nearest match
function getNearestSize(sizes, iconSize, bestSize) {
var bogusSize = 10000;
if (!bestSize) {
bestSize = bogusSize;
}
var nearestSize = sizes.reduce(function(nearestSize, sizeString, idx) {
var size = widthFromSizeString(sizeString);
if (isNaN(size)) {
return nearestSize;
}
if (sizeIsNearer(size, nearestSize, iconSize)) {
return size;
} else {
return nearestSize;
}
}, bestSize);
return nearestSize === bogusSize ? -1 : nearestSize;
}
// Given an icon size by string YYxYY returns the
// width measurement, so will assume this will be
// used by strings that identify a square size.
function widthFromSizeString(size) {
size = size || '';
var xIndex = size.indexOf('x');
if (!xIndex) {
return NaN;
}
return parseInt(size.substr(0, xIndex));
}
/**
* Return a promise that resolves to a dataStore for icons.
*
* @returns {Promise}
*/
function getStore() {
return new Promise(resolve => {
if (dataStore) {
return resolve(dataStore);
}
navigator.getDataStores('icons').then(stores => {
dataStore = stores[0];
return resolve(dataStore);
});
});
}
/**
* Clear all the icons in the store.
*
* @returns {Promise}
*/
function clear() {
return getStore().then(iconStore => {
iconStore.clear();
});
}
/**
* Return a promise that resolves to an object containing the blob url
* in pixels of an icon given its URL `iconUrl`.
*
* @param {string} iconUrl
* @returns {Promise}
*/
function fetchIcon(iconUrl) {
return new Promise((resolve, reject) => {
fetchIconBlob(iconUrl)
.then((iconBlob) => {
var img = document.createElement('img');
img.src = URL.createObjectURL(iconBlob);
img.onload = () => {
var iconSize = Math.max(img.naturalWidth, img.naturalHeight);
resolve({
blob: iconBlob,
url: iconUrl,
size: iconSize,
timestamp: Date.now()
});
};
img.onerror = () => {
reject(new Error(`Error while loading image.`));
};
})
.catch((e) => {
reject(new Error(`Error while loading image: ${e}`));
});
});
}
/**
* Return a promise that resolves to an object containing the blob
* given its URL `iconUrl`.
*
* @param {string} iconUrl
* @returns {Promise}
*/
function fetchIconBlob(iconUrl) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest({
mozAnon: true,
mozSystem: true
});
xhr.open('GET', iconUrl, true);
xhr.responseType = 'blob';
xhr.timeout = FETCH_XHR_TIMEOUT;
// Remember that send() can throw for some non http protocols.
// The promise wrapper here protects us.
xhr.send();
xhr.onload = () => {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var iconBlob = xhr.response;
resolve(iconBlob);
return;
}
reject(new Error(
`Got HTTP status ${xhr.status} trying to load ${iconUrl}.`));
};
xhr.onerror = xhr.ontimeout = () => {
reject(new Error(`Error while getting ${iconUrl}.`));
};
});
}
exports.IconsHelper = {
getIcon: getIcon,
getIconBlob: getIconBlob,
setElementIcon: setElementIcon,
getBestIconFromWebManifest: getBestIconFromWebManifest,
getBestIconFromMetaTags: getBestIconFromMetaTags,
fetchIcon: fetchIcon,
fetchIconBlob: fetchIconBlob,
get defaultIconSize() {
return getDefaultIconSize();
},
clear: clear,
// Make public for unit test purposes.
getNearestSize: getNearestSize,
};
})(window);