| 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 /** @suppress {duplicate} */ | 15 /** @suppress {duplicate} */ |
| 16 var remoting = remoting || {}; | 16 var remoting = remoting || {}; |
| 17 | 17 |
| 18 (function () { | 18 (function () { |
| 19 | 19 |
| 20 'use strict'; | 20 'use strict'; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * @constructor | 23 * @constructor |
| 24 * @param {remoting.ConnectingDialog} connectingDialog |
| 24 * @param {function()} reconnectCallback | 25 * @param {function()} reconnectCallback |
| 25 * @param {function()} disconnectCallback | 26 * @param {function()} disconnectCallback |
| 26 * @param {remoting.ClientSession} clientSession This represents the current | 27 * @param {remoting.ClientSession} clientSession This represents the current |
| 27 * remote desktop connection. It is used to monitor the changes in | 28 * remote desktop connection. It is used to monitor the changes in |
| 28 * connection state. | 29 * connection state. |
| 29 * @implements {base.Disposable} | 30 * @implements {base.Disposable} |
| 30 */ | 31 */ |
| 31 remoting.SmartReconnector = | 32 remoting.SmartReconnector = function(connectingDialog, reconnectCallback, |
| 32 function(reconnectCallback, disconnectCallback, clientSession) { | 33 disconnectCallback, clientSession) { |
| 33 /** @private */ | 34 /** @private */ |
| 34 this.reconnectCallback_ = reconnectCallback; | 35 this.reconnectCallback_ = reconnectCallback; |
| 35 | 36 |
| 36 /** @private */ | 37 /** @private */ |
| 37 this.disconnectCallback_ = disconnectCallback; | 38 this.disconnectCallback_ = disconnectCallback; |
| 38 | 39 |
| 39 /** @private */ | 40 /** @private */ |
| 40 this.clientSession_ = clientSession; | 41 this.clientSession_ = clientSession; |
| 41 | 42 |
| 42 /** | 43 /** |
| 43 * Placeholder of any pending reconnect operations, e.g. Waiting for online, | 44 * Placeholder of any pending reconnect operations, e.g. Waiting for online, |
| 44 * or a timeout to reconnect. | 45 * or a timeout to reconnect. |
| 45 * | 46 * |
| 46 * @private {base.Disposable} | 47 * @private {base.Disposable} |
| 47 */ | 48 */ |
| 48 this.pending_ = null; | 49 this.pending_ = null; |
| 49 | 50 |
| 51 /** @private */ |
| 52 this.connectingDialog_ = connectingDialog; |
| 53 |
| 50 var Events = remoting.ClientSession.Events; | 54 var Events = remoting.ClientSession.Events; |
| 51 /** @private */ | 55 /** @private */ |
| 52 this.eventHooks_ = new base.Disposables( | 56 this.eventHook_ = |
| 53 new base.EventHook(clientSession, Events.stateChanged, | |
| 54 this.stateChanged_.bind(this)), | |
| 55 new base.EventHook(clientSession, Events.videoChannelStateChanged, | 57 new base.EventHook(clientSession, Events.videoChannelStateChanged, |
| 56 this.videoChannelStateChanged_.bind(this))); | 58 this.videoChannelStateChanged_.bind(this)); |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 // The online event only means the network adapter is enabled, but | 61 // The online event only means the network adapter is enabled, but |
| 60 // it doesn't necessarily mean that we have a working internet connection. | 62 // it doesn't necessarily mean that we have a working internet connection. |
| 61 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the | 63 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the |
| 62 // network to connect. | 64 // network to connect. |
| 63 var RECONNECT_DELAY_MS = 2000; | 65 var RECONNECT_DELAY_MS = 2000; |
| 64 | 66 |
| 65 // If the video channel is inactive for 10 seconds reconnect the session. | 67 // If the video channel is inactive for 10 seconds reconnect the session. |
| 66 var CONNECTION_TIMEOUT_MS = 10000; | 68 var CONNECTION_TIMEOUT_MS = 10000; |
| 67 | 69 |
| 68 remoting.SmartReconnector.prototype.reconnect_ = function() { | 70 remoting.SmartReconnector.prototype.reconnect_ = function() { |
| 69 this.cancelPending_(); | 71 this.cancelPending_(); |
| 70 this.disconnectCallback_(); | 72 this.disconnectCallback_(); |
| 71 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | |
| 72 this.reconnectCallback_(); | 73 this.reconnectCallback_(); |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { | 76 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { |
| 76 this.cancelPending_(); | 77 this.cancelPending_(); |
| 77 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 78 this.connectingDialog_.show(); |
| 78 this.pending_ = | 79 this.pending_ = |
| 79 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); | 80 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); |
| 80 }; | 81 }; |
| 81 | 82 |
| 82 /** | 83 /** |
| 83 * @param {remoting.ClientSession.StateEvent=} event | 84 * @param {!remoting.Error} reason |
| 84 */ | 85 */ |
| 85 remoting.SmartReconnector.prototype.stateChanged_ = function(event) { | 86 remoting.SmartReconnector.prototype.onConnectionDropped = function(reason) { |
| 86 var State = remoting.ClientSession.State; | 87 this.cancelPending_(); |
| 87 if (event.previous === State.CONNECTED && event.current === State.FAILED) { | 88 if (navigator.onLine) { |
| 88 this.cancelPending_(); | 89 this.reconnect_(); |
| 89 if (navigator.onLine) { | 90 } else { |
| 90 this.reconnect_(); | 91 this.pending_ = new base.DomEventHook( |
| 91 } else { | 92 window, 'online', this.reconnectAsync_.bind(this), false); |
| 92 this.pending_ = new base.DomEventHook( | |
| 93 window, 'online', this.reconnectAsync_.bind(this), false); | |
| 94 } | |
| 95 } | 93 } |
| 96 }; | 94 }; |
| 97 | 95 |
| 98 /** | 96 /** |
| 99 * @param {boolean=} active True if the video channel is active. | 97 * @param {boolean=} active True if the video channel is active. |
| 100 */ | 98 */ |
| 101 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = | 99 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = |
| 102 function (active) { | 100 function (active) { |
| 103 this.cancelPending_(); | 101 this.cancelPending_(); |
| 104 if (!active) { | 102 if (!active) { |
| 105 this.pending_ = new base.DomEventHook( | 103 this.pending_ = new base.DomEventHook( |
| 106 window, 'online', this.startReconnectTimeout_.bind(this), false); | 104 window, 'online', this.startReconnectTimeout_.bind(this), false); |
| 107 } | 105 } |
| 108 }; | 106 }; |
| 109 | 107 |
| 110 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { | 108 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { |
| 111 this.cancelPending_(); | 109 this.cancelPending_(); |
| 112 this.pending_ = | 110 this.pending_ = |
| 113 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); | 111 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); |
| 114 }; | 112 }; |
| 115 | 113 |
| 116 /** @private */ | 114 /** @private */ |
| 117 remoting.SmartReconnector.prototype.cancelPending_ = function() { | 115 remoting.SmartReconnector.prototype.cancelPending_ = function() { |
| 118 base.dispose(this.pending_); | 116 base.dispose(this.pending_); |
| 119 this.pending_ = null; | 117 this.pending_ = null; |
| 120 }; | 118 }; |
| 121 | 119 |
| 122 remoting.SmartReconnector.prototype.dispose = function() { | 120 remoting.SmartReconnector.prototype.dispose = function() { |
| 123 this.cancelPending_(); | 121 this.cancelPending_(); |
| 124 base.dispose(this.eventHooks_); | 122 base.dispose(this.eventHook_); |
| 125 this.eventHooks_ = null; | 123 this.eventHook_ = null; |
| 126 }; | 124 }; |
| 127 | 125 |
| 128 })(); | 126 })(); |
| OLD | NEW |