OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Namespace for utility functions. | 6 * Namespace for utility functions. |
7 */ | 7 */ |
8 var util = {}; | 8 var util = {}; |
9 | 9 |
10 /** | 10 /** |
(...skipping 1141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 | 1152 |
1153 var overCapacity = Math.max(0, keys.length - util.AppCache.CAPACITY); | 1153 var overCapacity = Math.max(0, keys.length - util.AppCache.CAPACITY); |
1154 | 1154 |
1155 var itemsToDelete = Math.max(obsolete, overCapacity); | 1155 var itemsToDelete = Math.max(obsolete, overCapacity); |
1156 for (var i = 0; i != itemsToDelete; i++) { | 1156 for (var i = 0; i != itemsToDelete; i++) { |
1157 delete map[keys[i]]; | 1157 delete map[keys[i]]; |
1158 } | 1158 } |
1159 }; | 1159 }; |
1160 | 1160 |
1161 /** | 1161 /** |
1162 * RemoteImageLoader loads an image from a remote url. | |
1163 * | |
1164 * Fetches a blob via XHR, converts it to a data: url and assigns to img.src. | |
1165 * @constructor | |
1166 */ | |
1167 util.RemoteImageLoader = function() {}; | |
1168 | |
1169 /** | |
1170 * @param {Image} image Image element. | |
1171 * @param {string} url Remote url to load into the image. | |
1172 */ | |
1173 util.RemoteImageLoader.prototype.load = function(image, url) { | |
1174 this.onSuccess_ = function(dataURL) { image.src = dataURL }; | |
1175 this.onError_ = function() { image.onerror() }; | |
1176 | |
1177 var xhr = new XMLHttpRequest(); | |
1178 xhr.responseType = 'blob'; | |
1179 xhr.onload = function() { | |
1180 if (xhr.status == 200) { | |
1181 var reader = new FileReader; | |
1182 reader.onload = function(e) { | |
1183 this.onSuccess_(e.target.result); | |
1184 }.bind(this); | |
1185 reader.onerror = this.onError_; | |
1186 reader.readAsDataURL(xhr.response); | |
1187 } else { | |
1188 this.onError_(); | |
1189 } | |
1190 }.bind(this); | |
1191 xhr.onerror = this.onError_; | |
1192 | |
1193 try { | |
1194 xhr.open('GET', url, true); | |
1195 xhr.send(); | |
1196 } catch (e) { | |
1197 console.log(e); | |
1198 this.onError_(); | |
1199 } | |
1200 }; | |
1201 | |
1202 /** | |
1203 * Cancels the loading. | |
1204 */ | |
1205 util.RemoteImageLoader.prototype.cancel = function() { | |
1206 // We cannot really cancel the XHR.send and FileReader.readAsDataURL, | |
1207 // silencing the callbacks instead. | |
1208 this.onSuccess_ = this.onError_ = function() {}; | |
1209 }; | |
1210 | |
1211 /** | |
1212 * Load an image. | 1162 * Load an image. |
1213 * | 1163 * |
1214 * In packaged apps img.src is not allowed to point to http(s)://. | |
1215 * For such urls util.RemoteImageLoader is used. | |
1216 * | |
1217 * @param {Image} image Image element. | 1164 * @param {Image} image Image element. |
1218 * @param {string} url Source url. | 1165 * @param {string} url Source url. |
1219 * @return {util.RemoteImageLoader?} RemoteImageLoader object reference, use it | 1166 * @param {Object=} opt_options Hash array of options, eg. width, height, |
1220 * to cancel the loading. | 1167 * maxWidth, maxHeight, scale, cache. |
| 1168 * @param {function()=} opt_isValid Function returning false iff the task |
| 1169 * is not valid and should be aborted. |
| 1170 * @return {?number} Task identifier or null if fetched immediately from |
| 1171 * cache. |
1221 */ | 1172 */ |
1222 util.loadImage = function(image, url) { | 1173 util.loadImage = function(image, url, opt_options, opt_isValid) { |
1223 if (util.platform.v2() && url.match(/^http(s):/)) { | 1174 return ImageLoader.Client.loadToImage(url, |
1224 var imageLoader = new util.RemoteImageLoader(); | 1175 image, |
1225 imageLoader.load(image, url); | 1176 opt_options || {}, |
1226 return imageLoader; | 1177 function() { }, |
1227 } | 1178 function() { image.onerror(); }, |
1228 | 1179 opt_isValid); |
1229 // OK to load directly. | |
1230 image.src = url; | |
1231 return null; | |
1232 }; | 1180 }; |
1233 | 1181 |
1234 /** | 1182 /** |
| 1183 * Cancels loading an image. |
| 1184 * @param {number} taskId Task identifier returned by util.loadImage(). |
| 1185 */ |
| 1186 util.cancelLoadImage = function(taskId) { |
| 1187 ImageLoader.Client.getInstance().cancel(taskId); |
| 1188 }; |
| 1189 |
| 1190 /** |
1235 * Finds proerty descriptor in the object prototype chain. | 1191 * Finds proerty descriptor in the object prototype chain. |
1236 * @param {Object} object The object. | 1192 * @param {Object} object The object. |
1237 * @param {string} propertyName The property name. | 1193 * @param {string} propertyName The property name. |
1238 * @return {Object} Property descriptor. | 1194 * @return {Object} Property descriptor. |
1239 */ | 1195 */ |
1240 util.findPropertyDescriptor = function(object, propertyName) { | 1196 util.findPropertyDescriptor = function(object, propertyName) { |
1241 for (var p = object; p; p = Object.getPrototypeOf(p)) { | 1197 for (var p = object; p; p = Object.getPrototypeOf(p)) { |
1242 var d = Object.getOwnPropertyDescriptor(p, propertyName); | 1198 var d = Object.getOwnPropertyDescriptor(p, propertyName); |
1243 if (d) | 1199 if (d) |
1244 return d; | 1200 return d; |
1245 } | 1201 } |
1246 return null; | 1202 return null; |
1247 }; | 1203 }; |
1248 | 1204 |
1249 /** | 1205 /** |
1250 * Calls inherited property setter (useful when property is | 1206 * Calls inherited property setter (useful when property is |
1251 * overriden). | 1207 * overriden). |
1252 * @param {Object} object The object. | 1208 * @param {Object} object The object. |
1253 * @param {string} propertyName The property name. | 1209 * @param {string} propertyName The property name. |
1254 * @param {*} value Value to set. | 1210 * @param {*} value Value to set. |
1255 */ | 1211 */ |
1256 util.callInheritedSetter = function(object, propertyName, value) { | 1212 util.callInheritedSetter = function(object, propertyName, value) { |
1257 var d = util.findPropertyDescriptor(Object.getPrototypeOf(object), | 1213 var d = util.findPropertyDescriptor(Object.getPrototypeOf(object), |
1258 propertyName); | 1214 propertyName); |
1259 d.set.call(object, value); | 1215 d.set.call(object, value); |
1260 }; | 1216 }; |
OLD | NEW |