Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / idle_detector.js
blobc92d94aed91b61fc8e7e92a9c95805425bc6ee0c
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 * @private {number?} The id of the running timer, or null if no timer is
42 * running.
44 this.timerId_ = null;
46 /** @private {?function():void} */
47 this.resetTimeoutRef_ = null;
49 var manifest = chrome.runtime.getManifest();
50 var message = this.idleWarning_.querySelector('.idle-warning-message');
51 l10n.localizeElement(message, manifest.name);
53 var cont = this.idleWarning_.querySelector('.idle-dialog-continue');
54 cont.addEventListener('click', this.onContinue_.bind(this), false);
55 var quit = this.idleWarning_.querySelector('.idle-dialog-disconnect');
56 quit.addEventListener('click', this.onDisconnect_.bind(this), false);
58 remoting.windowShape.addCallback(this);
59 this.resetTimeout_();
62 /**
63 * @param {boolean} register True to register the callbacks; false to remove
64 * them.
65 * @private
67 remoting.IdleDetector.prototype.registerInputDetectionCallbacks_ =
68 function(register) {
69 var events = [ 'mousemove', 'mousedown', 'mouseup', 'click',
70 'keyup', 'keydown', 'keypress' ];
71 if (register) {
72 base.debug.assert(this.resetTimeoutRef_ == null);
73 this.resetTimeoutRef_ = this.resetTimeout_.bind(this);
74 for (var i = 0; i < events.length; ++i) {
75 document.body.addEventListener(events[i], this.resetTimeoutRef_, true);
77 } else {
78 base.debug.assert(this.resetTimeoutRef_ != null);
79 for (var i = 0; i < events.length; ++i) {
80 document.body.removeEventListener(events[i], this.resetTimeoutRef_, true);
82 this.resetTimeoutRef_ = null;
86 /**
87 * @private
89 remoting.IdleDetector.prototype.resetTimeout_ = function() {
90 if (this.timerId_ !== null) {
91 window.clearTimeout(this.timerId_);
93 if (this.resetTimeoutRef_ == null) {
94 this.registerInputDetectionCallbacks_(true);
96 this.timerId_ = window.setTimeout(this.onIdleTimeout_.bind(this),
97 remoting.IdleDetector.kIdleTimeoutMs);
101 * @private
103 remoting.IdleDetector.prototype.onIdleTimeout_ = function() {
104 this.registerInputDetectionCallbacks_(false);
105 this.showIdleWarning_(true);
106 this.timerId_ = window.setTimeout(this.onDialogTimeout_.bind(this),
107 remoting.IdleDetector.kDialogTimeoutMs);
111 * @private
113 remoting.IdleDetector.prototype.onDialogTimeout_ = function() {
114 this.timerId_ = null;
115 this.showIdleWarning_(false);
116 this.callback_();
120 * @private
122 remoting.IdleDetector.prototype.onContinue_ = function() {
123 this.showIdleWarning_(false);
124 this.resetTimeout_();
128 * @private
130 remoting.IdleDetector.prototype.onDisconnect_ = function() {
131 if (this.timerId_ !== null) {
132 window.clearTimeout(this.timerId_);
134 this.onDialogTimeout_();
138 * @param {boolean} show True to show the warning dialog; false to hide it.
139 * @private
141 remoting.IdleDetector.prototype.showIdleWarning_ = function(show) {
142 this.idleWarning_.hidden = !show;
143 remoting.windowShape.updateClientWindowShape();
147 * @param {Array<{left: number, top: number, width: number, height: number}>}
148 * rects List of rectangles.
150 remoting.IdleDetector.prototype.addToRegion = function(rects) {
151 if (!this.idleWarning_.hidden) {
152 var dialog = this.idleWarning_.querySelector('.kd-modaldialog');
153 var rect = /** @type {ClientRect} */ (dialog.getBoundingClientRect());
154 rects.push(rect);
158 // Time-out after 1hr of no activity.
159 remoting.IdleDetector.kIdleTimeoutMs = 60 * 60 * 1000;
161 // Show the idle warning dialog for 2 minutes.
162 remoting.IdleDetector.kDialogTimeoutMs = 2 * 60 * 1000;