OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Class handling reconnecting the session when it is disconnected due to | 7 * Class handling reconnecting the session when it is disconnected due to |
8 * network failure. | 8 * network failure. |
9 * | 9 * |
10 * The SmartReconnector listens for changes in connection state of | 10 * The SmartReconnector listens for changes in connection state of |
11 * |clientSession| to determine if a reconnection is needed. It then calls into | 11 * |clientSession| to determine if a reconnection is needed. It then calls into |
12 * |connector| to reconnect the session. | 12 * |connector| to reconnect the session. |
13 */ | 13 */ |
14 | 14 |
15 'use strict'; | |
16 | |
17 /** @suppress {duplicate} */ | 15 /** @suppress {duplicate} */ |
18 var remoting = remoting || {}; | 16 var remoting = remoting || {}; |
19 | 17 |
| 18 (function () { |
| 19 |
| 20 'use strict'; |
| 21 |
20 /** | 22 /** |
21 * @constructor | 23 * @constructor |
22 * @param {function()} reconnectCallback | 24 * @param {function()} reconnectCallback |
23 * @param {function()} disconnectCallback | 25 * @param {function()} disconnectCallback |
24 * @param {remoting.ClientSession} clientSession This represents the current | 26 * @param {remoting.ClientSession} clientSession This represents the current |
25 * remote desktop connection. It is used to monitor the changes in | 27 * remote desktop connection. It is used to monitor the changes in |
26 * connection state. | 28 * connection state. |
27 * @implements {base.Disposable} | 29 * @implements {base.Disposable} |
28 */ | 30 */ |
29 remoting.SmartReconnector = | 31 remoting.SmartReconnector = |
30 function(reconnectCallback, disconnectCallback, clientSession) { | 32 function(reconnectCallback, disconnectCallback, clientSession) { |
31 /** @private */ | 33 /** @private */ |
32 this.reconnectCallback_ = reconnectCallback; | 34 this.reconnectCallback_ = reconnectCallback; |
33 | 35 |
34 /** @private */ | 36 /** @private */ |
35 this.disconnectCallback_ = disconnectCallback; | 37 this.disconnectCallback_ = disconnectCallback; |
36 | 38 |
37 /** @private */ | 39 /** @private */ |
38 this.clientSession_ = clientSession; | 40 this.clientSession_ = clientSession; |
39 | 41 |
| 42 /** |
| 43 * Placeholder of any pending reconnect operations, e.g. Waiting for online, |
| 44 * or a timeout to reconnect. |
| 45 * |
| 46 * @private {base.Disposable} |
| 47 */ |
| 48 this.pending_ = null; |
| 49 |
| 50 var Events = remoting.ClientSession.Events; |
40 /** @private */ | 51 /** @private */ |
41 this.reconnectTimerId_ = null; | 52 this.eventHooks_ = new base.Disposables( |
42 | 53 new base.EventHook(clientSession, Events.stateChanged, |
43 /** @private */ | 54 this.stateChanged_.bind(this)), |
44 this.connectionTimeoutTimerId_ = null; | 55 new base.EventHook(clientSession, Events.videoChannelStateChanged, |
45 | 56 this.videoChannelStateChanged_.bind(this))); |
46 /** @private */ | |
47 this.bound_ = { | |
48 reconnect: this.reconnect_.bind(this), | |
49 reconnectAsync: this.reconnectAsync_.bind(this), | |
50 startReconnectTimeout: this.startReconnectTimeout_.bind(this), | |
51 stateChanged: this.stateChanged_.bind(this), | |
52 videoChannelStateChanged: this.videoChannelStateChanged_.bind(this) | |
53 }; | |
54 | |
55 clientSession.addEventListener( | |
56 remoting.ClientSession.Events.stateChanged, | |
57 this.bound_.stateChanged); | |
58 clientSession.addEventListener( | |
59 remoting.ClientSession.Events.videoChannelStateChanged, | |
60 this.bound_.videoChannelStateChanged); | |
61 }; | 57 }; |
62 | 58 |
63 // The online event only means the network adapter is enabled, but | 59 // The online event only means the network adapter is enabled, but |
64 // it doesn't necessarily mean that we have a working internet connection. | 60 // it doesn't necessarily mean that we have a working internet connection. |
65 // Therefore, delay the connection by |kReconnectDelay| to allow for the network | 61 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the |
66 // to connect. | 62 // network to connect. |
67 remoting.SmartReconnector.kReconnectDelay = 2000; | 63 var RECONNECT_DELAY_MS = 2000; |
68 | 64 |
69 // If the video channel is inactive for 10 seconds reconnect the session. | 65 // If the video channel is inactive for 10 seconds reconnect the session. |
70 remoting.SmartReconnector.kConnectionTimeout = 10000; | 66 var CONNECTION_TIMEOUT_MS = 10000; |
71 | 67 |
72 remoting.SmartReconnector.prototype = { | 68 remoting.SmartReconnector.prototype.reconnect_ = function() { |
73 reconnect_: function() { | 69 this.cancelPending_(); |
| 70 this.disconnectCallback_(); |
| 71 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 72 this.reconnectCallback_(); |
| 73 }; |
| 74 |
| 75 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { |
| 76 this.cancelPending_(); |
| 77 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 78 this.pending_ = |
| 79 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); |
| 80 }; |
| 81 |
| 82 /** |
| 83 * @param {remoting.ClientSession.StateEvent=} event |
| 84 */ |
| 85 remoting.SmartReconnector.prototype.stateChanged_ = function(event) { |
| 86 var State = remoting.ClientSession.State; |
| 87 if (event.previous === State.CONNECTED && event.current === State.FAILED) { |
74 this.cancelPending_(); | 88 this.cancelPending_(); |
75 this.disconnectCallback_(); | 89 if (navigator.onLine) { |
76 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 90 this.reconnect_(); |
77 this.reconnectCallback_(); | 91 } else { |
78 }, | 92 this.pending_ = new base.DomEventHook( |
79 | 93 window, 'online', this.reconnectAsync_.bind(this), false); |
80 reconnectAsync_: function() { | |
81 this.cancelPending_(); | |
82 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | |
83 this.reconnectTimerId_ = window.setTimeout( | |
84 this.bound_.reconnect, remoting.SmartReconnector.kReconnectDelay); | |
85 }, | |
86 | |
87 /** | |
88 * @param {remoting.ClientSession.StateEvent=} event | |
89 */ | |
90 stateChanged_: function(event) { | |
91 var State = remoting.ClientSession.State; | |
92 if (event.previous === State.CONNECTED && event.current === State.FAILED) { | |
93 this.cancelPending_(); | |
94 if (navigator.onLine) { | |
95 this.reconnect_(); | |
96 } else { | |
97 window.addEventListener('online', this.bound_.reconnectAsync, false); | |
98 } | |
99 } | 94 } |
100 }, | |
101 | |
102 /** | |
103 * @param {boolean=} active True if the video channel is active. | |
104 */ | |
105 videoChannelStateChanged_: function (active) { | |
106 this.cancelPending_(); | |
107 if (!active) { | |
108 window.addEventListener( | |
109 'online', this.bound_.startReconnectTimeout, false); | |
110 } | |
111 }, | |
112 | |
113 startReconnectTimeout_: function () { | |
114 this.cancelPending_(); | |
115 this.connectionTimeoutTimerId_ = window.setTimeout( | |
116 this.bound_.reconnect, remoting.SmartReconnector.kConnectionTimeout); | |
117 }, | |
118 | |
119 cancelPending_: function() { | |
120 window.removeEventListener( | |
121 'online', this.bound_.startReconnectTimeout, false); | |
122 window.removeEventListener('online', this.bound_.reconnectAsync, false); | |
123 window.clearTimeout(this.reconnectTimerId_); | |
124 window.clearTimeout(this.connectionTimeoutTimerId_); | |
125 this.reconnectTimerId_ = null; | |
126 this.connectionTimeoutTimerId_ = null; | |
127 }, | |
128 | |
129 dispose: function() { | |
130 this.clientSession_.removeEventListener( | |
131 remoting.ClientSession.Events.stateChanged, | |
132 this.bound_.stateChanged); | |
133 this.clientSession_.removeEventListener( | |
134 remoting.ClientSession.Events.videoChannelStateChanged, | |
135 this.bound_.videoChannelStateChanged); | |
136 } | 95 } |
137 }; | 96 }; |
| 97 |
| 98 /** |
| 99 * @param {boolean=} active True if the video channel is active. |
| 100 */ |
| 101 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = |
| 102 function (active) { |
| 103 this.cancelPending_(); |
| 104 if (!active) { |
| 105 this.pending_ = new base.DomEventHook( |
| 106 window, 'online', this.startReconnectTimeout_.bind(this), false); |
| 107 } |
| 108 }; |
| 109 |
| 110 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { |
| 111 this.cancelPending_(); |
| 112 this.pending_ = |
| 113 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); |
| 114 }; |
| 115 |
| 116 /** @private */ |
| 117 remoting.SmartReconnector.prototype.cancelPending_ = function() { |
| 118 base.dispose(this.pending_); |
| 119 this.pending_ = null; |
| 120 }; |
| 121 |
| 122 remoting.SmartReconnector.prototype.dispose = function() { |
| 123 this.cancelPending_(); |
| 124 base.dispose(this.eventHooks_); |
| 125 this.eventHooks_ = null; |
| 126 }; |
| 127 |
| 128 })(); |
OLD | NEW |