| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Definition of the BrowserChannel class. A BrowserChannel |
| 17 |
* simulates a bidirectional socket over HTTP. It is the basis of the |
| 18 |
* Gmail Chat IM connections to the server. |
| 19 |
* |
| 20 |
* Typical usage will look like |
| 21 |
* var handler = [handler object]; |
| 22 |
* var channel = new BrowserChannel(clientVersion); |
| 23 |
* channel.setHandler(handler); |
| 24 |
* channel.connect('channel/test', 'channel/bind'); |
| 25 |
* |
| 26 |
* See goog.net.BrowserChannel.Handler for the handler interface. |
| 27 |
* |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
goog.provide('goog.net.BrowserChannel'); |
| 32 |
goog.provide('goog.net.BrowserChannel.Error'); |
| 33 |
goog.provide('goog.net.BrowserChannel.Event'); |
| 34 |
goog.provide('goog.net.BrowserChannel.Handler'); |
| 35 |
goog.provide('goog.net.BrowserChannel.LogSaver'); |
| 36 |
goog.provide('goog.net.BrowserChannel.QueuedMap'); |
| 37 |
goog.provide('goog.net.BrowserChannel.ServerReachability'); |
| 38 |
goog.provide('goog.net.BrowserChannel.ServerReachabilityEvent'); |
| 39 |
goog.provide('goog.net.BrowserChannel.Stat'); |
| 40 |
goog.provide('goog.net.BrowserChannel.StatEvent'); |
| 41 |
goog.provide('goog.net.BrowserChannel.State'); |
| 42 |
goog.provide('goog.net.BrowserChannel.TimingEvent'); |
| 43 |
|
| 44 |
goog.require('goog.Uri'); |
| 45 |
goog.require('goog.array'); |
| 46 |
goog.require('goog.asserts'); |
| 47 |
goog.require('goog.debug.TextFormatter'); |
| 48 |
goog.require('goog.events.Event'); |
| 49 |
goog.require('goog.events.EventTarget'); |
| 50 |
goog.require('goog.json'); |
| 51 |
goog.require('goog.json.NativeJsonProcessor'); |
| 52 |
goog.require('goog.log'); |
| 53 |
goog.require('goog.net.BrowserTestChannel'); |
| 54 |
goog.require('goog.net.ChannelDebug'); |
| 55 |
goog.require('goog.net.ChannelRequest'); |
| 56 |
goog.require('goog.net.XhrIo'); |
| 57 |
goog.require('goog.net.tmpnetwork'); |
| 58 |
goog.require('goog.object'); |
| 59 |
goog.require('goog.string'); |
| 60 |
goog.require('goog.structs'); |
| 61 |
goog.require('goog.structs.CircularBuffer'); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Encapsulates the logic for a single BrowserChannel. |
| 67 |
* |
| 68 |
* @param {string=} opt_clientVersion An application-specific version number |
| 69 |
* that is sent to the server when connected. |
| 70 |
* @param {Array<string>=} opt_firstTestResults Previously determined results |
| 71 |
* of the first browser channel test. |
| 72 |
* @param {boolean=} opt_secondTestResults Previously determined results |
| 73 |
* of the second browser channel test. |
| 74 |
* @param {boolean=} opt_asyncTest Whether to perform the test requests |
| 75 |
* asynchronously. While the test is performed, we'll assume the worst |
| 76 |
* (connection is buffered), in order to avoid delaying the connection |
| 77 |
* until the test is performed. |
| 78 |
* @constructor |
| 79 |
*/ |
| 80 |
goog.net.BrowserChannel = function( |
| 81 |
opt_clientVersion, opt_firstTestResults, opt_secondTestResults, |
| 82 |
opt_asyncTest) { |
| 83 |
/** |
| 84 |
* The application specific version that is passed to the server. |
| 85 |
* @type {?string} |
| 86 |
* @private |
| 87 |
*/ |
| 88 |
this.clientVersion_ = opt_clientVersion || null; |
| 89 |
|
| 90 |
/** |
| 91 |
* The current state of the BrowserChannel. It should be one of the |
| 92 |
* goog.net.BrowserChannel.State constants. |
| 93 |
* @type {!goog.net.BrowserChannel.State} |
| 94 |
* @private |
| 95 |
*/ |
| 96 |
this.state_ = goog.net.BrowserChannel.State.INIT; |
| 97 |
|
| 98 |
/** |
| 99 |
* An array of queued maps that need to be sent to the server. |
| 100 |
* @type {Array<goog.net.BrowserChannel.QueuedMap>} |
| 101 |
* @private |
| 102 |
*/ |
| 103 |
this.outgoingMaps_ = []; |
| 104 |
|
| 105 |
/** |
| 106 |
* An array of dequeued maps that we have either received a non-successful |
| 107 |
* response for, or no response at all, and which therefore may or may not |
| 108 |
* have been received by the server. |
| 109 |
* @type {Array<goog.net.BrowserChannel.QueuedMap>} |
| 110 |
* @private |
| 111 |
*/ |
| 112 |
this.pendingMaps_ = []; |
| 113 |
|
| 114 |
/** |
| 115 |
* The channel debug used for browserchannel logging |
| 116 |
* @type {!goog.net.ChannelDebug} |
| 117 |
* @private |
| 118 |
*/ |
| 119 |
this.channelDebug_ = new goog.net.ChannelDebug(); |
| 120 |
|
| 121 |
/** |
| 122 |
* Parser for a response payload. The parser should return an array. |
| 123 |
* @type {!goog.string.Parser} |
| 124 |
* @private |
| 125 |
*/ |
| 126 |
this.parser_ = new goog.json.NativeJsonProcessor(); |
| 127 |
|
| 128 |
/** |
| 129 |
* An array of results for the first browser channel test call. |
| 130 |
* @type {Array<string>} |
| 131 |
* @private |
| 132 |
*/ |
| 133 |
this.firstTestResults_ = opt_firstTestResults || null; |
| 134 |
|
| 135 |
/** |
| 136 |
* The results of the second browser channel test. True implies the |
| 137 |
* connection is buffered, False means unbuffered, null means that |
| 138 |
* the results are not available. |
| 139 |
* @private |
| 140 |
*/ |
| 141 |
this.secondTestResults_ = goog.isDefAndNotNull(opt_secondTestResults) ? |
| 142 |
opt_secondTestResults : |
| 143 |
null; |
| 144 |
|
| 145 |
/** |
| 146 |
* Whether to perform the test requests asynchronously. While the test is |
| 147 |
* performed, we'll assume the worst (connection is buffered), in order to |
| 148 |
* avoid delaying the connection until the test is performed. |
| 149 |
* @private {boolean} |
| 150 |
*/ |
| 151 |
this.asyncTest_ = opt_asyncTest || false; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Simple container class for a (mapId, map) pair. |
| 158 |
* @param {number} mapId The id for this map. |
| 159 |
* @param {Object|goog.structs.Map} map The map itself. |
| 160 |
* @param {Object=} opt_context The context associated with the map. |
| 161 |
* @constructor |
| 162 |
* @final |
| 163 |
*/ |
| 164 |
goog.net.BrowserChannel.QueuedMap = function(mapId, map, opt_context) { |
| 165 |
/** |
| 166 |
* The id for this map. |
| 167 |
* @type {number} |
| 168 |
*/ |
| 169 |
this.mapId = mapId; |
| 170 |
|
| 171 |
/** |
| 172 |
* The map itself. |
| 173 |
* @type {Object} |
| 174 |
*/ |
| 175 |
this.map = map; |
| 176 |
|
| 177 |
/** |
| 178 |
* The context for the map. |
| 179 |
* @type {Object} |
| 180 |
*/ |
| 181 |
this.context = opt_context || null; |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
/** |
| 186 |
* Extra HTTP headers to add to all the requests sent to the server. |
| 187 |
* @type {Object} |
| 188 |
* @private |
| 189 |
*/ |
| 190 |
goog.net.BrowserChannel.prototype.extraHeaders_ = null; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Extra parameters to add to all the requests sent to the server. |
| 195 |
* @type {Object} |
| 196 |
* @private |
| 197 |
*/ |
| 198 |
goog.net.BrowserChannel.prototype.extraParams_ = null; |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* The current ChannelRequest object for the forwardchannel. |
| 203 |
* @type {goog.net.ChannelRequest?} |
| 204 |
* @private |
| 205 |
*/ |
| 206 |
goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* The ChannelRequest object for the backchannel. |
| 211 |
* @type {goog.net.ChannelRequest?} |
| 212 |
* @private |
| 213 |
*/ |
| 214 |
goog.net.BrowserChannel.prototype.backChannelRequest_ = null; |
| 215 |
|
| 216 |
|
| 217 |
/** |
| 218 |
* The relative path (in the context of the the page hosting the browser |
| 219 |
* channel) for making requests to the server. |
| 220 |
* @type {?string} |
| 221 |
* @private |
| 222 |
*/ |
| 223 |
goog.net.BrowserChannel.prototype.path_ = null; |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* The absolute URI for the forwardchannel request. |
| 228 |
* @type {goog.Uri} |
| 229 |
* @private |
| 230 |
*/ |
| 231 |
goog.net.BrowserChannel.prototype.forwardChannelUri_ = null; |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* The absolute URI for the backchannel request. |
| 236 |
* @type {goog.Uri} |
| 237 |
* @private |
| 238 |
*/ |
| 239 |
goog.net.BrowserChannel.prototype.backChannelUri_ = null; |
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* A subdomain prefix for using a subdomain in IE for the backchannel |
| 244 |
* requests. |
| 245 |
* @type {?string} |
| 246 |
* @private |
| 247 |
*/ |
| 248 |
goog.net.BrowserChannel.prototype.hostPrefix_ = null; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Whether we allow the use of a subdomain in IE for the backchannel requests. |
| 253 |
* @private |
| 254 |
*/ |
| 255 |
goog.net.BrowserChannel.prototype.allowHostPrefix_ = true; |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* The next id to use for the RID (request identifier) parameter. This |
| 260 |
* identifier uniquely identifies the forward channel request. |
| 261 |
* @type {number} |
| 262 |
* @private |
| 263 |
*/ |
| 264 |
goog.net.BrowserChannel.prototype.nextRid_ = 0; |
| 265 |
|
| 266 |
|
| 267 |
/** |
| 268 |
* The id to use for the next outgoing map. This identifier uniquely |
| 269 |
* identifies a sent map. |
| 270 |
* @type {number} |
| 271 |
* @private |
| 272 |
*/ |
| 273 |
goog.net.BrowserChannel.prototype.nextMapId_ = 0; |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Whether to fail forward-channel requests after one try, or after a few tries. |
| 278 |
* @type {boolean} |
| 279 |
* @private |
| 280 |
*/ |
| 281 |
goog.net.BrowserChannel.prototype.failFast_ = false; |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* The handler that receive callbacks for state changes and data. |
| 286 |
* @type {goog.net.BrowserChannel.Handler} |
| 287 |
* @private |
| 288 |
*/ |
| 289 |
goog.net.BrowserChannel.prototype.handler_ = null; |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Timer identifier for asynchronously making a forward channel request. |
| 294 |
* @type {?number} |
| 295 |
* @private |
| 296 |
*/ |
| 297 |
goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null; |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* Timer identifier for asynchronously making a back channel request. |
| 302 |
* @type {?number} |
| 303 |
* @private |
| 304 |
*/ |
| 305 |
goog.net.BrowserChannel.prototype.backChannelTimerId_ = null; |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* Timer identifier for the timer that waits for us to retry the backchannel in |
| 310 |
* the case where it is dead and no longer receiving data. |
| 311 |
* @type {?number} |
| 312 |
* @private |
| 313 |
*/ |
| 314 |
goog.net.BrowserChannel.prototype.deadBackChannelTimerId_ = null; |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* The BrowserTestChannel object which encapsulates the logic for determining |
| 319 |
* interesting network conditions about the client. |
| 320 |
* @type {goog.net.BrowserTestChannel?} |
| 321 |
* @private |
| 322 |
*/ |
| 323 |
goog.net.BrowserChannel.prototype.connectionTest_ = null; |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* Whether the client's network conditions can support chunked responses. |
| 328 |
* @type {?boolean} |
| 329 |
* @private |
| 330 |
*/ |
| 331 |
goog.net.BrowserChannel.prototype.useChunked_ = null; |
| 332 |
|
| 333 |
|
| 334 |
/** |
| 335 |
* Whether chunked mode is allowed. In certain debugging situations, it's |
| 336 |
* useful to disable this. |
| 337 |
* @private |
| 338 |
*/ |
| 339 |
goog.net.BrowserChannel.prototype.allowChunkedMode_ = true; |
| 340 |
|
| 341 |
|
| 342 |
/** |
| 343 |
* The array identifier of the last array received from the server for the |
| 344 |
* backchannel request. |
| 345 |
* @type {number} |
| 346 |
* @private |
| 347 |
*/ |
| 348 |
goog.net.BrowserChannel.prototype.lastArrayId_ = -1; |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* The array identifier of the last array sent by the server that we know about. |
| 353 |
* @type {number} |
| 354 |
* @private |
| 355 |
*/ |
| 356 |
goog.net.BrowserChannel.prototype.lastPostResponseArrayId_ = -1; |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* The last status code received. |
| 361 |
* @type {number} |
| 362 |
* @private |
| 363 |
*/ |
| 364 |
goog.net.BrowserChannel.prototype.lastStatusCode_ = -1; |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Number of times we have retried the current forward channel request. |
| 369 |
* @type {number} |
| 370 |
* @private |
| 371 |
*/ |
| 372 |
goog.net.BrowserChannel.prototype.forwardChannelRetryCount_ = 0; |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* Number of times it a row that we have retried the current back channel |
| 377 |
* request and received no data. |
| 378 |
* @type {number} |
| 379 |
* @private |
| 380 |
*/ |
| 381 |
goog.net.BrowserChannel.prototype.backChannelRetryCount_ = 0; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* The attempt id for the current back channel request. Starts at 1 and |
| 386 |
* increments for each reconnect. The server uses this to log if our connection |
| 387 |
* is flaky or not. |
| 388 |
* @type {number} |
| 389 |
* @private |
| 390 |
*/ |
| 391 |
goog.net.BrowserChannel.prototype.backChannelAttemptId_; |
| 392 |
|
| 393 |
|
| 394 |
/** |
| 395 |
* The base part of the time before firing next retry request. Default is 5 |
| 396 |
* seconds. Note that a random delay is added (see {@link retryDelaySeedMs_}) |
| 397 |
* for all retries, and linear backoff is applied to the sum for subsequent |
| 398 |
* retries. |
| 399 |
* @type {number} |
| 400 |
* @private |
| 401 |
*/ |
| 402 |
goog.net.BrowserChannel.prototype.baseRetryDelayMs_ = 5 * 1000; |
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* A random time between 0 and this number of MS is added to the |
| 407 |
* {@link baseRetryDelayMs_}. Default is 10 seconds. |
| 408 |
* @type {number} |
| 409 |
* @private |
| 410 |
*/ |
| 411 |
goog.net.BrowserChannel.prototype.retryDelaySeedMs_ = 10 * 1000; |
| 412 |
|
| 413 |
|
| 414 |
/** |
| 415 |
* Maximum number of attempts to connect to the server for forward channel |
| 416 |
* requests. Defaults to 2. |
| 417 |
* @type {number} |
| 418 |
* @private |
| 419 |
*/ |
| 420 |
goog.net.BrowserChannel.prototype.forwardChannelMaxRetries_ = 2; |
| 421 |
|
| 422 |
|
| 423 |
/** |
| 424 |
* The timeout in milliseconds for a forward channel request. Defaults to 20 |
| 425 |
* seconds. Note that part of this timeout can be randomized. |
| 426 |
* @type {number} |
| 427 |
* @private |
| 428 |
*/ |
| 429 |
goog.net.BrowserChannel.prototype.forwardChannelRequestTimeoutMs_ = 20 * 1000; |
| 430 |
|
| 431 |
|
| 432 |
/** |
| 433 |
* A throttle time in ms for readystatechange events for the backchannel. |
| 434 |
* Useful for throttling when ready state is INTERACTIVE (partial data). |
| 435 |
* |
| 436 |
* This throttle is useful if the server sends large data chunks down the |
| 437 |
* backchannel. It prevents examining XHR partial data on every |
| 438 |
* readystate change event. This is useful because large chunks can |
| 439 |
* trigger hundreds of readystatechange events, each of which takes ~5ms |
| 440 |
* or so to handle, in turn making the UI unresponsive for a significant period. |
| 441 |
* |
| 442 |
* If set to zero no throttle is used. |
| 443 |
* @type {number} |
| 444 |
* @private |
| 445 |
*/ |
| 446 |
goog.net.BrowserChannel.prototype.readyStateChangeThrottleMs_ = 0; |
| 447 |
|
| 448 |
|
| 449 |
/** |
| 450 |
* Whether cross origin requests are supported for the browser channel. |
| 451 |
* |
| 452 |
* See {@link goog.net.XhrIo#setWithCredentials}. |
| 453 |
* @type {boolean} |
| 454 |
* @private |
| 455 |
*/ |
| 456 |
goog.net.BrowserChannel.prototype.supportsCrossDomainXhrs_ = false; |
| 457 |
|
| 458 |
|
| 459 |
/** |
| 460 |
* The latest protocol version that this class supports. We request this version |
| 461 |
* from the server when opening the connection. Should match |
| 462 |
* com.google.net.browserchannel.BrowserChannel.LATEST_CHANNEL_VERSION. |
| 463 |
* @type {number} |
| 464 |
*/ |
| 465 |
goog.net.BrowserChannel.LATEST_CHANNEL_VERSION = 8; |
| 466 |
|
| 467 |
|
| 468 |
/** |
| 469 |
* The channel version that we negotiated with the server for this session. |
| 470 |
* Starts out as the version we request, and then is changed to the negotiated |
| 471 |
* version after the initial open. |
| 472 |
* @type {number} |
| 473 |
* @private |
| 474 |
*/ |
| 475 |
goog.net.BrowserChannel.prototype.channelVersion_ = |
| 476 |
goog.net.BrowserChannel.LATEST_CHANNEL_VERSION; |
| 477 |
|
| 478 |
|
| 479 |
/** |
| 480 |
* Enum type for the browser channel state machine. |
| 481 |
* @enum {number} |
| 482 |
*/ |
| 483 |
goog.net.BrowserChannel.State = { |
| 484 |
/** The channel is closed. */ |
| 485 |
CLOSED: 0, |
| 486 |
|
| 487 |
/** The channel has been initialized but hasn't yet initiated a connection. */ |
| 488 |
INIT: 1, |
| 489 |
|
| 490 |
/** The channel is in the process of opening a connection to the server. */ |
| 491 |
OPENING: 2, |
| 492 |
|
| 493 |
/** The channel is open. */ |
| 494 |
OPENED: 3 |
| 495 |
}; |
| 496 |
|
| 497 |
|
| 498 |
/** |
| 499 |
* The timeout in milliseconds for a forward channel request. |
| 500 |
* @type {number} |
| 501 |
*/ |
| 502 |
goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000; |
| 503 |
|
| 504 |
|
| 505 |
/** |
| 506 |
* Maximum number of attempts to connect to the server for back channel |
| 507 |
* requests. |
| 508 |
* @type {number} |
| 509 |
*/ |
| 510 |
goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES = 3; |
| 511 |
|
| 512 |
|
| 513 |
/** |
| 514 |
* A number in MS of how long we guess the maxmium amount of time a round trip |
| 515 |
* to the server should take. In the future this could be substituted with a |
| 516 |
* real measurement of the RTT. |
| 517 |
* @type {number} |
| 518 |
*/ |
| 519 |
goog.net.BrowserChannel.RTT_ESTIMATE = 3 * 1000; |
| 520 |
|
| 521 |
|
| 522 |
/** |
| 523 |
* When retrying for an inactive channel, we will multiply the total delay by |
| 524 |
* this number. |
| 525 |
* @type {number} |
| 526 |
*/ |
| 527 |
goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR = 2; |
| 528 |
|
| 529 |
|
| 530 |
/** |
| 531 |
* Enum type for identifying a BrowserChannel error. |
| 532 |
* @enum {number} |
| 533 |
*/ |
| 534 |
goog.net.BrowserChannel.Error = { |
| 535 |
/** Value that indicates no error has occurred. */ |
| 536 |
OK: 0, |
| 537 |
|
| 538 |
/** An error due to a request failing. */ |
| 539 |
REQUEST_FAILED: 2, |
| 540 |
|
| 541 |
/** An error due to the user being logged out. */ |
| 542 |
LOGGED_OUT: 4, |
| 543 |
|
| 544 |
/** An error due to server response which contains no data. */ |
| 545 |
NO_DATA: 5, |
| 546 |
|
| 547 |
/** An error due to a server response indicating an unknown session id */ |
| 548 |
UNKNOWN_SESSION_ID: 6, |
| 549 |
|
| 550 |
/** An error due to a server response requesting to stop the channel. */ |
| 551 |
STOP: 7, |
| 552 |
|
| 553 |
/** A general network error. */ |
| 554 |
NETWORK: 8, |
| 555 |
|
| 556 |
/** An error due to the channel being blocked by a network administrator. */ |
| 557 |
BLOCKED: 9, |
| 558 |
|
| 559 |
/** An error due to bad data being returned from the server. */ |
| 560 |
BAD_DATA: 10, |
| 561 |
|
| 562 |
/** An error due to a response that doesn't start with the magic cookie. */ |
| 563 |
BAD_RESPONSE: 11, |
| 564 |
|
| 565 |
/** ActiveX is blocked by the machine's admin settings. */ |
| 566 |
ACTIVE_X_BLOCKED: 12 |
| 567 |
}; |
| 568 |
|
| 569 |
|
| 570 |
/** |
| 571 |
* Internal enum type for the two browser channel channel types. |
| 572 |
* @enum {number} |
| 573 |
* @private |
| 574 |
*/ |
| 575 |
goog.net.BrowserChannel.ChannelType_ = { |
| 576 |
FORWARD_CHANNEL: 1, |
| 577 |
|
| 578 |
BACK_CHANNEL: 2 |
| 579 |
}; |
| 580 |
|
| 581 |
|
| 582 |
/** |
| 583 |
* The maximum number of maps that can be sent in one POST. Should match |
| 584 |
* com.google.net.browserchannel.BrowserChannel.MAX_MAPS_PER_REQUEST. |
| 585 |
* @type {number} |
| 586 |
* @private |
| 587 |
*/ |
| 588 |
goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ = 1000; |
| 589 |
|
| 590 |
|
| 591 |
/** |
| 592 |
* Singleton event target for firing stat events |
| 593 |
* @type {goog.events.EventTarget} |
| 594 |
* @private |
| 595 |
*/ |
| 596 |
goog.net.BrowserChannel.statEventTarget_ = new goog.events.EventTarget(); |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* Events fired by BrowserChannel and associated objects |
| 601 |
* @const |
| 602 |
*/ |
| 603 |
goog.net.BrowserChannel.Event = {}; |
| 604 |
|
| 605 |
|
| 606 |
/** |
| 607 |
* Stat Event that fires when things of interest happen that may be useful for |
| 608 |
* applications to know about for stats or debugging purposes. This event fires |
| 609 |
* on the EventTarget returned by getStatEventTarget. |
| 610 |
*/ |
| 611 |
goog.net.BrowserChannel.Event.STAT_EVENT = 'statevent'; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
/** |
| 616 |
* Event class for goog.net.BrowserChannel.Event.STAT_EVENT |
| 617 |
* |
| 618 |
* @param {goog.events.EventTarget} eventTarget The stat event target for |
| 619 |
the browser channel. |
| 620 |
* @param {goog.net.BrowserChannel.Stat} stat The stat. |
| 621 |
* @constructor |
| 622 |
* @extends {goog.events.Event} |
| 623 |
* @final |
| 624 |
*/ |
| 625 |
goog.net.BrowserChannel.StatEvent = function(eventTarget, stat) { |
| 626 |
goog.events.Event.call( |
| 627 |
this, goog.net.BrowserChannel.Event.STAT_EVENT, eventTarget); |
| 628 |
|
| 629 |
/** |
| 630 |
* The stat |
| 631 |
* @type {goog.net.BrowserChannel.Stat} |
| 632 |
*/ |
| 633 |
this.stat = stat; |
| 634 |
|
| 635 |
}; |
| 636 |
goog.inherits(goog.net.BrowserChannel.StatEvent, goog.events.Event); |
| 637 |
|
| 638 |
|
| 639 |
/** |
| 640 |
* An event that fires when POST requests complete successfully, indicating |
| 641 |
* the size of the POST and the round trip time. |
| 642 |
* This event fires on the EventTarget returned by getStatEventTarget. |
| 643 |
*/ |
| 644 |
goog.net.BrowserChannel.Event.TIMING_EVENT = 'timingevent'; |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
/** |
| 649 |
* Event class for goog.net.BrowserChannel.Event.TIMING_EVENT |
| 650 |
* |
| 651 |
* @param {goog.events.EventTarget} target The stat event target for |
| 652 |
the browser channel. |
| 653 |
* @param {number} size The number of characters in the POST data. |
| 654 |
* @param {number} rtt The total round trip time from POST to response in MS. |
| 655 |
* @param {number} retries The number of times the POST had to be retried. |
| 656 |
* @constructor |
| 657 |
* @extends {goog.events.Event} |
| 658 |
* @final |
| 659 |
*/ |
| 660 |
goog.net.BrowserChannel.TimingEvent = function(target, size, rtt, retries) { |
| 661 |
goog.events.Event.call( |
| 662 |
this, goog.net.BrowserChannel.Event.TIMING_EVENT, target); |
| 663 |
|
| 664 |
/** |
| 665 |
* @type {number} |
| 666 |
*/ |
| 667 |
this.size = size; |
| 668 |
|
| 669 |
/** |
| 670 |
* @type {number} |
| 671 |
*/ |
| 672 |
this.rtt = rtt; |
| 673 |
|
| 674 |
/** |
| 675 |
* @type {number} |
| 676 |
*/ |
| 677 |
this.retries = retries; |
| 678 |
|
| 679 |
}; |
| 680 |
goog.inherits(goog.net.BrowserChannel.TimingEvent, goog.events.Event); |
| 681 |
|
| 682 |
|
| 683 |
/** |
| 684 |
* The type of event that occurs every time some information about how reachable |
| 685 |
* the server is is discovered. |
| 686 |
*/ |
| 687 |
goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT = 'serverreachability'; |
| 688 |
|
| 689 |
|
| 690 |
/** |
| 691 |
* Types of events which reveal information about the reachability of the |
| 692 |
* server. |
| 693 |
* @enum {number} |
| 694 |
*/ |
| 695 |
goog.net.BrowserChannel.ServerReachability = { |
| 696 |
REQUEST_MADE: 1, |
| 697 |
REQUEST_SUCCEEDED: 2, |
| 698 |
REQUEST_FAILED: 3, |
| 699 |
BACK_CHANNEL_ACTIVITY: 4 |
| 700 |
}; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
/** |
| 705 |
* Event class for goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT. |
| 706 |
* |
| 707 |
* @param {goog.events.EventTarget} target The stat event target for |
| 708 |
the browser channel. |
| 709 |
* @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The |
| 710 |
* reachability event type. |
| 711 |
* @constructor |
| 712 |
* @extends {goog.events.Event} |
| 713 |
* @final |
| 714 |
*/ |
| 715 |
goog.net.BrowserChannel.ServerReachabilityEvent = function( |
| 716 |
target, reachabilityType) { |
| 717 |
goog.events.Event.call( |
| 718 |
this, goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT, target); |
| 719 |
|
| 720 |
/** |
| 721 |
* @type {goog.net.BrowserChannel.ServerReachability} |
| 722 |
*/ |
| 723 |
this.reachabilityType = reachabilityType; |
| 724 |
}; |
| 725 |
goog.inherits( |
| 726 |
goog.net.BrowserChannel.ServerReachabilityEvent, goog.events.Event); |
| 727 |
|
| 728 |
|
| 729 |
/** |
| 730 |
* Enum that identifies events for statistics that are interesting to track. |
| 731 |
* TODO(user) - Change name not to use Event or use EventTarget |
| 732 |
* @enum {number} |
| 733 |
*/ |
| 734 |
goog.net.BrowserChannel.Stat = { |
| 735 |
/** Event indicating a new connection attempt. */ |
| 736 |
CONNECT_ATTEMPT: 0, |
| 737 |
|
| 738 |
/** Event indicating a connection error due to a general network problem. */ |
| 739 |
ERROR_NETWORK: 1, |
| 740 |
|
| 741 |
/** |
| 742 |
* Event indicating a connection error that isn't due to a general network |
| 743 |
* problem. |
| 744 |
*/ |
| 745 |
ERROR_OTHER: 2, |
| 746 |
|
| 747 |
/** Event indicating the start of test stage one. */ |
| 748 |
TEST_STAGE_ONE_START: 3, |
| 749 |
|
| 750 |
|
| 751 |
/** Event indicating the channel is blocked by a network administrator. */ |
| 752 |
CHANNEL_BLOCKED: 4, |
| 753 |
|
| 754 |
/** Event indicating the start of test stage two. */ |
| 755 |
TEST_STAGE_TWO_START: 5, |
| 756 |
|
| 757 |
/** Event indicating the first piece of test data was received. */ |
| 758 |
TEST_STAGE_TWO_DATA_ONE: 6, |
| 759 |
|
| 760 |
/** |
| 761 |
* Event indicating that the second piece of test data was received and it was |
| 762 |
* received separately from the first. |
| 763 |
*/ |
| 764 |
TEST_STAGE_TWO_DATA_TWO: 7, |
| 765 |
|
| 766 |
/** Event indicating both pieces of test data were received simultaneously. */ |
| 767 |
TEST_STAGE_TWO_DATA_BOTH: 8, |
| 768 |
|
| 769 |
/** Event indicating stage one of the test request failed. */ |
| 770 |
TEST_STAGE_ONE_FAILED: 9, |
| 771 |
|
| 772 |
/** Event indicating stage two of the test request failed. */ |
| 773 |
TEST_STAGE_TWO_FAILED: 10, |
| 774 |
|
| 775 |
/** |
| 776 |
* Event indicating that a buffering proxy is likely between the client and |
| 777 |
* the server. |
| 778 |
*/ |
| 779 |
PROXY: 11, |
| 780 |
|
| 781 |
/** |
| 782 |
* Event indicating that no buffering proxy is likely between the client and |
| 783 |
* the server. |
| 784 |
*/ |
| 785 |
NOPROXY: 12, |
| 786 |
|
| 787 |
/** Event indicating an unknown SID error. */ |
| 788 |
REQUEST_UNKNOWN_SESSION_ID: 13, |
| 789 |
|
| 790 |
/** Event indicating a bad status code was received. */ |
| 791 |
REQUEST_BAD_STATUS: 14, |
| 792 |
|
| 793 |
/** Event indicating incomplete data was received */ |
| 794 |
REQUEST_INCOMPLETE_DATA: 15, |
| 795 |
|
| 796 |
/** Event indicating bad data was received */ |
| 797 |
REQUEST_BAD_DATA: 16, |
| 798 |
|
| 799 |
/** Event indicating no data was received when data was expected. */ |
| 800 |
REQUEST_NO_DATA: 17, |
| 801 |
|
| 802 |
/** Event indicating a request timeout. */ |
| 803 |
REQUEST_TIMEOUT: 18, |
| 804 |
|
| 805 |
/** |
| 806 |
* Event indicating that the server never received our hanging GET and so it |
| 807 |
* is being retried. |
| 808 |
*/ |
| 809 |
BACKCHANNEL_MISSING: 19, |
| 810 |
|
| 811 |
/** |
| 812 |
* Event indicating that we have determined that our hanging GET is not |
| 813 |
* receiving data when it should be. Thus it is dead dead and will be retried. |
| 814 |
*/ |
| 815 |
BACKCHANNEL_DEAD: 20, |
| 816 |
|
| 817 |
/** |
| 818 |
* The browser declared itself offline during the lifetime of a request, or |
| 819 |
* was offline when a request was initially made. |
| 820 |
*/ |
| 821 |
BROWSER_OFFLINE: 21, |
| 822 |
|
| 823 |
/** ActiveX is blocked by the machine's admin settings. */ |
| 824 |
ACTIVE_X_BLOCKED: 22 |
| 825 |
}; |
| 826 |
|
| 827 |
|
| 828 |
/** |
| 829 |
* A guess at a cutoff at which to no longer assume the backchannel is dead |
| 830 |
* when we are slow to receive data. Number in bytes. |
| 831 |
* |
| 832 |
* Assumption: The worst bandwidth we work on is 50 kilobits/sec |
| 833 |
* 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout |
| 834 |
* @type {number} |
| 835 |
*/ |
| 836 |
goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500; |
| 837 |
|
| 838 |
|
| 839 |
/** |
| 840 |
* Returns the browserchannel logger. |
| 841 |
* |
| 842 |
* @return {!goog.net.ChannelDebug} The channel debug object. |
| 843 |
*/ |
| 844 |
goog.net.BrowserChannel.prototype.getChannelDebug = function() { |
| 845 |
return this.channelDebug_; |
| 846 |
}; |
| 847 |
|
| 848 |
|
| 849 |
/** |
| 850 |
* Set the browserchannel logger. |
| 851 |
* TODO(user): Add interface for channel loggers or remove this function. |
| 852 |
* |
| 853 |
* @param {goog.net.ChannelDebug} channelDebug The channel debug object. |
| 854 |
*/ |
| 855 |
goog.net.BrowserChannel.prototype.setChannelDebug = function(channelDebug) { |
| 856 |
if (goog.isDefAndNotNull(channelDebug)) { |
| 857 |
this.channelDebug_ = channelDebug; |
| 858 |
} |
| 859 |
}; |
| 860 |
|
| 861 |
|
| 862 |
/** |
| 863 |
* Allows the application to set an execution hooks for when BrowserChannel |
| 864 |
* starts processing requests. This is useful to track timing or logging |
| 865 |
* special information. The function takes no parameters and return void. |
| 866 |
* @param {Function} startHook The function for the start hook. |
| 867 |
*/ |
| 868 |
goog.net.BrowserChannel.setStartThreadExecutionHook = function(startHook) { |
| 869 |
goog.net.BrowserChannel.startExecutionHook_ = startHook; |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
/** |
| 874 |
* Allows the application to set an execution hooks for when BrowserChannel |
| 875 |
* stops processing requests. This is useful to track timing or logging |
| 876 |
* special information. The function takes no parameters and return void. |
| 877 |
* @param {Function} endHook The function for the end hook. |
| 878 |
*/ |
| 879 |
goog.net.BrowserChannel.setEndThreadExecutionHook = function(endHook) { |
| 880 |
goog.net.BrowserChannel.endExecutionHook_ = endHook; |
| 881 |
}; |
| 882 |
|
| 883 |
|
| 884 |
/** |
| 885 |
* Application provided execution hook for the start hook. |
| 886 |
* |
| 887 |
* @type {Function} |
| 888 |
* @private |
| 889 |
*/ |
| 890 |
goog.net.BrowserChannel.startExecutionHook_ = function() {}; |
| 891 |
|
| 892 |
|
| 893 |
/** |
| 894 |
* Application provided execution hook for the end hook. |
| 895 |
* |
| 896 |
* @type {Function} |
| 897 |
* @private |
| 898 |
*/ |
| 899 |
goog.net.BrowserChannel.endExecutionHook_ = function() {}; |
| 900 |
|
| 901 |
|
| 902 |
/** |
| 903 |
* Instantiates a ChannelRequest with the given parameters. Overidden in tests. |
| 904 |
* |
| 905 |
* @param {goog.net.BrowserChannel|goog.net.BrowserTestChannel} channel |
| 906 |
* The BrowserChannel that owns this request. |
| 907 |
* @param {goog.net.ChannelDebug} channelDebug A ChannelDebug to use for |
| 908 |
* logging. |
| 909 |
* @param {string=} opt_sessionId The session id for the channel. |
| 910 |
* @param {string|number=} opt_requestId The request id for this request. |
| 911 |
* @param {number=} opt_retryId The retry id for this request. |
| 912 |
* @return {!goog.net.ChannelRequest} The created channel request. |
| 913 |
*/ |
| 914 |
goog.net.BrowserChannel.createChannelRequest = function( |
| 915 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) { |
| 916 |
return new goog.net.ChannelRequest( |
| 917 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId); |
| 918 |
}; |
| 919 |
|
| 920 |
|
| 921 |
/** |
| 922 |
* Starts the channel. This initiates connections to the server. |
| 923 |
* |
| 924 |
* @param {string} testPath The path for the test connection. |
| 925 |
* @param {string} channelPath The path for the channel connection. |
| 926 |
* @param {Object=} opt_extraParams Extra parameter keys and values to add to |
| 927 |
* the requests. |
| 928 |
* @param {string=} opt_oldSessionId Session ID from a previous session. |
| 929 |
* @param {number=} opt_oldArrayId The last array ID from a previous session. |
| 930 |
*/ |
| 931 |
goog.net.BrowserChannel.prototype.connect = function( |
| 932 |
testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) { |
| 933 |
this.channelDebug_.debug('connect()'); |
| 934 |
|
| 935 |
goog.net.BrowserChannel.notifyStatEvent( |
| 936 |
goog.net.BrowserChannel.Stat.CONNECT_ATTEMPT); |
| 937 |
|
| 938 |
this.path_ = channelPath; |
| 939 |
this.extraParams_ = opt_extraParams || {}; |
| 940 |
|
| 941 |
// Attach parameters about the previous session if reconnecting. |
| 942 |
if (opt_oldSessionId && goog.isDef(opt_oldArrayId)) { |
| 943 |
this.extraParams_['OSID'] = opt_oldSessionId; |
| 944 |
this.extraParams_['OAID'] = opt_oldArrayId; |
| 945 |
} |
| 946 |
|
| 947 |
if (this.asyncTest_) { |
| 948 |
goog.net.BrowserChannel.setTimeout( |
| 949 |
goog.bind(this.connectTest_, this, testPath), 100); |
| 950 |
this.connectChannel_(); |
| 951 |
} else { |
| 952 |
this.connectTest_(testPath); |
| 953 |
} |
| 954 |
}; |
| 955 |
|
| 956 |
|
| 957 |
/** |
| 958 |
* Disconnects and closes the channel. |
| 959 |
*/ |
| 960 |
goog.net.BrowserChannel.prototype.disconnect = function() { |
| 961 |
this.channelDebug_.debug('disconnect()'); |
| 962 |
|
| 963 |
this.cancelRequests_(); |
| 964 |
|
| 965 |
if (this.state_ == goog.net.BrowserChannel.State.OPENED) { |
| 966 |
var rid = this.nextRid_++; |
| 967 |
var uri = this.forwardChannelUri_.clone(); |
| 968 |
uri.setParameterValue('SID', this.sid_); |
| 969 |
uri.setParameterValue('RID', rid); |
| 970 |
uri.setParameterValue('TYPE', 'terminate'); |
| 971 |
|
| 972 |
// Add the reconnect parameters. |
| 973 |
this.addAdditionalParams_(uri); |
| 974 |
|
| 975 |
var request = goog.net.BrowserChannel.createChannelRequest( |
| 976 |
this, this.channelDebug_, this.sid_, rid); |
| 977 |
request.sendUsingImgTag(uri); |
| 978 |
} |
| 979 |
|
| 980 |
this.onClose_(); |
| 981 |
}; |
| 982 |
|
| 983 |
|
| 984 |
/** |
| 985 |
* Returns the session id of the channel. Only available after the |
| 986 |
* channel has been opened. |
| 987 |
* @return {string} Session ID. |
| 988 |
*/ |
| 989 |
goog.net.BrowserChannel.prototype.getSessionId = function() { |
| 990 |
return this.sid_; |
| 991 |
}; |
| 992 |
|
| 993 |
|
| 994 |
/** |
| 995 |
* Starts the test channel to determine network conditions. |
| 996 |
* |
| 997 |
* @param {string} testPath The relative PATH for the test connection. |
| 998 |
* @private |
| 999 |
*/ |
| 1000 |
goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) { |
| 1001 |
this.channelDebug_.debug('connectTest_()'); |
| 1002 |
if (!this.okToMakeRequest_()) { |
| 1003 |
return; // channel is cancelled |
| 1004 |
} |
| 1005 |
this.connectionTest_ = |
| 1006 |
new goog.net.BrowserTestChannel(this, this.channelDebug_); |
| 1007 |
this.connectionTest_.setExtraHeaders(this.extraHeaders_); |
| 1008 |
this.connectionTest_.setParser(this.parser_); |
| 1009 |
this.connectionTest_.connect(testPath); |
| 1010 |
}; |
| 1011 |
|
| 1012 |
|
| 1013 |
/** |
| 1014 |
* Starts the regular channel which is run after the test channel is complete. |
| 1015 |
* @private |
| 1016 |
*/ |
| 1017 |
goog.net.BrowserChannel.prototype.connectChannel_ = function() { |
| 1018 |
this.channelDebug_.debug('connectChannel_()'); |
| 1019 |
this.ensureInState_( |
| 1020 |
goog.net.BrowserChannel.State.INIT, goog.net.BrowserChannel.State.CLOSED); |
| 1021 |
this.forwardChannelUri_ = |
| 1022 |
this.getForwardChannelUri(/** @type {string} */ (this.path_)); |
| 1023 |
this.ensureForwardChannel_(); |
| 1024 |
}; |
| 1025 |
|
| 1026 |
|
| 1027 |
/** |
| 1028 |
* Cancels all outstanding requests. |
| 1029 |
* @private |
| 1030 |
*/ |
| 1031 |
goog.net.BrowserChannel.prototype.cancelRequests_ = function() { |
| 1032 |
if (this.connectionTest_) { |
| 1033 |
this.connectionTest_.abort(); |
| 1034 |
this.connectionTest_ = null; |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (this.backChannelRequest_) { |
| 1038 |
this.backChannelRequest_.cancel(); |
| 1039 |
this.backChannelRequest_ = null; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (this.backChannelTimerId_) { |
| 1043 |
goog.global.clearTimeout(this.backChannelTimerId_); |
| 1044 |
this.backChannelTimerId_ = null; |
| 1045 |
} |
| 1046 |
|
| 1047 |
this.clearDeadBackchannelTimer_(); |
| 1048 |
|
| 1049 |
if (this.forwardChannelRequest_) { |
| 1050 |
this.forwardChannelRequest_.cancel(); |
| 1051 |
this.forwardChannelRequest_ = null; |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (this.forwardChannelTimerId_) { |
| 1055 |
goog.global.clearTimeout(this.forwardChannelTimerId_); |
| 1056 |
this.forwardChannelTimerId_ = null; |
| 1057 |
} |
| 1058 |
}; |
| 1059 |
|
| 1060 |
|
| 1061 |
/** |
| 1062 |
* Returns the extra HTTP headers to add to all the requests sent to the server. |
| 1063 |
* |
| 1064 |
* @return {Object} The HTTP headers, or null. |
| 1065 |
*/ |
| 1066 |
goog.net.BrowserChannel.prototype.getExtraHeaders = function() { |
| 1067 |
return this.extraHeaders_; |
| 1068 |
}; |
| 1069 |
|
| 1070 |
|
| 1071 |
/** |
| 1072 |
* Sets extra HTTP headers to add to all the requests sent to the server. |
| 1073 |
* |
| 1074 |
* @param {Object} extraHeaders The HTTP headers, or null. |
| 1075 |
*/ |
| 1076 |
goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) { |
| 1077 |
this.extraHeaders_ = extraHeaders; |
| 1078 |
}; |
| 1079 |
|
| 1080 |
|
| 1081 |
/** |
| 1082 |
* Sets the throttle for handling onreadystatechange events for the request. |
| 1083 |
* |
| 1084 |
* @param {number} throttle The throttle in ms. A value of zero indicates |
| 1085 |
* no throttle. |
| 1086 |
*/ |
| 1087 |
goog.net.BrowserChannel.prototype.setReadyStateChangeThrottle = function( |
| 1088 |
throttle) { |
| 1089 |
this.readyStateChangeThrottleMs_ = throttle; |
| 1090 |
}; |
| 1091 |
|
| 1092 |
|
| 1093 |
/** |
| 1094 |
* Sets whether cross origin requests are supported for the browser channel. |
| 1095 |
* |
| 1096 |
* Setting this allows the creation of requests to secondary domains and |
| 1097 |
* sends XHRs with the CORS withCredentials bit set to true. |
| 1098 |
* |
| 1099 |
* In order for cross-origin requests to work, the server will also need to set |
| 1100 |
* CORS response headers as per: |
| 1101 |
* https://developer.mozilla.org/en-US/docs/HTTP_access_control |
| 1102 |
* |
| 1103 |
* See {@link goog.net.XhrIo#setWithCredentials}. |
| 1104 |
* @param {boolean} supportCrossDomain Whether cross domain XHRs are supported. |
| 1105 |
*/ |
| 1106 |
goog.net.BrowserChannel.prototype.setSupportsCrossDomainXhrs = function( |
| 1107 |
supportCrossDomain) { |
| 1108 |
this.supportsCrossDomainXhrs_ = supportCrossDomain; |
| 1109 |
}; |
| 1110 |
|
| 1111 |
|
| 1112 |
/** |
| 1113 |
* Returns the handler used for channel callback events. |
| 1114 |
* |
| 1115 |
* @return {goog.net.BrowserChannel.Handler} The handler. |
| 1116 |
*/ |
| 1117 |
goog.net.BrowserChannel.prototype.getHandler = function() { |
| 1118 |
return this.handler_; |
| 1119 |
}; |
| 1120 |
|
| 1121 |
|
| 1122 |
/** |
| 1123 |
* Sets the handler used for channel callback events. |
| 1124 |
* @param {goog.net.BrowserChannel.Handler} handler The handler to set. |
| 1125 |
*/ |
| 1126 |
goog.net.BrowserChannel.prototype.setHandler = function(handler) { |
| 1127 |
this.handler_ = handler; |
| 1128 |
}; |
| 1129 |
|
| 1130 |
|
| 1131 |
/** |
| 1132 |
* Returns whether the channel allows the use of a subdomain. There may be |
| 1133 |
* cases where this isn't allowed. |
| 1134 |
* @return {boolean} Whether a host prefix is allowed. |
| 1135 |
*/ |
| 1136 |
goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() { |
| 1137 |
return this.allowHostPrefix_; |
| 1138 |
}; |
| 1139 |
|
| 1140 |
|
| 1141 |
/** |
| 1142 |
* Sets whether the channel allows the use of a subdomain. There may be cases |
| 1143 |
* where this isn't allowed, for example, logging in with troutboard where |
| 1144 |
* using a subdomain causes Apache to force the user to authenticate twice. |
| 1145 |
* @param {boolean} allowHostPrefix Whether a host prefix is allowed. |
| 1146 |
*/ |
| 1147 |
goog.net.BrowserChannel.prototype.setAllowHostPrefix = function( |
| 1148 |
allowHostPrefix) { |
| 1149 |
this.allowHostPrefix_ = allowHostPrefix; |
| 1150 |
}; |
| 1151 |
|
| 1152 |
|
| 1153 |
/** |
| 1154 |
* Returns whether the channel is buffered or not. This state is valid for |
| 1155 |
* querying only after the test connection has completed. This may be |
| 1156 |
* queried in the goog.net.BrowserChannel.okToMakeRequest() callback. |
| 1157 |
* A channel may be buffered if the test connection determines that |
| 1158 |
* a chunked response could not be sent down within a suitable time. |
| 1159 |
* @return {boolean} Whether the channel is buffered. |
| 1160 |
*/ |
| 1161 |
goog.net.BrowserChannel.prototype.isBuffered = function() { |
| 1162 |
return !this.useChunked_; |
| 1163 |
}; |
| 1164 |
|
| 1165 |
|
| 1166 |
/** |
| 1167 |
* Returns whether chunked mode is allowed. In certain debugging situations, |
| 1168 |
* it's useful for the application to have a way to disable chunked mode for a |
| 1169 |
* user. |
| 1170 |
|
| 1171 |
* @return {boolean} Whether chunked mode is allowed. |
| 1172 |
*/ |
| 1173 |
goog.net.BrowserChannel.prototype.getAllowChunkedMode = function() { |
| 1174 |
return this.allowChunkedMode_; |
| 1175 |
}; |
| 1176 |
|
| 1177 |
|
| 1178 |
/** |
| 1179 |
* Sets whether chunked mode is allowed. In certain debugging situations, it's |
| 1180 |
* useful for the application to have a way to disable chunked mode for a user. |
| 1181 |
* @param {boolean} allowChunkedMode Whether chunked mode is allowed. |
| 1182 |
*/ |
| 1183 |
goog.net.BrowserChannel.prototype.setAllowChunkedMode = function( |
| 1184 |
allowChunkedMode) { |
| 1185 |
this.allowChunkedMode_ = allowChunkedMode; |
| 1186 |
}; |
| 1187 |
|
| 1188 |
|
| 1189 |
/** |
| 1190 |
* Sends a request to the server. The format of the request is a Map data |
| 1191 |
* structure of key/value pairs. These maps are then encoded in a format |
| 1192 |
* suitable for the wire and then reconstituted as a Map data structure that |
| 1193 |
* the server can process. |
| 1194 |
* @param {Object} map The map to send. |
| 1195 |
* @param {?Object=} opt_context The context associated with the map. |
| 1196 |
*/ |
| 1197 |
goog.net.BrowserChannel.prototype.sendMap = function(map, opt_context) { |
| 1198 |
if (this.state_ == goog.net.BrowserChannel.State.CLOSED) { |
| 1199 |
throw new Error('Invalid operation: sending map when state is closed'); |
| 1200 |
} |
| 1201 |
|
| 1202 |
// We can only send 1000 maps per POST, but typically we should never have |
| 1203 |
// that much to send, so warn if we exceed that (we still send all the maps). |
| 1204 |
if (this.outgoingMaps_.length == |
| 1205 |
goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_) { |
| 1206 |
// severe() is temporary so that we get these uploaded and can figure out |
| 1207 |
// what's causing them. Afterwards can change to warning(). |
| 1208 |
this.channelDebug_.severe( |
| 1209 |
'Already have ' + goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ + |
| 1210 |
' queued maps upon queueing ' + this.parser_.stringify(map)); |
| 1211 |
} |
| 1212 |
|
| 1213 |
this.outgoingMaps_.push( |
| 1214 |
new goog.net.BrowserChannel.QueuedMap( |
| 1215 |
this.nextMapId_++, map, opt_context)); |
| 1216 |
if (this.state_ == goog.net.BrowserChannel.State.OPENING || |
| 1217 |
this.state_ == goog.net.BrowserChannel.State.OPENED) { |
| 1218 |
this.ensureForwardChannel_(); |
| 1219 |
} |
| 1220 |
}; |
| 1221 |
|
| 1222 |
|
| 1223 |
/** |
| 1224 |
* When set to true, this changes the behavior of the forward channel so it |
| 1225 |
* will not retry requests; it will fail after one network failure, and if |
| 1226 |
* there was already one network failure, the request will fail immediately. |
| 1227 |
* @param {boolean} failFast Whether or not to fail fast. |
| 1228 |
*/ |
| 1229 |
goog.net.BrowserChannel.prototype.setFailFast = function(failFast) { |
| 1230 |
this.failFast_ = failFast; |
| 1231 |
this.channelDebug_.info('setFailFast: ' + failFast); |
| 1232 |
if ((this.forwardChannelRequest_ || this.forwardChannelTimerId_) && |
| 1233 |
this.forwardChannelRetryCount_ > this.getForwardChannelMaxRetries()) { |
| 1234 |
this.channelDebug_.info( |
| 1235 |
'Retry count ' + this.forwardChannelRetryCount_ + ' > new maxRetries ' + |
| 1236 |
this.getForwardChannelMaxRetries() + '. Fail immediately!'); |
| 1237 |
if (this.forwardChannelRequest_) { |
| 1238 |
this.forwardChannelRequest_.cancel(); |
| 1239 |
// Go through the standard onRequestComplete logic to expose the max-retry |
| 1240 |
// failure in the standard way. |
| 1241 |
this.onRequestComplete(this.forwardChannelRequest_); |
| 1242 |
} else { // i.e., this.forwardChannelTimerId_ |
| 1243 |
goog.global.clearTimeout(this.forwardChannelTimerId_); |
| 1244 |
this.forwardChannelTimerId_ = null; |
| 1245 |
// The error code from the last failed request is gone, so just use a |
| 1246 |
// generic one. |
| 1247 |
this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED); |
| 1248 |
} |
| 1249 |
} |
| 1250 |
}; |
| 1251 |
|
| 1252 |
|
| 1253 |
/** |
| 1254 |
* @return {number} The max number of forward-channel retries, which will be 0 |
| 1255 |
* in fail-fast mode. |
| 1256 |
*/ |
| 1257 |
goog.net.BrowserChannel.prototype.getForwardChannelMaxRetries = function() { |
| 1258 |
return this.failFast_ ? 0 : this.forwardChannelMaxRetries_; |
| 1259 |
}; |
| 1260 |
|
| 1261 |
|
| 1262 |
/** |
| 1263 |
* Sets the maximum number of attempts to connect to the server for forward |
| 1264 |
* channel requests. |
| 1265 |
* @param {number} retries The maximum number of attempts. |
| 1266 |
*/ |
| 1267 |
goog.net.BrowserChannel.prototype.setForwardChannelMaxRetries = function( |
| 1268 |
retries) { |
| 1269 |
this.forwardChannelMaxRetries_ = retries; |
| 1270 |
}; |
| 1271 |
|
| 1272 |
|
| 1273 |
/** |
| 1274 |
* Sets the timeout for a forward channel request. |
| 1275 |
* @param {number} timeoutMs The timeout in milliseconds. |
| 1276 |
*/ |
| 1277 |
goog.net.BrowserChannel.prototype.setForwardChannelRequestTimeout = function( |
| 1278 |
timeoutMs) { |
| 1279 |
this.forwardChannelRequestTimeoutMs_ = timeoutMs; |
| 1280 |
}; |
| 1281 |
|
| 1282 |
|
| 1283 |
/** |
| 1284 |
* @return {number} The max number of back-channel retries, which is a constant. |
| 1285 |
*/ |
| 1286 |
goog.net.BrowserChannel.prototype.getBackChannelMaxRetries = function() { |
| 1287 |
// Back-channel retries is a constant. |
| 1288 |
return goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES; |
| 1289 |
}; |
| 1290 |
|
| 1291 |
|
| 1292 |
/** |
| 1293 |
* Returns whether the channel is closed |
| 1294 |
* @return {boolean} true if the channel is closed. |
| 1295 |
*/ |
| 1296 |
goog.net.BrowserChannel.prototype.isClosed = function() { |
| 1297 |
return this.state_ == goog.net.BrowserChannel.State.CLOSED; |
| 1298 |
}; |
| 1299 |
|
| 1300 |
|
| 1301 |
/** |
| 1302 |
* Returns the browser channel state. |
| 1303 |
* @return {goog.net.BrowserChannel.State} The current state of the browser |
| 1304 |
* channel. |
| 1305 |
*/ |
| 1306 |
goog.net.BrowserChannel.prototype.getState = function() { |
| 1307 |
return this.state_; |
| 1308 |
}; |
| 1309 |
|
| 1310 |
|
| 1311 |
/** |
| 1312 |
* Return the last status code received for a request. |
| 1313 |
* @return {number} The last status code received for a request. |
| 1314 |
*/ |
| 1315 |
goog.net.BrowserChannel.prototype.getLastStatusCode = function() { |
| 1316 |
return this.lastStatusCode_; |
| 1317 |
}; |
| 1318 |
|
| 1319 |
|
| 1320 |
/** |
| 1321 |
* @return {number} The last array id received. |
| 1322 |
*/ |
| 1323 |
goog.net.BrowserChannel.prototype.getLastArrayId = function() { |
| 1324 |
return this.lastArrayId_; |
| 1325 |
}; |
| 1326 |
|
| 1327 |
|
| 1328 |
/** |
| 1329 |
* Returns whether there are outstanding requests servicing the channel. |
| 1330 |
* @return {boolean} true if there are outstanding requests. |
| 1331 |
*/ |
| 1332 |
goog.net.BrowserChannel.prototype.hasOutstandingRequests = function() { |
| 1333 |
return this.outstandingRequests_() != 0; |
| 1334 |
}; |
| 1335 |
|
| 1336 |
|
| 1337 |
/** |
| 1338 |
* Sets a new parser for the response payload. |
| 1339 |
* @param {!goog.string.Parser} parser Parser. |
| 1340 |
*/ |
| 1341 |
goog.net.BrowserChannel.prototype.setParser = function(parser) { |
| 1342 |
this.parser_ = parser; |
| 1343 |
}; |
| 1344 |
|
| 1345 |
|
| 1346 |
/** |
| 1347 |
* Returns the number of outstanding requests. |
| 1348 |
* @return {number} The number of outstanding requests to the server. |
| 1349 |
* @private |
| 1350 |
*/ |
| 1351 |
goog.net.BrowserChannel.prototype.outstandingRequests_ = function() { |
| 1352 |
var count = 0; |
| 1353 |
if (this.backChannelRequest_) { |
| 1354 |
count++; |
| 1355 |
} |
| 1356 |
if (this.forwardChannelRequest_) { |
| 1357 |
count++; |
| 1358 |
} |
| 1359 |
return count; |
| 1360 |
}; |
| 1361 |
|
| 1362 |
|
| 1363 |
/** |
| 1364 |
* Ensures that a forward channel request is scheduled. |
| 1365 |
* @private |
| 1366 |
*/ |
| 1367 |
goog.net.BrowserChannel.prototype.ensureForwardChannel_ = function() { |
| 1368 |
if (this.forwardChannelRequest_) { |
| 1369 |
// connection in process - no need to start a new request |
| 1370 |
return; |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (this.forwardChannelTimerId_) { |
| 1374 |
// no need to start a new request - one is already scheduled |
| 1375 |
return; |
| 1376 |
} |
| 1377 |
|
| 1378 |
this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout( |
| 1379 |
goog.bind(this.onStartForwardChannelTimer_, this), 0); |
| 1380 |
this.forwardChannelRetryCount_ = 0; |
| 1381 |
}; |
| 1382 |
|
| 1383 |
|
| 1384 |
/** |
| 1385 |
* Schedules a forward-channel retry for the specified request, unless the max |
| 1386 |
* retries has been reached. |
| 1387 |
* @param {goog.net.ChannelRequest} request The failed request to retry. |
| 1388 |
* @return {boolean} true iff a retry was scheduled. |
| 1389 |
* @private |
| 1390 |
*/ |
| 1391 |
goog.net.BrowserChannel.prototype.maybeRetryForwardChannel_ = function( |
| 1392 |
request) { |
| 1393 |
if (this.forwardChannelRequest_ || this.forwardChannelTimerId_) { |
| 1394 |
// Should be impossible to be called in this state. |
| 1395 |
this.channelDebug_.severe('Request already in progress'); |
| 1396 |
return false; |
| 1397 |
} |
| 1398 |
|
| 1399 |
if (this.state_ == goog.net.BrowserChannel.State.INIT || // no retry open_() |
| 1400 |
(this.forwardChannelRetryCount_ >= this.getForwardChannelMaxRetries())) { |
| 1401 |
return false; |
| 1402 |
} |
| 1403 |
|
| 1404 |
this.channelDebug_.debug('Going to retry POST'); |
| 1405 |
|
| 1406 |
this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout( |
| 1407 |
goog.bind(this.onStartForwardChannelTimer_, this, request), |
| 1408 |
this.getRetryTime_(this.forwardChannelRetryCount_)); |
| 1409 |
this.forwardChannelRetryCount_++; |
| 1410 |
return true; |
| 1411 |
}; |
| 1412 |
|
| 1413 |
|
| 1414 |
/** |
| 1415 |
* Timer callback for ensureForwardChannel |
| 1416 |
* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry. |
| 1417 |
* @private |
| 1418 |
*/ |
| 1419 |
goog.net.BrowserChannel.prototype.onStartForwardChannelTimer_ = function( |
| 1420 |
opt_retryRequest) { |
| 1421 |
this.forwardChannelTimerId_ = null; |
| 1422 |
this.startForwardChannel_(opt_retryRequest); |
| 1423 |
}; |
| 1424 |
|
| 1425 |
|
| 1426 |
/** |
| 1427 |
* Begins a new forward channel operation to the server. |
| 1428 |
* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry. |
| 1429 |
* @private |
| 1430 |
*/ |
| 1431 |
goog.net.BrowserChannel.prototype.startForwardChannel_ = function( |
| 1432 |
opt_retryRequest) { |
| 1433 |
this.channelDebug_.debug('startForwardChannel_'); |
| 1434 |
if (!this.okToMakeRequest_()) { |
| 1435 |
return; // channel is cancelled |
| 1436 |
} else if (this.state_ == goog.net.BrowserChannel.State.INIT) { |
| 1437 |
if (opt_retryRequest) { |
| 1438 |
this.channelDebug_.severe('Not supposed to retry the open'); |
| 1439 |
return; |
| 1440 |
} |
| 1441 |
this.open_(); |
| 1442 |
this.state_ = goog.net.BrowserChannel.State.OPENING; |
| 1443 |
} else if (this.state_ == goog.net.BrowserChannel.State.OPENED) { |
| 1444 |
if (opt_retryRequest) { |
| 1445 |
this.makeForwardChannelRequest_(opt_retryRequest); |
| 1446 |
return; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (this.outgoingMaps_.length == 0) { |
| 1450 |
this.channelDebug_.debug( |
| 1451 |
'startForwardChannel_ returned: ' + |
| 1452 |
'nothing to send'); |
| 1453 |
// no need to start a new forward channel request |
| 1454 |
return; |
| 1455 |
} |
| 1456 |
|
| 1457 |
if (this.forwardChannelRequest_) { |
| 1458 |
// Should be impossible to be called in this state. |
| 1459 |
this.channelDebug_.severe( |
| 1460 |
'startForwardChannel_ returned: ' + |
| 1461 |
'connection already in progress'); |
| 1462 |
return; |
| 1463 |
} |
| 1464 |
|
| 1465 |
this.makeForwardChannelRequest_(); |
| 1466 |
this.channelDebug_.debug('startForwardChannel_ finished, sent request'); |
| 1467 |
} |
| 1468 |
}; |
| 1469 |
|
| 1470 |
|
| 1471 |
/** |
| 1472 |
* Establishes a new channel session with the the server. |
| 1473 |
* @private |
| 1474 |
*/ |
| 1475 |
goog.net.BrowserChannel.prototype.open_ = function() { |
| 1476 |
this.channelDebug_.debug('open_()'); |
| 1477 |
this.nextRid_ = Math.floor(Math.random() * 100000); |
| 1478 |
|
| 1479 |
var rid = this.nextRid_++; |
| 1480 |
var request = goog.net.BrowserChannel.createChannelRequest( |
| 1481 |
this, this.channelDebug_, '', rid); |
| 1482 |
request.setExtraHeaders(this.extraHeaders_); |
| 1483 |
var requestText = this.dequeueOutgoingMaps_(); |
| 1484 |
var uri = this.forwardChannelUri_.clone(); |
| 1485 |
uri.setParameterValue('RID', rid); |
| 1486 |
if (this.clientVersion_) { |
| 1487 |
uri.setParameterValue('CVER', this.clientVersion_); |
| 1488 |
} |
| 1489 |
|
| 1490 |
// Add the reconnect parameters. |
| 1491 |
this.addAdditionalParams_(uri); |
| 1492 |
|
| 1493 |
request.xmlHttpPost(uri, requestText, true); |
| 1494 |
this.forwardChannelRequest_ = request; |
| 1495 |
}; |
| 1496 |
|
| 1497 |
|
| 1498 |
/** |
| 1499 |
* Makes a forward channel request using XMLHTTP. |
| 1500 |
* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry. |
| 1501 |
* @private |
| 1502 |
*/ |
| 1503 |
goog.net.BrowserChannel.prototype.makeForwardChannelRequest_ = function( |
| 1504 |
opt_retryRequest) { |
| 1505 |
var rid; |
| 1506 |
var requestText; |
| 1507 |
if (opt_retryRequest) { |
| 1508 |
if (this.channelVersion_ > 6) { |
| 1509 |
// In version 7 and up we can tack on new arrays to a retry. |
| 1510 |
this.requeuePendingMaps_(); |
| 1511 |
rid = this.nextRid_ - 1; // Must use last RID |
| 1512 |
requestText = this.dequeueOutgoingMaps_(); |
| 1513 |
} else { |
| 1514 |
// TODO(user): Remove this code and the opt_retryRequest passing |
| 1515 |
// once server-side support for ver 7 is ubiquitous. |
| 1516 |
rid = opt_retryRequest.getRequestId(); |
| 1517 |
requestText = /** @type {string} */ (opt_retryRequest.getPostData()); |
| 1518 |
} |
| 1519 |
} else { |
| 1520 |
rid = this.nextRid_++; |
| 1521 |
requestText = this.dequeueOutgoingMaps_(); |
| 1522 |
} |
| 1523 |
|
| 1524 |
var uri = this.forwardChannelUri_.clone(); |
| 1525 |
uri.setParameterValue('SID', this.sid_); |
| 1526 |
uri.setParameterValue('RID', rid); |
| 1527 |
uri.setParameterValue('AID', this.lastArrayId_); |
| 1528 |
// Add the additional reconnect parameters. |
| 1529 |
this.addAdditionalParams_(uri); |
| 1530 |
|
| 1531 |
var request = goog.net.BrowserChannel.createChannelRequest( |
| 1532 |
this, this.channelDebug_, this.sid_, rid, |
| 1533 |
this.forwardChannelRetryCount_ + 1); |
| 1534 |
request.setExtraHeaders(this.extraHeaders_); |
| 1535 |
|
| 1536 |
// randomize from 50%-100% of the forward channel timeout to avoid |
| 1537 |
// a big hit if servers happen to die at once. |
| 1538 |
request.setTimeout( |
| 1539 |
Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) + |
| 1540 |
Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random())); |
| 1541 |
this.forwardChannelRequest_ = request; |
| 1542 |
request.xmlHttpPost(uri, requestText, true); |
| 1543 |
}; |
| 1544 |
|
| 1545 |
|
| 1546 |
/** |
| 1547 |
* Adds the additional parameters from the handler to the given URI. |
| 1548 |
* @param {goog.Uri} uri The URI to add the parameters to. |
| 1549 |
* @private |
| 1550 |
*/ |
| 1551 |
goog.net.BrowserChannel.prototype.addAdditionalParams_ = function(uri) { |
| 1552 |
// Add the additional reconnect parameters as needed. |
| 1553 |
if (this.handler_) { |
| 1554 |
var params = this.handler_.getAdditionalParams(this); |
| 1555 |
if (params) { |
| 1556 |
goog.object.forEach( |
| 1557 |
params, function(value, key) { uri.setParameterValue(key, value); }); |
| 1558 |
} |
| 1559 |
} |
| 1560 |
}; |
| 1561 |
|
| 1562 |
|
| 1563 |
/** |
| 1564 |
* Returns the request text from the outgoing maps and resets it. |
| 1565 |
* @return {string} The encoded request text created from all the currently |
| 1566 |
* queued outgoing maps. |
| 1567 |
* @private |
| 1568 |
*/ |
| 1569 |
goog.net.BrowserChannel.prototype.dequeueOutgoingMaps_ = function() { |
| 1570 |
var count = Math.min( |
| 1571 |
this.outgoingMaps_.length, goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_); |
| 1572 |
var sb = ['count=' + count]; |
| 1573 |
var offset; |
| 1574 |
if (this.channelVersion_ > 6 && count > 0) { |
| 1575 |
// To save a bit of bandwidth, specify the base mapId and the rest as |
| 1576 |
// offsets from it. |
| 1577 |
offset = this.outgoingMaps_[0].mapId; |
| 1578 |
sb.push('ofs=' + offset); |
| 1579 |
} else { |
| 1580 |
offset = 0; |
| 1581 |
} |
| 1582 |
for (var i = 0; i < count; i++) { |
| 1583 |
var mapId = this.outgoingMaps_[i].mapId; |
| 1584 |
var map = this.outgoingMaps_[i].map; |
| 1585 |
if (this.channelVersion_ <= 6) { |
| 1586 |
// Map IDs were not used in ver 6 and before, just indexes in the request. |
| 1587 |
mapId = i; |
| 1588 |
} else { |
| 1589 |
mapId -= offset; |
| 1590 |
} |
| 1591 |
try { |
| 1592 |
goog.object.forEach(map, function(value, key, coll) { |
| 1593 |
sb.push('req' + mapId + '_' + key + '=' + encodeURIComponent(value)); |
| 1594 |
}); |
| 1595 |
} catch (ex) { |
| 1596 |
// We send a map here because lots of the retry logic relies on map IDs, |
| 1597 |
// so we have to send something. |
| 1598 |
sb.push( |
| 1599 |
'req' + mapId + '_' + |
| 1600 |
'type' + |
| 1601 |
'=' + encodeURIComponent('_badmap')); |
| 1602 |
if (this.handler_) { |
| 1603 |
this.handler_.badMapError(this, map); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
this.pendingMaps_ = |
| 1608 |
this.pendingMaps_.concat(this.outgoingMaps_.splice(0, count)); |
| 1609 |
return sb.join('&'); |
| 1610 |
}; |
| 1611 |
|
| 1612 |
|
| 1613 |
/** |
| 1614 |
* Requeues unacknowledged sent arrays for retransmission in the next forward |
| 1615 |
* channel request. |
| 1616 |
* @private |
| 1617 |
*/ |
| 1618 |
goog.net.BrowserChannel.prototype.requeuePendingMaps_ = function() { |
| 1619 |
this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_); |
| 1620 |
this.pendingMaps_.length = 0; |
| 1621 |
}; |
| 1622 |
|
| 1623 |
|
| 1624 |
/** |
| 1625 |
* Ensures there is a backchannel request for receiving data from the server. |
| 1626 |
* @private |
| 1627 |
*/ |
| 1628 |
goog.net.BrowserChannel.prototype.ensureBackChannel_ = function() { |
| 1629 |
if (this.backChannelRequest_) { |
| 1630 |
// already have one |
| 1631 |
return; |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (this.backChannelTimerId_) { |
| 1635 |
// no need to start a new request - one is already scheduled |
| 1636 |
return; |
| 1637 |
} |
| 1638 |
|
| 1639 |
this.backChannelAttemptId_ = 1; |
| 1640 |
this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout( |
| 1641 |
goog.bind(this.onStartBackChannelTimer_, this), 0); |
| 1642 |
this.backChannelRetryCount_ = 0; |
| 1643 |
}; |
| 1644 |
|
| 1645 |
|
| 1646 |
/** |
| 1647 |
* Schedules a back-channel retry, unless the max retries has been reached. |
| 1648 |
* @return {boolean} true iff a retry was scheduled. |
| 1649 |
* @private |
| 1650 |
*/ |
| 1651 |
goog.net.BrowserChannel.prototype.maybeRetryBackChannel_ = function() { |
| 1652 |
if (this.backChannelRequest_ || this.backChannelTimerId_) { |
| 1653 |
// Should be impossible to be called in this state. |
| 1654 |
this.channelDebug_.severe('Request already in progress'); |
| 1655 |
return false; |
| 1656 |
} |
| 1657 |
|
| 1658 |
if (this.backChannelRetryCount_ >= this.getBackChannelMaxRetries()) { |
| 1659 |
return false; |
| 1660 |
} |
| 1661 |
|
| 1662 |
this.channelDebug_.debug('Going to retry GET'); |
| 1663 |
|
| 1664 |
this.backChannelAttemptId_++; |
| 1665 |
this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout( |
| 1666 |
goog.bind(this.onStartBackChannelTimer_, this), |
| 1667 |
this.getRetryTime_(this.backChannelRetryCount_)); |
| 1668 |
this.backChannelRetryCount_++; |
| 1669 |
return true; |
| 1670 |
}; |
| 1671 |
|
| 1672 |
|
| 1673 |
/** |
| 1674 |
* Timer callback for ensureBackChannel_. |
| 1675 |
* @private |
| 1676 |
*/ |
| 1677 |
goog.net.BrowserChannel.prototype.onStartBackChannelTimer_ = function() { |
| 1678 |
this.backChannelTimerId_ = null; |
| 1679 |
this.startBackChannel_(); |
| 1680 |
}; |
| 1681 |
|
| 1682 |
|
| 1683 |
/** |
| 1684 |
* Begins a new back channel operation to the server. |
| 1685 |
* @private |
| 1686 |
*/ |
| 1687 |
goog.net.BrowserChannel.prototype.startBackChannel_ = function() { |
| 1688 |
if (!this.okToMakeRequest_()) { |
| 1689 |
// channel is cancelled |
| 1690 |
return; |
| 1691 |
} |
| 1692 |
|
| 1693 |
this.channelDebug_.debug('Creating new HttpRequest'); |
| 1694 |
this.backChannelRequest_ = goog.net.BrowserChannel.createChannelRequest( |
| 1695 |
this, this.channelDebug_, this.sid_, 'rpc', this.backChannelAttemptId_); |
| 1696 |
this.backChannelRequest_.setExtraHeaders(this.extraHeaders_); |
| 1697 |
this.backChannelRequest_.setReadyStateChangeThrottle( |
| 1698 |
this.readyStateChangeThrottleMs_); |
| 1699 |
var uri = this.backChannelUri_.clone(); |
| 1700 |
uri.setParameterValue('RID', 'rpc'); |
| 1701 |
uri.setParameterValue('SID', this.sid_); |
| 1702 |
uri.setParameterValue('CI', this.useChunked_ ? '0' : '1'); |
| 1703 |
uri.setParameterValue('AID', this.lastArrayId_); |
| 1704 |
|
| 1705 |
// Add the reconnect parameters. |
| 1706 |
this.addAdditionalParams_(uri); |
| 1707 |
|
| 1708 |
if (!goog.net.ChannelRequest.supportsXhrStreaming()) { |
| 1709 |
uri.setParameterValue('TYPE', 'html'); |
| 1710 |
this.backChannelRequest_.tridentGet(uri, Boolean(this.hostPrefix_)); |
| 1711 |
} else { |
| 1712 |
uri.setParameterValue('TYPE', 'xmlhttp'); |
| 1713 |
this.backChannelRequest_.xmlHttpGet( |
| 1714 |
uri, true /* decodeChunks */, this.hostPrefix_, |
| 1715 |
false /* opt_noClose */); |
| 1716 |
} |
| 1717 |
this.channelDebug_.debug('New Request created'); |
| 1718 |
}; |
| 1719 |
|
| 1720 |
|
| 1721 |
/** |
| 1722 |
* Gives the handler a chance to return an error code and stop channel |
| 1723 |
* execution. A handler might want to do this to check that the user is still |
| 1724 |
* logged in, for example. |
| 1725 |
* @private |
| 1726 |
* @return {boolean} If it's OK to make a request. |
| 1727 |
*/ |
| 1728 |
goog.net.BrowserChannel.prototype.okToMakeRequest_ = function() { |
| 1729 |
if (this.handler_) { |
| 1730 |
var result = this.handler_.okToMakeRequest(this); |
| 1731 |
if (result != goog.net.BrowserChannel.Error.OK) { |
| 1732 |
this.channelDebug_.debug( |
| 1733 |
'Handler returned error code from ' + |
| 1734 |
'okToMakeRequest'); |
| 1735 |
this.signalError_(result); |
| 1736 |
return false; |
| 1737 |
} |
| 1738 |
} |
| 1739 |
return true; |
| 1740 |
}; |
| 1741 |
|
| 1742 |
|
| 1743 |
/** |
| 1744 |
* Callback from BrowserTestChannel for when the channel is finished. |
| 1745 |
* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel. |
| 1746 |
* @param {boolean} useChunked Whether we can chunk responses. |
| 1747 |
*/ |
| 1748 |
goog.net.BrowserChannel.prototype.testConnectionFinished = function( |
| 1749 |
testChannel, useChunked) { |
| 1750 |
this.channelDebug_.debug('Test Connection Finished'); |
| 1751 |
|
| 1752 |
this.useChunked_ = this.allowChunkedMode_ && useChunked; |
| 1753 |
this.lastStatusCode_ = testChannel.getLastStatusCode(); |
| 1754 |
// When using asynchronous test, the channel is already open by connect(). |
| 1755 |
if (!this.asyncTest_) { |
| 1756 |
this.connectChannel_(); |
| 1757 |
} |
| 1758 |
}; |
| 1759 |
|
| 1760 |
|
| 1761 |
/** |
| 1762 |
* Callback from BrowserTestChannel for when the channel has an error. |
| 1763 |
* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel. |
| 1764 |
* @param {goog.net.ChannelRequest.Error} errorCode The error code of the |
| 1765 |
failure. |
| 1766 |
*/ |
| 1767 |
goog.net.BrowserChannel.prototype.testConnectionFailure = function( |
| 1768 |
testChannel, errorCode) { |
| 1769 |
this.channelDebug_.debug('Test Connection Failed'); |
| 1770 |
this.lastStatusCode_ = testChannel.getLastStatusCode(); |
| 1771 |
this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED); |
| 1772 |
}; |
| 1773 |
|
| 1774 |
|
| 1775 |
/** |
| 1776 |
* Callback from BrowserTestChannel for when the channel is blocked. |
| 1777 |
* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel. |
| 1778 |
*/ |
| 1779 |
goog.net.BrowserChannel.prototype.testConnectionBlocked = function( |
| 1780 |
testChannel) { |
| 1781 |
this.channelDebug_.debug('Test Connection Blocked'); |
| 1782 |
this.lastStatusCode_ = this.connectionTest_.getLastStatusCode(); |
| 1783 |
this.signalError_(goog.net.BrowserChannel.Error.BLOCKED); |
| 1784 |
}; |
| 1785 |
|
| 1786 |
|
| 1787 |
/** |
| 1788 |
* Callback from ChannelRequest for when new data is received |
| 1789 |
* @param {goog.net.ChannelRequest} request The request object. |
| 1790 |
* @param {string} responseText The text of the response. |
| 1791 |
*/ |
| 1792 |
goog.net.BrowserChannel.prototype.onRequestData = function( |
| 1793 |
request, responseText) { |
| 1794 |
if (this.state_ == goog.net.BrowserChannel.State.CLOSED || |
| 1795 |
(this.backChannelRequest_ != request && |
| 1796 |
this.forwardChannelRequest_ != request)) { |
| 1797 |
// either CLOSED or a request we don't know about (perhaps an old request) |
| 1798 |
return; |
| 1799 |
} |
| 1800 |
this.lastStatusCode_ = request.getLastStatusCode(); |
| 1801 |
|
| 1802 |
if (this.forwardChannelRequest_ == request && |
| 1803 |
this.state_ == goog.net.BrowserChannel.State.OPENED) { |
| 1804 |
if (this.channelVersion_ > 7) { |
| 1805 |
var response; |
| 1806 |
try { |
| 1807 |
response = this.parser_.parse(responseText); |
| 1808 |
} catch (ex) { |
| 1809 |
response = null; |
| 1810 |
} |
| 1811 |
if (goog.isArray(response) && response.length == 3) { |
| 1812 |
this.handlePostResponse_(response); |
| 1813 |
} else { |
| 1814 |
this.channelDebug_.debug('Bad POST response data returned'); |
| 1815 |
this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE); |
| 1816 |
} |
| 1817 |
} else if (responseText != goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE) { |
| 1818 |
this.channelDebug_.debug( |
| 1819 |
'Bad data returned - missing/invald ' + |
| 1820 |
'magic cookie'); |
| 1821 |
this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE); |
| 1822 |
} |
| 1823 |
} else { |
| 1824 |
if (this.backChannelRequest_ == request) { |
| 1825 |
this.clearDeadBackchannelTimer_(); |
| 1826 |
} |
| 1827 |
if (!goog.string.isEmptyOrWhitespace(responseText)) { |
| 1828 |
var response = this.parser_.parse(responseText); |
| 1829 |
goog.asserts.assert(goog.isArray(response)); |
| 1830 |
this.onInput_(/** @type {!Array<?>} */ (response)); |
| 1831 |
} |
| 1832 |
} |
| 1833 |
}; |
| 1834 |
|
| 1835 |
|
| 1836 |
/** |
| 1837 |
* Handles a POST response from the server. |
| 1838 |
* @param {Array<number>} responseValues The key value pairs in the POST |
| 1839 |
* response. |
| 1840 |
* @private |
| 1841 |
*/ |
| 1842 |
goog.net.BrowserChannel.prototype.handlePostResponse_ = function( |
| 1843 |
responseValues) { |
| 1844 |
// The first response value is set to 0 if server is missing backchannel. |
| 1845 |
if (responseValues[0] == 0) { |
| 1846 |
this.handleBackchannelMissing_(); |
| 1847 |
return; |
| 1848 |
} |
| 1849 |
this.lastPostResponseArrayId_ = responseValues[1]; |
| 1850 |
var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_; |
| 1851 |
if (0 < outstandingArrays) { |
| 1852 |
var numOutstandingBackchannelBytes = responseValues[2]; |
| 1853 |
this.channelDebug_.debug( |
| 1854 |
numOutstandingBackchannelBytes + ' bytes (in ' + outstandingArrays + |
| 1855 |
' arrays) are outstanding on the BackChannel'); |
| 1856 |
if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) { |
| 1857 |
return; |
| 1858 |
} |
| 1859 |
if (!this.deadBackChannelTimerId_) { |
| 1860 |
// We expect to receive data within 2 RTTs or we retry the backchannel. |
| 1861 |
this.deadBackChannelTimerId_ = goog.net.BrowserChannel.setTimeout( |
| 1862 |
goog.bind(this.onBackChannelDead_, this), |
| 1863 |
2 * goog.net.BrowserChannel.RTT_ESTIMATE); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
}; |
| 1867 |
|
| 1868 |
|
| 1869 |
/** |
| 1870 |
* Handles a POST response from the server telling us that it has detected that |
| 1871 |
* we have no hanging GET connection. |
| 1872 |
* @private |
| 1873 |
*/ |
| 1874 |
goog.net.BrowserChannel.prototype.handleBackchannelMissing_ = function() { |
| 1875 |
// As long as the back channel was started before the POST was sent, |
| 1876 |
// we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE |
| 1877 |
// so as not to excessively retry the backchannel |
| 1878 |
this.channelDebug_.debug('Server claims our backchannel is missing.'); |
| 1879 |
if (this.backChannelTimerId_) { |
| 1880 |
this.channelDebug_.debug('But we are currently starting the request.'); |
| 1881 |
return; |
| 1882 |
} else if (!this.backChannelRequest_) { |
| 1883 |
this.channelDebug_.warning('We do not have a BackChannel established'); |
| 1884 |
} else if ( |
| 1885 |
this.backChannelRequest_.getRequestStartTime() + |
| 1886 |
goog.net.BrowserChannel.RTT_ESTIMATE < |
| 1887 |
this.forwardChannelRequest_.getRequestStartTime()) { |
| 1888 |
this.clearDeadBackchannelTimer_(); |
| 1889 |
this.backChannelRequest_.cancel(); |
| 1890 |
this.backChannelRequest_ = null; |
| 1891 |
} else { |
| 1892 |
return; |
| 1893 |
} |
| 1894 |
this.maybeRetryBackChannel_(); |
| 1895 |
goog.net.BrowserChannel.notifyStatEvent( |
| 1896 |
goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING); |
| 1897 |
}; |
| 1898 |
|
| 1899 |
|
| 1900 |
/** |
| 1901 |
* Determines whether we should start the process of retrying a possibly |
| 1902 |
* dead backchannel. |
| 1903 |
* @param {number} outstandingBytes The number of bytes for which the server has |
| 1904 |
* not yet received acknowledgement. |
| 1905 |
* @return {boolean} Whether to start the backchannel retry timer. |
| 1906 |
* @private |
| 1907 |
*/ |
| 1908 |
goog.net.BrowserChannel.prototype.shouldRetryBackChannel_ = function( |
| 1909 |
outstandingBytes) { |
| 1910 |
// Not too many outstanding bytes, not buffered and not after a retry. |
| 1911 |
return outstandingBytes < |
| 1912 |
goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF && |
| 1913 |
!this.isBuffered() && this.backChannelRetryCount_ == 0; |
| 1914 |
}; |
| 1915 |
|
| 1916 |
|
| 1917 |
/** |
| 1918 |
* Decides which host prefix should be used, if any. If there is a handler, |
| 1919 |
* allows the handler to validate a host prefix provided by the server, and |
| 1920 |
* optionally override it. |
| 1921 |
* @param {?string} serverHostPrefix The host prefix provided by the server. |
| 1922 |
* @return {?string} The host prefix to actually use, if any. Will return null |
| 1923 |
* if the use of host prefixes was disabled via setAllowHostPrefix(). |
| 1924 |
*/ |
| 1925 |
goog.net.BrowserChannel.prototype.correctHostPrefix = function( |
| 1926 |
serverHostPrefix) { |
| 1927 |
if (this.allowHostPrefix_) { |
| 1928 |
if (this.handler_) { |
| 1929 |
return this.handler_.correctHostPrefix(serverHostPrefix); |
| 1930 |
} |
| 1931 |
return serverHostPrefix; |
| 1932 |
} |
| 1933 |
return null; |
| 1934 |
}; |
| 1935 |
|
| 1936 |
|
| 1937 |
/** |
| 1938 |
* Handles the timer that indicates that our backchannel is no longer able to |
| 1939 |
* successfully receive data from the server. |
| 1940 |
* @private |
| 1941 |
*/ |
| 1942 |
goog.net.BrowserChannel.prototype.onBackChannelDead_ = function() { |
| 1943 |
if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) { |
| 1944 |
this.deadBackChannelTimerId_ = null; |
| 1945 |
this.backChannelRequest_.cancel(); |
| 1946 |
this.backChannelRequest_ = null; |
| 1947 |
this.maybeRetryBackChannel_(); |
| 1948 |
goog.net.BrowserChannel.notifyStatEvent( |
| 1949 |
goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD); |
| 1950 |
} |
| 1951 |
}; |
| 1952 |
|
| 1953 |
|
| 1954 |
/** |
| 1955 |
* Clears the timer that indicates that our backchannel is no longer able to |
| 1956 |
* successfully receive data from the server. |
| 1957 |
* @private |
| 1958 |
*/ |
| 1959 |
goog.net.BrowserChannel.prototype.clearDeadBackchannelTimer_ = function() { |
| 1960 |
if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) { |
| 1961 |
goog.global.clearTimeout(this.deadBackChannelTimerId_); |
| 1962 |
this.deadBackChannelTimerId_ = null; |
| 1963 |
} |
| 1964 |
}; |
| 1965 |
|
| 1966 |
|
| 1967 |
/** |
| 1968 |
* Returns whether or not the given error/status combination is fatal or not. |
| 1969 |
* On fatal errors we immediately close the session rather than retrying the |
| 1970 |
* failed request. |
| 1971 |
* @param {goog.net.ChannelRequest.Error?} error The error code for the failed |
| 1972 |
* request. |
| 1973 |
* @param {number} statusCode The last HTTP status code. |
| 1974 |
* @return {boolean} Whether or not the error is fatal. |
| 1975 |
* @private |
| 1976 |
*/ |
| 1977 |
goog.net.BrowserChannel.isFatalError_ = function(error, statusCode) { |
| 1978 |
return error == goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID || |
| 1979 |
error == goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED || |
| 1980 |
(error == goog.net.ChannelRequest.Error.STATUS && statusCode > 0); |
| 1981 |
}; |
| 1982 |
|
| 1983 |
|
| 1984 |
/** |
| 1985 |
* Callback from ChannelRequest that indicates a request has completed. |
| 1986 |
* @param {goog.net.ChannelRequest} request The request object. |
| 1987 |
*/ |
| 1988 |
goog.net.BrowserChannel.prototype.onRequestComplete = function(request) { |
| 1989 |
this.channelDebug_.debug('Request complete'); |
| 1990 |
var type; |
| 1991 |
if (this.backChannelRequest_ == request) { |
| 1992 |
this.clearDeadBackchannelTimer_(); |
| 1993 |
this.backChannelRequest_ = null; |
| 1994 |
type = goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL; |
| 1995 |
} else if (this.forwardChannelRequest_ == request) { |
| 1996 |
this.forwardChannelRequest_ = null; |
| 1997 |
type = goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL; |
| 1998 |
} else { |
| 1999 |
// return if it was an old request from a previous session |
| 2000 |
return; |
| 2001 |
} |
| 2002 |
|
| 2003 |
this.lastStatusCode_ = request.getLastStatusCode(); |
| 2004 |
|
| 2005 |
if (this.state_ == goog.net.BrowserChannel.State.CLOSED) { |
| 2006 |
return; |
| 2007 |
} |
| 2008 |
|
| 2009 |
if (request.getSuccess()) { |
| 2010 |
// Yay! |
| 2011 |
if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) { |
| 2012 |
var size = request.getPostData() ? request.getPostData().length : 0; |
| 2013 |
goog.net.BrowserChannel.notifyTimingEvent( |
| 2014 |
size, goog.now() - request.getRequestStartTime(), |
| 2015 |
this.forwardChannelRetryCount_); |
| 2016 |
this.ensureForwardChannel_(); |
| 2017 |
this.onSuccess_(); |
| 2018 |
this.pendingMaps_.length = 0; |
| 2019 |
} else { // i.e., back-channel |
| 2020 |
this.ensureBackChannel_(); |
| 2021 |
} |
| 2022 |
return; |
| 2023 |
} |
| 2024 |
// Else unsuccessful. Fall through. |
| 2025 |
|
| 2026 |
var lastError = request.getLastError(); |
| 2027 |
if (!goog.net.BrowserChannel.isFatalError_(lastError, this.lastStatusCode_)) { |
| 2028 |
// Maybe retry. |
| 2029 |
this.channelDebug_.debug( |
| 2030 |
'Maybe retrying, last error: ' + |
| 2031 |
goog.net.ChannelRequest.errorStringFromCode( |
| 2032 |
/** @type {goog.net.ChannelRequest.Error} */ (lastError), |
| 2033 |
this.lastStatusCode_)); |
| 2034 |
if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) { |
| 2035 |
if (this.maybeRetryForwardChannel_(request)) { |
| 2036 |
return; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
if (type == goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL) { |
| 2040 |
if (this.maybeRetryBackChannel_()) { |
| 2041 |
return; |
| 2042 |
} |
| 2043 |
} |
| 2044 |
// Else exceeded max retries. Fall through. |
| 2045 |
this.channelDebug_.debug('Exceeded max number of retries'); |
| 2046 |
} else { |
| 2047 |
// Else fatal error. Fall through and mark the pending maps as failed. |
| 2048 |
this.channelDebug_.debug('Not retrying due to error type'); |
| 2049 |
} |
| 2050 |
|
| 2051 |
|
| 2052 |
// Can't save this session. :( |
| 2053 |
this.channelDebug_.debug('Error: HTTP request failed'); |
| 2054 |
switch (lastError) { |
| 2055 |
case goog.net.ChannelRequest.Error.NO_DATA: |
| 2056 |
this.signalError_(goog.net.BrowserChannel.Error.NO_DATA); |
| 2057 |
break; |
| 2058 |
case goog.net.ChannelRequest.Error.BAD_DATA: |
| 2059 |
this.signalError_(goog.net.BrowserChannel.Error.BAD_DATA); |
| 2060 |
break; |
| 2061 |
case goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID: |
| 2062 |
this.signalError_(goog.net.BrowserChannel.Error.UNKNOWN_SESSION_ID); |
| 2063 |
break; |
| 2064 |
case goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED: |
| 2065 |
this.signalError_(goog.net.BrowserChannel.Error.ACTIVE_X_BLOCKED); |
| 2066 |
break; |
| 2067 |
default: |
| 2068 |
this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED); |
| 2069 |
break; |
| 2070 |
} |
| 2071 |
}; |
| 2072 |
|
| 2073 |
|
| 2074 |
/** |
| 2075 |
* @param {number} retryCount Number of retries so far. |
| 2076 |
* @return {number} Time in ms before firing next retry request. |
| 2077 |
* @private |
| 2078 |
*/ |
| 2079 |
goog.net.BrowserChannel.prototype.getRetryTime_ = function(retryCount) { |
| 2080 |
var retryTime = this.baseRetryDelayMs_ + |
| 2081 |
Math.floor(Math.random() * this.retryDelaySeedMs_); |
| 2082 |
if (!this.isActive()) { |
| 2083 |
this.channelDebug_.debug('Inactive channel'); |
| 2084 |
retryTime = |
| 2085 |
retryTime * goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR; |
| 2086 |
} |
| 2087 |
// Backoff for subsequent retries |
| 2088 |
retryTime = retryTime * retryCount; |
| 2089 |
return retryTime; |
| 2090 |
}; |
| 2091 |
|
| 2092 |
|
| 2093 |
/** |
| 2094 |
* @param {number} baseDelayMs The base part of the retry delay, in ms. |
| 2095 |
* @param {number} delaySeedMs A random delay between 0 and this is added to |
| 2096 |
* the base part. |
| 2097 |
*/ |
| 2098 |
goog.net.BrowserChannel.prototype.setRetryDelay = function( |
| 2099 |
baseDelayMs, delaySeedMs) { |
| 2100 |
this.baseRetryDelayMs_ = baseDelayMs; |
| 2101 |
this.retryDelaySeedMs_ = delaySeedMs; |
| 2102 |
}; |
| 2103 |
|
| 2104 |
|
| 2105 |
/** |
| 2106 |
* Processes the data returned by the server. |
| 2107 |
* @param {!Array<!Array<?>>} respArray The response array returned |
| 2108 |
* by the server. |
| 2109 |
* @private |
| 2110 |
*/ |
| 2111 |
goog.net.BrowserChannel.prototype.onInput_ = function(respArray) { |
| 2112 |
var batch = |
| 2113 |
this.handler_ && this.handler_.channelHandleMultipleArrays ? [] : null; |
| 2114 |
for (var i = 0; i < respArray.length; i++) { |
| 2115 |
var nextArray = respArray[i]; |
| 2116 |
this.lastArrayId_ = nextArray[0]; |
| 2117 |
nextArray = nextArray[1]; |
| 2118 |
if (this.state_ == goog.net.BrowserChannel.State.OPENING) { |
| 2119 |
if (nextArray[0] == 'c') { |
| 2120 |
this.sid_ = nextArray[1]; |
| 2121 |
this.hostPrefix_ = this.correctHostPrefix(nextArray[2]); |
| 2122 |
var negotiatedVersion = nextArray[3]; |
| 2123 |
if (goog.isDefAndNotNull(negotiatedVersion)) { |
| 2124 |
this.channelVersion_ = negotiatedVersion; |
| 2125 |
} else { |
| 2126 |
// Servers prior to version 7 did not send this, so assume version 6. |
| 2127 |
this.channelVersion_ = 6; |
| 2128 |
} |
| 2129 |
this.state_ = goog.net.BrowserChannel.State.OPENED; |
| 2130 |
if (this.handler_) { |
| 2131 |
this.handler_.channelOpened(this); |
| 2132 |
} |
| 2133 |
this.backChannelUri_ = this.getBackChannelUri( |
| 2134 |
this.hostPrefix_, /** @type {string} */ (this.path_)); |
| 2135 |
// Open connection to receive data |
| 2136 |
this.ensureBackChannel_(); |
| 2137 |
} else if (nextArray[0] == 'stop') { |
| 2138 |
this.signalError_(goog.net.BrowserChannel.Error.STOP); |
| 2139 |
} |
| 2140 |
} else if (this.state_ == goog.net.BrowserChannel.State.OPENED) { |
| 2141 |
if (nextArray[0] == 'stop') { |
| 2142 |
if (batch && !goog.array.isEmpty(batch)) { |
| 2143 |
this.handler_.channelHandleMultipleArrays(this, batch); |
| 2144 |
batch.length = 0; |
| 2145 |
} |
| 2146 |
this.signalError_(goog.net.BrowserChannel.Error.STOP); |
| 2147 |
} else if (nextArray[0] == 'noop') { |
| 2148 |
// ignore - noop to keep connection happy |
| 2149 |
} else { |
| 2150 |
if (batch) { |
| 2151 |
batch.push(nextArray); |
| 2152 |
} else if (this.handler_) { |
| 2153 |
this.handler_.channelHandleArray(this, nextArray); |
| 2154 |
} |
| 2155 |
} |
| 2156 |
// We have received useful data on the back-channel, so clear its retry |
| 2157 |
// count. We do this because back-channels by design do not complete |
| 2158 |
// quickly, so on a flaky connection we could have many fail to complete |
| 2159 |
// fully but still deliver a lot of data before they fail. We don't want |
| 2160 |
// to count such failures towards the retry limit, because we don't want |
| 2161 |
// to give up on a session if we can still receive data. |
| 2162 |
this.backChannelRetryCount_ = 0; |
| 2163 |
} |
| 2164 |
} |
| 2165 |
if (batch && !goog.array.isEmpty(batch)) { |
| 2166 |
this.handler_.channelHandleMultipleArrays(this, batch); |
| 2167 |
} |
| 2168 |
}; |
| 2169 |
|
| 2170 |
|
| 2171 |
/** |
| 2172 |
* Helper to ensure the BrowserChannel is in the expected state. |
| 2173 |
* @param {...number} var_args The channel must be in one of the indicated |
| 2174 |
* states. |
| 2175 |
* @private |
| 2176 |
*/ |
| 2177 |
goog.net.BrowserChannel.prototype.ensureInState_ = function(var_args) { |
| 2178 |
if (!goog.array.contains(arguments, this.state_)) { |
| 2179 |
throw new Error('Unexpected channel state: ' + this.state_); |
| 2180 |
} |
| 2181 |
}; |
| 2182 |
|
| 2183 |
|
| 2184 |
/** |
| 2185 |
* Signals an error has occurred. |
| 2186 |
* @param {goog.net.BrowserChannel.Error} error The error code for the failure. |
| 2187 |
* @private |
| 2188 |
*/ |
| 2189 |
goog.net.BrowserChannel.prototype.signalError_ = function(error) { |
| 2190 |
this.channelDebug_.info('Error code ' + error); |
| 2191 |
if (error == goog.net.BrowserChannel.Error.REQUEST_FAILED || |
| 2192 |
error == goog.net.BrowserChannel.Error.BLOCKED) { |
| 2193 |
// Ping google to check if it's a server error or user's network error. |
| 2194 |
var imageUri = null; |
| 2195 |
if (this.handler_) { |
| 2196 |
imageUri = this.handler_.getNetworkTestImageUri(this); |
| 2197 |
} |
| 2198 |
goog.net.tmpnetwork.testGoogleCom( |
| 2199 |
goog.bind(this.testGoogleComCallback_, this), imageUri); |
| 2200 |
} else { |
| 2201 |
goog.net.BrowserChannel.notifyStatEvent( |
| 2202 |
goog.net.BrowserChannel.Stat.ERROR_OTHER); |
| 2203 |
} |
| 2204 |
this.onError_(error); |
| 2205 |
}; |
| 2206 |
|
| 2207 |
|
| 2208 |
/** |
| 2209 |
* Callback for testGoogleCom during error handling. |
| 2210 |
* @param {boolean} networkUp Whether the network is up. |
| 2211 |
* @private |
| 2212 |
*/ |
| 2213 |
goog.net.BrowserChannel.prototype.testGoogleComCallback_ = function(networkUp) { |
| 2214 |
if (networkUp) { |
| 2215 |
this.channelDebug_.info('Successfully pinged google.com'); |
| 2216 |
goog.net.BrowserChannel.notifyStatEvent( |
| 2217 |
goog.net.BrowserChannel.Stat.ERROR_OTHER); |
| 2218 |
} else { |
| 2219 |
this.channelDebug_.info('Failed to ping google.com'); |
| 2220 |
goog.net.BrowserChannel.notifyStatEvent( |
| 2221 |
goog.net.BrowserChannel.Stat.ERROR_NETWORK); |
| 2222 |
// We call onError_ here instead of signalError_ because the latter just |
| 2223 |
// calls notifyStatEvent, and we don't want to have another stat event. |
| 2224 |
this.onError_(goog.net.BrowserChannel.Error.NETWORK); |
| 2225 |
} |
| 2226 |
}; |
| 2227 |
|
| 2228 |
|
| 2229 |
/** |
| 2230 |
* Called when messages have been successfully sent from the queue. |
| 2231 |
* @private |
| 2232 |
*/ |
| 2233 |
goog.net.BrowserChannel.prototype.onSuccess_ = function() { |
| 2234 |
if (this.handler_) { |
| 2235 |
this.handler_.channelSuccess(this, this.pendingMaps_); |
| 2236 |
} |
| 2237 |
}; |
| 2238 |
|
| 2239 |
|
| 2240 |
/** |
| 2241 |
* Called when we've determined the final error for a channel. It closes the |
| 2242 |
* notifiers the handler of the error and closes the channel. |
| 2243 |
* @param {goog.net.BrowserChannel.Error} error The error code for the failure. |
| 2244 |
* @private |
| 2245 |
*/ |
| 2246 |
goog.net.BrowserChannel.prototype.onError_ = function(error) { |
| 2247 |
this.channelDebug_.debug('HttpChannel: error - ' + error); |
| 2248 |
this.state_ = goog.net.BrowserChannel.State.CLOSED; |
| 2249 |
if (this.handler_) { |
| 2250 |
this.handler_.channelError(this, error); |
| 2251 |
} |
| 2252 |
this.onClose_(); |
| 2253 |
this.cancelRequests_(); |
| 2254 |
}; |
| 2255 |
|
| 2256 |
|
| 2257 |
/** |
| 2258 |
* Called when the channel has been closed. It notifiers the handler of the |
| 2259 |
* event, and reports any pending or undelivered maps. |
| 2260 |
* @private |
| 2261 |
*/ |
| 2262 |
goog.net.BrowserChannel.prototype.onClose_ = function() { |
| 2263 |
this.state_ = goog.net.BrowserChannel.State.CLOSED; |
| 2264 |
this.lastStatusCode_ = -1; |
| 2265 |
if (this.handler_) { |
| 2266 |
if (this.pendingMaps_.length == 0 && this.outgoingMaps_.length == 0) { |
| 2267 |
this.handler_.channelClosed(this); |
| 2268 |
} else { |
| 2269 |
this.channelDebug_.debug( |
| 2270 |
'Number of undelivered maps' + |
| 2271 |
', pending: ' + this.pendingMaps_.length + ', outgoing: ' + |
| 2272 |
this.outgoingMaps_.length); |
| 2273 |
|
| 2274 |
var copyOfPendingMaps = goog.array.clone(this.pendingMaps_); |
| 2275 |
var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_); |
| 2276 |
this.pendingMaps_.length = 0; |
| 2277 |
this.outgoingMaps_.length = 0; |
| 2278 |
|
| 2279 |
this.handler_.channelClosed( |
| 2280 |
this, copyOfPendingMaps, copyOfUndeliveredMaps); |
| 2281 |
} |
| 2282 |
} |
| 2283 |
}; |
| 2284 |
|
| 2285 |
|
| 2286 |
/** |
| 2287 |
* Gets the Uri used for the connection that sends data to the server. |
| 2288 |
* @param {string} path The path on the host. |
| 2289 |
* @return {!goog.Uri} The forward channel URI. |
| 2290 |
*/ |
| 2291 |
goog.net.BrowserChannel.prototype.getForwardChannelUri = function(path) { |
| 2292 |
var uri = this.createDataUri(null, path); |
| 2293 |
this.channelDebug_.debug('GetForwardChannelUri: ' + uri); |
| 2294 |
return uri; |
| 2295 |
}; |
| 2296 |
|
| 2297 |
|
| 2298 |
/** |
| 2299 |
* Gets the results for the first browser channel test |
| 2300 |
* @return {Array<string>} The results. |
| 2301 |
*/ |
| 2302 |
goog.net.BrowserChannel.prototype.getFirstTestResults = function() { |
| 2303 |
return this.firstTestResults_; |
| 2304 |
}; |
| 2305 |
|
| 2306 |
|
| 2307 |
/** |
| 2308 |
* Gets the results for the second browser channel test |
| 2309 |
* @return {?boolean} The results. True -> buffered connection, |
| 2310 |
* False -> unbuffered, null -> unknown. |
| 2311 |
*/ |
| 2312 |
goog.net.BrowserChannel.prototype.getSecondTestResults = function() { |
| 2313 |
return this.secondTestResults_; |
| 2314 |
}; |
| 2315 |
|
| 2316 |
|
| 2317 |
/** |
| 2318 |
* Gets the Uri used for the connection that receives data from the server. |
| 2319 |
* @param {?string} hostPrefix The host prefix. |
| 2320 |
* @param {string} path The path on the host. |
| 2321 |
* @return {!goog.Uri} The back channel URI. |
| 2322 |
*/ |
| 2323 |
goog.net.BrowserChannel.prototype.getBackChannelUri = function( |
| 2324 |
hostPrefix, path) { |
| 2325 |
var uri = this.createDataUri( |
| 2326 |
this.shouldUseSecondaryDomains() ? hostPrefix : null, path); |
| 2327 |
this.channelDebug_.debug('GetBackChannelUri: ' + uri); |
| 2328 |
return uri; |
| 2329 |
}; |
| 2330 |
|
| 2331 |
|
| 2332 |
/** |
| 2333 |
* Creates a data Uri applying logic for secondary hostprefix, port |
| 2334 |
* overrides, and versioning. |
| 2335 |
* @param {?string} hostPrefix The host prefix. |
| 2336 |
* @param {string} path The path on the host (may be absolute or relative). |
| 2337 |
* @param {number=} opt_overridePort Optional override port. |
| 2338 |
* @return {!goog.Uri} The data URI. |
| 2339 |
*/ |
| 2340 |
goog.net.BrowserChannel.prototype.createDataUri = function( |
| 2341 |
hostPrefix, path, opt_overridePort) { |
| 2342 |
var uri = goog.Uri.parse(path); |
| 2343 |
var uriAbsolute = (uri.getDomain() != ''); |
| 2344 |
if (uriAbsolute) { |
| 2345 |
if (hostPrefix) { |
| 2346 |
uri.setDomain(hostPrefix + '.' + uri.getDomain()); |
| 2347 |
} |
| 2348 |
|
| 2349 |
uri.setPort(opt_overridePort || uri.getPort()); |
| 2350 |
} else { |
| 2351 |
var locationPage = window.location; |
| 2352 |
var hostName; |
| 2353 |
if (hostPrefix) { |
| 2354 |
hostName = hostPrefix + '.' + locationPage.hostname; |
| 2355 |
} else { |
| 2356 |
hostName = locationPage.hostname; |
| 2357 |
} |
| 2358 |
|
| 2359 |
var port = opt_overridePort || locationPage.port; |
| 2360 |
|
| 2361 |
uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path); |
| 2362 |
} |
| 2363 |
|
| 2364 |
if (this.extraParams_) { |
| 2365 |
goog.object.forEach(this.extraParams_, function(value, key) { |
| 2366 |
uri.setParameterValue(key, value); |
| 2367 |
}); |
| 2368 |
} |
| 2369 |
|
| 2370 |
// Add the protocol version to the URI. |
| 2371 |
uri.setParameterValue('VER', this.channelVersion_); |
| 2372 |
|
| 2373 |
// Add the reconnect parameters. |
| 2374 |
this.addAdditionalParams_(uri); |
| 2375 |
|
| 2376 |
return uri; |
| 2377 |
}; |
| 2378 |
|
| 2379 |
|
| 2380 |
/** |
| 2381 |
* Called when BC needs to create an XhrIo object. Override in a subclass if |
| 2382 |
* you need to customize the behavior, for example to enable the creation of |
| 2383 |
* XHR's capable of calling a secondary domain. Will also allow calling |
| 2384 |
* a secondary domain if withCredentials (CORS) is enabled. |
| 2385 |
* @param {?string} hostPrefix The host prefix, if we need an XhrIo object |
| 2386 |
* capable of calling a secondary domain. |
| 2387 |
* @return {!goog.net.XhrIo} A new XhrIo object. |
| 2388 |
*/ |
| 2389 |
goog.net.BrowserChannel.prototype.createXhrIo = function(hostPrefix) { |
| 2390 |
if (hostPrefix && !this.supportsCrossDomainXhrs_) { |
| 2391 |
throw new Error('Can\'t create secondary domain capable XhrIo object.'); |
| 2392 |
} |
| 2393 |
var xhr = new goog.net.XhrIo(); |
| 2394 |
xhr.setWithCredentials(this.supportsCrossDomainXhrs_); |
| 2395 |
return xhr; |
| 2396 |
}; |
| 2397 |
|
| 2398 |
|
| 2399 |
/** |
| 2400 |
* Gets whether this channel is currently active. This is used to determine the |
| 2401 |
* length of time to wait before retrying. This call delegates to the handler. |
| 2402 |
* @return {boolean} Whether the channel is currently active. |
| 2403 |
*/ |
| 2404 |
goog.net.BrowserChannel.prototype.isActive = function() { |
| 2405 |
return !!this.handler_ && this.handler_.isActive(this); |
| 2406 |
}; |
| 2407 |
|
| 2408 |
|
| 2409 |
/** |
| 2410 |
* Wrapper around SafeTimeout which calls the start and end execution hooks |
| 2411 |
* with a try...finally block. |
| 2412 |
* @param {Function} fn The callback function. |
| 2413 |
* @param {number} ms The time in MS for the timer. |
| 2414 |
* @return {number} The ID of the timer. |
| 2415 |
*/ |
| 2416 |
goog.net.BrowserChannel.setTimeout = function(fn, ms) { |
| 2417 |
if (!goog.isFunction(fn)) { |
| 2418 |
throw new Error('Fn must not be null and must be a function'); |
| 2419 |
} |
| 2420 |
return goog.global.setTimeout(function() { |
| 2421 |
goog.net.BrowserChannel.onStartExecution(); |
| 2422 |
try { |
| 2423 |
fn(); |
| 2424 |
} finally { |
| 2425 |
goog.net.BrowserChannel.onEndExecution(); |
| 2426 |
} |
| 2427 |
}, ms); |
| 2428 |
}; |
| 2429 |
|
| 2430 |
|
| 2431 |
/** |
| 2432 |
* Helper function to call the start hook |
| 2433 |
*/ |
| 2434 |
goog.net.BrowserChannel.onStartExecution = function() { |
| 2435 |
goog.net.BrowserChannel.startExecutionHook_(); |
| 2436 |
}; |
| 2437 |
|
| 2438 |
|
| 2439 |
/** |
| 2440 |
* Helper function to call the end hook |
| 2441 |
*/ |
| 2442 |
goog.net.BrowserChannel.onEndExecution = function() { |
| 2443 |
goog.net.BrowserChannel.endExecutionHook_(); |
| 2444 |
}; |
| 2445 |
|
| 2446 |
|
| 2447 |
/** |
| 2448 |
* Returns the singleton event target for stat events. |
| 2449 |
* @return {goog.events.EventTarget} The event target for stat events. |
| 2450 |
*/ |
| 2451 |
goog.net.BrowserChannel.getStatEventTarget = function() { |
| 2452 |
return goog.net.BrowserChannel.statEventTarget_; |
| 2453 |
}; |
| 2454 |
|
| 2455 |
|
| 2456 |
/** |
| 2457 |
* Notify the channel that a particular fine grained network event has occurred. |
| 2458 |
* Should be considered package-private. |
| 2459 |
* @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The |
| 2460 |
* reachability event type. |
| 2461 |
*/ |
| 2462 |
goog.net.BrowserChannel.prototype.notifyServerReachabilityEvent = function( |
| 2463 |
reachabilityType) { |
| 2464 |
var target = goog.net.BrowserChannel.statEventTarget_; |
| 2465 |
target.dispatchEvent( |
| 2466 |
new goog.net.BrowserChannel.ServerReachabilityEvent( |
| 2467 |
target, reachabilityType)); |
| 2468 |
}; |
| 2469 |
|
| 2470 |
|
| 2471 |
/** |
| 2472 |
* Helper function to call the stat event callback. |
| 2473 |
* @param {goog.net.BrowserChannel.Stat} stat The stat. |
| 2474 |
*/ |
| 2475 |
goog.net.BrowserChannel.notifyStatEvent = function(stat) { |
| 2476 |
var target = goog.net.BrowserChannel.statEventTarget_; |
| 2477 |
target.dispatchEvent(new goog.net.BrowserChannel.StatEvent(target, stat)); |
| 2478 |
}; |
| 2479 |
|
| 2480 |
|
| 2481 |
/** |
| 2482 |
* Helper function to notify listeners about POST request performance. |
| 2483 |
* |
| 2484 |
* @param {number} size Number of characters in the POST data. |
| 2485 |
* @param {number} rtt The amount of time from POST start to response. |
| 2486 |
* @param {number} retries The number of times the POST had to be retried. |
| 2487 |
*/ |
| 2488 |
goog.net.BrowserChannel.notifyTimingEvent = function(size, rtt, retries) { |
| 2489 |
var target = goog.net.BrowserChannel.statEventTarget_; |
| 2490 |
target.dispatchEvent( |
| 2491 |
new goog.net.BrowserChannel.TimingEvent(target, size, rtt, retries)); |
| 2492 |
}; |
| 2493 |
|
| 2494 |
|
| 2495 |
/** |
| 2496 |
* Determines whether to use a secondary domain when the server gives us |
| 2497 |
* a host prefix. This allows us to work around browser per-domain |
| 2498 |
* connection limits. |
| 2499 |
* |
| 2500 |
* Currently, we use secondary domains when using Trident's ActiveXObject, |
| 2501 |
* because it supports cross-domain requests out of the box. Note that in IE10 |
| 2502 |
* we no longer use ActiveX since it's not supported in Metro mode and IE10 |
| 2503 |
* supports XHR streaming. |
| 2504 |
* |
| 2505 |
* If you need to use secondary domains on other browsers and IE10, |
| 2506 |
* you have two choices: |
| 2507 |
* 1) If you only care about browsers that support CORS |
| 2508 |
* (https://developer.mozilla.org/en-US/docs/HTTP_access_control), you |
| 2509 |
* can use {@link #setSupportsCrossDomainXhrs} and set the appropriate |
| 2510 |
* CORS response headers on the server. |
| 2511 |
* 2) Or, override this method in a subclass, and make sure that those |
| 2512 |
* browsers use some messaging mechanism that works cross-domain (e.g |
| 2513 |
* iframes and window.postMessage). |
| 2514 |
* |
| 2515 |
* @return {boolean} Whether to use secondary domains. |
| 2516 |
* @see http://code.google.com/p/closure-library/issues/detail?id=339 |
| 2517 |
*/ |
| 2518 |
goog.net.BrowserChannel.prototype.shouldUseSecondaryDomains = function() { |
| 2519 |
return this.supportsCrossDomainXhrs_ || |
| 2520 |
!goog.net.ChannelRequest.supportsXhrStreaming(); |
| 2521 |
}; |
| 2522 |
|
| 2523 |
|
| 2524 |
/** |
| 2525 |
* A LogSaver that can be used to accumulate all the debug logs for |
| 2526 |
* BrowserChannels so they can be sent to the server when a problem is |
| 2527 |
* detected. |
| 2528 |
* @const |
| 2529 |
*/ |
| 2530 |
goog.net.BrowserChannel.LogSaver = {}; |
| 2531 |
|
| 2532 |
|
| 2533 |
/** |
| 2534 |
* Buffer for accumulating the debug log |
| 2535 |
* @type {goog.structs.CircularBuffer} |
| 2536 |
* @private |
| 2537 |
*/ |
| 2538 |
goog.net.BrowserChannel.LogSaver.buffer_ = |
| 2539 |
new goog.structs.CircularBuffer(1000); |
| 2540 |
|
| 2541 |
|
| 2542 |
/** |
| 2543 |
* Whether we're currently accumulating the debug log. |
| 2544 |
* @type {boolean} |
| 2545 |
* @private |
| 2546 |
*/ |
| 2547 |
goog.net.BrowserChannel.LogSaver.enabled_ = false; |
| 2548 |
|
| 2549 |
|
| 2550 |
/** |
| 2551 |
* Formatter for saving logs. |
| 2552 |
* @type {goog.debug.Formatter} |
| 2553 |
* @private |
| 2554 |
*/ |
| 2555 |
goog.net.BrowserChannel.LogSaver.formatter_ = new goog.debug.TextFormatter(); |
| 2556 |
|
| 2557 |
|
| 2558 |
/** |
| 2559 |
* Returns whether the LogSaver is enabled. |
| 2560 |
* @return {boolean} Whether saving is enabled or disabled. |
| 2561 |
*/ |
| 2562 |
goog.net.BrowserChannel.LogSaver.isEnabled = function() { |
| 2563 |
return goog.net.BrowserChannel.LogSaver.enabled_; |
| 2564 |
}; |
| 2565 |
|
| 2566 |
|
| 2567 |
/** |
| 2568 |
* Enables of disables the LogSaver. |
| 2569 |
* @param {boolean} enable Whether to enable or disable saving. |
| 2570 |
*/ |
| 2571 |
goog.net.BrowserChannel.LogSaver.setEnabled = function(enable) { |
| 2572 |
if (enable == goog.net.BrowserChannel.LogSaver.enabled_) { |
| 2573 |
return; |
| 2574 |
} |
| 2575 |
|
| 2576 |
var fn = goog.net.BrowserChannel.LogSaver.addLogRecord; |
| 2577 |
var logger = goog.log.getLogger('goog.net'); |
| 2578 |
if (enable) { |
| 2579 |
goog.log.addHandler(logger, fn); |
| 2580 |
} else { |
| 2581 |
goog.log.removeHandler(logger, fn); |
| 2582 |
} |
| 2583 |
}; |
| 2584 |
|
| 2585 |
|
| 2586 |
/** |
| 2587 |
* Adds a log record. |
| 2588 |
* @param {goog.log.LogRecord} logRecord the LogRecord. |
| 2589 |
*/ |
| 2590 |
goog.net.BrowserChannel.LogSaver.addLogRecord = function(logRecord) { |
| 2591 |
goog.net.BrowserChannel.LogSaver.buffer_.add( |
| 2592 |
goog.net.BrowserChannel.LogSaver.formatter_.formatRecord(logRecord)); |
| 2593 |
}; |
| 2594 |
|
| 2595 |
|
| 2596 |
/** |
| 2597 |
* Returns the log as a single string. |
| 2598 |
* @return {string} The log as a single string. |
| 2599 |
*/ |
| 2600 |
goog.net.BrowserChannel.LogSaver.getBuffer = function() { |
| 2601 |
return goog.net.BrowserChannel.LogSaver.buffer_.getValues().join(''); |
| 2602 |
}; |
| 2603 |
|
| 2604 |
|
| 2605 |
/** |
| 2606 |
* Clears the buffer |
| 2607 |
*/ |
| 2608 |
goog.net.BrowserChannel.LogSaver.clearBuffer = function() { |
| 2609 |
goog.net.BrowserChannel.LogSaver.buffer_.clear(); |
| 2610 |
}; |
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
/** |
| 2615 |
* Abstract base class for the browser channel handler |
| 2616 |
* @constructor |
| 2617 |
*/ |
| 2618 |
goog.net.BrowserChannel.Handler = function() {}; |
| 2619 |
|
| 2620 |
|
| 2621 |
/** |
| 2622 |
* Callback handler for when a batch of response arrays is received from the |
| 2623 |
* server. |
| 2624 |
* @type {?function(!goog.net.BrowserChannel, !Array<!Array<?>>)} |
| 2625 |
*/ |
| 2626 |
goog.net.BrowserChannel.Handler.prototype.channelHandleMultipleArrays = null; |
| 2627 |
|
| 2628 |
|
| 2629 |
/** |
| 2630 |
* Whether it's okay to make a request to the server. A handler can return |
| 2631 |
* false if the channel should fail. For example, if the user has logged out, |
| 2632 |
* the handler may want all requests to fail immediately. |
| 2633 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2634 |
* @return {goog.net.BrowserChannel.Error} An error code. The code should |
| 2635 |
* return goog.net.BrowserChannel.Error.OK to indicate it's okay. Any other |
| 2636 |
* error code will cause a failure. |
| 2637 |
*/ |
| 2638 |
goog.net.BrowserChannel.Handler.prototype.okToMakeRequest = function( |
| 2639 |
browserChannel) { |
| 2640 |
return goog.net.BrowserChannel.Error.OK; |
| 2641 |
}; |
| 2642 |
|
| 2643 |
|
| 2644 |
/** |
| 2645 |
* Indicates the BrowserChannel has successfully negotiated with the server |
| 2646 |
* and can now send and receive data. |
| 2647 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2648 |
*/ |
| 2649 |
goog.net.BrowserChannel.Handler.prototype.channelOpened = function( |
| 2650 |
browserChannel) {}; |
| 2651 |
|
| 2652 |
|
| 2653 |
/** |
| 2654 |
* New input is available for the application to process. |
| 2655 |
* |
| 2656 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2657 |
* @param {Array<?>} array The data array. |
| 2658 |
*/ |
| 2659 |
goog.net.BrowserChannel.Handler.prototype.channelHandleArray = function( |
| 2660 |
browserChannel, array) {}; |
| 2661 |
|
| 2662 |
|
| 2663 |
/** |
| 2664 |
* Indicates maps were successfully sent on the BrowserChannel. |
| 2665 |
* |
| 2666 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2667 |
* @param {Array<goog.net.BrowserChannel.QueuedMap>} deliveredMaps The |
| 2668 |
* array of maps that have been delivered to the server. This is a direct |
| 2669 |
* reference to the internal BrowserChannel array, so a copy should be made |
| 2670 |
* if the caller desires a reference to the data. |
| 2671 |
*/ |
| 2672 |
goog.net.BrowserChannel.Handler.prototype.channelSuccess = function( |
| 2673 |
browserChannel, deliveredMaps) {}; |
| 2674 |
|
| 2675 |
|
| 2676 |
/** |
| 2677 |
* Indicates an error occurred on the BrowserChannel. |
| 2678 |
* |
| 2679 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2680 |
* @param {goog.net.BrowserChannel.Error} error The error code. |
| 2681 |
*/ |
| 2682 |
goog.net.BrowserChannel.Handler.prototype.channelError = function( |
| 2683 |
browserChannel, error) {}; |
| 2684 |
|
| 2685 |
|
| 2686 |
/** |
| 2687 |
* Indicates the BrowserChannel is closed. Also notifies about which maps, |
| 2688 |
* if any, that may not have been delivered to the server. |
| 2689 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2690 |
* @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_pendingMaps The |
| 2691 |
* array of pending maps, which may or may not have been delivered to the |
| 2692 |
* server. |
| 2693 |
* @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_undeliveredMaps |
| 2694 |
* The array of undelivered maps, which have definitely not been delivered |
| 2695 |
* to the server. |
| 2696 |
*/ |
| 2697 |
goog.net.BrowserChannel.Handler.prototype.channelClosed = function( |
| 2698 |
browserChannel, opt_pendingMaps, opt_undeliveredMaps) {}; |
| 2699 |
|
| 2700 |
|
| 2701 |
/** |
| 2702 |
* Gets any parameters that should be added at the time another connection is |
| 2703 |
* made to the server. |
| 2704 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2705 |
* @return {!Object} Extra parameter keys and values to add to the |
| 2706 |
* requests. |
| 2707 |
*/ |
| 2708 |
goog.net.BrowserChannel.Handler.prototype.getAdditionalParams = function( |
| 2709 |
browserChannel) { |
| 2710 |
return {}; |
| 2711 |
}; |
| 2712 |
|
| 2713 |
|
| 2714 |
/** |
| 2715 |
* Gets the URI of an image that can be used to test network connectivity. |
| 2716 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2717 |
* @return {goog.Uri?} A custom URI to load for the network test. |
| 2718 |
*/ |
| 2719 |
goog.net.BrowserChannel.Handler.prototype.getNetworkTestImageUri = function( |
| 2720 |
browserChannel) { |
| 2721 |
return null; |
| 2722 |
}; |
| 2723 |
|
| 2724 |
|
| 2725 |
/** |
| 2726 |
* Gets whether this channel is currently active. This is used to determine the |
| 2727 |
* length of time to wait before retrying. |
| 2728 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2729 |
* @return {boolean} Whether the channel is currently active. |
| 2730 |
*/ |
| 2731 |
goog.net.BrowserChannel.Handler.prototype.isActive = function(browserChannel) { |
| 2732 |
return true; |
| 2733 |
}; |
| 2734 |
|
| 2735 |
|
| 2736 |
/** |
| 2737 |
* Called by the channel if enumeration of the map throws an exception. |
| 2738 |
* @param {goog.net.BrowserChannel} browserChannel The browser channel. |
| 2739 |
* @param {Object} map The map that can't be enumerated. |
| 2740 |
*/ |
| 2741 |
goog.net.BrowserChannel.Handler.prototype.badMapError = function( |
| 2742 |
browserChannel, map) { |
| 2743 |
return; |
| 2744 |
}; |
| 2745 |
|
| 2746 |
|
| 2747 |
/** |
| 2748 |
* Allows the handler to override a host prefix provided by the server. Will |
| 2749 |
* be called whenever the channel has received such a prefix and is considering |
| 2750 |
* its use. |
| 2751 |
* @param {?string} serverHostPrefix The host prefix provided by the server. |
| 2752 |
* @return {?string} The host prefix the client should use. |
| 2753 |
*/ |
| 2754 |
goog.net.BrowserChannel.Handler.prototype.correctHostPrefix = function( |
| 2755 |
serverHostPrefix) { |
| 2756 |
return serverHostPrefix; |
| 2757 |
}; |