Chromium Code Reviews| Index: remoting/webapp/suspend_monitor.js |
| diff --git a/remoting/webapp/suspend_monitor.js b/remoting/webapp/suspend_monitor.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5bf9400d52945fe8564c4ca726f2f8f1d5c14c2a |
| --- /dev/null |
| +++ b/remoting/webapp/suspend_monitor.js |
| @@ -0,0 +1,47 @@ |
| +// Copyright (c) 2012 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. |
| + |
| +/** |
| + * @fileoverview |
| + * Class to detect when the device is suspended. If the device is suspended, |
| + * for example, a laptop's lid is closed, any subsequent errors detected by |
| + * the active ClientSession should not be logged, as they arise from explicit |
| + * user action. |
| + */ |
| + |
| +/** |
| + * @param {function():void} callback Callback function to invoke when a |
| + * suspend+resume operation has been detected. |
| + * |
| + * @constructor |
| + */ |
| +remoting.SuspendMonitor = function (callback) { |
| + /** @type {function():void} @private */ |
| + this.callback_ = callback; |
| + /** @type {number} @private */ |
| + this.timerIntervalMs_ = 60 * 1000; |
| + /** @type {number} @private */ |
| + this.lateToleranceMs_ = 60 * 1000; |
| + /** @type {number} @private */ |
| + this.callbackExpectedTime_ = 0; |
| + this.start_(); |
|
simonmorris
2012/08/03 21:48:49
It might be better to start the suspend monitor on
Jamie
2012/08/03 23:15:14
I don't think that will happen. If the callback is
|
| +}; |
| + |
| +/** @private */ |
| +remoting.SuspendMonitor.prototype.start_ = function() { |
| + window.setTimeout(this.checkSuspend_.bind(this), this.timerIntervalMs_); |
| + this.callbackExpectedTime_ = new Date().getTime() + this.timerIntervalMs_; |
| +}; |
| + |
| +/** @private */ |
| +remoting.SuspendMonitor.prototype.checkSuspend_ = function() { |
| + var lateByMs = new Date().getTime() - this.callbackExpectedTime_; |
| + if (lateByMs > this.lateToleranceMs_) { |
| + this.callback_(); |
|
simonmorris
2012/08/03 21:48:49
This will typically be called 30 seconds after the
Jamie
2012/08/03 23:15:14
That is only true for very short suspension period
|
| + } |
| + this.start_(); |
| +}; |
| + |
| +/** @type {remoting.SuspendMonitor?} */ |
| +remoting.suspendMonitor = null; |