Chromium Code Reviews| 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 {function( | 15 * @param {function( |
| 16 * function(remoting.SignalStrategy.State) | 16 * function(remoting.SignalStrategy.State) |
| 17 * ):remoting.SignalStrategy} primaryFactory | 17 * ):remoting.SignalStrategy} primaryFactory |
| 18 * @param {function( | 18 * @param {function( |
| 19 * function(remoting.SignalStrategy.State) | 19 * function(remoting.SignalStrategy.State) |
| 20 * ):remoting.SignalStrategy} secondaryFactory | 20 * ):remoting.SignalStrategy} secondaryFactory |
| 21 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback | 21 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback |
| 22 * @param {function(remoting.FallbackSignalStrategy.Progress)} | |
| 23 * onProgressCallback | |
| 24 * | 22 * |
| 25 * @implements {remoting.SignalStrategy} | 23 * @implements {remoting.SignalStrategy} |
| 26 * @constructor | 24 * @constructor |
| 27 */ | 25 */ |
| 28 remoting.FallbackSignalStrategy = function( | 26 remoting.FallbackSignalStrategy = function( |
| 29 primaryFactory, secondaryFactory, | 27 primaryFactory, secondaryFactory, onStateChangedCallback) { |
| 30 onStateChangedCallback, onProgressCallback) { | |
| 31 /** | 28 /** |
| 32 * @type {remoting.SignalStrategy} | 29 * @type {remoting.SignalStrategy} |
| 33 * @private | 30 * @private |
| 34 */ | 31 */ |
| 35 this.primary_ = primaryFactory(this.onPrimaryStateChanged_.bind(this)); | 32 this.primary_ = primaryFactory(this.onPrimaryStateChanged_.bind(this)); |
| 36 | 33 |
| 37 /** | 34 /** |
| 38 * @type {remoting.SignalStrategy} | 35 * @type {remoting.SignalStrategy} |
| 39 * @private | 36 * @private |
| 40 */ | 37 */ |
| 41 this.secondary_ = secondaryFactory(this.onSecondaryStateChanged_.bind(this)); | 38 this.secondary_ = secondaryFactory(this.onSecondaryStateChanged_.bind(this)); |
| 42 | 39 |
| 43 /** | 40 /** |
| 44 * @type {function(remoting.SignalStrategy.State)} | 41 * @type {function(remoting.SignalStrategy.State)} |
| 45 * @private | 42 * @private |
| 46 */ | 43 */ |
| 47 this.onStateChangedCallback_ = onStateChangedCallback; | 44 this.onStateChangedCallback_ = onStateChangedCallback; |
| 48 | 45 |
| 49 /** | 46 /** |
| 50 * @type {function(remoting.FallbackSignalStrategy.Progress)} | |
| 51 * @private | |
| 52 */ | |
| 53 this.onProgressCallback_ = onProgressCallback; | |
| 54 | |
| 55 /** | |
| 56 * @type {?function(Element):void} | 47 * @type {?function(Element):void} |
| 57 * @private | 48 * @private |
| 58 */ | 49 */ |
| 59 this.onIncomingStanzaCallback_ = null; | 50 this.onIncomingStanzaCallback_ = null; |
| 60 | 51 |
| 61 /** | 52 /** |
| 62 * @type {number} | 53 * @type {number} |
| 63 * @private | 54 * @private |
| 64 * @const | 55 * @const |
| 65 */ | 56 */ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 * @type {string} | 98 * @type {string} |
| 108 * @private | 99 * @private |
| 109 */ | 100 */ |
| 110 this.authToken_ = ''; | 101 this.authToken_ = ''; |
| 111 | 102 |
| 112 /** | 103 /** |
| 113 * @type {number} | 104 * @type {number} |
| 114 * @private | 105 * @private |
| 115 */ | 106 */ |
| 116 this.primaryConnectTimerId_ = 0; | 107 this.primaryConnectTimerId_ = 0; |
| 108 | |
| 109 /** | |
| 110 * @type {remoting.LogToServer} | |
| 111 * @private | |
| 112 */ | |
| 113 this.logToServer_ = null; | |
| 114 | |
| 115 /** | |
| 116 * @type {Array.<{progress: remoting.FallbackSignalStrategy.Progress, | |
| 117 * elapsed: number}>} | |
| 118 */ | |
| 119 this.connectionSetupResults_ = []; | |
| 120 | |
| 121 /** | |
| 122 * @type {number} | |
| 123 * @private | |
| 124 */ | |
| 125 this.startTime_ = 0; | |
| 117 }; | 126 }; |
| 118 | 127 |
| 119 /** | 128 /** |
| 120 * @enum {string} | 129 * @enum {string} |
| 121 */ | 130 */ |
| 122 remoting.FallbackSignalStrategy.Progress = { | 131 remoting.FallbackSignalStrategy.Progress = { |
| 123 PRIMARY_SUCCEEDED: 'primary-succeeded', | 132 PRIMARY_SUCCEEDED: 'primary-succeeded', |
|
rmsousa
2015/01/17 00:23:17
This naming is fairly ambiguous - I assume the int
Jamie
2015/01/21 19:41:08
Done.
| |
| 124 PRIMARY_FAILED: 'primary-failed', | 133 PRIMARY_FAILED: 'primary-failed', |
| 125 PRIMARY_TIMED_OUT: 'primary-timed-out', | 134 PRIMARY_TIMED_OUT: 'primary-timed-out', |
| 126 PRIMARY_SUCCEEDED_LATE: 'primary-succeeded-late', | 135 PRIMARY_SUCCEEDED_LATE: 'primary-succeeded-late', |
| 127 PRIMARY_FAILED_LATE: 'primary-failed-late', | 136 PRIMARY_FAILED_LATE: 'primary-failed-late', |
| 128 SECONDARY_SUCCEEDED: 'secondary-succeeded', | 137 SECONDARY_SUCCEEDED: 'secondary-succeeded', |
| 129 SECONDARY_FAILED: 'secondary-failed' | 138 SECONDARY_FAILED: 'secondary-failed' |
| 130 }; | 139 }; |
| 131 | 140 |
| 132 remoting.FallbackSignalStrategy.prototype.dispose = function() { | 141 remoting.FallbackSignalStrategy.prototype.dispose = function() { |
| 133 this.primary_.dispose(); | 142 this.primary_.dispose(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 155 * @param {string} username | 164 * @param {string} username |
| 156 * @param {string} authToken | 165 * @param {string} authToken |
| 157 */ | 166 */ |
| 158 remoting.FallbackSignalStrategy.prototype.connect = | 167 remoting.FallbackSignalStrategy.prototype.connect = |
| 159 function(server, username, authToken) { | 168 function(server, username, authToken) { |
| 160 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); | 169 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); |
| 161 this.server_ = server; | 170 this.server_ = server; |
| 162 this.username_ = username; | 171 this.username_ = username; |
| 163 this.authToken_ = authToken; | 172 this.authToken_ = authToken; |
| 164 this.state_ = this.State.PRIMARY_PENDING; | 173 this.state_ = this.State.PRIMARY_PENDING; |
| 174 this.startTime_ = new Date().getTime(); | |
| 165 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 175 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
| 166 this.primary_.connect(server, username, authToken); | 176 this.primary_.connect(server, username, authToken); |
| 167 this.primaryConnectTimerId_ = | 177 this.primaryConnectTimerId_ = |
| 168 window.setTimeout(this.onPrimaryTimeout_.bind(this), | 178 window.setTimeout(this.onPrimaryTimeout_.bind(this), |
| 169 this.PRIMARY_CONNECT_TIMEOUT_MS_); | 179 this.PRIMARY_CONNECT_TIMEOUT_MS_); |
| 170 }; | 180 }; |
| 171 | 181 |
| 172 /** | 182 /** |
| 173 * Sends a message. Can be called only in CONNECTED state. | 183 * Sends a message. Can be called only in CONNECTED state. |
| 174 * @param {string} message | 184 * @param {string} message |
| 175 */ | 185 */ |
| 176 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { | 186 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { |
| 177 this.getConnectedSignalStrategy_().sendMessage(message); | 187 this.getConnectedSignalStrategy_().sendMessage(message); |
| 178 }; | 188 }; |
| 179 | 189 |
| 190 /** | |
| 191 * Send any messages accumulated during connection set-up. | |
| 192 * | |
| 193 * @param {remoting.LogToServer} logToServer The LogToServer instance for the | |
| 194 * connection. | |
| 195 */ | |
| 196 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResults = | |
| 197 function(logToServer) { | |
| 198 this.logToServer_ = logToServer; | |
| 199 this.sendConnectionSetupResultsInternal_(); | |
| 200 } | |
| 201 | |
| 202 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResultsInternal_ = | |
| 203 function() { | |
| 204 for (var i = 0; i < this.connectionSetupResults_.length; ++i) { | |
| 205 var result = this.connectionSetupResults_[i]; | |
| 206 this.logToServer_.logSignalStrategyProgress(result.progress, | |
| 207 result.elapsed); | |
| 208 } | |
| 209 this.connectionSetupResults_ = []; | |
| 210 }; | |
| 211 | |
| 180 /** @return {remoting.SignalStrategy.State} Current state */ | 212 /** @return {remoting.SignalStrategy.State} Current state */ |
| 181 remoting.FallbackSignalStrategy.prototype.getState = function() { | 213 remoting.FallbackSignalStrategy.prototype.getState = function() { |
| 182 return (this.externalState_ === null) | 214 return (this.externalState_ === null) |
| 183 ? remoting.SignalStrategy.State.NOT_CONNECTED | 215 ? remoting.SignalStrategy.State.NOT_CONNECTED |
| 184 : this.externalState_; | 216 : this.externalState_; |
| 185 }; | 217 }; |
| 186 | 218 |
| 187 /** @return {remoting.Error} Error when in FAILED state. */ | 219 /** @return {remoting.Error} Error when in FAILED state. */ |
| 188 remoting.FallbackSignalStrategy.prototype.getError = function() { | 220 remoting.FallbackSignalStrategy.prototype.getError = function() { |
| 189 base.debug.assert(this.state_ == this.State.SECONDARY_FAILED); | 221 base.debug.assert(this.state_ == this.State.SECONDARY_FAILED); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 /** | 255 /** |
| 224 * @param {remoting.SignalStrategy.State} state | 256 * @param {remoting.SignalStrategy.State} state |
| 225 * @private | 257 * @private |
| 226 */ | 258 */ |
| 227 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = | 259 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = |
| 228 function(state) { | 260 function(state) { |
| 229 switch (state) { | 261 switch (state) { |
| 230 case remoting.SignalStrategy.State.CONNECTED: | 262 case remoting.SignalStrategy.State.CONNECTED: |
| 231 if (this.state_ == this.State.PRIMARY_PENDING) { | 263 if (this.state_ == this.State.PRIMARY_PENDING) { |
| 232 window.clearTimeout(this.primaryConnectTimerId_); | 264 window.clearTimeout(this.primaryConnectTimerId_); |
| 233 this.onProgressCallback_( | 265 this.updateProgress_( |
| 234 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED); | 266 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED); |
| 235 this.state_ = this.State.PRIMARY_SUCCEEDED; | 267 this.state_ = this.State.PRIMARY_SUCCEEDED; |
| 236 } else { | 268 } else { |
| 237 this.onProgressCallback_( | 269 this.updateProgress_( |
| 238 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED_LATE); | 270 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED_LATE); |
| 239 } | 271 } |
| 240 break; | 272 break; |
| 241 | 273 |
| 242 case remoting.SignalStrategy.State.FAILED: | 274 case remoting.SignalStrategy.State.FAILED: |
| 243 if (this.state_ == this.State.PRIMARY_PENDING) { | 275 if (this.state_ == this.State.PRIMARY_PENDING) { |
| 244 window.clearTimeout(this.primaryConnectTimerId_); | 276 window.clearTimeout(this.primaryConnectTimerId_); |
| 245 this.onProgressCallback_( | 277 this.updateProgress_( |
| 246 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED); | 278 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED); |
| 247 this.connectSecondary_(); | 279 this.connectSecondary_(); |
| 248 } else { | 280 } else { |
| 249 this.onProgressCallback_( | 281 this.updateProgress_( |
| 250 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED_LATE); | 282 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED_LATE); |
| 251 } | 283 } |
| 252 return; // Don't notify the external callback | 284 return; // Don't notify the external callback |
| 253 | 285 |
| 254 case remoting.SignalStrategy.State.CLOSED: | 286 case remoting.SignalStrategy.State.CLOSED: |
| 255 this.state_ = this.State.CLOSED; | 287 this.state_ = this.State.CLOSED; |
| 256 break; | 288 break; |
| 257 } | 289 } |
| 258 | 290 |
| 259 this.notifyExternalCallback_(state); | 291 this.notifyExternalCallback_(state); |
| 260 }; | 292 }; |
| 261 | 293 |
| 262 /** | 294 /** |
| 263 * @param {remoting.SignalStrategy.State} state | 295 * @param {remoting.SignalStrategy.State} state |
| 264 * @private | 296 * @private |
| 265 */ | 297 */ |
| 266 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = | 298 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = |
| 267 function(state) { | 299 function(state) { |
| 268 switch (state) { | 300 switch (state) { |
| 269 case remoting.SignalStrategy.State.CONNECTED: | 301 case remoting.SignalStrategy.State.CONNECTED: |
| 270 this.onProgressCallback_( | 302 this.updateProgress_( |
| 271 remoting.FallbackSignalStrategy.Progress.SECONDARY_SUCCEEDED); | 303 remoting.FallbackSignalStrategy.Progress.SECONDARY_SUCCEEDED); |
| 272 this.state_ = this.State.SECONDARY_SUCCEEDED; | 304 this.state_ = this.State.SECONDARY_SUCCEEDED; |
| 273 break; | 305 break; |
| 274 | 306 |
| 275 case remoting.SignalStrategy.State.FAILED: | 307 case remoting.SignalStrategy.State.FAILED: |
| 276 this.onProgressCallback_( | 308 this.updateProgress_( |
| 277 remoting.FallbackSignalStrategy.Progress.SECONDARY_FAILED); | 309 remoting.FallbackSignalStrategy.Progress.SECONDARY_FAILED); |
| 278 this.state_ = this.State.SECONDARY_FAILED; | 310 this.state_ = this.State.SECONDARY_FAILED; |
| 279 break; | 311 break; |
| 280 | 312 |
| 281 case remoting.SignalStrategy.State.CLOSED: | 313 case remoting.SignalStrategy.State.CLOSED: |
| 282 this.state_ = this.State.CLOSED; | 314 this.state_ = this.State.CLOSED; |
| 283 break; | 315 break; |
| 284 } | 316 } |
| 285 | 317 |
| 286 this.notifyExternalCallback_(state); | 318 this.notifyExternalCallback_(state); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 316 this.state_ = this.State.SECONDARY_PENDING; | 348 this.state_ = this.State.SECONDARY_PENDING; |
| 317 this.primary_.setIncomingStanzaCallback(null); | 349 this.primary_.setIncomingStanzaCallback(null); |
| 318 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 350 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
| 319 this.secondary_.connect(this.server_, this.username_, this.authToken_); | 351 this.secondary_.connect(this.server_, this.username_, this.authToken_); |
| 320 }; | 352 }; |
| 321 | 353 |
| 322 /** | 354 /** |
| 323 * @private | 355 * @private |
| 324 */ | 356 */ |
| 325 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { | 357 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { |
| 326 this.onProgressCallback_( | 358 this.updateProgress_( |
| 327 remoting.FallbackSignalStrategy.Progress.PRIMARY_TIMED_OUT); | 359 remoting.FallbackSignalStrategy.Progress.PRIMARY_TIMED_OUT); |
| 328 this.connectSecondary_(); | 360 this.connectSecondary_(); |
| 329 }; | 361 }; |
| 362 | |
| 363 /** | |
| 364 * @param {remoting.FallbackSignalStrategy.Progress} progress | |
| 365 * @private | |
| 366 */ | |
| 367 remoting.FallbackSignalStrategy.prototype.updateProgress_ = function(progress) { | |
| 368 console.log('FallbackSignalStrategy progress: ' + progress); | |
| 369 this.connectionSetupResults_.push({ | |
| 370 'progress': progress, | |
| 371 'elapsed': new Date().getTime() - this.startTime_ | |
| 372 }); | |
| 373 if (this.logToServer_) { | |
| 374 this.sendConnectionSetupResultsInternal_(); | |
| 375 } | |
| 376 }; | |
| OLD | NEW |