OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview |
| 7 * Class to detect when the device is suspended, for example when a laptop's |
| 8 * lid is closed. |
| 9 */ |
| 10 |
| 11 |
| 12 'use strict'; |
| 13 |
| 14 /** @suppress {duplicate} */ |
| 15 var remoting = remoting || {}; |
| 16 |
| 17 /** |
| 18 * @param {function():void} callback Callback function to invoke when a |
| 19 * suspend+resume operation has been detected. |
| 20 * |
| 21 * @constructor |
| 22 */ |
| 23 remoting.SuspendMonitor = function (callback) { |
| 24 /** @type {function():void} @private */ |
| 25 this.callback_ = callback; |
| 26 /** @type {number} @private */ |
| 27 this.timerIntervalMs_ = 60 * 1000; |
| 28 /** @type {number} @private */ |
| 29 this.lateToleranceMs_ = 60 * 1000; |
| 30 /** @type {number} @private */ |
| 31 this.callbackExpectedTime_ = 0; |
| 32 this.start_(); |
| 33 }; |
| 34 |
| 35 /** @private */ |
| 36 remoting.SuspendMonitor.prototype.start_ = function() { |
| 37 window.setTimeout(this.checkSuspend_.bind(this), this.timerIntervalMs_); |
| 38 this.callbackExpectedTime_ = new Date().getTime() + this.timerIntervalMs_; |
| 39 }; |
| 40 |
| 41 /** @private */ |
| 42 remoting.SuspendMonitor.prototype.checkSuspend_ = function() { |
| 43 var lateByMs = new Date().getTime() - this.callbackExpectedTime_; |
| 44 if (lateByMs > this.lateToleranceMs_) { |
| 45 this.callback_(); |
| 46 } |
| 47 this.start_(); |
| 48 }; |
| 49 |
| 50 /** @type {remoting.SuspendMonitor?} */ |
| 51 remoting.suspendMonitor = null; |
OLD | NEW |