| 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 ChannelRequest class. The request |
| 17 |
* object encapsulates the logic for making a single request, either for the |
| 18 |
* forward channel, back channel, or test channel, to the server. It contains |
| 19 |
* the logic for the two types of transports we use: |
| 20 |
* XMLHTTP and Image request. It provides timeout detection. More transports |
| 21 |
* to be added in future, such as Fetch, WebSocket. |
| 22 |
* |
| 23 |
* @visibility {:internal} |
| 24 |
*/ |
| 25 |
|
| 26 |
|
| 27 |
goog.provide('goog.labs.net.webChannel.ChannelRequest'); |
| 28 |
|
| 29 |
goog.require('goog.Timer'); |
| 30 |
goog.require('goog.async.Throttle'); |
| 31 |
goog.require('goog.events.EventHandler'); |
| 32 |
goog.require('goog.labs.net.webChannel.Channel'); |
| 33 |
goog.require('goog.labs.net.webChannel.WebChannelDebug'); |
| 34 |
goog.require('goog.labs.net.webChannel.requestStats'); |
| 35 |
goog.require('goog.net.ErrorCode'); |
| 36 |
goog.require('goog.net.EventType'); |
| 37 |
goog.require('goog.net.XmlHttp'); |
| 38 |
goog.require('goog.object'); |
| 39 |
goog.require('goog.userAgent'); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* A new ChannelRequest is created for each request to the server. |
| 45 |
* |
| 46 |
* @param {goog.labs.net.webChannel.Channel} channel |
| 47 |
* The channel that owns this request. |
| 48 |
* @param {goog.labs.net.webChannel.WebChannelDebug} channelDebug A |
| 49 |
* WebChannelDebug to use for logging. |
| 50 |
* @param {string=} opt_sessionId The session id for the channel. |
| 51 |
* @param {string|number=} opt_requestId The request id for this request. |
| 52 |
* @param {number=} opt_retryId The retry id for this request. |
| 53 |
* @constructor |
| 54 |
* @struct |
| 55 |
* @final |
| 56 |
*/ |
| 57 |
goog.labs.net.webChannel.ChannelRequest = function( |
| 58 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) { |
| 59 |
/** |
| 60 |
* The channel object that owns the request. |
| 61 |
* @private {goog.labs.net.webChannel.Channel} |
| 62 |
*/ |
| 63 |
this.channel_ = channel; |
| 64 |
|
| 65 |
/** |
| 66 |
* The channel debug to use for logging |
| 67 |
* @private {goog.labs.net.webChannel.WebChannelDebug} |
| 68 |
*/ |
| 69 |
this.channelDebug_ = channelDebug; |
| 70 |
|
| 71 |
/** |
| 72 |
* The Session ID for the channel. |
| 73 |
* @private {string|undefined} |
| 74 |
*/ |
| 75 |
this.sid_ = opt_sessionId; |
| 76 |
|
| 77 |
/** |
| 78 |
* The RID (request ID) for the request. |
| 79 |
* @private {string|number|undefined} |
| 80 |
*/ |
| 81 |
this.rid_ = opt_requestId; |
| 82 |
|
| 83 |
/** |
| 84 |
* The attempt number of the current request. |
| 85 |
* @private {number} |
| 86 |
*/ |
| 87 |
this.retryId_ = opt_retryId || 1; |
| 88 |
|
| 89 |
/** |
| 90 |
* An object to keep track of the channel request event listeners. |
| 91 |
* @private {!goog.events.EventHandler< |
| 92 |
* !goog.labs.net.webChannel.ChannelRequest>} |
| 93 |
*/ |
| 94 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 95 |
|
| 96 |
/** |
| 97 |
* The timeout in ms before failing the request. |
| 98 |
* @private {number} |
| 99 |
*/ |
| 100 |
this.timeout_ = goog.labs.net.webChannel.ChannelRequest.TIMEOUT_MS_; |
| 101 |
|
| 102 |
/** |
| 103 |
* A timer for polling responseText in browsers that don't fire |
| 104 |
* onreadystatechange during incremental loading of responseText. |
| 105 |
* @private {goog.Timer} |
| 106 |
*/ |
| 107 |
this.pollingTimer_ = new goog.Timer(); |
| 108 |
|
| 109 |
this.pollingTimer_.setInterval( |
| 110 |
goog.labs.net.webChannel.ChannelRequest.POLLING_INTERVAL_MS_); |
| 111 |
|
| 112 |
/** |
| 113 |
* Extra HTTP headers to add to all the requests sent to the server. |
| 114 |
* @private {Object} |
| 115 |
*/ |
| 116 |
this.extraHeaders_ = null; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Whether the request was successful. This is only set to true after the |
| 121 |
* request successfully completes. |
| 122 |
* @private {boolean} |
| 123 |
*/ |
| 124 |
this.successful_ = false; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* The TimerID of the timer used to detect if the request has timed-out. |
| 129 |
* @type {?number} |
| 130 |
* @private |
| 131 |
*/ |
| 132 |
this.watchDogTimerId_ = null; |
| 133 |
|
| 134 |
/** |
| 135 |
* The time in the future when the request will timeout. |
| 136 |
* @private {?number} |
| 137 |
*/ |
| 138 |
this.watchDogTimeoutTime_ = null; |
| 139 |
|
| 140 |
/** |
| 141 |
* The time the request started. |
| 142 |
* @private {?number} |
| 143 |
*/ |
| 144 |
this.requestStartTime_ = null; |
| 145 |
|
| 146 |
/** |
| 147 |
* The type of request (XMLHTTP, IMG) |
| 148 |
* @private {?number} |
| 149 |
*/ |
| 150 |
this.type_ = null; |
| 151 |
|
| 152 |
/** |
| 153 |
* The base Uri for the request. The includes all the parameters except the |
| 154 |
* one that indicates the retry number. |
| 155 |
* @private {goog.Uri} |
| 156 |
*/ |
| 157 |
this.baseUri_ = null; |
| 158 |
|
| 159 |
/** |
| 160 |
* The request Uri that was actually used for the most recent request attempt. |
| 161 |
* @private {goog.Uri} |
| 162 |
*/ |
| 163 |
this.requestUri_ = null; |
| 164 |
|
| 165 |
/** |
| 166 |
* The post data, if the request is a post. |
| 167 |
* @private {?string} |
| 168 |
*/ |
| 169 |
this.postData_ = null; |
| 170 |
|
| 171 |
/** |
| 172 |
* An array of pending messages that we have either received a non-successful |
| 173 |
* response for, or no response at all, and which therefore may or may not |
| 174 |
* have been received by the server. |
| 175 |
* @private {!Array<goog.labs.net.webChannel.Wire.QueuedMap>} |
| 176 |
*/ |
| 177 |
this.pendingMessages_ = []; |
| 178 |
|
| 179 |
/** |
| 180 |
* The XhrLte request if the request is using XMLHTTP |
| 181 |
* @private {goog.net.XhrIo} |
| 182 |
*/ |
| 183 |
this.xmlHttp_ = null; |
| 184 |
|
| 185 |
/** |
| 186 |
* The position of where the next unprocessed chunk starts in the response |
| 187 |
* text. |
| 188 |
* @private {number} |
| 189 |
*/ |
| 190 |
this.xmlHttpChunkStart_ = 0; |
| 191 |
|
| 192 |
/** |
| 193 |
* The verb (Get or Post) for the request. |
| 194 |
* @private {?string} |
| 195 |
*/ |
| 196 |
this.verb_ = null; |
| 197 |
|
| 198 |
/** |
| 199 |
* The last error if the request failed. |
| 200 |
* @private {?goog.labs.net.webChannel.ChannelRequest.Error} |
| 201 |
*/ |
| 202 |
this.lastError_ = null; |
| 203 |
|
| 204 |
/** |
| 205 |
* The last status code received. |
| 206 |
* @private {number} |
| 207 |
*/ |
| 208 |
this.lastStatusCode_ = -1; |
| 209 |
|
| 210 |
/** |
| 211 |
* Whether the request has been cancelled due to a call to cancel. |
| 212 |
* @private {boolean} |
| 213 |
*/ |
| 214 |
this.cancelled_ = false; |
| 215 |
|
| 216 |
/** |
| 217 |
* A throttle time in ms for readystatechange events for the backchannel. |
| 218 |
* Useful for throttling when ready state is INTERACTIVE (partial data). |
| 219 |
* If set to zero no throttle is used. |
| 220 |
* |
| 221 |
* See WebChannelBase.prototype.readyStateChangeThrottleMs_ |
| 222 |
* |
| 223 |
* @private {number} |
| 224 |
*/ |
| 225 |
this.readyStateChangeThrottleMs_ = 0; |
| 226 |
|
| 227 |
/** |
| 228 |
* The throttle for readystatechange events for the current request, or null |
| 229 |
* if there is none. |
| 230 |
* @private {goog.async.Throttle} |
| 231 |
*/ |
| 232 |
this.readyStateChangeThrottle_ = null; |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Whether to the result is expected to be encoded for chunking and thus |
| 237 |
* requires decoding. |
| 238 |
* @private {boolean} |
| 239 |
*/ |
| 240 |
this.decodeChunks_ = false; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
goog.scope(function() { |
| 245 |
var Channel = goog.labs.net.webChannel.Channel; |
| 246 |
var ChannelRequest = goog.labs.net.webChannel.ChannelRequest; |
| 247 |
var requestStats = goog.labs.net.webChannel.requestStats; |
| 248 |
var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Default timeout in MS for a request. The server must return data within this |
| 253 |
* time limit for the request to not timeout. |
| 254 |
* @private {number} |
| 255 |
*/ |
| 256 |
ChannelRequest.TIMEOUT_MS_ = 45 * 1000; |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* How often to poll (in MS) for changes to responseText in browsers that don't |
| 261 |
* fire onreadystatechange during incremental loading of responseText. |
| 262 |
* @private {number} |
| 263 |
*/ |
| 264 |
ChannelRequest.POLLING_INTERVAL_MS_ = 250; |
| 265 |
|
| 266 |
|
| 267 |
/** |
| 268 |
* Enum for channel requests type |
| 269 |
* @enum {number} |
| 270 |
* @private |
| 271 |
*/ |
| 272 |
ChannelRequest.Type_ = { |
| 273 |
/** |
| 274 |
* XMLHTTP requests. |
| 275 |
*/ |
| 276 |
XML_HTTP: 1, |
| 277 |
|
| 278 |
/** |
| 279 |
* IMG requests. |
| 280 |
*/ |
| 281 |
CLOSE_REQUEST: 2 |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
/** |
| 286 |
* Enum type for identifying an error. |
| 287 |
* @enum {number} |
| 288 |
*/ |
| 289 |
ChannelRequest.Error = { |
| 290 |
/** |
| 291 |
* Errors due to a non-200 status code. |
| 292 |
*/ |
| 293 |
STATUS: 0, |
| 294 |
|
| 295 |
/** |
| 296 |
* Errors due to no data being returned. |
| 297 |
*/ |
| 298 |
NO_DATA: 1, |
| 299 |
|
| 300 |
/** |
| 301 |
* Errors due to a timeout. |
| 302 |
*/ |
| 303 |
TIMEOUT: 2, |
| 304 |
|
| 305 |
/** |
| 306 |
* Errors due to the server returning an unknown. |
| 307 |
*/ |
| 308 |
UNKNOWN_SESSION_ID: 3, |
| 309 |
|
| 310 |
/** |
| 311 |
* Errors due to bad data being received. |
| 312 |
*/ |
| 313 |
BAD_DATA: 4, |
| 314 |
|
| 315 |
/** |
| 316 |
* Errors due to the handler throwing an exception. |
| 317 |
*/ |
| 318 |
HANDLER_EXCEPTION: 5, |
| 319 |
|
| 320 |
/** |
| 321 |
* The browser declared itself offline during the request. |
| 322 |
*/ |
| 323 |
BROWSER_OFFLINE: 6 |
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Returns a useful error string for debugging based on the specified error |
| 329 |
* code. |
| 330 |
* @param {?ChannelRequest.Error} errorCode The error code. |
| 331 |
* @param {number} statusCode The HTTP status code. |
| 332 |
* @return {string} The error string for the given code combination. |
| 333 |
*/ |
| 334 |
ChannelRequest.errorStringFromCode = function(errorCode, statusCode) { |
| 335 |
switch (errorCode) { |
| 336 |
case ChannelRequest.Error.STATUS: |
| 337 |
return 'Non-200 return code (' + statusCode + ')'; |
| 338 |
case ChannelRequest.Error.NO_DATA: |
| 339 |
return 'XMLHTTP failure (no data)'; |
| 340 |
case ChannelRequest.Error.TIMEOUT: |
| 341 |
return 'HttpConnection timeout'; |
| 342 |
default: |
| 343 |
return 'Unknown error'; |
| 344 |
} |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* Sentinel value used to indicate an invalid chunk in a multi-chunk response. |
| 350 |
* @private {Object} |
| 351 |
*/ |
| 352 |
ChannelRequest.INVALID_CHUNK_ = {}; |
| 353 |
|
| 354 |
|
| 355 |
/** |
| 356 |
* Sentinel value used to indicate an incomplete chunk in a multi-chunk |
| 357 |
* response. |
| 358 |
* @private {Object} |
| 359 |
*/ |
| 360 |
ChannelRequest.INCOMPLETE_CHUNK_ = {}; |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* Returns whether XHR streaming is supported on this browser. |
| 365 |
* |
| 366 |
* @return {boolean} Whether XHR streaming is supported. |
| 367 |
* @see http://code.google.com/p/closure-library/issues/detail?id=346 |
| 368 |
*/ |
| 369 |
ChannelRequest.supportsXhrStreaming = function() { |
| 370 |
return !goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(10); |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
/** |
| 375 |
* Sets extra HTTP headers to add to all the requests sent to the server. |
| 376 |
* |
| 377 |
* @param {Object} extraHeaders The HTTP headers. |
| 378 |
*/ |
| 379 |
ChannelRequest.prototype.setExtraHeaders = function(extraHeaders) { |
| 380 |
this.extraHeaders_ = extraHeaders; |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Overrides the default HTTP method. |
| 386 |
* |
| 387 |
* @param {string} verb The HTTP method |
| 388 |
*/ |
| 389 |
ChannelRequest.prototype.setVerb = function(verb) { |
| 390 |
this.verb_ = verb; |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
/** |
| 395 |
* Sets the timeout for a request |
| 396 |
* |
| 397 |
* @param {number} timeout The timeout in MS for when we fail the request. |
| 398 |
*/ |
| 399 |
ChannelRequest.prototype.setTimeout = function(timeout) { |
| 400 |
this.timeout_ = timeout; |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Sets the throttle for handling onreadystatechange events for the request. |
| 406 |
* |
| 407 |
* @param {number} throttle The throttle in ms. A value of zero indicates |
| 408 |
* no throttle. |
| 409 |
*/ |
| 410 |
ChannelRequest.prototype.setReadyStateChangeThrottle = function(throttle) { |
| 411 |
this.readyStateChangeThrottleMs_ = throttle; |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
/** |
| 416 |
* Sets the pending messages that this request is handling. |
| 417 |
* |
| 418 |
* @param {!Array<goog.labs.net.webChannel.Wire.QueuedMap>} pendingMessages |
| 419 |
* The pending messages for this request. |
| 420 |
*/ |
| 421 |
ChannelRequest.prototype.setPendingMessages = function(pendingMessages) { |
| 422 |
this.pendingMessages_ = pendingMessages; |
| 423 |
}; |
| 424 |
|
| 425 |
|
| 426 |
/** |
| 427 |
* Gets the pending messages that this request is handling, in case of a retry. |
| 428 |
* |
| 429 |
* @return {!Array<goog.labs.net.webChannel.Wire.QueuedMap>} The pending |
| 430 |
* messages for this request. |
| 431 |
*/ |
| 432 |
ChannelRequest.prototype.getPendingMessages = function() { |
| 433 |
return this.pendingMessages_; |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
/** |
| 438 |
* Uses XMLHTTP to send an HTTP POST to the server. |
| 439 |
* |
| 440 |
* @param {goog.Uri} uri The uri of the request. |
| 441 |
* @param {?string} postData The data for the post body. |
| 442 |
* @param {boolean} decodeChunks Whether to the result is expected to be |
| 443 |
* encoded for chunking and thus requires decoding. |
| 444 |
*/ |
| 445 |
ChannelRequest.prototype.xmlHttpPost = function(uri, postData, decodeChunks) { |
| 446 |
this.type_ = ChannelRequest.Type_.XML_HTTP; |
| 447 |
this.baseUri_ = uri.clone().makeUnique(); |
| 448 |
this.postData_ = postData; |
| 449 |
this.decodeChunks_ = decodeChunks; |
| 450 |
this.sendXmlHttp_(null /* hostPrefix */); |
| 451 |
}; |
| 452 |
|
| 453 |
|
| 454 |
/** |
| 455 |
* Uses XMLHTTP to send an HTTP GET to the server. |
| 456 |
* |
| 457 |
* @param {goog.Uri} uri The uri of the request. |
| 458 |
* @param {boolean} decodeChunks Whether to the result is expected to be |
| 459 |
* encoded for chunking and thus requires decoding. |
| 460 |
* @param {?string} hostPrefix The host prefix, if we might be using a |
| 461 |
* secondary domain. Note that it should also be in the URL, adding this |
| 462 |
* won't cause it to be added to the URL. |
| 463 |
*/ |
| 464 |
ChannelRequest.prototype.xmlHttpGet = function(uri, decodeChunks, hostPrefix) { |
| 465 |
this.type_ = ChannelRequest.Type_.XML_HTTP; |
| 466 |
this.baseUri_ = uri.clone().makeUnique(); |
| 467 |
this.postData_ = null; |
| 468 |
this.decodeChunks_ = decodeChunks; |
| 469 |
|
| 470 |
this.sendXmlHttp_(hostPrefix); |
| 471 |
}; |
| 472 |
|
| 473 |
|
| 474 |
/** |
| 475 |
* Sends a request via XMLHTTP according to the current state of the request |
| 476 |
* object. |
| 477 |
* |
| 478 |
* @param {?string} hostPrefix The host prefix, if we might be using a secondary |
| 479 |
* domain. |
| 480 |
* @private |
| 481 |
*/ |
| 482 |
ChannelRequest.prototype.sendXmlHttp_ = function(hostPrefix) { |
| 483 |
this.requestStartTime_ = goog.now(); |
| 484 |
this.ensureWatchDogTimer_(); |
| 485 |
|
| 486 |
// clone the base URI to create the request URI. The request uri has the |
| 487 |
// attempt number as a parameter which helps in debugging. |
| 488 |
this.requestUri_ = this.baseUri_.clone(); |
| 489 |
this.requestUri_.setParameterValues('t', this.retryId_); |
| 490 |
|
| 491 |
// send the request either as a POST or GET |
| 492 |
this.xmlHttpChunkStart_ = 0; |
| 493 |
var useSecondaryDomains = this.channel_.shouldUseSecondaryDomains(); |
| 494 |
this.xmlHttp_ = |
| 495 |
this.channel_.createXhrIo(useSecondaryDomains ? hostPrefix : null); |
| 496 |
|
| 497 |
if (this.readyStateChangeThrottleMs_ > 0) { |
| 498 |
this.readyStateChangeThrottle_ = new goog.async.Throttle( |
| 499 |
goog.bind(this.xmlHttpHandler_, this, this.xmlHttp_), |
| 500 |
this.readyStateChangeThrottleMs_); |
| 501 |
} |
| 502 |
|
| 503 |
this.eventHandler_.listen( |
| 504 |
this.xmlHttp_, goog.net.EventType.READY_STATE_CHANGE, |
| 505 |
this.readyStateChangeHandler_); |
| 506 |
|
| 507 |
var headers = this.extraHeaders_ ? goog.object.clone(this.extraHeaders_) : {}; |
| 508 |
if (this.postData_) { |
| 509 |
if (!this.verb_) { |
| 510 |
this.verb_ = 'POST'; |
| 511 |
} |
| 512 |
headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 513 |
this.xmlHttp_.send(this.requestUri_, this.verb_, this.postData_, headers); |
| 514 |
} else { |
| 515 |
this.verb_ = 'GET'; |
| 516 |
this.xmlHttp_.send(this.requestUri_, this.verb_, null, headers); |
| 517 |
} |
| 518 |
requestStats.notifyServerReachabilityEvent( |
| 519 |
requestStats.ServerReachability.REQUEST_MADE); |
| 520 |
this.channelDebug_.xmlHttpChannelRequest( |
| 521 |
this.verb_, this.requestUri_, this.rid_, this.retryId_, this.postData_); |
| 522 |
}; |
| 523 |
|
| 524 |
|
| 525 |
/** |
| 526 |
* Handles a readystatechange event. |
| 527 |
* @param {goog.events.Event} evt The event. |
| 528 |
* @private |
| 529 |
*/ |
| 530 |
ChannelRequest.prototype.readyStateChangeHandler_ = function(evt) { |
| 531 |
var xhr = /** @type {goog.net.XhrIo} */ (evt.target); |
| 532 |
var throttle = this.readyStateChangeThrottle_; |
| 533 |
if (throttle && |
| 534 |
xhr.getReadyState() == goog.net.XmlHttp.ReadyState.INTERACTIVE) { |
| 535 |
// Only throttle in the partial data case. |
| 536 |
this.channelDebug_.debug('Throttling readystatechange.'); |
| 537 |
throttle.fire(); |
| 538 |
} else { |
| 539 |
// If we haven't throttled, just handle response directly. |
| 540 |
this.xmlHttpHandler_(xhr); |
| 541 |
} |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
/** |
| 546 |
* XmlHttp handler |
| 547 |
* @param {goog.net.XhrIo} xmlhttp The XhrIo object for the current request. |
| 548 |
* @private |
| 549 |
*/ |
| 550 |
ChannelRequest.prototype.xmlHttpHandler_ = function(xmlhttp) { |
| 551 |
requestStats.onStartExecution(); |
| 552 |
|
| 553 |
|
| 554 |
try { |
| 555 |
if (xmlhttp == this.xmlHttp_) { |
| 556 |
this.onXmlHttpReadyStateChanged_(); |
| 557 |
} else { |
| 558 |
this.channelDebug_.warning( |
| 559 |
'Called back with an ' + |
| 560 |
'unexpected xmlhttp'); |
| 561 |
} |
| 562 |
} catch (ex) { |
| 563 |
this.channelDebug_.debug('Failed call to OnXmlHttpReadyStateChanged_'); |
| 564 |
if (this.xmlHttp_ && this.xmlHttp_.getResponseText()) { |
| 565 |
var channelRequest = this; |
| 566 |
this.channelDebug_.dumpException(ex, function() { |
| 567 |
return 'ResponseText: ' + channelRequest.xmlHttp_.getResponseText(); |
| 568 |
}); |
| 569 |
} else { |
| 570 |
this.channelDebug_.dumpException(ex, 'No response text'); |
| 571 |
} |
| 572 |
} finally { |
| 573 |
requestStats.onEndExecution(); |
| 574 |
} |
| 575 |
}; |
| 576 |
|
| 577 |
|
| 578 |
/** |
| 579 |
* Called by the readystate handler for XMLHTTP requests. |
| 580 |
* |
| 581 |
* @private |
| 582 |
*/ |
| 583 |
ChannelRequest.prototype.onXmlHttpReadyStateChanged_ = function() { |
| 584 |
var readyState = this.xmlHttp_.getReadyState(); |
| 585 |
var errorCode = this.xmlHttp_.getLastErrorCode(); |
| 586 |
var statusCode = this.xmlHttp_.getStatus(); |
| 587 |
|
| 588 |
// we get partial results in browsers that support ready state interactive. |
| 589 |
// We also make sure that getResponseText is not null in interactive mode |
| 590 |
// before we continue. However, we don't do it in Opera because it only |
| 591 |
// fire readyState == INTERACTIVE once. We need the following code to poll |
| 592 |
if (readyState < goog.net.XmlHttp.ReadyState.INTERACTIVE || |
| 593 |
readyState == goog.net.XmlHttp.ReadyState.INTERACTIVE && |
| 594 |
!goog.userAgent.OPERA && !this.xmlHttp_.getResponseText()) { |
| 595 |
// not yet ready |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
// Dispatch any appropriate network events. |
| 600 |
if (!this.cancelled_ && readyState == goog.net.XmlHttp.ReadyState.COMPLETE && |
| 601 |
errorCode != goog.net.ErrorCode.ABORT) { |
| 602 |
// Pretty conservative, these are the only known scenarios which we'd |
| 603 |
// consider indicative of a truly non-functional network connection. |
| 604 |
if (errorCode == goog.net.ErrorCode.TIMEOUT || statusCode <= 0) { |
| 605 |
requestStats.notifyServerReachabilityEvent( |
| 606 |
requestStats.ServerReachability.REQUEST_FAILED); |
| 607 |
} else { |
| 608 |
requestStats.notifyServerReachabilityEvent( |
| 609 |
requestStats.ServerReachability.REQUEST_SUCCEEDED); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
// got some data so cancel the watchdog timer |
| 614 |
this.cancelWatchDogTimer_(); |
| 615 |
|
| 616 |
var status = this.xmlHttp_.getStatus(); |
| 617 |
this.lastStatusCode_ = status; |
| 618 |
var responseText = this.xmlHttp_.getResponseText(); |
| 619 |
if (!responseText) { |
| 620 |
var channelRequest = this; |
| 621 |
this.channelDebug_.debug(function() { |
| 622 |
return 'No response text for uri ' + channelRequest.requestUri_ + |
| 623 |
' status ' + status; |
| 624 |
}); |
| 625 |
} |
| 626 |
this.successful_ = (status == 200); |
| 627 |
|
| 628 |
this.channelDebug_.xmlHttpChannelResponseMetaData( |
| 629 |
/** @type {string} */ (this.verb_), this.requestUri_, this.rid_, |
| 630 |
this.retryId_, readyState, status); |
| 631 |
|
| 632 |
if (!this.successful_) { |
| 633 |
if (status == 400 && responseText.indexOf('Unknown SID') > 0) { |
| 634 |
// the server error string will include 'Unknown SID' which indicates the |
| 635 |
// server doesn't know about the session (maybe it got restarted, maybe |
| 636 |
// the user got moved to another server, etc.,). Handlers can special |
| 637 |
// case this error |
| 638 |
this.lastError_ = ChannelRequest.Error.UNKNOWN_SESSION_ID; |
| 639 |
requestStats.notifyStatEvent( |
| 640 |
requestStats.Stat.REQUEST_UNKNOWN_SESSION_ID); |
| 641 |
this.channelDebug_.warning('XMLHTTP Unknown SID (' + this.rid_ + ')'); |
| 642 |
} else { |
| 643 |
this.lastError_ = ChannelRequest.Error.STATUS; |
| 644 |
requestStats.notifyStatEvent(requestStats.Stat.REQUEST_BAD_STATUS); |
| 645 |
this.channelDebug_.warning( |
| 646 |
'XMLHTTP Bad status ' + status + ' (' + this.rid_ + ')'); |
| 647 |
} |
| 648 |
this.cleanup_(); |
| 649 |
this.dispatchFailure_(); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
if (this.decodeChunks_) { |
| 654 |
this.decodeNextChunks_(readyState, responseText); |
| 655 |
if (goog.userAgent.OPERA && this.successful_ && |
| 656 |
readyState == goog.net.XmlHttp.ReadyState.INTERACTIVE) { |
| 657 |
this.startPolling_(); |
| 658 |
} |
| 659 |
} else { |
| 660 |
this.channelDebug_.xmlHttpChannelResponseText( |
| 661 |
this.rid_, responseText, null); |
| 662 |
this.safeOnRequestData_(responseText); |
| 663 |
} |
| 664 |
|
| 665 |
if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) { |
| 666 |
this.cleanup_(); |
| 667 |
} |
| 668 |
|
| 669 |
if (!this.successful_) { |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
if (!this.cancelled_) { |
| 674 |
if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) { |
| 675 |
this.channel_.onRequestComplete(this); |
| 676 |
} else { |
| 677 |
// The default is false, the result from this callback shouldn't carry |
| 678 |
// over to the next callback, otherwise the request looks successful if |
| 679 |
// the watchdog timer gets called |
| 680 |
this.successful_ = false; |
| 681 |
this.ensureWatchDogTimer_(); |
| 682 |
} |
| 683 |
} |
| 684 |
}; |
| 685 |
|
| 686 |
|
| 687 |
/** |
| 688 |
* Decodes the next set of available chunks in the response. |
| 689 |
* @param {number} readyState The value of readyState. |
| 690 |
* @param {string} responseText The value of responseText. |
| 691 |
* @private |
| 692 |
*/ |
| 693 |
ChannelRequest.prototype.decodeNextChunks_ = function( |
| 694 |
readyState, responseText) { |
| 695 |
var decodeNextChunksSuccessful = true; |
| 696 |
while (!this.cancelled_ && this.xmlHttpChunkStart_ < responseText.length) { |
| 697 |
var chunkText = this.getNextChunk_(responseText); |
| 698 |
if (chunkText == ChannelRequest.INCOMPLETE_CHUNK_) { |
| 699 |
if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE) { |
| 700 |
// should have consumed entire response when the request is done |
| 701 |
this.lastError_ = ChannelRequest.Error.BAD_DATA; |
| 702 |
requestStats.notifyStatEvent(requestStats.Stat.REQUEST_INCOMPLETE_DATA); |
| 703 |
decodeNextChunksSuccessful = false; |
| 704 |
} |
| 705 |
this.channelDebug_.xmlHttpChannelResponseText( |
| 706 |
this.rid_, null, '[Incomplete Response]'); |
| 707 |
break; |
| 708 |
} else if (chunkText == ChannelRequest.INVALID_CHUNK_) { |
| 709 |
this.lastError_ = ChannelRequest.Error.BAD_DATA; |
| 710 |
requestStats.notifyStatEvent(requestStats.Stat.REQUEST_BAD_DATA); |
| 711 |
this.channelDebug_.xmlHttpChannelResponseText( |
| 712 |
this.rid_, responseText, '[Invalid Chunk]'); |
| 713 |
decodeNextChunksSuccessful = false; |
| 714 |
break; |
| 715 |
} else { |
| 716 |
this.channelDebug_.xmlHttpChannelResponseText( |
| 717 |
this.rid_, /** @type {string} */ (chunkText), null); |
| 718 |
this.safeOnRequestData_(/** @type {string} */ (chunkText)); |
| 719 |
} |
| 720 |
} |
| 721 |
if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE && |
| 722 |
responseText.length == 0) { |
| 723 |
// also an error if we didn't get any response |
| 724 |
this.lastError_ = ChannelRequest.Error.NO_DATA; |
| 725 |
requestStats.notifyStatEvent(requestStats.Stat.REQUEST_NO_DATA); |
| 726 |
decodeNextChunksSuccessful = false; |
| 727 |
} |
| 728 |
this.successful_ = this.successful_ && decodeNextChunksSuccessful; |
| 729 |
if (!decodeNextChunksSuccessful) { |
| 730 |
// malformed response - we make this trigger retry logic |
| 731 |
this.channelDebug_.xmlHttpChannelResponseText( |
| 732 |
this.rid_, responseText, '[Invalid Chunked Response]'); |
| 733 |
this.cleanup_(); |
| 734 |
this.dispatchFailure_(); |
| 735 |
} |
| 736 |
}; |
| 737 |
|
| 738 |
|
| 739 |
/** |
| 740 |
* Polls the response for new data. |
| 741 |
* @private |
| 742 |
*/ |
| 743 |
ChannelRequest.prototype.pollResponse_ = function() { |
| 744 |
var readyState = this.xmlHttp_.getReadyState(); |
| 745 |
var responseText = this.xmlHttp_.getResponseText(); |
| 746 |
if (this.xmlHttpChunkStart_ < responseText.length) { |
| 747 |
this.cancelWatchDogTimer_(); |
| 748 |
this.decodeNextChunks_(readyState, responseText); |
| 749 |
if (this.successful_ && |
| 750 |
readyState != goog.net.XmlHttp.ReadyState.COMPLETE) { |
| 751 |
this.ensureWatchDogTimer_(); |
| 752 |
} |
| 753 |
} |
| 754 |
}; |
| 755 |
|
| 756 |
|
| 757 |
/** |
| 758 |
* Starts a polling interval for changes to responseText of the |
| 759 |
* XMLHttpRequest, for browsers that don't fire onreadystatechange |
| 760 |
* as data comes in incrementally. This timer is disabled in |
| 761 |
* cleanup_(). |
| 762 |
* @private |
| 763 |
*/ |
| 764 |
ChannelRequest.prototype.startPolling_ = function() { |
| 765 |
this.eventHandler_.listen( |
| 766 |
this.pollingTimer_, goog.Timer.TICK, this.pollResponse_); |
| 767 |
this.pollingTimer_.start(); |
| 768 |
}; |
| 769 |
|
| 770 |
|
| 771 |
/** |
| 772 |
* Returns the next chunk of a chunk-encoded response. This is not standard |
| 773 |
* HTTP chunked encoding because browsers don't expose the chunk boundaries to |
| 774 |
* the application through XMLHTTP. So we have an additional chunk encoding at |
| 775 |
* the application level that lets us tell where the beginning and end of |
| 776 |
* individual responses are so that we can only try to eval a complete JS array. |
| 777 |
* |
| 778 |
* The encoding is the size of the chunk encoded as a decimal string followed |
| 779 |
* by a newline followed by the data. |
| 780 |
* |
| 781 |
* @param {string} responseText The response text from the XMLHTTP response. |
| 782 |
* @return {string|Object} The next chunk string or a sentinel object |
| 783 |
* indicating a special condition. |
| 784 |
* @private |
| 785 |
*/ |
| 786 |
ChannelRequest.prototype.getNextChunk_ = function(responseText) { |
| 787 |
var sizeStartIndex = this.xmlHttpChunkStart_; |
| 788 |
var sizeEndIndex = responseText.indexOf('\n', sizeStartIndex); |
| 789 |
if (sizeEndIndex == -1) { |
| 790 |
return ChannelRequest.INCOMPLETE_CHUNK_; |
| 791 |
} |
| 792 |
|
| 793 |
var sizeAsString = responseText.substring(sizeStartIndex, sizeEndIndex); |
| 794 |
var size = Number(sizeAsString); |
| 795 |
if (isNaN(size)) { |
| 796 |
return ChannelRequest.INVALID_CHUNK_; |
| 797 |
} |
| 798 |
|
| 799 |
var chunkStartIndex = sizeEndIndex + 1; |
| 800 |
if (chunkStartIndex + size > responseText.length) { |
| 801 |
return ChannelRequest.INCOMPLETE_CHUNK_; |
| 802 |
} |
| 803 |
|
| 804 |
var chunkText = responseText.substr(chunkStartIndex, size); |
| 805 |
this.xmlHttpChunkStart_ = chunkStartIndex + size; |
| 806 |
return chunkText; |
| 807 |
}; |
| 808 |
|
| 809 |
|
| 810 |
/** |
| 811 |
* Uses an IMG tag or navigator.sendBeacon to send an HTTP get to the server. |
| 812 |
* |
| 813 |
* This is only currently used to terminate the connection, as an IMG tag is |
| 814 |
* the most reliable way to send something to the server while the page |
| 815 |
* is getting torn down. |
| 816 |
* |
| 817 |
* Navigator.sendBeacon is available on Chrome and Firefox as a formal |
| 818 |
* solution to ensure delivery without blocking window close. See |
| 819 |
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon |
| 820 |
* |
| 821 |
* For Chrome Apps, sendBeacon is always necessary due to Content Security |
| 822 |
* Policy (CSP) violation of using an IMG tag. |
| 823 |
* |
| 824 |
* For react-native, we use xhr to send the actual close request, and assume |
| 825 |
* there is no page-close issue with react-native. |
| 826 |
* |
| 827 |
* @param {goog.Uri} uri The uri to send a request to. |
| 828 |
*/ |
| 829 |
ChannelRequest.prototype.sendCloseRequest = function(uri) { |
| 830 |
this.type_ = ChannelRequest.Type_.CLOSE_REQUEST; |
| 831 |
this.baseUri_ = uri.clone().makeUnique(); |
| 832 |
|
| 833 |
var requestSent = false; |
| 834 |
|
| 835 |
if (goog.global.navigator && goog.global.navigator.sendBeacon) { |
| 836 |
// empty string body to avoid 413 error on chrome < 41 |
| 837 |
requestSent = |
| 838 |
goog.global.navigator.sendBeacon(this.baseUri_.toString(), ''); |
| 839 |
} |
| 840 |
|
| 841 |
if (!requestSent && goog.global.Image) { |
| 842 |
var eltImg = new Image(); |
| 843 |
eltImg.src = this.baseUri_; |
| 844 |
requestSent = true; |
| 845 |
} |
| 846 |
|
| 847 |
if (!requestSent) { |
| 848 |
// no handler is set to match the sendBeacon/Image behavior |
| 849 |
this.xmlHttp_ = this.channel_.createXhrIo(null); |
| 850 |
this.xmlHttp_.send(this.baseUri_); |
| 851 |
} |
| 852 |
|
| 853 |
this.requestStartTime_ = goog.now(); |
| 854 |
this.ensureWatchDogTimer_(); |
| 855 |
}; |
| 856 |
|
| 857 |
|
| 858 |
/** |
| 859 |
* Cancels the request no matter what the underlying transport is. |
| 860 |
*/ |
| 861 |
ChannelRequest.prototype.cancel = function() { |
| 862 |
this.cancelled_ = true; |
| 863 |
this.cleanup_(); |
| 864 |
}; |
| 865 |
|
| 866 |
|
| 867 |
/** |
| 868 |
* Ensures that there is watchdog timeout which is used to ensure that |
| 869 |
* the connection completes in time. |
| 870 |
* |
| 871 |
* @private |
| 872 |
*/ |
| 873 |
ChannelRequest.prototype.ensureWatchDogTimer_ = function() { |
| 874 |
this.watchDogTimeoutTime_ = goog.now() + this.timeout_; |
| 875 |
this.startWatchDogTimer_(this.timeout_); |
| 876 |
}; |
| 877 |
|
| 878 |
|
| 879 |
/** |
| 880 |
* Starts the watchdog timer which is used to ensure that the connection |
| 881 |
* completes in time. |
| 882 |
* @param {number} time The number of milliseconds to wait. |
| 883 |
* @private |
| 884 |
*/ |
| 885 |
ChannelRequest.prototype.startWatchDogTimer_ = function(time) { |
| 886 |
if (this.watchDogTimerId_ != null) { |
| 887 |
// assertion |
| 888 |
throw new Error('WatchDog timer not null'); |
| 889 |
} |
| 890 |
this.watchDogTimerId_ = |
| 891 |
requestStats.setTimeout(goog.bind(this.onWatchDogTimeout_, this), time); |
| 892 |
}; |
| 893 |
|
| 894 |
|
| 895 |
/** |
| 896 |
* Cancels the watchdog timer if it has been started. |
| 897 |
* |
| 898 |
* @private |
| 899 |
*/ |
| 900 |
ChannelRequest.prototype.cancelWatchDogTimer_ = function() { |
| 901 |
if (this.watchDogTimerId_) { |
| 902 |
goog.global.clearTimeout(this.watchDogTimerId_); |
| 903 |
this.watchDogTimerId_ = null; |
| 904 |
} |
| 905 |
}; |
| 906 |
|
| 907 |
|
| 908 |
/** |
| 909 |
* Called when the watchdog timer is triggered. It also handles a case where it |
| 910 |
* is called too early which we suspect may be happening sometimes |
| 911 |
* (not sure why) |
| 912 |
* |
| 913 |
* @private |
| 914 |
*/ |
| 915 |
ChannelRequest.prototype.onWatchDogTimeout_ = function() { |
| 916 |
this.watchDogTimerId_ = null; |
| 917 |
var now = goog.now(); |
| 918 |
if (now - this.watchDogTimeoutTime_ >= 0) { |
| 919 |
this.handleTimeout_(); |
| 920 |
} else { |
| 921 |
// got called too early for some reason |
| 922 |
this.channelDebug_.warning('WatchDog timer called too early'); |
| 923 |
this.startWatchDogTimer_(this.watchDogTimeoutTime_ - now); |
| 924 |
} |
| 925 |
}; |
| 926 |
|
| 927 |
|
| 928 |
/** |
| 929 |
* Called when the request has actually timed out. Will cleanup and notify the |
| 930 |
* channel of the failure. |
| 931 |
* |
| 932 |
* @private |
| 933 |
*/ |
| 934 |
ChannelRequest.prototype.handleTimeout_ = function() { |
| 935 |
if (this.successful_) { |
| 936 |
// Should never happen. |
| 937 |
this.channelDebug_.severe( |
| 938 |
'Received watchdog timeout even though request loaded successfully'); |
| 939 |
} |
| 940 |
|
| 941 |
this.channelDebug_.timeoutResponse(this.requestUri_); |
| 942 |
|
| 943 |
// IMG or SendBeacon requests never notice if they were successful, |
| 944 |
// and always 'time out'. This fact says nothing about reachability. |
| 945 |
if (this.type_ != ChannelRequest.Type_.CLOSE_REQUEST) { |
| 946 |
requestStats.notifyServerReachabilityEvent( |
| 947 |
requestStats.ServerReachability.REQUEST_FAILED); |
| 948 |
requestStats.notifyStatEvent(requestStats.Stat.REQUEST_TIMEOUT); |
| 949 |
} |
| 950 |
|
| 951 |
this.cleanup_(); |
| 952 |
|
| 953 |
// Set error and dispatch failure. |
| 954 |
// This is called for CLOSE_REQUEST too to ensure channel_.onRequestComplete. |
| 955 |
this.lastError_ = ChannelRequest.Error.TIMEOUT; |
| 956 |
this.dispatchFailure_(); |
| 957 |
}; |
| 958 |
|
| 959 |
|
| 960 |
/** |
| 961 |
* Notifies the channel that this request failed. |
| 962 |
* @private |
| 963 |
*/ |
| 964 |
ChannelRequest.prototype.dispatchFailure_ = function() { |
| 965 |
if (this.channel_.isClosed() || this.cancelled_) { |
| 966 |
return; |
| 967 |
} |
| 968 |
|
| 969 |
this.channel_.onRequestComplete(this); |
| 970 |
}; |
| 971 |
|
| 972 |
|
| 973 |
/** |
| 974 |
* Cleans up the objects used to make the request. This function is |
| 975 |
* idempotent. |
| 976 |
* |
| 977 |
* @private |
| 978 |
*/ |
| 979 |
ChannelRequest.prototype.cleanup_ = function() { |
| 980 |
this.cancelWatchDogTimer_(); |
| 981 |
|
| 982 |
goog.dispose(this.readyStateChangeThrottle_); |
| 983 |
this.readyStateChangeThrottle_ = null; |
| 984 |
|
| 985 |
// Stop the polling timer, if necessary. |
| 986 |
this.pollingTimer_.stop(); |
| 987 |
|
| 988 |
// Unhook all event handlers. |
| 989 |
this.eventHandler_.removeAll(); |
| 990 |
|
| 991 |
if (this.xmlHttp_) { |
| 992 |
// clear out this.xmlHttp_ before aborting so we handle getting reentered |
| 993 |
// inside abort |
| 994 |
var xmlhttp = this.xmlHttp_; |
| 995 |
this.xmlHttp_ = null; |
| 996 |
xmlhttp.abort(); |
| 997 |
xmlhttp.dispose(); |
| 998 |
} |
| 999 |
}; |
| 1000 |
|
| 1001 |
|
| 1002 |
/** |
| 1003 |
* Indicates whether the request was successful. Only valid after the handler |
| 1004 |
* is called to indicate completion of the request. |
| 1005 |
* |
| 1006 |
* @return {boolean} True if the request succeeded. |
| 1007 |
*/ |
| 1008 |
ChannelRequest.prototype.getSuccess = function() { |
| 1009 |
return this.successful_; |
| 1010 |
}; |
| 1011 |
|
| 1012 |
|
| 1013 |
/** |
| 1014 |
* If the request was not successful, returns the reason. |
| 1015 |
* |
| 1016 |
* @return {?ChannelRequest.Error} The last error. |
| 1017 |
*/ |
| 1018 |
ChannelRequest.prototype.getLastError = function() { |
| 1019 |
return this.lastError_; |
| 1020 |
}; |
| 1021 |
|
| 1022 |
|
| 1023 |
/** |
| 1024 |
* Returns the status code of the last request. |
| 1025 |
* @return {number} The status code of the last request. |
| 1026 |
*/ |
| 1027 |
ChannelRequest.prototype.getLastStatusCode = function() { |
| 1028 |
return this.lastStatusCode_; |
| 1029 |
}; |
| 1030 |
|
| 1031 |
|
| 1032 |
/** |
| 1033 |
* Returns the session id for this channel. |
| 1034 |
* |
| 1035 |
* @return {string|undefined} The session ID. |
| 1036 |
*/ |
| 1037 |
ChannelRequest.prototype.getSessionId = function() { |
| 1038 |
return this.sid_; |
| 1039 |
}; |
| 1040 |
|
| 1041 |
|
| 1042 |
/** |
| 1043 |
* Returns the request id for this request. Each request has a unique request |
| 1044 |
* id and the request IDs are a sequential increasing count. |
| 1045 |
* |
| 1046 |
* @return {string|number|undefined} The request ID. |
| 1047 |
*/ |
| 1048 |
ChannelRequest.prototype.getRequestId = function() { |
| 1049 |
return this.rid_; |
| 1050 |
}; |
| 1051 |
|
| 1052 |
|
| 1053 |
/** |
| 1054 |
* Returns the data for a post, if this request is a post. |
| 1055 |
* |
| 1056 |
* @return {?string} The POST data provided by the request initiator. |
| 1057 |
*/ |
| 1058 |
ChannelRequest.prototype.getPostData = function() { |
| 1059 |
return this.postData_; |
| 1060 |
}; |
| 1061 |
|
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Returns the XhrIo request object. |
| 1065 |
* |
| 1066 |
* @return {?goog.net.XhrIo} Any XhrIo request created for this object. |
| 1067 |
*/ |
| 1068 |
ChannelRequest.prototype.getXhr = function() { |
| 1069 |
return this.xmlHttp_; |
| 1070 |
}; |
| 1071 |
|
| 1072 |
|
| 1073 |
/** |
| 1074 |
* Returns the time that the request started, if it has started. |
| 1075 |
* |
| 1076 |
* @return {?number} The time the request started, as returned by goog.now(). |
| 1077 |
*/ |
| 1078 |
ChannelRequest.prototype.getRequestStartTime = function() { |
| 1079 |
return this.requestStartTime_; |
| 1080 |
}; |
| 1081 |
|
| 1082 |
|
| 1083 |
/** |
| 1084 |
* Helper to call the callback's onRequestData, which catches any |
| 1085 |
* exception and cleans up the request. |
| 1086 |
* @param {string} data The request data. |
| 1087 |
* @private |
| 1088 |
*/ |
| 1089 |
ChannelRequest.prototype.safeOnRequestData_ = function(data) { |
| 1090 |
|
| 1091 |
try { |
| 1092 |
this.channel_.onRequestData(this, data); |
| 1093 |
var stats = requestStats.ServerReachability; |
| 1094 |
requestStats.notifyServerReachabilityEvent(stats.BACK_CHANNEL_ACTIVITY); |
| 1095 |
} catch (e) { |
| 1096 |
// Dump debug info, but keep going without closing the channel. |
| 1097 |
this.channelDebug_.dumpException(e, 'Error in httprequest callback'); |
| 1098 |
} |
| 1099 |
}; |
| 1100 |
|
| 1101 |
|
| 1102 |
/** |
| 1103 |
* Convenience factory method. |
| 1104 |
* |
| 1105 |
* @param {Channel} channel The channel object that owns this request. |
| 1106 |
* @param {WebChannelDebug} channelDebug A WebChannelDebug to use for logging. |
| 1107 |
* @param {string=} opt_sessionId The session id for the channel. |
| 1108 |
* @param {string|number=} opt_requestId The request id for this request. |
| 1109 |
* @param {number=} opt_retryId The retry id for this request. |
| 1110 |
* @return {!ChannelRequest} The created channel request. |
| 1111 |
*/ |
| 1112 |
ChannelRequest.createChannelRequest = function( |
| 1113 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) { |
| 1114 |
return new ChannelRequest( |
| 1115 |
channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId); |
| 1116 |
}; |
| 1117 |
}); // goog.scope |