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 || {};
12 var INTERVAL_IN_MS = 500;
14 var TIMER_INACCURACY_IN_MS = 10;
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 (!Number.isInteger(opt_maxSuspendInMs)) {
28 opt_maxSuspendInMs = TIMER_INACCURACY_IN_MS;
32 this.maxSuspendInMs_ = Math.max(opt_maxSuspendInMs, TIMER_INACCURACY_IN_MS);
35 * JavaScript timer is paused while the computer is suspended, we need to use
36 * a higher resolution timer instead of |this.maxSuspendInMs_| to ensure the
37 * resume event fires promptly after the system wakes up from sleep.
41 new base.RepeatingTimer(this.onTick_.bind(this), INTERVAL_IN_MS);
44 this.lastTick_ = new Date();
47 remoting.SuspendDetector.prototype.dispose = function() {
48 base.dispose(this.timer_);
53 remoting.SuspendDetector.prototype.onTick_ = function() {
55 // If the computer has just resumed from sleep, the sleep duration will
56 // roughly equal the |delta| between the ticks.
57 var delta = now - this.lastTick_;
59 if (delta > this.maxSuspendInMs_) {
60 this.raiseEvent(remoting.SuspendDetector.Events.resume, delta);
67 remoting.SuspendDetector.Events = {
68 // Fired when the computer resumes up from sleep with the approximate sleep
69 // duration in milliseconds. The sleep duration is only an approximation with
70 // and an uncertainty of |INTERVAL_IN_MS|.
71 // {number} sleepDuration