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 * @typedef {{ | 6 * @typedef {{ |
7 * cache: (boolean|undefined), | 7 * cache: (boolean|undefined), |
8 * priority: (number|undefined), | 8 * priority: (number|undefined), |
9 * taskId: number, | 9 * taskId: number, |
10 * timestamp: (number|undefined), | 10 * timestamp: (number|undefined), |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 }; | 111 }; |
112 | 112 |
113 /** | 113 /** |
114 * Tries to load the image from cache if exists and sends the response. | 114 * Tries to load the image from cache if exists and sends the response. |
115 * | 115 * |
116 * @param {function()} onSuccess Success callback. | 116 * @param {function()} onSuccess Success callback. |
117 * @param {function()} onFailure Failure callback. | 117 * @param {function()} onFailure Failure callback. |
118 */ | 118 */ |
119 Request.prototype.loadFromCacheAndProcess = function(onSuccess, onFailure) { | 119 Request.prototype.loadFromCacheAndProcess = function(onSuccess, onFailure) { |
120 this.loadFromCache_( | 120 this.loadFromCache_( |
121 function(data) { // Found in cache. | 121 function(data, width, height) { // Found in cache. |
122 this.sendImageData_(data); | 122 this.sendImageData_(data, width, height); |
123 onSuccess(); | 123 onSuccess(); |
124 }.bind(this), | 124 }.bind(this), |
125 onFailure); // Not found in cache. | 125 onFailure); // Not found in cache. |
126 }; | 126 }; |
127 | 127 |
128 /** | 128 /** |
129 * Tries to download the image, resizes and sends the response. | 129 * Tries to download the image, resizes and sends the response. |
130 * @param {function()} callback Completion callback. | 130 * @param {function()} callback Completion callback. |
131 */ | 131 */ |
132 Request.prototype.downloadAndProcess = function(callback) { | 132 Request.prototype.downloadAndProcess = function(callback) { |
133 if (this.downloadCallback_) | 133 if (this.downloadCallback_) |
134 throw new Error('Downloading already started.'); | 134 throw new Error('Downloading already started.'); |
135 | 135 |
136 this.downloadCallback_ = callback; | 136 this.downloadCallback_ = callback; |
137 this.downloadOriginal_(this.onImageLoad_.bind(this), | 137 this.downloadOriginal_(this.onImageLoad_.bind(this), |
138 this.onImageError_.bind(this)); | 138 this.onImageError_.bind(this)); |
139 }; | 139 }; |
140 | 140 |
141 /** | 141 /** |
142 * Fetches the image from the persistent cache. | 142 * Fetches the image from the persistent cache. |
143 * | 143 * |
144 * @param {function(string)} onSuccess Success callback. | 144 * @param {function(string, number, number)} onSuccess Success callback. |
145 * @param {function()} onFailure Failure callback. | 145 * @param {function()} onFailure Failure callback. |
146 * @private | 146 * @private |
147 */ | 147 */ |
148 Request.prototype.loadFromCache_ = function(onSuccess, onFailure) { | 148 Request.prototype.loadFromCache_ = function(onSuccess, onFailure) { |
149 var cacheKey = Cache.createKey(this.request_); | 149 var cacheKey = Cache.createKey(this.request_); |
150 | 150 |
151 if (!cacheKey) { | 151 if (!cacheKey) { |
152 // Cache key is not provided for the request. | 152 // Cache key is not provided for the request. |
153 onFailure(); | 153 onFailure(); |
154 return; | 154 return; |
(...skipping 16 matching lines...) Expand all Loading... |
171 this.cache_.loadImage(cacheKey, | 171 this.cache_.loadImage(cacheKey, |
172 this.request_.timestamp, | 172 this.request_.timestamp, |
173 onSuccess, | 173 onSuccess, |
174 onFailure); | 174 onFailure); |
175 }; | 175 }; |
176 | 176 |
177 /** | 177 /** |
178 * Saves the image to the persistent cache. | 178 * Saves the image to the persistent cache. |
179 * | 179 * |
180 * @param {string} data The image's data. | 180 * @param {string} data The image's data. |
| 181 * @param {number} width Image width. |
| 182 * @param {number} height Image height. |
181 * @private | 183 * @private |
182 */ | 184 */ |
183 Request.prototype.saveToCache_ = function(data) { | 185 Request.prototype.saveToCache_ = function(data, width, height) { |
184 if (!this.request_.cache || !this.request_.timestamp) { | 186 if (!this.request_.cache || !this.request_.timestamp) { |
185 // Persistent cache is available only when a timestamp is provided. | 187 // Persistent cache is available only when a timestamp is provided. |
186 return; | 188 return; |
187 } | 189 } |
188 | 190 |
189 var cacheKey = Cache.createKey(this.request_); | 191 var cacheKey = Cache.createKey(this.request_); |
190 if (!cacheKey) { | 192 if (!cacheKey) { |
191 // Cache key is not provided for the request. | 193 // Cache key is not provided for the request. |
192 return; | 194 return; |
193 } | 195 } |
194 | 196 |
195 this.cache_.saveImage(cacheKey, | 197 this.cache_.saveImage(cacheKey, |
196 data, | 198 data, |
| 199 width, |
| 200 height, |
197 this.request_.timestamp); | 201 this.request_.timestamp); |
198 }; | 202 }; |
199 | 203 |
200 /** | 204 /** |
201 * Downloads an image directly or for remote resources using the XmlHttpRequest. | 205 * Downloads an image directly or for remote resources using the XmlHttpRequest. |
202 * | 206 * |
203 * @param {function()} onSuccess Success callback. | 207 * @param {function()} onSuccess Success callback. |
204 * @param {function()} onFailure Failure callback. | 208 * @param {function()} onFailure Failure callback. |
205 * @private | 209 * @private |
206 */ | 210 */ |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 361 |
358 /** | 362 /** |
359 * Sends the resized image via the callback. If the image has been changed, | 363 * Sends the resized image via the callback. If the image has been changed, |
360 * then packs the canvas contents, otherwise sends the raw image data. | 364 * then packs the canvas contents, otherwise sends the raw image data. |
361 * | 365 * |
362 * @param {boolean} imageChanged Whether the image has been changed. | 366 * @param {boolean} imageChanged Whether the image has been changed. |
363 * @private | 367 * @private |
364 */ | 368 */ |
365 Request.prototype.sendImage_ = function(imageChanged) { | 369 Request.prototype.sendImage_ = function(imageChanged) { |
366 var imageData; | 370 var imageData; |
| 371 var width; |
| 372 var height; |
367 if (!imageChanged) { | 373 if (!imageChanged) { |
368 // The image hasn't been processed, so the raw data can be directly | 374 // The image hasn't been processed, so the raw data can be directly |
369 // forwarded for speed (no need to encode the image again). | 375 // forwarded for speed (no need to encode the image again). |
370 imageData = this.image_.src; | 376 imageData = this.image_.src; |
| 377 width = this.image_.width; |
| 378 height = this.image_.height; |
371 } else { | 379 } else { |
372 // The image has been resized or rotated, therefore the canvas has to be | 380 // The image has been resized or rotated, therefore the canvas has to be |
373 // encoded to get the correct compressed image data. | 381 // encoded to get the correct compressed image data. |
| 382 width = this.canvas_.width; |
| 383 height = this.canvas_.height; |
| 384 |
374 switch (this.contentType_) { | 385 switch (this.contentType_) { |
375 case 'image/gif': | 386 case 'image/gif': |
376 case 'image/png': | 387 case 'image/png': |
377 case 'image/svg': | 388 case 'image/svg': |
378 case 'image/bmp': | 389 case 'image/bmp': |
379 imageData = this.canvas_.toDataURL('image/png'); | 390 imageData = this.canvas_.toDataURL('image/png'); |
380 break; | 391 break; |
381 case 'image/jpeg': | 392 case 'image/jpeg': |
382 default: | 393 default: |
383 imageData = this.canvas_.toDataURL('image/jpeg', 0.9); | 394 imageData = this.canvas_.toDataURL('image/jpeg', 0.9); |
384 } | 395 } |
385 } | 396 } |
386 | 397 |
387 // Send and store in the persistent cache. | 398 // Send and store in the persistent cache. |
388 this.sendImageData_(imageData); | 399 this.sendImageData_(imageData, width, height); |
389 this.saveToCache_(imageData); | 400 this.saveToCache_(imageData, width, height); |
390 }; | 401 }; |
391 | 402 |
392 /** | 403 /** |
393 * Sends the resized image via the callback. | 404 * Sends the resized image via the callback. |
394 * @param {string} data Compressed image data. | 405 * @param {string} data Compressed image data. |
| 406 * @param {number} width Width. |
| 407 * @param {number} height Height. |
395 * @private | 408 * @private |
396 */ | 409 */ |
397 Request.prototype.sendImageData_ = function(data) { | 410 Request.prototype.sendImageData_ = function(data, width, height) { |
398 this.sendResponse_( | 411 this.sendResponse_({ |
399 {status: 'success', data: data, taskId: this.request_.taskId}); | 412 status: 'success', data: data, width: width, height: height, |
| 413 taskId: this.request_.taskId |
| 414 }); |
400 }; | 415 }; |
401 | 416 |
402 /** | 417 /** |
403 * Handler, when contents are loaded into the image element. Performs resizing | 418 * Handler, when contents are loaded into the image element. Performs resizing |
404 * and finalizes the request process. | 419 * and finalizes the request process. |
405 * @private | 420 * @private |
406 */ | 421 */ |
407 Request.prototype.onImageLoad_ = function() { | 422 Request.prototype.onImageLoad_ = function() { |
408 // Perform processing if the url is not a data url, or if there are some | 423 // Perform processing if the url is not a data url, or if there are some |
409 // operations requested. | 424 // operations requested. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + | 470 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + |
456 'ABAAEAAAICTAEAOw=='; | 471 'ABAAEAAAICTAEAOw=='; |
457 | 472 |
458 this.xhr_.onload = function() {}; | 473 this.xhr_.onload = function() {}; |
459 this.xhr_.abort(); | 474 this.xhr_.abort(); |
460 | 475 |
461 // Dispose memory allocated by Canvas. | 476 // Dispose memory allocated by Canvas. |
462 this.canvas_.width = 0; | 477 this.canvas_.width = 0; |
463 this.canvas_.height = 0; | 478 this.canvas_.height = 0; |
464 }; | 479 }; |
OLD | NEW |