xhrmanager.js 23 KB

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