1 // Copyright 2014 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.
7 * Class for detecting when the application is idle. Note that chrome.idle is
8 * not suitable for this purpose because it detects when the computer is idle,
9 * and we'd like to close the application and free up VM resources even if the
10 * user has been using another application for a long time.
12 * There are two idle timeouts. The first controls the visibility of the idle
13 * timeout warning dialog and is reset on mouse input; when it expires, the
14 * idle warning dialog is displayed. The second controls the length of time
15 * for which the idle warning dialog is displayed; when it expires, the ctor
16 * callback is invoked, which it is assumed will exit the application--no
17 * further idle detection is done.
22 /** @suppress {duplicate} */
23 var remoting
= remoting
|| {};
26 * @param {HTMLElement} idleWarning The idle warning dialog.
27 * @param {function():void} callback Called when the idle warning dialog has
28 * timed out or the user has explicitly indicated that they are no longer
31 * @implements {remoting.WindowShape.ClientUI}
33 remoting
.IdleDetector = function(idleWarning
, callback
) {
35 this.idleWarning_
= idleWarning
;
38 this.callback_
= callback
;
41 * @type {number?} The id of the running timer, or null if no timer is
48 * @type {?function():void}
51 this.resetTimeoutRef_
= null;
53 var manifest
= chrome
.runtime
.getManifest();
54 var message
= this.idleWarning_
.querySelector('.idle-warning-message');
55 l10n
.localizeElement(message
, manifest
.name
);
57 var cont
= this.idleWarning_
.querySelector('.idle-dialog-continue');
58 cont
.addEventListener('click', this.onContinue_
.bind(this), false);
59 var quit
= this.idleWarning_
.querySelector('.idle-dialog-disconnect');
60 quit
.addEventListener('click', this.onDisconnect_
.bind(this), false);
62 remoting
.windowShape
.addCallback(this);
67 * @param {boolean} register True to register the callbacks; false to remove
71 remoting
.IdleDetector
.prototype.registerInputDetectionCallbacks_
=
73 var events
= [ 'mousemove', 'mousedown', 'mouseup', 'click',
74 'keyup', 'keydown', 'keypress' ];
76 base
.debug
.assert(this.resetTimeoutRef_
== null);
77 this.resetTimeoutRef_
= this.resetTimeout_
.bind(this);
78 for (var i
= 0; i
< events
.length
; ++i
) {
79 document
.body
.addEventListener(events
[i
], this.resetTimeoutRef_
, true);
82 base
.debug
.assert(this.resetTimeoutRef_
!= null);
83 for (var i
= 0; i
< events
.length
; ++i
) {
84 document
.body
.removeEventListener(events
[i
], this.resetTimeoutRef_
, true);
86 this.resetTimeoutRef_
= null;
93 remoting
.IdleDetector
.prototype.resetTimeout_ = function() {
94 if (this.timerId_
!== null) {
95 window
.clearTimeout(this.timerId_
);
97 if (this.resetTimeoutRef_
== null) {
98 this.registerInputDetectionCallbacks_(true);
100 this.timerId_
= window
.setTimeout(this.onIdleTimeout_
.bind(this),
101 remoting
.IdleDetector
.kIdleTimeoutMs
);
107 remoting
.IdleDetector
.prototype.onIdleTimeout_ = function() {
108 this.registerInputDetectionCallbacks_(false);
109 this.showIdleWarning_(true);
110 this.timerId_
= window
.setTimeout(this.onDialogTimeout_
.bind(this),
111 remoting
.IdleDetector
.kDialogTimeoutMs
);
117 remoting
.IdleDetector
.prototype.onDialogTimeout_ = function() {
118 this.timerId_
= null;
119 this.showIdleWarning_(false);
126 remoting
.IdleDetector
.prototype.onContinue_ = function() {
127 this.showIdleWarning_(false);
128 this.resetTimeout_();
134 remoting
.IdleDetector
.prototype.onDisconnect_ = function() {
135 if (this.timerId_
!== null) {
136 window
.clearTimeout(this.timerId_
);
138 this.onDialogTimeout_();
142 * @param {boolean} show True to show the warning dialog; false to hide it.
145 remoting
.IdleDetector
.prototype.showIdleWarning_ = function(show
) {
146 this.idleWarning_
.hidden
= !show
;
147 remoting
.windowShape
.updateClientWindowShape();
151 * @param {Array<{left: number, top: number, width: number, height: number}>}
152 * rects List of rectangles.
154 remoting
.IdleDetector
.prototype.addToRegion = function(rects
) {
155 if (!this.idleWarning_
.hidden
) {
156 var dialog
= this.idleWarning_
.querySelector('.kd-modaldialog');
157 var rect
= /** @type {ClientRect} */ (dialog
.getBoundingClientRect());
162 // Time-out after 1hr of no activity.
163 remoting
.IdleDetector
.kIdleTimeoutMs
= 60 * 60 * 1000;
165 // Show the idle warning dialog for 2 minutes.
166 remoting
.IdleDetector
.kDialogTimeoutMs
= 2 * 60 * 1000;