Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / suspend_detector.js
blobdb87fb96582a212577a6239411d66deb75d01183
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 /** @suppress {duplicate} */
6 var remoting = remoting || {};
8 (function () {
10 'use strict';
12 var INTERVAL_IN_MS = 500;
14 var TIMER_INACCURACY_IN_MS = 10;
16 /**
17 * @constructor
18 * @param {number=} opt_maxSuspendInMs The maximum permitted suspend duration
19 * to raise the resume event.
20 * @extends {base.EventSourceImpl}
21 * @implements {base.Disposable}
23 remoting.SuspendDetector = function(opt_maxSuspendInMs) {
24 base.inherits(this, base.EventSourceImpl);
25 this.defineEvents(base.values(remoting.SuspendDetector.Events));
27 if (opt_maxSuspendInMs == undefined ||
28 !Number.isInteger(opt_maxSuspendInMs)) {
29 opt_maxSuspendInMs = TIMER_INACCURACY_IN_MS;
32 /** @private */
33 this.maxSuspendInMs_ = Math.max(opt_maxSuspendInMs, TIMER_INACCURACY_IN_MS);
35 /**
36 * JavaScript timer is paused while the computer is suspended, we need to use
37 * a higher resolution timer instead of |this.maxSuspendInMs_| to ensure the
38 * resume event fires promptly after the system wakes up from sleep.
39 * @private
41 this.timer_ =
42 new base.RepeatingTimer(this.onTick_.bind(this), INTERVAL_IN_MS);
44 /** @private */
45 this.lastTick_ = new Date();
48 remoting.SuspendDetector.prototype.dispose = function() {
49 base.dispose(this.timer_);
50 this.timer = null;
53 /** @private */
54 remoting.SuspendDetector.prototype.onTick_ = function() {
55 var now = new Date();
56 // If the computer has just resumed from sleep, the sleep duration will
57 // roughly equal the |delta| between the ticks.
58 var delta = now - this.lastTick_;
59 this.lastTick_ = now;
60 if (delta > this.maxSuspendInMs_) {
61 this.raiseEvent(remoting.SuspendDetector.Events.resume, delta);
65 })();
67 /** @enum {string} */
68 remoting.SuspendDetector.Events = {
69 // Fired when the computer resumes up from sleep with the approximate sleep
70 // duration in milliseconds. The sleep duration is only an approximation with
71 // and an uncertainty of |INTERVAL_IN_MS|.
72 // {number} sleepDuration
73 resume: 'resume'