| 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 Manages a pool of XhrIo's. This handles all the details of |
| 17 |
* dealing with the XhrPool and provides a simple interface for sending requests |
| 18 |
* and managing events. |
| 19 |
* |
| 20 |
* This class supports queueing & prioritization of requests (XhrIoPool |
| 21 |
* handles this) and retrying of requests. |
| 22 |
* |
| 23 |
* The events fired by the XhrManager are an aggregation of the events of |
| 24 |
* each of its XhrIo objects (with some filtering, i.e., ERROR only called |
| 25 |
* when there are no more retries left). For this reason, all send requests have |
| 26 |
* to have an id, so that the user of this object can know which event is for |
| 27 |
* which request. |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
goog.provide('goog.net.XhrManager'); |
| 32 |
goog.provide('goog.net.XhrManager.Event'); |
| 33 |
goog.provide('goog.net.XhrManager.Request'); |
| 34 |
|
| 35 |
goog.require('goog.events'); |
| 36 |
goog.require('goog.events.Event'); |
| 37 |
goog.require('goog.events.EventHandler'); |
| 38 |
goog.require('goog.events.EventTarget'); |
| 39 |
goog.require('goog.net.ErrorCode'); |
| 40 |
goog.require('goog.net.EventType'); |
| 41 |
goog.require('goog.net.XhrIo'); |
| 42 |
goog.require('goog.net.XhrIoPool'); |
| 43 |
goog.require('goog.structs.Map'); |
| 44 |
|
| 45 |
// TODO(user): Add some time in between retries. |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* A manager of an XhrIoPool. |
| 51 |
* @param {number=} opt_maxRetries Max. number of retries (Default: 1). |
| 52 |
* @param {goog.structs.Map=} opt_headers Map of default headers to add to every |
| 53 |
* request. |
| 54 |
* @param {number=} opt_minCount Min. number of objects (Default: 0). |
| 55 |
* @param {number=} opt_maxCount Max. number of objects (Default: 10). |
| 56 |
* @param {number=} opt_timeoutInterval Timeout (in ms) before aborting an |
| 57 |
* attempt (Default: 0ms). |
| 58 |
* @param {boolean=} opt_withCredentials Add credentials to every request |
| 59 |
* (Default: false). |
| 60 |
* @constructor |
| 61 |
* @extends {goog.events.EventTarget} |
| 62 |
*/ |
| 63 |
goog.net.XhrManager = function( |
| 64 |
opt_maxRetries, opt_headers, opt_minCount, opt_maxCount, |
| 65 |
opt_timeoutInterval, opt_withCredentials) { |
| 66 |
goog.net.XhrManager.base(this, 'constructor'); |
| 67 |
|
| 68 |
/** |
| 69 |
* Maximum number of retries for a given request |
| 70 |
* @type {number} |
| 71 |
* @private |
| 72 |
*/ |
| 73 |
this.maxRetries_ = goog.isDef(opt_maxRetries) ? opt_maxRetries : 1; |
| 74 |
|
| 75 |
/** |
| 76 |
* Timeout interval for an attempt of a given request. |
| 77 |
* @type {number} |
| 78 |
* @private |
| 79 |
*/ |
| 80 |
this.timeoutInterval_ = |
| 81 |
goog.isDef(opt_timeoutInterval) ? Math.max(0, opt_timeoutInterval) : 0; |
| 82 |
|
| 83 |
/** |
| 84 |
* Add credentials to every request. |
| 85 |
* @private {boolean} |
| 86 |
*/ |
| 87 |
this.withCredentials_ = !!opt_withCredentials; |
| 88 |
|
| 89 |
/** |
| 90 |
* The pool of XhrIo's to use. |
| 91 |
* @type {goog.net.XhrIoPool} |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
this.xhrPool_ = new goog.net.XhrIoPool( |
| 95 |
opt_headers, opt_minCount, opt_maxCount, opt_withCredentials); |
| 96 |
|
| 97 |
/** |
| 98 |
* Map of ID's to requests. |
| 99 |
* @type {goog.structs.Map<string, !goog.net.XhrManager.Request>} |
| 100 |
* @private |
| 101 |
*/ |
| 102 |
this.requests_ = new goog.structs.Map(); |
| 103 |
|
| 104 |
/** |
| 105 |
* The event handler. |
| 106 |
* @type {goog.events.EventHandler<!goog.net.XhrManager>} |
| 107 |
* @private |
| 108 |
*/ |
| 109 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 110 |
}; |
| 111 |
goog.inherits(goog.net.XhrManager, goog.events.EventTarget); |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Error to throw when a send is attempted with an ID that the manager already |
| 116 |
* has registered for another request. |
| 117 |
* @type {string} |
| 118 |
* @private |
| 119 |
*/ |
| 120 |
goog.net.XhrManager.ERROR_ID_IN_USE_ = '[goog.net.XhrManager] ID in use'; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* The goog.net.EventType's to listen/unlisten for on the XhrIo object. |
| 125 |
* @type {Array<goog.net.EventType>} |
| 126 |
* @private |
| 127 |
*/ |
| 128 |
goog.net.XhrManager.XHR_EVENT_TYPES_ = [ |
| 129 |
goog.net.EventType.READY, goog.net.EventType.COMPLETE, |
| 130 |
goog.net.EventType.SUCCESS, goog.net.EventType.ERROR, |
| 131 |
goog.net.EventType.ABORT, goog.net.EventType.TIMEOUT |
| 132 |
]; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Sets the number of milliseconds after which an incomplete request will be |
| 137 |
* aborted. Zero means no timeout is set. |
| 138 |
* @param {number} ms Timeout interval in milliseconds; 0 means none. |
| 139 |
*/ |
| 140 |
goog.net.XhrManager.prototype.setTimeoutInterval = function(ms) { |
| 141 |
this.timeoutInterval_ = Math.max(0, ms); |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the number of requests either in flight, or waiting to be sent. |
| 147 |
* The count will include the current request if used within a COMPLETE event |
| 148 |
* handler or callback. |
| 149 |
* @return {number} The number of requests in flight or pending send. |
| 150 |
*/ |
| 151 |
goog.net.XhrManager.prototype.getOutstandingCount = function() { |
| 152 |
return this.requests_.getCount(); |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Returns an array of request ids that are either in flight, or waiting to |
| 158 |
* be sent. The id of the current request will be included if used within a |
| 159 |
* COMPLETE event handler or callback. |
| 160 |
* @return {!Array<string>} Request ids in flight or pending send. |
| 161 |
*/ |
| 162 |
goog.net.XhrManager.prototype.getOutstandingRequestIds = function() { |
| 163 |
return this.requests_.getKeys(); |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Registers the given request to be sent. Throws an error if a request |
| 169 |
* already exists with the given ID. |
| 170 |
* NOTE: It is not sent immediately. It is buffered and will be sent when an |
| 171 |
* XhrIo object becomes available, taking into account the request's |
| 172 |
* priority. Note also that requests of equal priority are sent in an |
| 173 |
* implementation specific order - to get FIFO queue semantics use a |
| 174 |
* monotonically increasing priority for successive requests. |
| 175 |
* @param {string} id The id of the request. |
| 176 |
* @param {string} url Uri to make the request to. |
| 177 |
* @param {string=} opt_method Send method, default: GET. |
| 178 |
* @param {ArrayBuffer|ArrayBufferView|Blob|Document|FormData|string=} |
| 179 |
* opt_content Post data. |
| 180 |
* @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the |
| 181 |
* request. |
| 182 |
* @param {number=} opt_priority The priority of the request. A smaller value |
| 183 |
* means a higher priority. |
| 184 |
* @param {Function=} opt_callback Callback function for when request is |
| 185 |
* complete. The only param is the event object from the COMPLETE event. |
| 186 |
* @param {number=} opt_maxRetries The maximum number of times the request |
| 187 |
* should be retried. |
| 188 |
* @param {goog.net.XhrIo.ResponseType=} opt_responseType The response type of |
| 189 |
* this request; defaults to goog.net.XhrIo.ResponseType.DEFAULT. |
| 190 |
* @param {boolean=} opt_withCredentials Add credentials to this request, |
| 191 |
* default: false. |
| 192 |
* @return {!goog.net.XhrManager.Request} The queued request object. |
| 193 |
*/ |
| 194 |
goog.net.XhrManager.prototype.send = function( |
| 195 |
id, url, opt_method, opt_content, opt_headers, opt_priority, opt_callback, |
| 196 |
opt_maxRetries, opt_responseType, opt_withCredentials) { |
| 197 |
var requests = this.requests_; |
| 198 |
// Check if there is already a request with the given id. |
| 199 |
if (requests.get(id)) { |
| 200 |
throw new Error(goog.net.XhrManager.ERROR_ID_IN_USE_); |
| 201 |
} |
| 202 |
|
| 203 |
// Make the Request object. |
| 204 |
var request = new goog.net.XhrManager.Request( |
| 205 |
url, goog.bind(this.handleEvent_, this, id), opt_method, opt_content, |
| 206 |
opt_headers, opt_callback, |
| 207 |
goog.isDef(opt_maxRetries) ? opt_maxRetries : this.maxRetries_, |
| 208 |
opt_responseType, |
| 209 |
goog.isDef(opt_withCredentials) ? opt_withCredentials : |
| 210 |
this.withCredentials_); |
| 211 |
this.requests_.set(id, request); |
| 212 |
|
| 213 |
// Setup the callback for the pool. |
| 214 |
var callback = goog.bind(this.handleAvailableXhr_, this, id); |
| 215 |
this.xhrPool_.getObject(callback, opt_priority); |
| 216 |
|
| 217 |
return request; |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Aborts the request associated with id. |
| 223 |
* @param {string} id The id of the request to abort. |
| 224 |
* @param {boolean=} opt_force If true, remove the id now so it can be reused. |
| 225 |
* No events are fired and the callback is not called when forced. |
| 226 |
*/ |
| 227 |
goog.net.XhrManager.prototype.abort = function(id, opt_force) { |
| 228 |
var request = this.requests_.get(id); |
| 229 |
if (request) { |
| 230 |
var xhrIo = request.xhrIo; |
| 231 |
request.setAborted(true); |
| 232 |
if (opt_force) { |
| 233 |
if (xhrIo) { |
| 234 |
// We remove listeners to make sure nothing gets called if a new request |
| 235 |
// with the same id is made. |
| 236 |
this.removeXhrListener_(xhrIo, request.getXhrEventCallback()); |
| 237 |
goog.events.listenOnce(xhrIo, goog.net.EventType.READY, function() { |
| 238 |
this.xhrPool_.releaseObject(xhrIo); |
| 239 |
}, false, this); |
| 240 |
} |
| 241 |
this.requests_.remove(id); |
| 242 |
} |
| 243 |
if (xhrIo) { |
| 244 |
xhrIo.abort(); |
| 245 |
} |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Handles when an XhrIo object becomes available. Sets up the events, fires |
| 252 |
* the READY event, and starts the process to send the request. |
| 253 |
* @param {string} id The id of the request the XhrIo is for. |
| 254 |
* @param {goog.net.XhrIo} xhrIo The available XhrIo object. |
| 255 |
* @private |
| 256 |
*/ |
| 257 |
goog.net.XhrManager.prototype.handleAvailableXhr_ = function(id, xhrIo) { |
| 258 |
var request = this.requests_.get(id); |
| 259 |
// Make sure the request doesn't already have an XhrIo attached. This can |
| 260 |
// happen if a forced abort occurs before an XhrIo is available, and a new |
| 261 |
// request with the same id is made. |
| 262 |
if (request && !request.xhrIo) { |
| 263 |
this.addXhrListener_(xhrIo, request.getXhrEventCallback()); |
| 264 |
|
| 265 |
// Set properties for the XhrIo. |
| 266 |
xhrIo.setTimeoutInterval(this.timeoutInterval_); |
| 267 |
xhrIo.setResponseType(request.getResponseType()); |
| 268 |
xhrIo.setWithCredentials(request.getWithCredentials()); |
| 269 |
|
| 270 |
// Add a reference to the XhrIo object to the request. |
| 271 |
request.xhrIo = xhrIo; |
| 272 |
|
| 273 |
// Notify the listeners. |
| 274 |
this.dispatchEvent( |
| 275 |
new goog.net.XhrManager.Event( |
| 276 |
goog.net.EventType.READY, this, id, xhrIo)); |
| 277 |
|
| 278 |
// Send the request. |
| 279 |
this.retry_(id, xhrIo); |
| 280 |
|
| 281 |
// If the request was aborted before it got an XhrIo object, abort it now. |
| 282 |
if (request.getAborted()) { |
| 283 |
xhrIo.abort(); |
| 284 |
} |
| 285 |
} else { |
| 286 |
// If the request has an XhrIo object already, or no request exists, just |
| 287 |
// return the XhrIo back to the pool. |
| 288 |
this.xhrPool_.releaseObject(xhrIo); |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Handles all events fired by the XhrIo object for a given request. |
| 295 |
* @param {string} id The id of the request. |
| 296 |
* @param {goog.events.Event} e The event. |
| 297 |
* @return {Object} The return value from the handler, if any. |
| 298 |
* @private |
| 299 |
*/ |
| 300 |
goog.net.XhrManager.prototype.handleEvent_ = function(id, e) { |
| 301 |
var xhrIo = /** @type {goog.net.XhrIo} */ (e.target); |
| 302 |
switch (e.type) { |
| 303 |
case goog.net.EventType.READY: |
| 304 |
this.retry_(id, xhrIo); |
| 305 |
break; |
| 306 |
|
| 307 |
case goog.net.EventType.COMPLETE: |
| 308 |
return this.handleComplete_(id, xhrIo, e); |
| 309 |
|
| 310 |
case goog.net.EventType.SUCCESS: |
| 311 |
this.handleSuccess_(id, xhrIo); |
| 312 |
break; |
| 313 |
|
| 314 |
// A timeout is handled like an error. |
| 315 |
case goog.net.EventType.TIMEOUT: |
| 316 |
case goog.net.EventType.ERROR: |
| 317 |
this.handleError_(id, xhrIo); |
| 318 |
break; |
| 319 |
|
| 320 |
case goog.net.EventType.ABORT: |
| 321 |
this.handleAbort_(id, xhrIo); |
| 322 |
break; |
| 323 |
} |
| 324 |
return null; |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
/** |
| 329 |
* Attempts to retry the given request. If the request has already attempted |
| 330 |
* the maximum number of retries, then it removes the request and releases |
| 331 |
* the XhrIo object back into the pool. |
| 332 |
* @param {string} id The id of the request. |
| 333 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object. |
| 334 |
* @private |
| 335 |
*/ |
| 336 |
goog.net.XhrManager.prototype.retry_ = function(id, xhrIo) { |
| 337 |
var request = this.requests_.get(id); |
| 338 |
|
| 339 |
// If the request has not completed and it is below its max. retries. |
| 340 |
if (request && !request.getCompleted() && !request.hasReachedMaxRetries()) { |
| 341 |
request.increaseAttemptCount(); |
| 342 |
xhrIo.send( |
| 343 |
request.getUrl(), request.getMethod(), request.getContent(), |
| 344 |
request.getHeaders()); |
| 345 |
} else { |
| 346 |
if (request) { |
| 347 |
// Remove the events on the XhrIo objects. |
| 348 |
this.removeXhrListener_(xhrIo, request.getXhrEventCallback()); |
| 349 |
|
| 350 |
// Remove the request. |
| 351 |
this.requests_.remove(id); |
| 352 |
} |
| 353 |
// Release the XhrIo object back into the pool. |
| 354 |
this.xhrPool_.releaseObject(xhrIo); |
| 355 |
} |
| 356 |
}; |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* Handles the complete of a request. Dispatches the COMPLETE event and sets the |
| 361 |
* the request as completed if the request has succeeded, or is done retrying. |
| 362 |
* @param {string} id The id of the request. |
| 363 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object. |
| 364 |
* @param {goog.events.Event} e The original event. |
| 365 |
* @return {Object} The return value from the callback, if any. |
| 366 |
* @private |
| 367 |
*/ |
| 368 |
goog.net.XhrManager.prototype.handleComplete_ = function(id, xhrIo, e) { |
| 369 |
// Only if the request is done processing should a COMPLETE event be fired. |
| 370 |
var request = this.requests_.get(id); |
| 371 |
if (xhrIo.getLastErrorCode() == goog.net.ErrorCode.ABORT || |
| 372 |
xhrIo.isSuccess() || request.hasReachedMaxRetries()) { |
| 373 |
this.dispatchEvent( |
| 374 |
new goog.net.XhrManager.Event( |
| 375 |
goog.net.EventType.COMPLETE, this, id, xhrIo)); |
| 376 |
|
| 377 |
// If the request exists, we mark it as completed and call the callback |
| 378 |
if (request) { |
| 379 |
request.setCompleted(true); |
| 380 |
// Call the complete callback as if it was set as a COMPLETE event on the |
| 381 |
// XhrIo directly. |
| 382 |
if (request.getCompleteCallback()) { |
| 383 |
return request.getCompleteCallback().call(xhrIo, e); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
return null; |
| 388 |
}; |
| 389 |
|
| 390 |
|
| 391 |
/** |
| 392 |
* Handles the abort of an underlying XhrIo object. |
| 393 |
* @param {string} id The id of the request. |
| 394 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object. |
| 395 |
* @private |
| 396 |
*/ |
| 397 |
goog.net.XhrManager.prototype.handleAbort_ = function(id, xhrIo) { |
| 398 |
// Fire event. |
| 399 |
// NOTE: The complete event should always be fired before the abort event, so |
| 400 |
// the bulk of the work is done in handleComplete. |
| 401 |
this.dispatchEvent( |
| 402 |
new goog.net.XhrManager.Event(goog.net.EventType.ABORT, this, id, xhrIo)); |
| 403 |
}; |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Handles the success of a request. Dispatches the SUCCESS event and sets the |
| 408 |
* the request as completed. |
| 409 |
* @param {string} id The id of the request. |
| 410 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object. |
| 411 |
* @private |
| 412 |
*/ |
| 413 |
goog.net.XhrManager.prototype.handleSuccess_ = function(id, xhrIo) { |
| 414 |
// Fire event. |
| 415 |
// NOTE: We don't release the XhrIo object from the pool here. |
| 416 |
// It is released in the retry method, when we know it is back in the |
| 417 |
// ready state. |
| 418 |
this.dispatchEvent( |
| 419 |
new goog.net.XhrManager.Event( |
| 420 |
goog.net.EventType.SUCCESS, this, id, xhrIo)); |
| 421 |
}; |
| 422 |
|
| 423 |
|
| 424 |
/** |
| 425 |
* Handles the error of a request. If the request has not reach its maximum |
| 426 |
* number of retries, then it lets the request retry naturally (will let the |
| 427 |
* request hit the READY state). Else, it dispatches the ERROR event. |
| 428 |
* @param {string} id The id of the request. |
| 429 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object. |
| 430 |
* @private |
| 431 |
*/ |
| 432 |
goog.net.XhrManager.prototype.handleError_ = function(id, xhrIo) { |
| 433 |
var request = this.requests_.get(id); |
| 434 |
|
| 435 |
// If the maximum number of retries has been reached. |
| 436 |
if (request.hasReachedMaxRetries()) { |
| 437 |
// Fire event. |
| 438 |
// NOTE: We don't release the XhrIo object from the pool here. |
| 439 |
// It is released in the retry method, when we know it is back in the |
| 440 |
// ready state. |
| 441 |
this.dispatchEvent( |
| 442 |
new goog.net.XhrManager.Event( |
| 443 |
goog.net.EventType.ERROR, this, id, xhrIo)); |
| 444 |
} |
| 445 |
}; |
| 446 |
|
| 447 |
|
| 448 |
/** |
| 449 |
* Remove listeners for XHR events on an XhrIo object. |
| 450 |
* @param {goog.net.XhrIo} xhrIo The object to stop listenening to events on. |
| 451 |
* @param {Function} func The callback to remove from event handling. |
| 452 |
* @param {string|Array<string>=} opt_types Event types to remove listeners |
| 453 |
* for. Defaults to XHR_EVENT_TYPES_. |
| 454 |
* @private |
| 455 |
*/ |
| 456 |
goog.net.XhrManager.prototype.removeXhrListener_ = function( |
| 457 |
xhrIo, func, opt_types) { |
| 458 |
var types = opt_types || goog.net.XhrManager.XHR_EVENT_TYPES_; |
| 459 |
this.eventHandler_.unlisten(xhrIo, types, func); |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
/** |
| 464 |
* Adds a listener for XHR events on an XhrIo object. |
| 465 |
* @param {goog.net.XhrIo} xhrIo The object listen to events on. |
| 466 |
* @param {Function} func The callback when the event occurs. |
| 467 |
* @param {string|Array<string>=} opt_types Event types to attach listeners to. |
| 468 |
* Defaults to XHR_EVENT_TYPES_. |
| 469 |
* @private |
| 470 |
*/ |
| 471 |
goog.net.XhrManager.prototype.addXhrListener_ = function( |
| 472 |
xhrIo, func, opt_types) { |
| 473 |
var types = opt_types || goog.net.XhrManager.XHR_EVENT_TYPES_; |
| 474 |
this.eventHandler_.listen(xhrIo, types, func); |
| 475 |
}; |
| 476 |
|
| 477 |
|
| 478 |
/** @override */ |
| 479 |
goog.net.XhrManager.prototype.disposeInternal = function() { |
| 480 |
goog.net.XhrManager.superClass_.disposeInternal.call(this); |
| 481 |
|
| 482 |
this.xhrPool_.dispose(); |
| 483 |
this.xhrPool_ = null; |
| 484 |
|
| 485 |
this.eventHandler_.dispose(); |
| 486 |
this.eventHandler_ = null; |
| 487 |
|
| 488 |
this.requests_.clear(); |
| 489 |
this.requests_ = null; |
| 490 |
}; |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
/** |
| 495 |
* An event dispatched by XhrManager. |
| 496 |
* |
| 497 |
* @param {goog.net.EventType} type Event Type. |
| 498 |
* @param {goog.net.XhrManager} target Reference to the object that is the |
| 499 |
* target of this event. |
| 500 |
* @param {string} id The id of the request this event is for. |
| 501 |
* @param {goog.net.XhrIo} xhrIo The XhrIo object of the request. |
| 502 |
* @constructor |
| 503 |
* @extends {goog.events.Event} |
| 504 |
* @final |
| 505 |
*/ |
| 506 |
goog.net.XhrManager.Event = function(type, target, id, xhrIo) { |
| 507 |
goog.events.Event.call(this, type, target); |
| 508 |
|
| 509 |
/** |
| 510 |
* The id of the request this event is for. |
| 511 |
* @type {string} |
| 512 |
*/ |
| 513 |
this.id = id; |
| 514 |
|
| 515 |
/** |
| 516 |
* The XhrIo object of the request. |
| 517 |
* @type {goog.net.XhrIo} |
| 518 |
*/ |
| 519 |
this.xhrIo = xhrIo; |
| 520 |
}; |
| 521 |
goog.inherits(goog.net.XhrManager.Event, goog.events.Event); |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
/** |
| 526 |
* An encapsulation of everything needed to make a Xhr request. |
| 527 |
* NOTE: This is used internal to the XhrManager. |
| 528 |
* |
| 529 |
* @param {string} url Uri to make the request too. |
| 530 |
* @param {Function} xhrEventCallback Callback attached to the events of the |
| 531 |
* XhrIo object of the request. |
| 532 |
* @param {string=} opt_method Send method, default: GET. |
| 533 |
* @param {ArrayBuffer|ArrayBufferView|Blob|Document|FormData|string=} |
| 534 |
* opt_content Post data. |
| 535 |
* @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the |
| 536 |
* request. |
| 537 |
* @param {Function=} opt_callback Callback function for when request is |
| 538 |
* complete. NOTE: Only 1 callback supported across all events. |
| 539 |
* @param {number=} opt_maxRetries The maximum number of times the request |
| 540 |
* should be retried (Default: 1). |
| 541 |
* @param {goog.net.XhrIo.ResponseType=} opt_responseType The response type of |
| 542 |
* this request; defaults to goog.net.XhrIo.ResponseType.DEFAULT. |
| 543 |
* @param {boolean=} opt_withCredentials Add credentials to this request, |
| 544 |
* default: false. |
| 545 |
* |
| 546 |
* @constructor |
| 547 |
* @final |
| 548 |
*/ |
| 549 |
goog.net.XhrManager.Request = function( |
| 550 |
url, xhrEventCallback, opt_method, opt_content, opt_headers, opt_callback, |
| 551 |
opt_maxRetries, opt_responseType, opt_withCredentials) { |
| 552 |
/** |
| 553 |
* Uri to make the request too. |
| 554 |
* @type {string} |
| 555 |
* @private |
| 556 |
*/ |
| 557 |
this.url_ = url; |
| 558 |
|
| 559 |
/** |
| 560 |
* Send method. |
| 561 |
* @type {string} |
| 562 |
* @private |
| 563 |
*/ |
| 564 |
this.method_ = opt_method || 'GET'; |
| 565 |
|
| 566 |
/** |
| 567 |
* Post data. |
| 568 |
* @type {ArrayBuffer|ArrayBufferView|Blob|Document|FormData|string|undefined} |
| 569 |
* @private |
| 570 |
*/ |
| 571 |
this.content_ = opt_content; |
| 572 |
|
| 573 |
/** |
| 574 |
* Map of headers |
| 575 |
* @type {Object|goog.structs.Map|null} |
| 576 |
* @private |
| 577 |
*/ |
| 578 |
this.headers_ = opt_headers || null; |
| 579 |
|
| 580 |
/** |
| 581 |
* The maximum number of times the request should be retried. |
| 582 |
* @type {number} |
| 583 |
* @private |
| 584 |
*/ |
| 585 |
this.maxRetries_ = goog.isDef(opt_maxRetries) ? opt_maxRetries : 1; |
| 586 |
|
| 587 |
/** |
| 588 |
* The number of attempts so far. |
| 589 |
* @type {number} |
| 590 |
* @private |
| 591 |
*/ |
| 592 |
this.attemptCount_ = 0; |
| 593 |
|
| 594 |
/** |
| 595 |
* Whether the request has been completed. |
| 596 |
* @type {boolean} |
| 597 |
* @private |
| 598 |
*/ |
| 599 |
this.completed_ = false; |
| 600 |
|
| 601 |
/** |
| 602 |
* Whether the request has been aborted. |
| 603 |
* @type {boolean} |
| 604 |
* @private |
| 605 |
*/ |
| 606 |
this.aborted_ = false; |
| 607 |
|
| 608 |
/** |
| 609 |
* Callback attached to the events of the XhrIo object. |
| 610 |
* @type {Function} |
| 611 |
* @private |
| 612 |
*/ |
| 613 |
this.xhrEventCallback_ = xhrEventCallback; |
| 614 |
|
| 615 |
/** |
| 616 |
* Callback function called when request is complete. |
| 617 |
* @type {Function|undefined} |
| 618 |
* @private |
| 619 |
*/ |
| 620 |
this.completeCallback_ = opt_callback; |
| 621 |
|
| 622 |
/** |
| 623 |
* A response type to set on this.xhrIo when it's populated. |
| 624 |
* @type {!goog.net.XhrIo.ResponseType} |
| 625 |
* @private |
| 626 |
*/ |
| 627 |
this.responseType_ = opt_responseType || goog.net.XhrIo.ResponseType.DEFAULT; |
| 628 |
|
| 629 |
/** |
| 630 |
* Send credentials with this request, or not. |
| 631 |
* @private {boolean} |
| 632 |
*/ |
| 633 |
this.withCredentials_ = !!opt_withCredentials; |
| 634 |
|
| 635 |
/** |
| 636 |
* The XhrIo instance handling this request. Set in handleAvailableXhr. |
| 637 |
* @type {goog.net.XhrIo} |
| 638 |
*/ |
| 639 |
this.xhrIo = null; |
| 640 |
|
| 641 |
}; |
| 642 |
|
| 643 |
|
| 644 |
/** |
| 645 |
* Gets the uri. |
| 646 |
* @return {string} The uri to make the request to. |
| 647 |
*/ |
| 648 |
goog.net.XhrManager.Request.prototype.getUrl = function() { |
| 649 |
return this.url_; |
| 650 |
}; |
| 651 |
|
| 652 |
|
| 653 |
/** |
| 654 |
* Gets the send method. |
| 655 |
* @return {string} The send method. |
| 656 |
*/ |
| 657 |
goog.net.XhrManager.Request.prototype.getMethod = function() { |
| 658 |
return this.method_; |
| 659 |
}; |
| 660 |
|
| 661 |
|
| 662 |
/** |
| 663 |
* Gets the post data. |
| 664 |
* @return {ArrayBuffer|ArrayBufferView|Blob|Document|FormData|string|undefined} |
| 665 |
* The post data. |
| 666 |
*/ |
| 667 |
goog.net.XhrManager.Request.prototype.getContent = function() { |
| 668 |
return this.content_; |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* Gets the map of headers. |
| 674 |
* @return {Object|goog.structs.Map} The map of headers. |
| 675 |
*/ |
| 676 |
goog.net.XhrManager.Request.prototype.getHeaders = function() { |
| 677 |
return this.headers_; |
| 678 |
}; |
| 679 |
|
| 680 |
|
| 681 |
/** |
| 682 |
* Gets the withCredentials flag. |
| 683 |
* @return {boolean} Add credentials, or not. |
| 684 |
*/ |
| 685 |
goog.net.XhrManager.Request.prototype.getWithCredentials = function() { |
| 686 |
return this.withCredentials_; |
| 687 |
}; |
| 688 |
|
| 689 |
|
| 690 |
/** |
| 691 |
* Gets the maximum number of times the request should be retried. |
| 692 |
* @return {number} The maximum number of times the request should be retried. |
| 693 |
*/ |
| 694 |
goog.net.XhrManager.Request.prototype.getMaxRetries = function() { |
| 695 |
return this.maxRetries_; |
| 696 |
}; |
| 697 |
|
| 698 |
|
| 699 |
/** |
| 700 |
* Gets the number of attempts so far. |
| 701 |
* @return {number} The number of attempts so far. |
| 702 |
*/ |
| 703 |
goog.net.XhrManager.Request.prototype.getAttemptCount = function() { |
| 704 |
return this.attemptCount_; |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
/** |
| 709 |
* Increases the number of attempts so far. |
| 710 |
*/ |
| 711 |
goog.net.XhrManager.Request.prototype.increaseAttemptCount = function() { |
| 712 |
this.attemptCount_++; |
| 713 |
}; |
| 714 |
|
| 715 |
|
| 716 |
/** |
| 717 |
* Returns whether the request has reached the maximum number of retries. |
| 718 |
* @return {boolean} Whether the request has reached the maximum number of |
| 719 |
* retries. |
| 720 |
*/ |
| 721 |
goog.net.XhrManager.Request.prototype.hasReachedMaxRetries = function() { |
| 722 |
return this.attemptCount_ > this.maxRetries_; |
| 723 |
}; |
| 724 |
|
| 725 |
|
| 726 |
/** |
| 727 |
* Sets the completed status. |
| 728 |
* @param {boolean} complete The completed status. |
| 729 |
*/ |
| 730 |
goog.net.XhrManager.Request.prototype.setCompleted = function(complete) { |
| 731 |
this.completed_ = complete; |
| 732 |
}; |
| 733 |
|
| 734 |
|
| 735 |
/** |
| 736 |
* Gets the completed status. |
| 737 |
* @return {boolean} The completed status. |
| 738 |
*/ |
| 739 |
goog.net.XhrManager.Request.prototype.getCompleted = function() { |
| 740 |
return this.completed_; |
| 741 |
}; |
| 742 |
|
| 743 |
|
| 744 |
/** |
| 745 |
* Sets the aborted status. |
| 746 |
* @param {boolean} aborted True if the request was aborted, otherwise False. |
| 747 |
*/ |
| 748 |
goog.net.XhrManager.Request.prototype.setAborted = function(aborted) { |
| 749 |
this.aborted_ = aborted; |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
/** |
| 754 |
* Gets the aborted status. |
| 755 |
* @return {boolean} True if request was aborted, otherwise False. |
| 756 |
*/ |
| 757 |
goog.net.XhrManager.Request.prototype.getAborted = function() { |
| 758 |
return this.aborted_; |
| 759 |
}; |
| 760 |
|
| 761 |
|
| 762 |
/** |
| 763 |
* Gets the callback attached to the events of the XhrIo object. |
| 764 |
* @return {Function} The callback attached to the events of the |
| 765 |
* XhrIo object. |
| 766 |
*/ |
| 767 |
goog.net.XhrManager.Request.prototype.getXhrEventCallback = function() { |
| 768 |
return this.xhrEventCallback_; |
| 769 |
}; |
| 770 |
|
| 771 |
|
| 772 |
/** |
| 773 |
* Gets the callback for when the request is complete. |
| 774 |
* @return {Function|undefined} The callback for when the request is complete. |
| 775 |
*/ |
| 776 |
goog.net.XhrManager.Request.prototype.getCompleteCallback = function() { |
| 777 |
return this.completeCallback_; |
| 778 |
}; |
| 779 |
|
| 780 |
|
| 781 |
/** |
| 782 |
* Gets the response type that will be set on this request's XhrIo when it's |
| 783 |
* available. |
| 784 |
* @return {!goog.net.XhrIo.ResponseType} The response type to be set |
| 785 |
* when an XhrIo becomes available to this request. |
| 786 |
*/ |
| 787 |
goog.net.XhrManager.Request.prototype.getResponseType = function() { |
| 788 |
return this.responseType_; |
| 789 |
}; |