OLD | NEW |
| (Empty) |
1 // Copyright 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 name: '', | |
46 date: undefined | |
47 }; | |
48 this.loadStats_ = []; | |
49 }; | |
50 | |
51 WallpaperLoader.prototype = { | |
52 // When moving through users quickly at login screen, set a timeout to | |
53 // prevent loading intermediate wallpapers. | |
54 loadWallpaperTimeout_: null, | |
55 | |
56 // When waiting for wallpaper load, remember load start time. | |
57 // wallpaperLoadInProgress_: { name: '', date: undefined } | |
58 wallpaperLoadInProgress_: undefined, | |
59 | |
60 // Wait a delay and then load this wallpaper. Value = username. | |
61 wallpaperLoadPending_: undefined, | |
62 | |
63 // Wait untill this Date before loading next wallpaper. | |
64 wallpaperLoadTryNextAfter_: undefined, | |
65 | |
66 // Username, owner of current wallpaper. | |
67 currentWallpaper_: '', | |
68 | |
69 // Array of times (in milliseconds) of last wallpaper load attempts. | |
70 // Length is limited by WALLPAPER_LOAD_STATS_MAX_LENGTH. | |
71 loadStats_: undefined, | |
72 | |
73 /** | |
74 * Stop load timer. Clear pending record. | |
75 */ | |
76 reset: function() { | |
77 delete this.wallpaperLoadPending_; | |
78 if (this.loadWallpaperTimeout_ != null) | |
79 window.clearTimeout(this.loadWallpaperTimeout_); | |
80 this.loadWallpaperTimeout_ = null; | |
81 }, | |
82 | |
83 /** | |
84 * Schedules wallpaper load. | |
85 */ | |
86 scheduleLoad: function(email) { | |
87 if (this.wallpaperLoadPending_ && this.wallpaperLoadPending_ == email) | |
88 return; | |
89 this.reset(); | |
90 if (this.wallpaperLoadInProgress_.name == email) | |
91 return; | |
92 if ((this.wallpaperLoadInProgress_.name == '') && | |
93 (this.currentWallpaper_ == email)) | |
94 return; | |
95 | |
96 this.wallpaperLoadPending_ = email; | |
97 var now = new Date(); | |
98 var timeout = WALLPAPER_LOAD_MIN_DELAY_MS; | |
99 if (this.wallpaperLoadTryNextAfter_) | |
100 timeout = Math.max(timeout, this.wallpaperLoadTryNextAfter_ - now); | |
101 | |
102 this.loadWallpaperTimeout_ = window.setTimeout( | |
103 this.loadWallpaper_.bind(this), timeout); | |
104 }, | |
105 | |
106 | |
107 /** | |
108 * Loads pending wallpaper, if any. | |
109 * @private | |
110 */ | |
111 loadWallpaper_: function() { | |
112 this.loadWallpaperTimeout_ = null; | |
113 if (!this.wallpaperLoadPending_) | |
114 return; | |
115 if (this.wallpaperLoadInProgress_.name != '') | |
116 return; | |
117 var email = this.wallpaperLoadPending_; | |
118 delete this.wallpaperLoadPending_; | |
119 if (email == this.currentWallpaper_) | |
120 return; | |
121 this.wallpaperLoadInProgress_.name = email; | |
122 this.wallpaperLoadInProgress_.date = new Date(); | |
123 chrome.send('loadWallpaper', [email]); | |
124 }, | |
125 | |
126 /** | |
127 * Calculates average wallpaper load time. | |
128 */ | |
129 calcLoadStatsAvg: function() { | |
130 return this.loadStats_.reduce( | |
131 function(previousValue, currentValue) { | |
132 return previousValue + currentValue; | |
133 }) / this.loadStats_.length; | |
134 }, | |
135 | |
136 /** | |
137 * Calculates average next wallpaper load delay time. | |
138 */ | |
139 getWallpaperLoadTime_: function() { | |
140 var avg = WALLPAPER_DEFAULT_LOAD_TIME_MS; | |
141 | |
142 if (this.loadStats_.length == 0) | |
143 return avg; | |
144 | |
145 avg = this.calcLoadStatsAvg(); | |
146 if (avg < WALLPAPER_MIN_LOAD_TIME_MS) | |
147 avg = WALLPAPER_MIN_LOAD_TIME_MS; | |
148 | |
149 if (avg > WALLPAPER_MAX_LOAD_TIME_MS) | |
150 avg = WALLPAPER_MAX_LOAD_TIME_MS; | |
151 | |
152 return avg; | |
153 }, | |
154 | |
155 /** | |
156 * Handles 'onWallpaperLoaded' event. Recalculates statistics and | |
157 * [re]schedules next wallpaper load. | |
158 */ | |
159 onWallpaperLoaded: function(email) { | |
160 this.currentWallpaper_ = email; | |
161 if (email != this.wallpaperLoadInProgress_.name) | |
162 return; | |
163 this.wallpaperLoadInProgress_.name = ''; | |
164 var started = this.wallpaperLoadInProgress_.date; | |
165 var finished = new Date(); | |
166 var elapsed = started ? finished - started : | |
167 WALLPAPER_DEFAULT_LOAD_TIME_MS; | |
168 this.loadStats_.push(elapsed); | |
169 if (this.loadStats_.length > WALLPAPER_LOAD_STATS_MAX_LENGTH) | |
170 this.loadStats_.shift(); | |
171 | |
172 this.wallpaperLoadTryNextAfter_ = new Date(Date.now() + 2 * | |
173 this.getWallpaperLoadTime_()); | |
174 if (this.wallpaperLoadPending_) { | |
175 var newWallpaperEmail = this.wallpaperLoadPending_; | |
176 this.reset(); | |
177 this.scheduleLoad(newWallpaperEmail); | |
178 } | |
179 } | |
180 }; | |
181 | |
182 return { | |
183 WallpaperLoader: WallpaperLoader | |
184 }; | |
185 }); | |
OLD | NEW |