[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / resources / downloads / throttled_icon_loader.js
blob5a10024e480980df95bd7c7be5dd1b833f845c45
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}} */
6 var LoadIconRequest;
8 cr.define('downloads', function() {
9 /**
10 * @param {number} maxAllowed The maximum number of simultaneous downloads
11 * allowed.
12 * @constructor
14 function ThrottledIconLoader(maxAllowed) {
15 assert(maxAllowed > 0);
17 /** @private {number} */
18 this.maxAllowed_ = maxAllowed;
20 /** @private {!Array<!LoadIconRequest>} */
21 this.requests_ = [];
24 ThrottledIconLoader.prototype = {
25 /** @private {number} */
26 loading_: 0,
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.
33 loadScaledIcon: function(img, url) {
34 var scaledUrl = url + '?scale=' + window.devicePixelRatio + 'x';
35 if (img.src == scaledUrl)
36 return;
38 this.requests_.push({img: img, url: scaledUrl});
39 this.loadNextIcon_();
42 /** @private */
43 loadNextIcon_: function() {
44 if (this.loading_ > this.maxAllowed_ || !this.requests_.length)
45 return;
47 var request = this.requests_.shift();
48 var img = request.img;
50 img.onabort = img.onerror = img.onload = function() {
51 this.loading_--;
52 this.loadNextIcon_();
53 }.bind(this);
55 this.loading_++;
56 img.src = request.url;
60 return {ThrottledIconLoader: ThrottledIconLoader};
61 });