| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
| 8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * A signal strategy encapsulating a primary and a back-up strategy. If the | 11 * A signal strategy encapsulating a primary and a back-up strategy. If the |
| 12 * primary fails or times out, then the secondary is used. Information about | 12 * primary fails or times out, then the secondary is used. Information about |
| 13 * which strategy was used, and why, is returned via |onProgressCallback|. | 13 * which strategy was used, and why, is returned via |onProgressCallback|. |
| 14 * | 14 * |
| 15 * @param {remoting.SignalStrategy} primary | 15 * @param {remoting.SignalStrategy} primary |
| 16 * @param {remoting.SignalStrategy} secondary | 16 * @param {remoting.SignalStrategy} secondary |
| 17 * @param {function(remoting.FallbackSignalStrategy.Progress)} | |
| 18 * onProgressCallback | |
| 19 * | 17 * |
| 20 * @implements {remoting.SignalStrategy} | 18 * @implements {remoting.SignalStrategy} |
| 21 * @constructor | 19 * @constructor |
| 22 */ | 20 */ |
| 23 remoting.FallbackSignalStrategy = function(primary, | 21 remoting.FallbackSignalStrategy = function(primary, |
| 24 secondary, | 22 secondary) { |
| 25 onProgressCallback) { | |
| 26 /** | 23 /** |
| 27 * @type {remoting.SignalStrategy} | 24 * @type {remoting.SignalStrategy} |
| 28 * @private | 25 * @private |
| 29 */ | 26 */ |
| 30 this.primary_ = primary; | 27 this.primary_ = primary; |
| 31 this.primary_.setStateChangedCallback(this.onPrimaryStateChanged_.bind(this)); | 28 this.primary_.setStateChangedCallback(this.onPrimaryStateChanged_.bind(this)); |
| 32 | 29 |
| 33 /** | 30 /** |
| 34 * @type {remoting.SignalStrategy} | 31 * @type {remoting.SignalStrategy} |
| 35 * @private | 32 * @private |
| 36 */ | 33 */ |
| 37 this.secondary_ = secondary; | 34 this.secondary_ = secondary; |
| 38 this.secondary_.setStateChangedCallback( | 35 this.secondary_.setStateChangedCallback( |
| 39 this.onSecondaryStateChanged_.bind(this)); | 36 this.onSecondaryStateChanged_.bind(this)); |
| 40 | 37 |
| 41 /** | 38 /** |
| 42 * @type {?function(remoting.SignalStrategy.State)} | 39 * @type {?function(remoting.SignalStrategy.State)} |
| 43 * @private | 40 * @private |
| 44 */ | 41 */ |
| 45 this.onStateChangedCallback_ = null; | 42 this.onStateChangedCallback_ = null; |
| 46 | 43 |
| 47 /** | 44 /** |
| 48 * @type {function(remoting.FallbackSignalStrategy.Progress)} | |
| 49 * @private | |
| 50 */ | |
| 51 this.onProgressCallback_ = onProgressCallback; | |
| 52 | |
| 53 /** | |
| 54 * @type {?function(Element):void} | 45 * @type {?function(Element):void} |
| 55 * @private | 46 * @private |
| 56 */ | 47 */ |
| 57 this.onIncomingStanzaCallback_ = null; | 48 this.onIncomingStanzaCallback_ = null; |
| 58 | 49 |
| 59 /** | 50 /** |
| 60 * @type {number} | 51 * @type {number} |
| 61 * @private | 52 * @private |
| 62 * @const | 53 * @const |
| 63 */ | 54 */ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 * @type {string} | 96 * @type {string} |
| 106 * @private | 97 * @private |
| 107 */ | 98 */ |
| 108 this.authToken_ = ''; | 99 this.authToken_ = ''; |
| 109 | 100 |
| 110 /** | 101 /** |
| 111 * @type {number} | 102 * @type {number} |
| 112 * @private | 103 * @private |
| 113 */ | 104 */ |
| 114 this.primaryConnectTimerId_ = 0; | 105 this.primaryConnectTimerId_ = 0; |
| 106 |
| 107 /** |
| 108 * @type {remoting.LogToServer} |
| 109 * @private |
| 110 */ |
| 111 this.logToServer_ = null; |
| 112 |
| 113 /** |
| 114 * @type {Array.<{strategyType: remoting.SignalStrategy.Type, |
| 115 progress: remoting.FallbackSignalStrategy.Progress, |
| 116 * elapsed: number}>} |
| 117 */ |
| 118 this.connectionSetupResults_ = []; |
| 119 |
| 120 /** |
| 121 * @type {number} |
| 122 * @private |
| 123 */ |
| 124 this.startTime_ = 0; |
| 115 }; | 125 }; |
| 116 | 126 |
| 117 /** | 127 /** |
| 118 * @enum {string} | 128 * @enum {string} |
| 119 */ | 129 */ |
| 120 remoting.FallbackSignalStrategy.Progress = { | 130 remoting.FallbackSignalStrategy.Progress = { |
| 121 PRIMARY_SUCCEEDED: 'primary-succeeded', | 131 SUCCEEDED: 'succeeded', |
| 122 PRIMARY_FAILED: 'primary-failed', | 132 FAILED: 'failed', |
| 123 PRIMARY_TIMED_OUT: 'primary-timed-out', | 133 TIMED_OUT: 'timed-out', |
| 124 PRIMARY_SUCCEEDED_LATE: 'primary-succeeded-late', | 134 SUCCEEDED_LATE: 'succeeded-late', |
| 125 PRIMARY_FAILED_LATE: 'primary-failed-late', | 135 FAILED_LATE: 'failed-late', |
| 126 SECONDARY_SUCCEEDED: 'secondary-succeeded', | |
| 127 SECONDARY_FAILED: 'secondary-failed' | |
| 128 }; | 136 }; |
| 129 | 137 |
| 130 remoting.FallbackSignalStrategy.prototype.dispose = function() { | 138 remoting.FallbackSignalStrategy.prototype.dispose = function() { |
| 131 this.primary_.dispose(); | 139 this.primary_.dispose(); |
| 132 this.secondary_.dispose(); | 140 this.secondary_.dispose(); |
| 133 }; | 141 }; |
| 134 | 142 |
| 135 /** | 143 /** |
| 136 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback | 144 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback |
| 137 * Callback to call on state change. | 145 * Callback to call on state change. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 163 * @param {string} authToken | 171 * @param {string} authToken |
| 164 */ | 172 */ |
| 165 remoting.FallbackSignalStrategy.prototype.connect = | 173 remoting.FallbackSignalStrategy.prototype.connect = |
| 166 function(server, username, authToken) { | 174 function(server, username, authToken) { |
| 167 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); | 175 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); |
| 168 base.debug.assert(this.onStateChangedCallback_ != null); | 176 base.debug.assert(this.onStateChangedCallback_ != null); |
| 169 this.server_ = server; | 177 this.server_ = server; |
| 170 this.username_ = username; | 178 this.username_ = username; |
| 171 this.authToken_ = authToken; | 179 this.authToken_ = authToken; |
| 172 this.state_ = this.State.PRIMARY_PENDING; | 180 this.state_ = this.State.PRIMARY_PENDING; |
| 181 this.startTime_ = new Date().getTime(); |
| 173 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 182 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
| 174 this.primary_.connect(server, username, authToken); | 183 this.primary_.connect(server, username, authToken); |
| 175 this.primaryConnectTimerId_ = | 184 this.primaryConnectTimerId_ = |
| 176 window.setTimeout(this.onPrimaryTimeout_.bind(this), | 185 window.setTimeout(this.onPrimaryTimeout_.bind(this), |
| 177 this.PRIMARY_CONNECT_TIMEOUT_MS_); | 186 this.PRIMARY_CONNECT_TIMEOUT_MS_); |
| 178 }; | 187 }; |
| 179 | 188 |
| 180 /** | 189 /** |
| 181 * Sends a message. Can be called only in CONNECTED state. | 190 * Sends a message. Can be called only in CONNECTED state. |
| 182 * @param {string} message | 191 * @param {string} message |
| 183 */ | 192 */ |
| 184 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { | 193 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { |
| 185 this.getConnectedSignalStrategy_().sendMessage(message); | 194 this.getConnectedSignalStrategy_().sendMessage(message); |
| 186 }; | 195 }; |
| 187 | 196 |
| 197 /** |
| 198 * Send any messages accumulated during connection set-up. |
| 199 * |
| 200 * @param {remoting.LogToServer} logToServer The LogToServer instance for the |
| 201 * connection. |
| 202 */ |
| 203 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResults = |
| 204 function(logToServer) { |
| 205 this.logToServer_ = logToServer; |
| 206 this.sendConnectionSetupResultsInternal_(); |
| 207 } |
| 208 |
| 209 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResultsInternal_ = |
| 210 function() { |
| 211 for (var i = 0; i < this.connectionSetupResults_.length; ++i) { |
| 212 var result = this.connectionSetupResults_[i]; |
| 213 this.logToServer_.logSignalStrategyProgress(result.strategyType, |
| 214 result.progress, |
| 215 result.elapsed); |
| 216 } |
| 217 this.connectionSetupResults_ = []; |
| 218 }; |
| 219 |
| 188 /** @return {remoting.SignalStrategy.State} Current state */ | 220 /** @return {remoting.SignalStrategy.State} Current state */ |
| 189 remoting.FallbackSignalStrategy.prototype.getState = function() { | 221 remoting.FallbackSignalStrategy.prototype.getState = function() { |
| 190 return (this.externalState_ === null) | 222 return (this.externalState_ === null) |
| 191 ? remoting.SignalStrategy.State.NOT_CONNECTED | 223 ? remoting.SignalStrategy.State.NOT_CONNECTED |
| 192 : this.externalState_; | 224 : this.externalState_; |
| 193 }; | 225 }; |
| 194 | 226 |
| 195 /** @return {remoting.Error} Error when in FAILED state. */ | 227 /** @return {remoting.Error} Error when in FAILED state. */ |
| 196 remoting.FallbackSignalStrategy.prototype.getError = function() { | 228 remoting.FallbackSignalStrategy.prototype.getError = function() { |
| 197 base.debug.assert(this.state_ == this.State.SECONDARY_FAILED); | 229 base.debug.assert(this.state_ == this.State.SECONDARY_FAILED); |
| 198 base.debug.assert( | 230 base.debug.assert( |
| 199 this.secondary_.getState() == remoting.SignalStrategy.State.FAILED); | 231 this.secondary_.getState() == remoting.SignalStrategy.State.FAILED); |
| 200 return this.secondary_.getError(); | 232 return this.secondary_.getError(); |
| 201 }; | 233 }; |
| 202 | 234 |
| 203 /** @return {string} Current JID when in CONNECTED state. */ | 235 /** @return {string} Current JID when in CONNECTED state. */ |
| 204 remoting.FallbackSignalStrategy.prototype.getJid = function() { | 236 remoting.FallbackSignalStrategy.prototype.getJid = function() { |
| 205 return this.getConnectedSignalStrategy_().getJid(); | 237 return this.getConnectedSignalStrategy_().getJid(); |
| 206 }; | 238 }; |
| 207 | 239 |
| 240 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */ |
| 241 remoting.FallbackSignalStrategy.prototype.getType = function() { |
| 242 return this.getConnectedSignalStrategy_().getType(); |
| 243 }; |
| 244 |
| 208 /** | 245 /** |
| 209 * @return {remoting.SignalStrategy} The active signal strategy, if the | 246 * @return {remoting.SignalStrategy} The active signal strategy, if the |
| 210 * connection has succeeded. | 247 * connection has succeeded. |
| 211 * @private | 248 * @private |
| 212 */ | 249 */ |
| 213 remoting.FallbackSignalStrategy.prototype.getConnectedSignalStrategy_ = | 250 remoting.FallbackSignalStrategy.prototype.getConnectedSignalStrategy_ = |
| 214 function() { | 251 function() { |
| 215 if (this.state_ == this.State.PRIMARY_SUCCEEDED) { | 252 if (this.state_ == this.State.PRIMARY_SUCCEEDED) { |
| 216 base.debug.assert( | 253 base.debug.assert( |
| 217 this.primary_.getState() == remoting.SignalStrategy.State.CONNECTED); | 254 this.primary_.getState() == remoting.SignalStrategy.State.CONNECTED); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 231 /** | 268 /** |
| 232 * @param {remoting.SignalStrategy.State} state | 269 * @param {remoting.SignalStrategy.State} state |
| 233 * @private | 270 * @private |
| 234 */ | 271 */ |
| 235 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = | 272 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = |
| 236 function(state) { | 273 function(state) { |
| 237 switch (state) { | 274 switch (state) { |
| 238 case remoting.SignalStrategy.State.CONNECTED: | 275 case remoting.SignalStrategy.State.CONNECTED: |
| 239 if (this.state_ == this.State.PRIMARY_PENDING) { | 276 if (this.state_ == this.State.PRIMARY_PENDING) { |
| 240 window.clearTimeout(this.primaryConnectTimerId_); | 277 window.clearTimeout(this.primaryConnectTimerId_); |
| 241 this.onProgressCallback_( | 278 this.updateProgress_( |
| 242 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED); | 279 this.primary_, |
| 280 remoting.FallbackSignalStrategy.Progress.SUCCEEDED); |
| 243 this.state_ = this.State.PRIMARY_SUCCEEDED; | 281 this.state_ = this.State.PRIMARY_SUCCEEDED; |
| 244 } else { | 282 } else { |
| 245 this.onProgressCallback_( | 283 this.updateProgress_( |
| 246 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED_LATE); | 284 this.primary_, |
| 285 remoting.FallbackSignalStrategy.Progress.SUCCEEDED_LATE); |
| 247 } | 286 } |
| 248 break; | 287 break; |
| 249 | 288 |
| 250 case remoting.SignalStrategy.State.FAILED: | 289 case remoting.SignalStrategy.State.FAILED: |
| 251 if (this.state_ == this.State.PRIMARY_PENDING) { | 290 if (this.state_ == this.State.PRIMARY_PENDING) { |
| 252 window.clearTimeout(this.primaryConnectTimerId_); | 291 window.clearTimeout(this.primaryConnectTimerId_); |
| 253 this.onProgressCallback_( | 292 this.updateProgress_( |
| 254 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED); | 293 this.primary_, |
| 294 remoting.FallbackSignalStrategy.Progress.FAILED); |
| 255 this.connectSecondary_(); | 295 this.connectSecondary_(); |
| 256 } else { | 296 } else { |
| 257 this.onProgressCallback_( | 297 this.updateProgress_( |
| 258 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED_LATE); | 298 this.primary_, |
| 299 remoting.FallbackSignalStrategy.Progress.FAILED_LATE); |
| 259 } | 300 } |
| 260 return; // Don't notify the external callback | 301 return; // Don't notify the external callback |
| 261 | 302 |
| 262 case remoting.SignalStrategy.State.CLOSED: | 303 case remoting.SignalStrategy.State.CLOSED: |
| 263 this.state_ = this.State.CLOSED; | 304 this.state_ = this.State.CLOSED; |
| 264 break; | 305 break; |
| 265 } | 306 } |
| 266 | 307 |
| 267 this.notifyExternalCallback_(state); | 308 this.notifyExternalCallback_(state); |
| 268 }; | 309 }; |
| 269 | 310 |
| 270 /** | 311 /** |
| 271 * @param {remoting.SignalStrategy.State} state | 312 * @param {remoting.SignalStrategy.State} state |
| 272 * @private | 313 * @private |
| 273 */ | 314 */ |
| 274 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = | 315 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = |
| 275 function(state) { | 316 function(state) { |
| 276 switch (state) { | 317 switch (state) { |
| 277 case remoting.SignalStrategy.State.CONNECTED: | 318 case remoting.SignalStrategy.State.CONNECTED: |
| 278 this.onProgressCallback_( | 319 this.updateProgress_( |
| 279 remoting.FallbackSignalStrategy.Progress.SECONDARY_SUCCEEDED); | 320 this.secondary_, |
| 321 remoting.FallbackSignalStrategy.Progress.SUCCEEDED); |
| 280 this.state_ = this.State.SECONDARY_SUCCEEDED; | 322 this.state_ = this.State.SECONDARY_SUCCEEDED; |
| 281 break; | 323 break; |
| 282 | 324 |
| 283 case remoting.SignalStrategy.State.FAILED: | 325 case remoting.SignalStrategy.State.FAILED: |
| 284 this.onProgressCallback_( | 326 this.updateProgress_( |
| 285 remoting.FallbackSignalStrategy.Progress.SECONDARY_FAILED); | 327 this.secondary_, |
| 328 remoting.FallbackSignalStrategy.Progress.FAILED); |
| 286 this.state_ = this.State.SECONDARY_FAILED; | 329 this.state_ = this.State.SECONDARY_FAILED; |
| 287 break; | 330 break; |
| 288 | 331 |
| 289 case remoting.SignalStrategy.State.CLOSED: | 332 case remoting.SignalStrategy.State.CLOSED: |
| 290 this.state_ = this.State.CLOSED; | 333 this.state_ = this.State.CLOSED; |
| 291 break; | 334 break; |
| 292 } | 335 } |
| 293 | 336 |
| 294 this.notifyExternalCallback_(state); | 337 this.notifyExternalCallback_(state); |
| 295 }; | 338 }; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 324 this.state_ = this.State.SECONDARY_PENDING; | 367 this.state_ = this.State.SECONDARY_PENDING; |
| 325 this.primary_.setIncomingStanzaCallback(null); | 368 this.primary_.setIncomingStanzaCallback(null); |
| 326 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 369 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
| 327 this.secondary_.connect(this.server_, this.username_, this.authToken_); | 370 this.secondary_.connect(this.server_, this.username_, this.authToken_); |
| 328 }; | 371 }; |
| 329 | 372 |
| 330 /** | 373 /** |
| 331 * @private | 374 * @private |
| 332 */ | 375 */ |
| 333 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { | 376 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { |
| 334 this.onProgressCallback_( | 377 this.updateProgress_( |
| 335 remoting.FallbackSignalStrategy.Progress.PRIMARY_TIMED_OUT); | 378 this.primary_, |
| 379 remoting.FallbackSignalStrategy.Progress.TIMED_OUT); |
| 336 this.connectSecondary_(); | 380 this.connectSecondary_(); |
| 337 }; | 381 }; |
| 382 |
| 383 /** |
| 384 * @param {remoting.SignalStrategy} strategy |
| 385 * @param {remoting.FallbackSignalStrategy.Progress} progress |
| 386 * @private |
| 387 */ |
| 388 remoting.FallbackSignalStrategy.prototype.updateProgress_ = function( |
| 389 strategy, progress) { |
| 390 console.log('FallbackSignalStrategy progress: ' + strategy.getType() + ' ' + |
| 391 progress); |
| 392 this.connectionSetupResults_.push({ |
| 393 'strategyType': strategy.getType(), |
| 394 'progress': progress, |
| 395 'elapsed': new Date().getTime() - this.startTime_ |
| 396 }); |
| 397 if (this.logToServer_) { |
| 398 this.sendConnectionSetupResultsInternal_(); |
| 399 } |
| 400 }; |
| OLD | NEW |