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.<{strategyType: remoting.SignalStrategy.Type, |
| 117 progress: remoting.FallbackSignalStrategy.Progress, |
| 118 * elapsed: number}>} |
| 119 */ |
| 120 this.connectionSetupResults_ = []; |
| 121 |
| 122 /** |
| 123 * @type {number} |
| 124 * @private |
| 125 */ |
| 126 this.startTime_ = 0; |
117 }; | 127 }; |
118 | 128 |
119 /** | 129 /** |
120 * @enum {string} | 130 * @enum {string} |
121 */ | 131 */ |
122 remoting.FallbackSignalStrategy.Progress = { | 132 remoting.FallbackSignalStrategy.Progress = { |
123 PRIMARY_SUCCEEDED: 'primary-succeeded', | 133 SUCCEEDED: 'succeeded', |
124 PRIMARY_FAILED: 'primary-failed', | 134 FAILED: 'failed', |
125 PRIMARY_TIMED_OUT: 'primary-timed-out', | 135 TIMED_OUT: 'timed-out', |
126 PRIMARY_SUCCEEDED_LATE: 'primary-succeeded-late', | 136 SUCCEEDED_LATE: 'succeeded-late', |
127 PRIMARY_FAILED_LATE: 'primary-failed-late', | 137 FAILED_LATE: 'failed-late', |
128 SECONDARY_SUCCEEDED: 'secondary-succeeded', | |
129 SECONDARY_FAILED: 'secondary-failed' | |
130 }; | 138 }; |
131 | 139 |
132 remoting.FallbackSignalStrategy.prototype.dispose = function() { | 140 remoting.FallbackSignalStrategy.prototype.dispose = function() { |
133 this.primary_.dispose(); | 141 this.primary_.dispose(); |
134 this.secondary_.dispose(); | 142 this.secondary_.dispose(); |
135 }; | 143 }; |
136 | 144 |
137 /** | 145 /** |
138 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on | 146 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on |
139 * incoming messages. | 147 * incoming messages. |
(...skipping 15 matching lines...) Expand all Loading... |
155 * @param {string} username | 163 * @param {string} username |
156 * @param {string} authToken | 164 * @param {string} authToken |
157 */ | 165 */ |
158 remoting.FallbackSignalStrategy.prototype.connect = | 166 remoting.FallbackSignalStrategy.prototype.connect = |
159 function(server, username, authToken) { | 167 function(server, username, authToken) { |
160 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); | 168 base.debug.assert(this.state_ == this.State.NOT_CONNECTED); |
161 this.server_ = server; | 169 this.server_ = server; |
162 this.username_ = username; | 170 this.username_ = username; |
163 this.authToken_ = authToken; | 171 this.authToken_ = authToken; |
164 this.state_ = this.State.PRIMARY_PENDING; | 172 this.state_ = this.State.PRIMARY_PENDING; |
| 173 this.startTime_ = new Date().getTime(); |
165 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 174 this.primary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
166 this.primary_.connect(server, username, authToken); | 175 this.primary_.connect(server, username, authToken); |
167 this.primaryConnectTimerId_ = | 176 this.primaryConnectTimerId_ = |
168 window.setTimeout(this.onPrimaryTimeout_.bind(this), | 177 window.setTimeout(this.onPrimaryTimeout_.bind(this), |
169 this.PRIMARY_CONNECT_TIMEOUT_MS_); | 178 this.PRIMARY_CONNECT_TIMEOUT_MS_); |
170 }; | 179 }; |
171 | 180 |
172 /** | 181 /** |
173 * Sends a message. Can be called only in CONNECTED state. | 182 * Sends a message. Can be called only in CONNECTED state. |
174 * @param {string} message | 183 * @param {string} message |
175 */ | 184 */ |
176 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { | 185 remoting.FallbackSignalStrategy.prototype.sendMessage = function(message) { |
177 this.getConnectedSignalStrategy_().sendMessage(message); | 186 this.getConnectedSignalStrategy_().sendMessage(message); |
178 }; | 187 }; |
179 | 188 |
| 189 /** |
| 190 * Send any messages accumulated during connection set-up. |
| 191 * |
| 192 * @param {remoting.LogToServer} logToServer The LogToServer instance for the |
| 193 * connection. |
| 194 */ |
| 195 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResults = |
| 196 function(logToServer) { |
| 197 this.logToServer_ = logToServer; |
| 198 this.sendConnectionSetupResultsInternal_(); |
| 199 } |
| 200 |
| 201 remoting.FallbackSignalStrategy.prototype.sendConnectionSetupResultsInternal_ = |
| 202 function() { |
| 203 for (var i = 0; i < this.connectionSetupResults_.length; ++i) { |
| 204 var result = this.connectionSetupResults_[i]; |
| 205 this.logToServer_.logSignalStrategyProgress(result.strategyType, |
| 206 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); |
190 base.debug.assert( | 222 base.debug.assert( |
191 this.secondary_.getState() == remoting.SignalStrategy.State.FAILED); | 223 this.secondary_.getState() == remoting.SignalStrategy.State.FAILED); |
192 return this.secondary_.getError(); | 224 return this.secondary_.getError(); |
193 }; | 225 }; |
194 | 226 |
195 /** @return {string} Current JID when in CONNECTED state. */ | 227 /** @return {string} Current JID when in CONNECTED state. */ |
196 remoting.FallbackSignalStrategy.prototype.getJid = function() { | 228 remoting.FallbackSignalStrategy.prototype.getJid = function() { |
197 return this.getConnectedSignalStrategy_().getJid(); | 229 return this.getConnectedSignalStrategy_().getJid(); |
198 }; | 230 }; |
199 | 231 |
| 232 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */ |
| 233 remoting.FallbackSignalStrategy.prototype.getType = function() { |
| 234 return this.getConnectedSignalStrategy_().getType(); |
| 235 }; |
| 236 |
200 /** | 237 /** |
201 * @return {remoting.SignalStrategy} The active signal strategy, if the | 238 * @return {remoting.SignalStrategy} The active signal strategy, if the |
202 * connection has succeeded. | 239 * connection has succeeded. |
203 * @private | 240 * @private |
204 */ | 241 */ |
205 remoting.FallbackSignalStrategy.prototype.getConnectedSignalStrategy_ = | 242 remoting.FallbackSignalStrategy.prototype.getConnectedSignalStrategy_ = |
206 function() { | 243 function() { |
207 if (this.state_ == this.State.PRIMARY_SUCCEEDED) { | 244 if (this.state_ == this.State.PRIMARY_SUCCEEDED) { |
208 base.debug.assert( | 245 base.debug.assert( |
209 this.primary_.getState() == remoting.SignalStrategy.State.CONNECTED); | 246 this.primary_.getState() == remoting.SignalStrategy.State.CONNECTED); |
(...skipping 13 matching lines...) Expand all Loading... |
223 /** | 260 /** |
224 * @param {remoting.SignalStrategy.State} state | 261 * @param {remoting.SignalStrategy.State} state |
225 * @private | 262 * @private |
226 */ | 263 */ |
227 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = | 264 remoting.FallbackSignalStrategy.prototype.onPrimaryStateChanged_ = |
228 function(state) { | 265 function(state) { |
229 switch (state) { | 266 switch (state) { |
230 case remoting.SignalStrategy.State.CONNECTED: | 267 case remoting.SignalStrategy.State.CONNECTED: |
231 if (this.state_ == this.State.PRIMARY_PENDING) { | 268 if (this.state_ == this.State.PRIMARY_PENDING) { |
232 window.clearTimeout(this.primaryConnectTimerId_); | 269 window.clearTimeout(this.primaryConnectTimerId_); |
233 this.onProgressCallback_( | 270 this.updateProgress_( |
234 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED); | 271 this.primary_, |
| 272 remoting.FallbackSignalStrategy.Progress.SUCCEEDED); |
235 this.state_ = this.State.PRIMARY_SUCCEEDED; | 273 this.state_ = this.State.PRIMARY_SUCCEEDED; |
236 } else { | 274 } else { |
237 this.onProgressCallback_( | 275 this.updateProgress_( |
238 remoting.FallbackSignalStrategy.Progress.PRIMARY_SUCCEEDED_LATE); | 276 this.primary_, |
| 277 remoting.FallbackSignalStrategy.Progress.SUCCEEDED_LATE); |
239 } | 278 } |
240 break; | 279 break; |
241 | 280 |
242 case remoting.SignalStrategy.State.FAILED: | 281 case remoting.SignalStrategy.State.FAILED: |
243 if (this.state_ == this.State.PRIMARY_PENDING) { | 282 if (this.state_ == this.State.PRIMARY_PENDING) { |
244 window.clearTimeout(this.primaryConnectTimerId_); | 283 window.clearTimeout(this.primaryConnectTimerId_); |
245 this.onProgressCallback_( | 284 this.updateProgress_( |
246 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED); | 285 this.primary_, |
| 286 remoting.FallbackSignalStrategy.Progress.FAILED); |
247 this.connectSecondary_(); | 287 this.connectSecondary_(); |
248 } else { | 288 } else { |
249 this.onProgressCallback_( | 289 this.updateProgress_( |
250 remoting.FallbackSignalStrategy.Progress.PRIMARY_FAILED_LATE); | 290 this.primary_, |
| 291 remoting.FallbackSignalStrategy.Progress.FAILED_LATE); |
251 } | 292 } |
252 return; // Don't notify the external callback | 293 return; // Don't notify the external callback |
253 | 294 |
254 case remoting.SignalStrategy.State.CLOSED: | 295 case remoting.SignalStrategy.State.CLOSED: |
255 this.state_ = this.State.CLOSED; | 296 this.state_ = this.State.CLOSED; |
256 break; | 297 break; |
257 } | 298 } |
258 | 299 |
259 this.notifyExternalCallback_(state); | 300 this.notifyExternalCallback_(state); |
260 }; | 301 }; |
261 | 302 |
262 /** | 303 /** |
263 * @param {remoting.SignalStrategy.State} state | 304 * @param {remoting.SignalStrategy.State} state |
264 * @private | 305 * @private |
265 */ | 306 */ |
266 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = | 307 remoting.FallbackSignalStrategy.prototype.onSecondaryStateChanged_ = |
267 function(state) { | 308 function(state) { |
268 switch (state) { | 309 switch (state) { |
269 case remoting.SignalStrategy.State.CONNECTED: | 310 case remoting.SignalStrategy.State.CONNECTED: |
270 this.onProgressCallback_( | 311 this.updateProgress_( |
271 remoting.FallbackSignalStrategy.Progress.SECONDARY_SUCCEEDED); | 312 this.secondary_, |
| 313 remoting.FallbackSignalStrategy.Progress.SUCCEEDED); |
272 this.state_ = this.State.SECONDARY_SUCCEEDED; | 314 this.state_ = this.State.SECONDARY_SUCCEEDED; |
273 break; | 315 break; |
274 | 316 |
275 case remoting.SignalStrategy.State.FAILED: | 317 case remoting.SignalStrategy.State.FAILED: |
276 this.onProgressCallback_( | 318 this.updateProgress_( |
277 remoting.FallbackSignalStrategy.Progress.SECONDARY_FAILED); | 319 this.secondary_, |
| 320 remoting.FallbackSignalStrategy.Progress.FAILED); |
278 this.state_ = this.State.SECONDARY_FAILED; | 321 this.state_ = this.State.SECONDARY_FAILED; |
279 break; | 322 break; |
280 | 323 |
281 case remoting.SignalStrategy.State.CLOSED: | 324 case remoting.SignalStrategy.State.CLOSED: |
282 this.state_ = this.State.CLOSED; | 325 this.state_ = this.State.CLOSED; |
283 break; | 326 break; |
284 } | 327 } |
285 | 328 |
286 this.notifyExternalCallback_(state); | 329 this.notifyExternalCallback_(state); |
287 }; | 330 }; |
(...skipping 28 matching lines...) Expand all Loading... |
316 this.state_ = this.State.SECONDARY_PENDING; | 359 this.state_ = this.State.SECONDARY_PENDING; |
317 this.primary_.setIncomingStanzaCallback(null); | 360 this.primary_.setIncomingStanzaCallback(null); |
318 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); | 361 this.secondary_.setIncomingStanzaCallback(this.onIncomingStanzaCallback_); |
319 this.secondary_.connect(this.server_, this.username_, this.authToken_); | 362 this.secondary_.connect(this.server_, this.username_, this.authToken_); |
320 }; | 363 }; |
321 | 364 |
322 /** | 365 /** |
323 * @private | 366 * @private |
324 */ | 367 */ |
325 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { | 368 remoting.FallbackSignalStrategy.prototype.onPrimaryTimeout_ = function() { |
326 this.onProgressCallback_( | 369 this.updateProgress_( |
327 remoting.FallbackSignalStrategy.Progress.PRIMARY_TIMED_OUT); | 370 this.primary_, |
| 371 remoting.FallbackSignalStrategy.Progress.TIMED_OUT); |
328 this.connectSecondary_(); | 372 this.connectSecondary_(); |
329 }; | 373 }; |
| 374 |
| 375 /** |
| 376 * @param {remoting.SignalStrategy} strategy |
| 377 * @param {remoting.FallbackSignalStrategy.Progress} progress |
| 378 * @private |
| 379 */ |
| 380 remoting.FallbackSignalStrategy.prototype.updateProgress_ = function( |
| 381 strategy, progress) { |
| 382 console.log('FallbackSignalStrategy progress: ' + strategy.getType() + ' ' + |
| 383 progress); |
| 384 this.connectionSetupResults_.push({ |
| 385 'strategyType': strategy.getType(), |
| 386 'progress': progress, |
| 387 'elapsed': new Date().getTime() - this.startTime_ |
| 388 }); |
| 389 if (this.logToServer_) { |
| 390 this.sendConnectionSetupResultsInternal_(); |
| 391 } |
| 392 }; |
OLD | NEW |