OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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('login', function() { | |
6 | |
7 /** | |
8 * Minimum wallpaper load delay in milliseconds. | |
9 * @type {number} | |
10 * @const | |
11 */ | |
12 var WALLPAPER_LOAD_MIN_DELAY_MS = 100; | |
13 | |
14 /** | |
15 * If last walpaper load time cannot be calculated, assume this value. | |
16 * @type {number} | |
17 * @const | |
18 */ | |
19 var WALLPAPER_DEFAULT_LOAD_TIME_MS = 200; | |
20 | |
21 /** | |
22 * Min and Max average wallpaper load time. | |
23 * Delay to next wallpaper load is 2 * <average load time>. | |
24 * @type {number} | |
25 * @const | |
26 */ | |
27 var WALLPAPER_MIN_LOAD_TIME_MS = 50; | |
28 var WALLPAPER_MAX_LOAD_TIME_MS = 2000; | |
29 | |
30 /** | |
31 * Number last wallpaper load times to remember. | |
32 * @type {number} | |
33 * @const | |
34 */ | |
35 var WALLPAPER_LOAD_STATS_MAX_LENGTH = 4; | |
36 | |
37 | |
38 /** | |
39 * Creates a new pod row element. | |
40 * @constructor | |
41 * @extends {HTMLDivElement} | |
42 */ | |
43 var WallpaperLoader = function() { | |
44 this.wallpaperLoadInProgress_ = {}; | |
45 this.wallpaperLoadStats_ = []; | |
46 }; | |
47 | |
48 WallpaperLoader.prototype = { | |
49 // When moving through users quickly at login screen, set a timeout to | |
50 // prevent loading intermediate wallpapers. | |
51 loadWallpaperTimeout_: null, | |
52 | |
53 // When waiting for wallpaper load, remember load start time. | |
54 // This is actually associative array with key = username and value = | |
55 // start time. Associative array. | |
56 wallpaperLoadInProgress_: null, | |
dzhioev (left Google)
2013/10/17 12:47:49
We load only one wallpaper at time, so such dictio
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
57 | |
58 // Wait a delay and then load this wallpaper. Value = username. | |
59 wallpaperLoadPending_: undefined, | |
60 | |
61 // Wait untill this Date before loading next wallpaper. | |
62 wallpaperLoadTryNextAfter_: undefined, | |
63 | |
64 // Username, owner of current wallpaper. | |
65 wallpaperCurrent_: '', | |
dzhioev (left Google)
2013/10/17 12:47:49
currentWallpaper_
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
66 | |
67 // Array of times (in milliseconds) of last wallpaper load attempts. | |
68 // Length is limited by WALLPAPER_LOAD_STATS_MAX_LENGTH. | |
69 wallpaperLoadStats_: undefined, | |
dzhioev (left Google)
2013/10/17 12:47:49
Just loadStats_
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
70 | |
71 reset: function() { | |
dzhioev (left Google)
2013/10/17 12:47:49
You can delete 'wallpaperLoadPending_' in this met
dzhioev (left Google)
2013/10/17 12:47:49
Please add comment for method.
Alexander Alekseev
2013/10/18 17:01:52
Done.
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
72 if (this.loadWallpaperTimeout_) | |
dzhioev (left Google)
2013/10/17 12:47:49
I think setTimeout can return 0 as a valid ID. So
Alexander Alekseev
2013/10/18 17:01:52
probably if (loadWallpaperTimeout_ != null) ?
| |
73 clearTimeout(this.loadWallpaperTimeout_); | |
dzhioev (left Google)
2013/10/17 12:47:49
window.clearTimeout
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
74 this.loadWallpaperTimeout_ = null; | |
75 }, | |
76 | |
77 /** | |
78 * Schedules wallpaper load. | |
79 */ | |
80 scheduleLoad: function(email) { | |
81 if (this.wallpaperLoadPending_ && this.wallpaperLoadPending_ == email) | |
82 return; | |
83 delete this.wallpaperLoadPending_; | |
84 this.reset(); | |
85 if (this.wallpaperLoadInProgress_.hasOwnProperty(email)) | |
86 return; | |
87 if ((Object.getOwnPropertyNames(this.wallpaperLoadInProgress_).length == | |
88 0) && (this.wallpaperCurrent_ == email)) { | |
89 return; | |
90 } | |
91 this.wallpaperLoadPending_ = email; | |
92 var now = new Date(); | |
93 var timeout = WALLPAPER_LOAD_MIN_DELAY_MS; | |
94 if (this.wallpaperLoadTryNextAfter_ && | |
dzhioev (left Google)
2013/10/17 12:47:49
I think this if-block can be simplified to:
if (th
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
95 (now < this.wallpaperLoadTryNextAfter_) && | |
96 (this.wallpaperLoadTryNextAfter_.getTime() - | |
97 now.getTime() > WALLPAPER_LOAD_MIN_DELAY_MS)) | |
98 timeout = this.wallpaperLoadTryNextAfter_.getTime() - | |
dzhioev (left Google)
2013/10/17 12:47:49
You can drop getTime() in arithmetic calculations
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
99 now.getTime(); | |
100 | |
101 this.loadWallpaperTimeout_ = window.setTimeout( | |
102 this.loadWallpaper_.bind(this), timeout); | |
103 }, | |
104 | |
105 | |
106 /** | |
107 * Loads pending wallpaper, if any. | |
108 * @private | |
109 */ | |
110 loadWallpaper_: function() { | |
111 this.loadWallpaperTimeout_ = null; | |
112 if (!this.wallpaperLoadPending_) | |
113 return; | |
114 if (Object.getOwnPropertyNames(this.wallpaperLoadInProgress_).length != 0) | |
115 return; | |
116 var email = this.wallpaperLoadPending_; | |
117 delete this.wallpaperLoadPending_; | |
118 if (email == this.wallpaperCurrent_) | |
119 return; | |
120 this.wallpaperLoadInProgress_[email] = new Date(); | |
121 chrome.send('loadWallpaper', [email]); | |
122 }, | |
123 | |
124 /** | |
125 * Calculates average wallpaper load time. | |
126 */ | |
127 getWallpaperLoadDelay_: function() { | |
128 var avg = WALLPAPER_DEFAULT_LOAD_TIME_MS; | |
129 if (this.wallpaperLoadStats_.length) { | |
130 avg = this.wallpaperLoadStats_.reduce( | |
dzhioev (left Google)
2013/10/17 12:47:49
Maybe it should be moved to separate method.
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
131 function(previousValue, currentValue, index, array) { | |
dzhioev (left Google)
2013/10/17 12:47:49
You can drop 'index' and 'array'.
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
132 return previousValue + currentValue; | |
133 }) / this.wallpaperLoadStats_.length; | |
134 if (avg < WALLPAPER_MIN_LOAD_TIME_MS) { | |
135 avg = WALLPAPER_MIN_LOAD_TIME_MS; | |
136 } | |
137 if (avg > WALLPAPER_MAX_LOAD_TIME_MS) { | |
138 avg = WALLPAPER_MAX_LOAD_TIME_MS; | |
139 } | |
dzhioev (left Google)
2013/10/17 12:47:49
Brackets not needed for one line body.
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
140 } | |
141 return avg; | |
142 }, | |
143 | |
144 /** | |
145 * Handles 'onWallpaperLoaded' event. Recalculates statistics and | |
146 * [re]schedules next wallpaper load. | |
147 */ | |
148 onWallpaperLoaded: function(email) { | |
149 this.wallpaperCurrent_ = email; | |
150 var started = this.wallpaperLoadInProgress_[email]; | |
151 delete this.wallpaperLoadInProgress_[email]; | |
152 var finished = new Date(); | |
153 var elapsed = started ? finished.getTime() - started.getTime() : | |
154 WALLPAPER_DEFAULT_LOAD_TIME_MS; | |
155 this.wallpaperLoadStats_.push(elapsed); | |
156 if (this.wallpaperLoadStats_.length > | |
157 WALLPAPER_LOAD_STATS_MAX_LENGTH) | |
dzhioev (left Google)
2013/10/17 12:47:49
No need to split condition that fits in one line.
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
158 this.wallpaperLoadStats_.shift(); | |
159 | |
160 var nextAfter = new Date(); | |
161 nextAfter.setTime(nextAfter.getTime() + | |
162 2 * this.getWallpaperLoadDelay_()); | |
163 this.wallpaperLoadTryNextAfter_ = nextAfter; | |
dzhioev (left Google)
2013/10/17 12:47:49
I think we delay should be multiplied by 2 inside
Alexander Alekseev
2013/10/18 17:01:52
Done.
I've renamed getWallpaperLoadDelay_ to getW
| |
164 if (this.wallpaperLoadPending_) { | |
165 var newWallpaperEmail = this.wallpaperLoadPending_; | |
166 delete this.wallpaperLoadPending_; | |
167 if (newWallpaperEmail == email) { | |
dzhioev (left Google)
2013/10/17 12:47:49
How this can happen? I thought it's already checke
Alexander Alekseev
2013/10/18 17:01:52
Done.
| |
168 this.reset(); | |
169 } else { | |
170 this.scheduleLoad(newWallpaperEmail); | |
171 } | |
172 } | |
173 } | |
174 }; | |
175 | |
176 return { | |
177 WallpaperLoader: WallpaperLoader | |
178 }; | |
179 }); | |
OLD | NEW |