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.
5 /** @typedef {{img: HTMLImageElement, url: string}} */
8 cr.define('downloads', function() {
10 * @param {number} maxAllowed The maximum number of simultaneous downloads
14 function ThrottledIconLoader(maxAllowed) {
15 assert(maxAllowed > 0);
17 /** @private {number} */
18 this.maxAllowed_ = maxAllowed;
20 /** @private {!Array<!LoadIconRequest>} */
24 ThrottledIconLoader.prototype = {
25 /** @private {number} */
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.
33 loadScaledIcon: function(img, url) {
34 var scaledUrl = url + '?scale=' + window.devicePixelRatio + 'x';
35 if (img.src == scaledUrl)
38 this.requests_.push({img: img, url: scaledUrl});
43 loadNextIcon_: function() {
44 if (this.loading_ > this.maxAllowed_ || !this.requests_.length)
47 var request = this.requests_.shift();
48 var img = request.img;
50 img.onabort = img.onerror = img.onload = function() {
56 img.src = request.url;
60 return {ThrottledIconLoader: ThrottledIconLoader};