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.
5 cr.define('login', function() {
8 * Minimum wallpaper load delay in milliseconds.
12 var WALLPAPER_LOAD_MIN_DELAY_MS = 100;
15 * If last walpaper load time cannot be calculated, assume this value.
19 var WALLPAPER_DEFAULT_LOAD_TIME_MS = 200;
22 * Min and Max average wallpaper load time.
23 * Delay to next wallpaper load is 2 * <average load time>.
27 var WALLPAPER_MIN_LOAD_TIME_MS = 50;
28 var WALLPAPER_MAX_LOAD_TIME_MS = 2000;
31 * Number last wallpaper load times to remember.
35 var WALLPAPER_LOAD_STATS_MAX_LENGTH = 4;
39 * Creates a new pod row element.
41 * @extends {HTMLDivElement}
43 var WallpaperLoader = function() {
44 this.wallpaperLoadInProgress_ = {
51 WallpaperLoader.prototype = {
52 // When moving through users quickly at login screen, set a timeout to
53 // prevent loading intermediate wallpapers.
54 loadWallpaperTimeout_: null,
56 // When waiting for wallpaper load, remember load start time.
57 // wallpaperLoadInProgress_: { name: '', date: undefined }
58 wallpaperLoadInProgress_: undefined,
60 // Wait a delay and then load this wallpaper. Value = username.
61 wallpaperLoadPending_: undefined,
63 // Wait untill this Date before loading next wallpaper.
64 wallpaperLoadTryNextAfter_: undefined,
66 // Username, owner of current wallpaper.
67 currentWallpaper_: '',
69 // Array of times (in milliseconds) of last wallpaper load attempts.
70 // Length is limited by WALLPAPER_LOAD_STATS_MAX_LENGTH.
71 loadStats_: undefined,
74 * Stop load timer. Clear pending record.
77 delete this.wallpaperLoadPending_;
78 if (this.loadWallpaperTimeout_ != null)
79 window.clearTimeout(this.loadWallpaperTimeout_);
80 this.loadWallpaperTimeout_ = null;
84 * Schedules wallpaper load.
86 scheduleLoad: function(email) {
87 if (this.wallpaperLoadPending_ && this.wallpaperLoadPending_ == email)
90 if (this.wallpaperLoadInProgress_.name == email)
92 if ((this.wallpaperLoadInProgress_.name == '') &&
93 (this.currentWallpaper_ == email))
96 this.wallpaperLoadPending_ = email;
98 var timeout = WALLPAPER_LOAD_MIN_DELAY_MS;
99 if (this.wallpaperLoadTryNextAfter_)
100 timeout = Math.max(timeout, this.wallpaperLoadTryNextAfter_ - now);
102 this.loadWallpaperTimeout_ = window.setTimeout(
103 this.loadWallpaper_.bind(this), timeout);
108 * Loads pending wallpaper, if any.
111 loadWallpaper_: function() {
112 this.loadWallpaperTimeout_ = null;
113 if (!this.wallpaperLoadPending_)
115 if (this.wallpaperLoadInProgress_.name != '')
117 var email = this.wallpaperLoadPending_;
118 delete this.wallpaperLoadPending_;
119 if (email == this.currentWallpaper_)
121 this.wallpaperLoadInProgress_.name = email;
122 this.wallpaperLoadInProgress_.date = new Date();
123 chrome.send('loadWallpaper', [email]);
127 * Calculates average wallpaper load time.
129 calcLoadStatsAvg: function() {
130 return this.loadStats_.reduce(
131 function(previousValue, currentValue) {
132 return previousValue + currentValue;
133 }) / this.loadStats_.length;
137 * Calculates average next wallpaper load delay time.
139 getWallpaperLoadTime_: function() {
140 var avg = WALLPAPER_DEFAULT_LOAD_TIME_MS;
142 if (this.loadStats_.length == 0)
145 avg = this.calcLoadStatsAvg();
146 if (avg < WALLPAPER_MIN_LOAD_TIME_MS)
147 avg = WALLPAPER_MIN_LOAD_TIME_MS;
149 if (avg > WALLPAPER_MAX_LOAD_TIME_MS)
150 avg = WALLPAPER_MAX_LOAD_TIME_MS;
156 * Handles 'onWallpaperLoaded' event. Recalculates statistics and
157 * [re]schedules next wallpaper load.
159 onWallpaperLoaded: function(email) {
160 this.currentWallpaper_ = email;
161 if (email != this.wallpaperLoadInProgress_.name)
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();
172 this.wallpaperLoadTryNextAfter_ = new Date(Date.now() + 2 *
173 this.getWallpaperLoadTime_());
174 if (this.wallpaperLoadPending_) {
175 var newWallpaperEmail = this.wallpaperLoadPending_;
177 this.scheduleLoad(newWallpaperEmail);
183 WallpaperLoader: WallpaperLoader