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 (opt_maxSuspendInMs
== undefined ||
28 !Number
.isInteger(opt_maxSuspendInMs
)) {
29 opt_maxSuspendInMs
= TIMER_INACCURACY_IN_MS
;
33 this.maxSuspendInMs_
= Math
.max(opt_maxSuspendInMs
, TIMER_INACCURACY_IN_MS
);
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.
42 new base
.RepeatingTimer(this.onTick_
.bind(this), INTERVAL_IN_MS
);
45 this.lastTick_
= new Date();
48 remoting
.SuspendDetector
.prototype.dispose = function() {
49 base
.dispose(this.timer_
);
54 remoting
.SuspendDetector
.prototype.onTick_ = function() {
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_
;
60 if (delta
> this.maxSuspendInMs_
) {
61 this.raiseEvent(remoting
.SuspendDetector
.Events
.resume
, delta
);
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