OLD | NEW |
| (Empty) |
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. | |
4 | |
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 */ | |
14 | |
15 /** @suppress {duplicate} */ | |
16 var remoting = remoting || {}; | |
17 | |
18 (function () { | |
19 | |
20 'use strict'; | |
21 | |
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; | |
36 | |
37 /** @private */ | |
38 this.disconnectCallback_ = disconnectCallback; | |
39 | |
40 /** @private */ | |
41 this.clientSession_ = clientSession; | |
42 | |
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; | |
50 | |
51 /** @private */ | |
52 this.connectingDialog_ = connectingDialog; | |
53 | |
54 var Events = remoting.ClientSession.Events; | |
55 /** @private */ | |
56 this.eventHook_ = | |
57 new base.EventHook(clientSession, Events.videoChannelStateChanged, | |
58 this.videoChannelStateChanged_.bind(this)); | |
59 }; | |
60 | |
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; | |
66 | |
67 // If the video channel is inactive for 10 seconds reconnect the session. | |
68 var CONNECTION_TIMEOUT_MS = 10000; | |
69 | |
70 remoting.SmartReconnector.prototype.reconnect_ = function() { | |
71 this.cancelPending_(); | |
72 this.disconnectCallback_(); | |
73 this.reconnectCallback_(); | |
74 }; | |
75 | |
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); | |
81 }; | |
82 | |
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 } | |
94 }; | |
95 | |
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 } | |
106 }; | |
107 | |
108 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { | |
109 this.cancelPending_(); | |
110 this.pending_ = | |
111 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); | |
112 }; | |
113 | |
114 /** @private */ | |
115 remoting.SmartReconnector.prototype.cancelPending_ = function() { | |
116 base.dispose(this.pending_); | |
117 this.pending_ = null; | |
118 }; | |
119 | |
120 remoting.SmartReconnector.prototype.dispose = function() { | |
121 this.cancelPending_(); | |
122 base.dispose(this.eventHook_); | |
123 this.eventHook_ = null; | |
124 }; | |
125 | |
126 })(); | |
OLD | NEW |