Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3037)

Unified Diff: chrome/browser/resources/chromeos/login/wallpaper_loader.js

Issue 24625003: Delay wallpaper load by 2 * average wallpaper load time. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move WallpaperLoader to separate object, simplify. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/chromeos/login/wallpaper_loader.js
diff --git a/chrome/browser/resources/chromeos/login/wallpaper_loader.js b/chrome/browser/resources/chromeos/login/wallpaper_loader.js
new file mode 100644
index 0000000000000000000000000000000000000000..60b85c97c2ac71b633444bdf76ec855e053572e1
--- /dev/null
+++ b/chrome/browser/resources/chromeos/login/wallpaper_loader.js
@@ -0,0 +1,179 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+cr.define('login', function() {
+
+ /**
+ * Minimum wallpaper load delay in milliseconds.
+ * @type {number}
+ * @const
+ */
+ var WALLPAPER_LOAD_MIN_DELAY_MS = 100;
+
+ /**
+ * If last walpaper load time cannot be calculated, assume this value.
+ * @type {number}
+ * @const
+ */
+ var WALLPAPER_DEFAULT_LOAD_TIME_MS = 200;
+
+ /**
+ * Min and Max average wallpaper load time.
+ * Delay to next wallpaper load is 2 * <average load time>.
+ * @type {number}
+ * @const
+ */
+ var WALLPAPER_MIN_LOAD_TIME_MS = 50;
+ var WALLPAPER_MAX_LOAD_TIME_MS = 2000;
+
+ /**
+ * Number last wallpaper load times to remember.
+ * @type {number}
+ * @const
+ */
+ var WALLPAPER_LOAD_STATS_MAX_LENGTH = 4;
+
+
+ /**
+ * Creates a new pod row element.
+ * @constructor
+ * @extends {HTMLDivElement}
+ */
+ var WallpaperLoader = function() {
+ this.wallpaperLoadInProgress_ = {};
+ this.wallpaperLoadStats_ = [];
+ };
+
+ WallpaperLoader.prototype = {
+ // When moving through users quickly at login screen, set a timeout to
+ // prevent loading intermediate wallpapers.
+ loadWallpaperTimeout_: null,
+
+ // When waiting for wallpaper load, remember load start time.
+ // This is actually associative array with key = username and value =
+ // start time. Associative array.
+ 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.
+
+ // Wait a delay and then load this wallpaper. Value = username.
+ wallpaperLoadPending_: undefined,
+
+ // Wait untill this Date before loading next wallpaper.
+ wallpaperLoadTryNextAfter_: undefined,
+
+ // Username, owner of current wallpaper.
+ wallpaperCurrent_: '',
dzhioev (left Google) 2013/10/17 12:47:49 currentWallpaper_
Alexander Alekseev 2013/10/18 17:01:52 Done.
+
+ // Array of times (in milliseconds) of last wallpaper load attempts.
+ // Length is limited by WALLPAPER_LOAD_STATS_MAX_LENGTH.
+ wallpaperLoadStats_: undefined,
dzhioev (left Google) 2013/10/17 12:47:49 Just loadStats_
Alexander Alekseev 2013/10/18 17:01:52 Done.
+
+ 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.
+ 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) ?
+ clearTimeout(this.loadWallpaperTimeout_);
dzhioev (left Google) 2013/10/17 12:47:49 window.clearTimeout
Alexander Alekseev 2013/10/18 17:01:52 Done.
+ this.loadWallpaperTimeout_ = null;
+ },
+
+ /**
+ * Schedules wallpaper load.
+ */
+ scheduleLoad: function(email) {
+ if (this.wallpaperLoadPending_ && this.wallpaperLoadPending_ == email)
+ return;
+ delete this.wallpaperLoadPending_;
+ this.reset();
+ if (this.wallpaperLoadInProgress_.hasOwnProperty(email))
+ return;
+ if ((Object.getOwnPropertyNames(this.wallpaperLoadInProgress_).length ==
+ 0) && (this.wallpaperCurrent_ == email)) {
+ return;
+ }
+ this.wallpaperLoadPending_ = email;
+ var now = new Date();
+ var timeout = WALLPAPER_LOAD_MIN_DELAY_MS;
+ 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.
+ (now < this.wallpaperLoadTryNextAfter_) &&
+ (this.wallpaperLoadTryNextAfter_.getTime() -
+ now.getTime() > WALLPAPER_LOAD_MIN_DELAY_MS))
+ 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.
+ now.getTime();
+
+ this.loadWallpaperTimeout_ = window.setTimeout(
+ this.loadWallpaper_.bind(this), timeout);
+ },
+
+
+ /**
+ * Loads pending wallpaper, if any.
+ * @private
+ */
+ loadWallpaper_: function() {
+ this.loadWallpaperTimeout_ = null;
+ if (!this.wallpaperLoadPending_)
+ return;
+ if (Object.getOwnPropertyNames(this.wallpaperLoadInProgress_).length != 0)
+ return;
+ var email = this.wallpaperLoadPending_;
+ delete this.wallpaperLoadPending_;
+ if (email == this.wallpaperCurrent_)
+ return;
+ this.wallpaperLoadInProgress_[email] = new Date();
+ chrome.send('loadWallpaper', [email]);
+ },
+
+ /**
+ * Calculates average wallpaper load time.
+ */
+ getWallpaperLoadDelay_: function() {
+ var avg = WALLPAPER_DEFAULT_LOAD_TIME_MS;
+ if (this.wallpaperLoadStats_.length) {
+ 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.
+ 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.
+ return previousValue + currentValue;
+ }) / this.wallpaperLoadStats_.length;
+ if (avg < WALLPAPER_MIN_LOAD_TIME_MS) {
+ avg = WALLPAPER_MIN_LOAD_TIME_MS;
+ }
+ if (avg > WALLPAPER_MAX_LOAD_TIME_MS) {
+ avg = WALLPAPER_MAX_LOAD_TIME_MS;
+ }
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.
+ }
+ return avg;
+ },
+
+ /**
+ * Handles 'onWallpaperLoaded' event. Recalculates statistics and
+ * [re]schedules next wallpaper load.
+ */
+ onWallpaperLoaded: function(email) {
+ this.wallpaperCurrent_ = email;
+ var started = this.wallpaperLoadInProgress_[email];
+ delete this.wallpaperLoadInProgress_[email];
+ var finished = new Date();
+ var elapsed = started ? finished.getTime() - started.getTime() :
+ WALLPAPER_DEFAULT_LOAD_TIME_MS;
+ this.wallpaperLoadStats_.push(elapsed);
+ if (this.wallpaperLoadStats_.length >
+ 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.
+ this.wallpaperLoadStats_.shift();
+
+ var nextAfter = new Date();
+ nextAfter.setTime(nextAfter.getTime() +
+ 2 * this.getWallpaperLoadDelay_());
+ 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
+ if (this.wallpaperLoadPending_) {
+ var newWallpaperEmail = this.wallpaperLoadPending_;
+ delete this.wallpaperLoadPending_;
+ 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.
+ this.reset();
+ } else {
+ this.scheduleLoad(newWallpaperEmail);
+ }
+ }
+ }
+ };
+
+ return {
+ WallpaperLoader: WallpaperLoader
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698