Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / idle_detector.js
blob9d6c58bcfcdcb782714eac9b4ac2299763e3cf65
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.
5 /**
6 * @fileoverview
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.
20 'use strict';
22 /** @suppress {duplicate} */
23 var remoting = remoting || {};
25 /**
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
29 * using the session.
30 * @constructor
31 * @implements {remoting.WindowShape.ClientUI}
33 remoting.IdleDetector = function(idleWarning, callback) {
34 /** @private */
35 this.idleWarning_ = idleWarning;
37 /** @private */
38 this.callback_ = callback;
40 /**
41 * @type {number?} The id of the running timer, or null if no timer is
42 * running.
43 * @private
45 this.timerId_ = null;
47 /**
48 * @type {?function():void}
49 * @private
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);
63 this.resetTimeout_();
66 /**
67 * @param {boolean} register True to register the callbacks; false to remove
68 * them.
69 * @private
71 remoting.IdleDetector.prototype.registerInputDetectionCallbacks_ =
72 function(register) {
73 var events = [ 'mousemove', 'mousedown', 'mouseup', 'click',
74 'keyup', 'keydown', 'keypress' ];
75 if (register) {
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);
81 } else {
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;
90 /**
91 * @private
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);
105 * @private
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);
115 * @private
117 remoting.IdleDetector.prototype.onDialogTimeout_ = function() {
118 this.timerId_ = null;
119 this.showIdleWarning_(false);
120 this.callback_();
124 * @private
126 remoting.IdleDetector.prototype.onContinue_ = function() {
127 this.showIdleWarning_(false);
128 this.resetTimeout_();
132 * @private
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.
143 * @private
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());
158 rects.push(rect);
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;