forked from mozilla-b2g/gaia
/
common.js
206 lines (162 loc) · 5.84 KB
/
common.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
'use strict';
/* global eme */
/* global Promise */
(function (exports) {
// choose best ratio for device
function chooseBackgroundRatio(pixelRatio) {
// background ratios supported by mozilla cdn
var backgroundOptions = [1, 1.5, 2, 2.25];
return Math.max.apply(null,
backgroundOptions.filter((r) => pixelRatio >= r));
}
const backgroundRatio = chooseBackgroundRatio(window.devicePixelRatio);
const suffix =
backgroundRatio === 1 ? '.jpg' : ('@' + backgroundRatio + 'x.jpg');
const mozBgUrl =
'http://fxos.cdn.mozilla.net/collection/background/{categoryId}' + suffix;
const APPS_IN_ICON = 3;
function Common() {}
Common.prototype = {
APPS_IN_ICON: APPS_IN_ICON,
chooseBackgroundRatio: chooseBackgroundRatio,
b64toBlob: function b64toBlob(b64) {
return new Promise((resolve, reject) => {
var img = new Image();
img.onload = function onload() {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
canvas.toBlob(resolve);
};
img.onerror = reject;
img.src = b64;
});
},
// TODO
// add support for 'size' parameter (like getEmeBackground has) to fetch
// smaller backgrounds when the full size image is not required
// (like when creating collection icons)
getMozBackground: function getMozBackground(collection) {
if (!collection.categoryId) {
return Promise.reject('no categoryId');
}
var url = mozBgUrl.replace('{categoryId}', collection.categoryId);
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.open('GET', url, true);
xhr.responseType = 'blob';
return new Promise((resolve, reject) => {
xhr.onload = function onload() {
if (xhr.status === 200) {
var blob = new Blob([xhr.response], {type: 'image/jpg'});
eme.log('getMozBackground', 'success', url);
resolve({
blob: blob,
source: url,
checksum: 'mozilla' // TODO: generate checksum from image data
});
} else {
reject('xhr.status ' + xhr.status);
}
};
xhr.onerror = function onerror() {
reject('xhr error');
};
xhr.send();
});
},
getEmeBackground: function getEmeBackground(collection, size) {
var checksum;
var options = {
width: size || undefined,
height: size || undefined
};
if (collection.categoryId) {
options.categoryId = collection.categoryId;
}
else {
options.query = collection.query;
}
if (collection.background) {
checksum = collection.background.checksum;
// when we send _checksum server will not return an image if checksum
// was not updated, so check that we really have background data
if (collection.background.blob) {
options._checksum = checksum;
}
}
return eme.api.Search.bgimage(options)
.then((response) => {
if (checksum && checksum === response.checksum) {
eme.log('background didn\'t change (checksum match)');
return collection.background;
} else {
var b64;
var image = response.response.image;
if (image) {
b64 = image.data;
if (/image\//.test(image.MIMEType)) { // base64 image data
b64 = 'data:' + image.MIMEType + ';base64,' + image.data;
}
}
return this.b64toBlob(b64).then(function toBg(blob) {
return {
blob: blob,
source: response.response.source,
checksum: response.checksum || null
};
});
}
});
},
// get background for a collection from
// 1. mozilla cdn
// 2. e.me api
getBackground: function getBackground(collection, size) {
return this.getMozBackground(collection)
.catch(function (e) {
eme.log('getBackground', e, 'trying e.me background');
return this.getEmeBackground(collection, size);
}.bind(this))
.catch(function (e) {
eme.log('getBackground', 'failed', e);
});
},
getWebIcons: function getWebIcons(collection) {
var options =
(collection.categoryId) ? {categoryId: collection.categoryId} :
{query: collection.query};
options.limit = APPS_IN_ICON;
return eme.api.Apps.search(options).then(response => {
var webicons =
response.response.apps.slice(0, APPS_IN_ICON).map(app => app.icon);
return webicons;
});
},
/**
* prepares the assets needed for rendering a collection's icon:
* 1. background
* 2. web icons
*
* returns a promise resolved when all assets requests are done and
* updates the collection instance but not the db
*
*/
prepareAssets: function prepareAssets(collection) {
var ready = Promise.resolve(null);
var backgroundPromise = collection.backgroundReady ?
ready : this.getBackground(collection);
var iconsPromise = collection.iconsReady ?
ready : this.getWebIcons(collection);
return Promise.all([iconsPromise, backgroundPromise])
.then((results) => {
// results are null if not fetched
collection.webicons = results[0] || collection.webicons;
collection.background = results[1] || collection.background;
return collection;
});
}
};
exports.Common = new Common();
}(window));