Chromium Code Reviews| 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 /** @private {base.Disposable} */ | |
| 43 this.pending_ = null; | |
|
Jamie
2015/04/16 20:05:49
You're using this for two separate pending operati
kelvinp
2015/04/16 20:57:58
|this.pending_| is intended to be a placeholder of
| |
| 44 | |
| 45 var Events = remoting.ClientSession.Events; | |
| 40 /** @private */ | 46 /** @private */ |
| 41 this.reconnectTimerId_ = null; | 47 this.eventHooks_ = new base.Disposables( |
| 42 | 48 new base.EventHook(clientSession, Events.stateChanged, |
| 43 /** @private */ | 49 this.stateChanged_.bind(this)), |
| 44 this.connectionTimeoutTimerId_ = null; | 50 new base.EventHook(clientSession, Events.videoChannelStateChanged, |
| 45 | 51 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 }; | 52 }; |
| 62 | 53 |
| 63 // The online event only means the network adapter is enabled, but | 54 // The online event only means the network adapter is enabled, but |
| 64 // it doesn't necessarily mean that we have a working internet connection. | 55 // it doesn't necessarily mean that we have a working internet connection. |
| 65 // Therefore, delay the connection by |kReconnectDelay| to allow for the network | 56 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the |
| 66 // to connect. | 57 // network to connect. |
| 67 remoting.SmartReconnector.kReconnectDelay = 2000; | 58 var RECONNECT_DELAY_MS = 2000; |
| 68 | 59 |
| 69 // If the video channel is inactive for 10 seconds reconnect the session. | 60 // If the video channel is inactive for 10 seconds reconnect the session. |
| 70 remoting.SmartReconnector.kConnectionTimeout = 10000; | 61 var CONNECTION_TIMEOUT_MS = 10000; |
| 71 | 62 |
| 72 remoting.SmartReconnector.prototype = { | 63 remoting.SmartReconnector.prototype.reconnect_ = function() { |
| 73 reconnect_: function() { | 64 base.dispose(this.pending_); |
|
Jamie
2015/04/16 20:05:49
Reset pending_ to null, here and below.
kelvinp
2015/04/16 20:57:58
Done.
| |
| 74 this.cancelPending_(); | 65 this.disconnectCallback_(); |
| 75 this.disconnectCallback_(); | 66 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 76 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 67 this.reconnectCallback_(); |
| 77 this.reconnectCallback_(); | 68 }; |
| 78 }, | |
| 79 | 69 |
| 80 reconnectAsync_: function() { | 70 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { |
| 81 this.cancelPending_(); | 71 base.dispose(this.pending_); |
| 82 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 72 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 83 this.reconnectTimerId_ = window.setTimeout( | 73 this.pending_ = |
| 84 this.bound_.reconnect, remoting.SmartReconnector.kReconnectDelay); | 74 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); |
| 85 }, | 75 }; |
| 86 | 76 |
| 87 /** | 77 /** |
| 88 * @param {remoting.ClientSession.StateEvent=} event | 78 * @param {remoting.ClientSession.StateEvent=} event |
| 89 */ | 79 */ |
| 90 stateChanged_: function(event) { | 80 remoting.SmartReconnector.prototype.stateChanged_ = function(event) { |
| 91 var State = remoting.ClientSession.State; | 81 var State = remoting.ClientSession.State; |
| 92 if (event.previous === State.CONNECTED && event.current === State.FAILED) { | 82 if (event.previous === State.CONNECTED && event.current === State.FAILED) { |
| 93 this.cancelPending_(); | 83 base.dispose(this.pending_); |
| 94 if (navigator.onLine) { | 84 if (navigator.onLine) { |
| 95 this.reconnect_(); | 85 this.reconnect_(); |
| 96 } else { | 86 } else { |
| 97 window.addEventListener('online', this.bound_.reconnectAsync, false); | 87 this.pending_ = new base.DomEventHook( |
| 98 } | 88 window, 'online', this.reconnectAsync_.bind(this), false); |
| 99 } | 89 } |
| 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 } | 90 } |
| 137 }; | 91 }; |
| 92 | |
| 93 /** | |
| 94 * @param {boolean=} active True if the video channel is active. | |
| 95 */ | |
| 96 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = | |
| 97 function (active) { | |
| 98 base.dispose(this.pending_); | |
| 99 if (!active) { | |
| 100 this.pending_ = new base.DomEventHook( | |
|
Jamie
2015/04/16 20:05:49
Indentation.
kelvinp
2015/04/16 20:57:58
Done.
Jamie
2015/04/17 00:02:47
This still looks wrong. This line is indented by 4
kelvinp
2015/04/17 00:12:53
My bad. My this.pending_ change overwrote the ind
| |
| 101 window, 'online', this.startReconnectTimeout_.bind(this), false); | |
| 102 } | |
| 103 }; | |
| 104 | |
| 105 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { | |
| 106 base.dispose(this.pending_); | |
| 107 this.pending_ = | |
| 108 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); | |
| 109 }; | |
| 110 | |
| 111 remoting.SmartReconnector.prototype.dispose = function() { | |
| 112 base.dispose(this.pending_); | |
| 113 this.pending_ = null; | |
| 114 base.dispose(this.eventHooks_); | |
| 115 this.eventHooks_ = null; | |
| 116 }; | |
| 117 | |
| 118 })(); | |
| OLD | NEW |