| 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 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 /** | 42 /** |
| 43 * Placeholder of any pending reconnect operations, e.g. Waiting for online, | 43 * Placeholder of any pending reconnect operations, e.g. Waiting for online, |
| 44 * or a timeout to reconnect. | 44 * or a timeout to reconnect. |
| 45 * | 45 * |
| 46 * @private {base.Disposable} | 46 * @private {base.Disposable} |
| 47 */ | 47 */ |
| 48 this.pending_ = null; | 48 this.pending_ = null; |
| 49 | 49 |
| 50 var Events = remoting.ClientSession.Events; | 50 var Events = remoting.ClientSession.Events; |
| 51 /** @private */ | 51 /** @private */ |
| 52 this.eventHooks_ = new base.Disposables( | 52 this.eventHook_ = |
| 53 new base.EventHook(clientSession, Events.stateChanged, | |
| 54 this.stateChanged_.bind(this)), | |
| 55 new base.EventHook(clientSession, Events.videoChannelStateChanged, | 53 new base.EventHook(clientSession, Events.videoChannelStateChanged, |
| 56 this.videoChannelStateChanged_.bind(this))); | 54 this.videoChannelStateChanged_.bind(this)); |
| 57 }; | 55 }; |
| 58 | 56 |
| 59 // The online event only means the network adapter is enabled, but | 57 // The online event only means the network adapter is enabled, but |
| 60 // it doesn't necessarily mean that we have a working internet connection. | 58 // 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 | 59 // Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the |
| 62 // network to connect. | 60 // network to connect. |
| 63 var RECONNECT_DELAY_MS = 2000; | 61 var RECONNECT_DELAY_MS = 2000; |
| 64 | 62 |
| 65 // If the video channel is inactive for 10 seconds reconnect the session. | 63 // If the video channel is inactive for 10 seconds reconnect the session. |
| 66 var CONNECTION_TIMEOUT_MS = 10000; | 64 var CONNECTION_TIMEOUT_MS = 10000; |
| 67 | 65 |
| 68 remoting.SmartReconnector.prototype.reconnect_ = function() { | 66 remoting.SmartReconnector.prototype.reconnect_ = function() { |
| 69 this.cancelPending_(); | 67 this.cancelPending_(); |
| 70 this.disconnectCallback_(); | 68 this.disconnectCallback_(); |
| 71 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 69 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 72 this.reconnectCallback_(); | 70 this.reconnectCallback_(); |
| 73 }; | 71 }; |
| 74 | 72 |
| 75 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { | 73 remoting.SmartReconnector.prototype.reconnectAsync_ = function() { |
| 76 this.cancelPending_(); | 74 this.cancelPending_(); |
| 77 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 75 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 78 this.pending_ = | 76 this.pending_ = |
| 79 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); | 77 new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); |
| 80 }; | 78 }; |
| 81 | 79 |
| 82 /** | 80 /** |
| 83 * @param {remoting.ClientSession.StateEvent=} event | 81 * @param {!remoting.Error} reason |
| 84 */ | 82 */ |
| 85 remoting.SmartReconnector.prototype.stateChanged_ = function(event) { | 83 remoting.SmartReconnector.prototype.onConnectionDropped = function(reason) { |
| 86 var State = remoting.ClientSession.State; | 84 this.cancelPending_(); |
| 87 if (event.previous === State.CONNECTED && event.current === State.FAILED) { | 85 if (navigator.onLine) { |
| 88 this.cancelPending_(); | 86 this.reconnect_(); |
| 89 if (navigator.onLine) { | 87 } else { |
| 90 this.reconnect_(); | 88 this.pending_ = new base.DomEventHook( |
| 91 } else { | 89 window, 'online', this.reconnectAsync_.bind(this), false); |
| 92 this.pending_ = new base.DomEventHook( | |
| 93 window, 'online', this.reconnectAsync_.bind(this), false); | |
| 94 } | |
| 95 } | 90 } |
| 96 }; | 91 }; |
| 97 | 92 |
| 98 /** | 93 /** |
| 99 * @param {boolean=} active True if the video channel is active. | 94 * @param {boolean=} active True if the video channel is active. |
| 100 */ | 95 */ |
| 101 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = | 96 remoting.SmartReconnector.prototype.videoChannelStateChanged_ = |
| 102 function (active) { | 97 function (active) { |
| 103 this.cancelPending_(); | 98 this.cancelPending_(); |
| 104 if (!active) { | 99 if (!active) { |
| 105 this.pending_ = new base.DomEventHook( | 100 this.pending_ = new base.DomEventHook( |
| 106 window, 'online', this.startReconnectTimeout_.bind(this), false); | 101 window, 'online', this.startReconnectTimeout_.bind(this), false); |
| 107 } | 102 } |
| 108 }; | 103 }; |
| 109 | 104 |
| 110 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { | 105 remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { |
| 111 this.cancelPending_(); | 106 this.cancelPending_(); |
| 112 this.pending_ = | 107 this.pending_ = |
| 113 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); | 108 new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); |
| 114 }; | 109 }; |
| 115 | 110 |
| 116 /** @private */ | 111 /** @private */ |
| 117 remoting.SmartReconnector.prototype.cancelPending_ = function() { | 112 remoting.SmartReconnector.prototype.cancelPending_ = function() { |
| 118 base.dispose(this.pending_); | 113 base.dispose(this.pending_); |
| 119 this.pending_ = null; | 114 this.pending_ = null; |
| 120 }; | 115 }; |
| 121 | 116 |
| 122 remoting.SmartReconnector.prototype.dispose = function() { | 117 remoting.SmartReconnector.prototype.dispose = function() { |
| 123 this.cancelPending_(); | 118 this.cancelPending_(); |
| 124 base.dispose(this.eventHooks_); | 119 base.dispose(this.eventHook_); |
| 125 this.eventHooks_ = null; | 120 this.eventHook_ = null; |
| 126 }; | 121 }; |
| 127 | 122 |
| 128 })(); | 123 })(); |
| OLD | NEW |