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 handling reconnecting the session when it is disconnected due to
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.
15 /** @suppress {duplicate} */
16 var remoting
= remoting
|| {};
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
30 * @implements {base.Disposable}
32 remoting
.SmartReconnector = function(connectingDialog
, reconnectCallback
,
33 disconnectCallback
, clientSession
) {
35 this.reconnectCallback_
= reconnectCallback
;
38 this.disconnectCallback_
= disconnectCallback
;
41 this.clientSession_
= clientSession
;
44 * Placeholder of any pending reconnect operations, e.g. Waiting for online,
45 * or a timeout to reconnect.
47 * @private {base.Disposable}
52 this.connectingDialog_
= connectingDialog
;
54 var Events
= remoting
.ClientSession
.Events
;
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();
80 new base
.OneShotTimer(this.reconnect_
.bind(this), RECONNECT_DELAY_MS
);
84 * @param {!remoting.Error} reason
86 remoting
.SmartReconnector
.prototype.onConnectionDropped = function(reason
) {
87 this.cancelPending_();
88 if (navigator
.onLine
) {
91 this.pending_
= new base
.DomEventHook(
92 window
, 'online', this.reconnectAsync_
.bind(this), false);
97 * @param {boolean=} active True if the video channel is active.
99 remoting
.SmartReconnector
.prototype.videoChannelStateChanged_
=
101 this.cancelPending_();
103 this.pending_
= new base
.DomEventHook(
104 window
, 'online', this.startReconnectTimeout_
.bind(this), false);
108 remoting
.SmartReconnector
.prototype.startReconnectTimeout_ = function() {
109 this.cancelPending_();
111 new base
.OneShotTimer(this.reconnect_
.bind(this), CONNECTION_TIMEOUT_MS
);
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;