| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Client used to connect to the remote ImageLoader extension. Client class runs | 8 * Client used to connect to the remote ImageLoader extension. Client class runs |
| 9 * in the extension, where the client.js is included (eg. Files.app). | 9 * in the extension, where the client.js is included (eg. Files.app). |
| 10 * It sends remote requests using IPC to the ImageLoader class and forwards | 10 * It sends remote requests using IPC to the ImageLoader class and forwards |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 224 |
| 225 /** | 225 /** |
| 226 * Creates a cache key. | 226 * Creates a cache key. |
| 227 * | 227 * |
| 228 * @param {string} url Image url. | 228 * @param {string} url Image url. |
| 229 * @param {Object=} opt_options Loader options as a hash array. | 229 * @param {Object=} opt_options Loader options as a hash array. |
| 230 * @return {string} Cache key. | 230 * @return {string} Cache key. |
| 231 */ | 231 */ |
| 232 ImageLoaderClient.Cache.createKey = function(url, opt_options) { | 232 ImageLoaderClient.Cache.createKey = function(url, opt_options) { |
| 233 opt_options = opt_options || {}; | 233 opt_options = opt_options || {}; |
| 234 return JSON.stringify({url: url, | 234 return JSON.stringify({ |
| 235 orientation: opt_options.orientation, | 235 url: url, |
| 236 scale: opt_options.scale, | 236 orientation: opt_options.orientation, |
| 237 width: opt_options.width, | 237 scale: opt_options.scale, |
| 238 height: opt_options.height, | 238 width: opt_options.width, |
| 239 maxWidth: opt_options.maxWidth, | 239 height: opt_options.height, |
| 240 maxHeight: opt_options.maxHeight}); | 240 maxWidth: opt_options.maxWidth, |
| 241 maxHeight: opt_options.maxHeight}); |
| 241 }; | 242 }; |
| 242 | 243 |
| 243 /** | 244 /** |
| 244 * Evicts the least used elements in cache to make space for a new image. | 245 * Evicts the least used elements in cache to make space for a new image. |
| 245 * | 246 * |
| 246 * @param {number} size Requested size. | 247 * @param {number} size Requested size. |
| 247 * @private | 248 * @private |
| 248 */ | 249 */ |
| 249 ImageLoaderClient.Cache.prototype.evictCache_ = function(size) { | 250 ImageLoaderClient.Cache.prototype.evictCache_ = function(size) { |
| 250 // Sort from the most recent to the oldest. | 251 // Sort from the most recent to the oldest. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 274 this.removeImage(key); | 275 this.removeImage(key); |
| 275 | 276 |
| 276 if (ImageLoaderClient.Cache.MEMORY_LIMIT - this.size_ < data.length) { | 277 if (ImageLoaderClient.Cache.MEMORY_LIMIT - this.size_ < data.length) { |
| 277 ImageLoaderClient.recordBinary('Evicted', 1); | 278 ImageLoaderClient.recordBinary('Evicted', 1); |
| 278 this.evictCache_(data.length); | 279 this.evictCache_(data.length); |
| 279 } else { | 280 } else { |
| 280 ImageLoaderClient.recordBinary('Evicted', 0); | 281 ImageLoaderClient.recordBinary('Evicted', 0); |
| 281 } | 282 } |
| 282 | 283 |
| 283 if (ImageLoaderClient.Cache.MEMORY_LIMIT - this.size_ >= data.length) { | 284 if (ImageLoaderClient.Cache.MEMORY_LIMIT - this.size_ >= data.length) { |
| 284 this.images_[key] = {lastLoadTimestamp: Date.now(), | 285 this.images_[key] = { |
| 285 timestamp: opt_timestamp ? opt_timestamp : null, | 286 lastLoadTimestamp: Date.now(), |
| 286 data: data}; | 287 timestamp: opt_timestamp ? opt_timestamp : null, |
| 288 data: data |
| 289 }; |
| 287 this.size_ += data.length; | 290 this.size_ += data.length; |
| 288 } | 291 } |
| 289 }; | 292 }; |
| 290 | 293 |
| 291 /** | 294 /** |
| 292 * Loads an image from the cache (if available) or returns null. | 295 * Loads an image from the cache (if available) or returns null. |
| 293 * | 296 * |
| 294 * @param {string} key Cache key. | 297 * @param {string} key Cache key. |
| 295 * @param {number=} opt_timestamp Last modification timestamp. If different | 298 * @param {number=} opt_timestamp Last modification timestamp. If different |
| 296 * that the one in cache, then the entry will be invalidated. | 299 * that the one in cache, then the entry will be invalidated. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 // Helper functions. | 340 // Helper functions. |
| 338 | 341 |
| 339 /** | 342 /** |
| 340 * Loads and resizes and image. Use opt_isValid to easily cancel requests | 343 * Loads and resizes and image. Use opt_isValid to easily cancel requests |
| 341 * which are not valid anymore, which will reduce cpu consumption. | 344 * which are not valid anymore, which will reduce cpu consumption. |
| 342 * | 345 * |
| 343 * @param {string} url Url of the requested image. | 346 * @param {string} url Url of the requested image. |
| 344 * @param {Image} image Image node to load the requested picture into. | 347 * @param {Image} image Image node to load the requested picture into. |
| 345 * @param {Object} options Loader options, such as: orientation, scale, | 348 * @param {Object} options Loader options, such as: orientation, scale, |
| 346 * maxHeight, width, height and/or cache. | 349 * maxHeight, width, height and/or cache. |
| 347 * @param {function=} onSuccess Callback for success. | 350 * @param {function} onSuccess Callback for success. |
| 348 * @param {function=} onError Callback for failure. | 351 * @param {function} onError Callback for failure. |
| 349 * @param {function=} opt_isValid Function returning false in case | 352 * @param {function=} opt_isValid Function returning false in case |
| 350 * a request is not valid anymore, eg. parent node has been detached. | 353 * a request is not valid anymore, eg. parent node has been detached. |
| 351 * @return {?number} Remote task id or null if loaded from cache. | 354 * @return {?number} Remote task id or null if loaded from cache. |
| 352 */ | 355 */ |
| 353 ImageLoaderClient.loadToImage = function( | 356 ImageLoaderClient.loadToImage = function( |
| 354 url, image, options, onSuccess, onError, opt_isValid) { | 357 url, image, options, onSuccess, onError, opt_isValid) { |
| 355 var callback = function(result) { | 358 var callback = function(result) { |
| 356 if (result.status == 'error') { | 359 if (result.status == 'error') { |
| 357 onError(); | 360 onError(); |
| 358 return; | 361 return; |
| 359 } | 362 } |
| 360 image.src = result.data; | 363 image.src = result.data; |
| 361 onSuccess(); | 364 onSuccess(); |
| 362 }; | 365 }; |
| 363 | 366 |
| 364 return ImageLoaderClient.getInstance().load( | 367 return ImageLoaderClient.getInstance().load( |
| 365 url, callback, options, opt_isValid); | 368 url, callback, options, opt_isValid); |
| 366 }; | 369 }; |
| OLD | NEW |