| 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 /** @typedef {{img: HTMLImageElement, url: string}} */ | |
| 6 var LoadIconRequest; | |
| 7 | |
| 8 cr.define('downloads', function() { | |
| 9 /** | |
| 10 * @param {number} maxAllowed The maximum number of simultaneous downloads | |
| 11 * allowed. | |
| 12 * @constructor | |
| 13 */ | |
| 14 function ThrottledIconLoader(maxAllowed) { | |
| 15 assert(maxAllowed > 0); | |
| 16 | |
| 17 /** @private {number} */ | |
| 18 this.maxAllowed_ = maxAllowed; | |
| 19 | |
| 20 /** @private {!Array<!LoadIconRequest>} */ | |
| 21 this.requests_ = []; | |
| 22 } | |
| 23 | |
| 24 ThrottledIconLoader.prototype = { | |
| 25 /** @private {number} */ | |
| 26 loading_: 0, | |
| 27 | |
| 28 /** | |
| 29 * Load the provided |url| into |img.src| after appending ?scale=. | |
| 30 * @param {!HTMLImageElement} img An <img> to show the loaded image in. | |
| 31 * @param {string} url A remote image URL to load. | |
| 32 */ | |
| 33 loadScaledIcon: function(img, url) { | |
| 34 var scaledUrl = url + '?scale=' + window.devicePixelRatio + 'x'; | |
| 35 if (img.src == scaledUrl) | |
| 36 return; | |
| 37 | |
| 38 this.requests_.push({img: img, url: scaledUrl}); | |
| 39 this.loadNextIcon_(); | |
| 40 }, | |
| 41 | |
| 42 /** @private */ | |
| 43 loadNextIcon_: function() { | |
| 44 if (this.loading_ > this.maxAllowed_ || !this.requests_.length) | |
| 45 return; | |
| 46 | |
| 47 var request = this.requests_.shift(); | |
| 48 var img = request.img; | |
| 49 | |
| 50 img.onabort = img.onerror = img.onload = function() { | |
| 51 this.loading_--; | |
| 52 this.loadNextIcon_(); | |
| 53 }.bind(this); | |
| 54 | |
| 55 this.loading_++; | |
| 56 img.src = request.url; | |
| 57 }, | |
| 58 }; | |
| 59 | |
| 60 return {ThrottledIconLoader: ThrottledIconLoader}; | |
| 61 }); | |
| OLD | NEW |