Chromium Code Reviews| 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 /** | 5 /** |
| 6 * Client used to connect to the remote ImageLoader extension. Client class runs | 6 * Client used to connect to the remote ImageLoader extension. Client class runs |
| 7 * in the extension, where the client.js is included (eg. Files.app). | 7 * in the extension, where the client.js is included (eg. Files.app). |
| 8 * It sends remote requests using IPC to the ImageLoader class and forwards | 8 * It sends remote requests using IPC to the ImageLoader class and forwards |
| 9 * its responses. | 9 * its responses. |
| 10 * | 10 * |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 this.tasks_ = {}; | 21 this.tasks_ = {}; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * @type {number} | 24 * @type {number} |
| 25 * @private | 25 * @private |
| 26 */ | 26 */ |
| 27 this.lastTaskId_ = 0; | 27 this.lastTaskId_ = 0; |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * LRU cache for images. | 30 * LRU cache for images. |
| 31 * @type {!LRUCache.<{data: string, timestamp: ?number}>} | 31 * @type {!LRUCache.<{ |
| 32 * data: string, width:number, height:number, timestamp: ?number}>} | |
| 32 * @private | 33 * @private |
| 33 */ | 34 */ |
| 34 this.cache_ = new LRUCache(ImageLoaderClient.CACHE_MEMORY_LIMIT); | 35 this.cache_ = new LRUCache(ImageLoaderClient.CACHE_MEMORY_LIMIT); |
| 35 } | 36 } |
| 36 | 37 |
| 37 /** | 38 /** |
| 38 * Image loader's extension id. | 39 * Image loader's extension id. |
| 39 * @const | 40 * @const |
| 40 * @type {string} | 41 * @type {string} |
| 41 */ | 42 */ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 task.accept(message); | 111 task.accept(message); |
| 111 | 112 |
| 112 delete this.tasks_[message.taskId]; | 113 delete this.tasks_[message.taskId]; |
| 113 }; | 114 }; |
| 114 | 115 |
| 115 /** | 116 /** |
| 116 * Loads and resizes and image. Use opt_isValid to easily cancel requests | 117 * Loads and resizes and image. Use opt_isValid to easily cancel requests |
| 117 * which are not valid anymore, which will reduce cpu consumption. | 118 * which are not valid anymore, which will reduce cpu consumption. |
| 118 * | 119 * |
| 119 * @param {string} url Url of the requested image. | 120 * @param {string} url Url of the requested image. |
| 120 * @param {function(Object)} callback Callback used to return response. | 121 * @param {function(Object)} callback Callback used to return response. |
|
hirono
2015/02/02 06:56:01
Could you describe about what are stored in Object
yawano
2015/02/02 07:13:30
Done.
| |
| 121 * @param {Object=} opt_options Loader options, such as: scale, maxHeight, | 122 * @param {Object=} opt_options Loader options, such as: scale, maxHeight, |
| 122 * width, height and/or cache. | 123 * width, height and/or cache. |
| 123 * @param {function(): boolean=} opt_isValid Function returning false in case | 124 * @param {function(): boolean=} opt_isValid Function returning false in case |
| 124 * a request is not valid anymore, eg. parent node has been detached. | 125 * a request is not valid anymore, eg. parent node has been detached. |
| 125 * @return {?number} Remote task id or null if loaded from cache. | 126 * @return {?number} Remote task id or null if loaded from cache. |
| 126 */ | 127 */ |
| 127 ImageLoaderClient.prototype.load = function( | 128 ImageLoaderClient.prototype.load = function( |
| 128 url, callback, opt_options, opt_isValid) { | 129 url, callback, opt_options, opt_isValid) { |
| 129 opt_options = /** @type {{cache: (boolean|undefined)}} */(opt_options || {}); | 130 opt_options = /** @type {{cache: (boolean|undefined)}} */(opt_options || {}); |
| 130 opt_isValid = opt_isValid || function() { return true; }; | 131 opt_isValid = opt_isValid || function() { return true; }; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 157 if (cacheKey) { | 158 if (cacheKey) { |
| 158 if (opt_options.cache) { | 159 if (opt_options.cache) { |
| 159 // Load from cache. | 160 // Load from cache. |
| 160 ImageLoaderClient.recordBinary('Cached', true); | 161 ImageLoaderClient.recordBinary('Cached', true); |
| 161 var cachedValue = this.cache_.get(cacheKey); | 162 var cachedValue = this.cache_.get(cacheKey); |
| 162 // Check if the image in cache is up to date. If not, then remove it. | 163 // Check if the image in cache is up to date. If not, then remove it. |
| 163 if (cachedValue && cachedValue.timestamp != opt_options.timestamp) { | 164 if (cachedValue && cachedValue.timestamp != opt_options.timestamp) { |
| 164 this.cache_.remove(cacheKey); | 165 this.cache_.remove(cacheKey); |
| 165 cachedValue = null; | 166 cachedValue = null; |
| 166 } | 167 } |
| 167 if (cachedValue && cachedValue.data) { | 168 if (cachedValue && cachedValue.data && |
| 169 cachedValue.width && cachedValue.height) { | |
| 168 ImageLoaderClient.recordBinary('Cache.HitMiss', true); | 170 ImageLoaderClient.recordBinary('Cache.HitMiss', true); |
| 169 callback({status: 'success', data: cachedValue.data}); | 171 callback({ |
| 172 status: 'success', data: cachedValue.data, | |
| 173 width: cachedValue.width, height: cachedValue.height | |
| 174 }); | |
| 170 return null; | 175 return null; |
| 171 } else { | 176 } else { |
| 172 ImageLoaderClient.recordBinary('Cache.HitMiss', false); | 177 ImageLoaderClient.recordBinary('Cache.HitMiss', false); |
| 173 } | 178 } |
| 174 } else { | 179 } else { |
| 175 // Remove from cache. | 180 // Remove from cache. |
| 176 ImageLoaderClient.recordBinary('Cached', false); | 181 ImageLoaderClient.recordBinary('Cached', false); |
| 177 this.cache_.remove(cacheKey); | 182 this.cache_.remove(cacheKey); |
| 178 } | 183 } |
| 179 } | 184 } |
| 180 | 185 |
| 181 // Not available in cache, performing a request to a remote extension. | 186 // Not available in cache, performing a request to a remote extension. |
| 182 var request = opt_options; | 187 var request = opt_options; |
| 183 this.lastTaskId_++; | 188 this.lastTaskId_++; |
| 184 var task = {isValid: opt_isValid}; | 189 var task = {isValid: opt_isValid}; |
| 185 this.tasks_[this.lastTaskId_] = task; | 190 this.tasks_[this.lastTaskId_] = task; |
| 186 | 191 |
| 187 request.url = url; | 192 request.url = url; |
| 188 request.taskId = this.lastTaskId_; | 193 request.taskId = this.lastTaskId_; |
| 189 request.timestamp = opt_options.timestamp; | 194 request.timestamp = opt_options.timestamp; |
| 190 | 195 |
| 191 ImageLoaderClient.sendMessage_( | 196 ImageLoaderClient.sendMessage_( |
| 192 request, | 197 request, |
| 193 function(result) { | 198 function(result) { |
| 194 // Save to cache. | 199 // Save to cache. |
| 195 if (cacheKey && result.status == 'success' && opt_options.cache) { | 200 if (cacheKey && result.status == 'success' && opt_options.cache) { |
| 196 var value = { | 201 var value = { |
| 197 timestamp: opt_options.timestamp ? opt_options.timestamp : null, | 202 timestamp: opt_options.timestamp ? opt_options.timestamp : null, |
| 198 data: result.data | 203 data: result.data, width: result.width, height: result.height |
| 199 }; | 204 }; |
| 200 this.cache_.put(cacheKey, value, result.data.length); | 205 this.cache_.put(cacheKey, value, result.data.length); |
| 201 } | 206 } |
| 202 callback(result); | 207 callback(result); |
| 203 }.bind(this)); | 208 }.bind(this)); |
| 204 return request.taskId; | 209 return request.taskId; |
| 205 }; | 210 }; |
| 206 | 211 |
| 207 /** | 212 /** |
| 208 * Cancels the request. | 213 * Cancels the request. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 onError(); | 271 onError(); |
| 267 return; | 272 return; |
| 268 } | 273 } |
| 269 image.src = result.data; | 274 image.src = result.data; |
| 270 onSuccess(); | 275 onSuccess(); |
| 271 }; | 276 }; |
| 272 | 277 |
| 273 return ImageLoaderClient.getInstance().load( | 278 return ImageLoaderClient.getInstance().load( |
| 274 url, callback, options, opt_isValid); | 279 url, callback, options, opt_isValid); |
| 275 }; | 280 }; |
| OLD | NEW |