Chromium Code Reviews| 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 | 7 |
| 8 // Namespace object for the utilities. | 8 // Namespace object for the utilities. |
| 9 function ImageUtil() {} | 9 function ImageUtil() {} |
| 10 | 10 |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 transformFetcher(url, onTransform.bind(this, e.target)); | 475 transformFetcher(url, onTransform.bind(this, e.target)); |
| 476 }.bind(this); | 476 }.bind(this); |
| 477 // errorCallback has an optional error argument, which in case of general | 477 // errorCallback has an optional error argument, which in case of general |
| 478 // error should not be specified | 478 // error should not be specified |
| 479 this.image_.onerror = onError.bind(this, 'IMAGE_ERROR'); | 479 this.image_.onerror = onError.bind(this, 'IMAGE_ERROR'); |
| 480 this.taskId_ = util.loadImage(this.image_, url, {priority: 1}); | 480 this.taskId_ = util.loadImage(this.image_, url, {priority: 1}); |
| 481 }.bind(this); | 481 }.bind(this); |
| 482 | 482 |
| 483 // The clients of this class sometimes request the same url repeatedly. | 483 // The clients of this class sometimes request the same url repeatedly. |
| 484 // The onload fires only if the src is different from the previous value. | 484 // The onload fires only if the src is different from the previous value. |
| 485 // To work around that we reset the src temporarily to an 1x1 pixel. | 485 // To work around that we reset the src temporarily to an 1x1 pixel to force |
| 486 // Load an empty 1x1 pixel image first. | 486 // garbage collecting, and then resetting src to an empty value. |
| 487 var resetImage = function(callback) { | 487 var resetImage = function(callback) { |
| 488 this.image_.onload = callback; | 488 var clearSrc = function() { |
| 489 this.image_.onerror = onError.bind(this, 'IMAGE_ERROR'); | 489 this.image_.onload = callback; |
| 490 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAA' + | 490 this.image_.onerror = callback; |
| 491 this.image_.src = ''; | |
| 492 }.bind(this); | |
| 493 var emptyImage = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAA' + | |
| 491 'AAABAAEAAAICTAEAOw=='; | 494 'AAABAAEAAAICTAEAOw=='; |
| 495 if (this.image_.src != emptyImage) { | |
| 496 // Load an empty image, then clear src. | |
| 497 this.image_.onload = clearSrc; | |
| 498 this.image_.onerror = onError.bind(this, 'IMAGE_ERROR'); | |
| 499 this.image_.src = emptyImage; | |
| 500 } else { | |
| 501 // Empty image already loaded, so clear src immediately. | |
| 502 clearSrc(); | |
| 503 } | |
| 492 }.bind(this); | 504 }.bind(this); |
| 493 | 505 |
| 494 // Loads the image. If already loaded, then forces reloads. | 506 // Loads the image. If already loaded, then forces a reload. |
| 495 var startLoad = function() { | 507 var startLoad = function() { |
|
yoshiki
2013/06/11 06:45:39
nit:
var startLoad = resetImage.bind(null, loadIma
mtomasz
2013/06/11 08:14:13
Done.
| |
| 496 if (this.image_.src == url) | 508 resetImage(loadImage); |
| 497 resetImage(loadImage); | |
| 498 else | |
| 499 loadImage(); | |
| 500 }.bind(this); | 509 }.bind(this); |
| 501 | 510 |
| 502 if (opt_delay) { | 511 if (opt_delay) { |
| 503 this.timeout_ = setTimeout(startLoad, opt_delay); | 512 this.timeout_ = setTimeout(startLoad, opt_delay); |
| 504 } else { | 513 } else { |
| 505 startLoad(); | 514 startLoad(); |
| 506 } | 515 } |
| 507 }; | 516 }; |
| 508 | 517 |
| 509 /** | 518 /** |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 689 * @return {string} Full name. | 698 * @return {string} Full name. |
| 690 */ | 699 */ |
| 691 ImageUtil.getMetricName = function(name) { | 700 ImageUtil.getMetricName = function(name) { |
| 692 return 'PhotoEditor.' + name; | 701 return 'PhotoEditor.' + name; |
| 693 }; | 702 }; |
| 694 | 703 |
| 695 /** | 704 /** |
| 696 * Used for metrics reporting, keep in sync with the histogram description. | 705 * Used for metrics reporting, keep in sync with the histogram description. |
| 697 */ | 706 */ |
| 698 ImageUtil.FILE_TYPES = ['jpg', 'png', 'gif', 'bmp', 'webp']; | 707 ImageUtil.FILE_TYPES = ['jpg', 'png', 'gif', 'bmp', 'webp']; |
| OLD | NEW |