| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('downloads', function() { |
| 6 // TODO(dbeam): does this need to be a function + @constructor? |
| 7 var IconLoader = {}; |
| 8 |
| 9 /** @private {Array<{img: HTMLImageElement, url: string}>} */ |
| 10 IconLoader.iconsToLoad_ = []; |
| 11 |
| 12 /** |
| 13 * Load the provided |url| into |img.src| after appending ?scale=. |
| 14 * @param {!HTMLImageElement} img An <img> to show the loaded image in. |
| 15 * @param {string} url A remote image URL to load. |
| 16 */ |
| 17 IconLoader.loadScaledIcon = function(img, url) { |
| 18 var scale = '?scale=' + window.devicePixelRatio + 'x'; |
| 19 IconLoader.iconsToLoad_.push({img: img, url: url + scale}); |
| 20 IconLoader.loadNextIcon_(); |
| 21 }; |
| 22 |
| 23 /** @private */ |
| 24 IconLoader.loadNextIcon_ = function() { |
| 25 if (IconLoader.isIconLoading_) |
| 26 return; |
| 27 |
| 28 IconLoader.isIconLoading_ = true; |
| 29 |
| 30 while (IconLoader.iconsToLoad_.length) { |
| 31 var request = IconLoader.iconsToLoad_.shift(); |
| 32 var img = request.img; |
| 33 |
| 34 if (img.src == request.url) |
| 35 continue; |
| 36 |
| 37 img.onabort = img.onerror = img.onload = function() { |
| 38 IconLoader.isIconLoading_ = false; |
| 39 IconLoader.loadNextIcon_(); |
| 40 }; |
| 41 |
| 42 img.src = request.url; |
| 43 return; |
| 44 } |
| 45 |
| 46 // If we reached here, there's no more work to do. |
| 47 IconLoader.isIconLoading_ = false; |
| 48 }; |
| 49 |
| 50 return {IconLoader: IconLoader}; |
| 51 }); |
| OLD | NEW |