Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / base / js / smart_reconnector.js
blobafb6975a916176decb762439b83179442eb2fe0d
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 handling reconnecting the session when it is disconnected due to
8  * network failure.
9  *
10  * The SmartReconnector listens for changes in connection state of
11  * |clientSession| to determine if a reconnection is needed.  It then calls into
12  * |connector| to reconnect the session.
13  */
15 /** @suppress {duplicate} */
16 var remoting = remoting || {};
18 (function () {
20 'use strict';
22 /**
23  * @constructor
24  * @param {remoting.ConnectingDialog} connectingDialog
25  * @param {function()} reconnectCallback
26  * @param {function()} disconnectCallback
27  * @param {remoting.ClientSession} clientSession This represents the current
28  *    remote desktop connection.  It is used to monitor the changes in
29  *    connection state.
30  * @implements {base.Disposable}
31  */
32 remoting.SmartReconnector = function(connectingDialog, reconnectCallback,
33                                      disconnectCallback, clientSession) {
34   /** @private */
35   this.reconnectCallback_ = reconnectCallback;
37   /** @private */
38   this.disconnectCallback_ = disconnectCallback;
40   /** @private */
41   this.clientSession_ = clientSession;
43   /**
44    * Placeholder of any pending reconnect operations, e.g. Waiting for online,
45    * or a timeout to reconnect.
46    *
47    * @private {base.Disposable}
48    */
49   this.pending_ = null;
51   /** @private */
52   this.connectingDialog_ = connectingDialog;
54   var Events = remoting.ClientSession.Events;
55   /** @private */
56   this.eventHook_ =
57       new base.EventHook(clientSession, Events.videoChannelStateChanged,
58                          this.videoChannelStateChanged_.bind(this));
61 // The online event only means the network adapter is enabled, but
62 // it doesn't necessarily mean that we have a working internet connection.
63 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the
64 // network to connect.
65 var RECONNECT_DELAY_MS = 2000;
67 // If the video channel is inactive for 10 seconds reconnect the session.
68 var CONNECTION_TIMEOUT_MS = 10000;
70 remoting.SmartReconnector.prototype.reconnect_ = function() {
71   this.cancelPending_();
72   this.disconnectCallback_();
73   this.reconnectCallback_();
76 remoting.SmartReconnector.prototype.reconnectAsync_ = function() {
77   this.cancelPending_();
78   this.connectingDialog_.show();
79   this.pending_ =
80       new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS);
83 /**
84  * @param {!remoting.Error} reason
85  */
86 remoting.SmartReconnector.prototype.onConnectionDropped = function(reason) {
87   this.cancelPending_();
88   if (navigator.onLine) {
89     this.reconnect_();
90   } else {
91     this.pending_ = new base.DomEventHook(
92         window, 'online', this.reconnectAsync_.bind(this), false);
93   }
96 /**
97  * @param {boolean=} active  True if the video channel is active.
98  */
99 remoting.SmartReconnector.prototype.videoChannelStateChanged_ =
100     function (active) {
101   this.cancelPending_();
102   if (!active) {
103     this.pending_ = new base.DomEventHook(
104         window, 'online', this.startReconnectTimeout_.bind(this), false);
105   }
108 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() {
109   this.cancelPending_();
110   this.pending_ =
111       new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS);
114 /** @private */
115 remoting.SmartReconnector.prototype.cancelPending_ = function() {
116   base.dispose(this.pending_);
117   this.pending_ = null;
120 remoting.SmartReconnector.prototype.dispose = function() {
121   this.cancelPending_();
122   base.dispose(this.eventHook_);
123   this.eventHook_ = null;
126 })();