xhriopool.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Creates a pool of XhrIo objects to use. This allows multiple
  16. * XhrIo objects to be grouped together and requests will use next available
  17. * XhrIo object.
  18. *
  19. */
  20. goog.provide('goog.net.XhrIoPool');
  21. goog.require('goog.net.XhrIo');
  22. goog.require('goog.structs.PriorityPool');
  23. /**
  24. * A pool of XhrIo objects.
  25. * @param {goog.structs.Map=} opt_headers Map of default headers to add to every
  26. * request.
  27. * @param {number=} opt_minCount Minimum number of objects (Default: 0).
  28. * @param {number=} opt_maxCount Maximum number of objects (Default: 10).
  29. * @param {boolean=} opt_withCredentials Add credentials to every request
  30. * (Default: false).
  31. * @constructor
  32. * @extends {goog.structs.PriorityPool}
  33. */
  34. goog.net.XhrIoPool = function(
  35. opt_headers, opt_minCount, opt_maxCount, opt_withCredentials) {
  36. /**
  37. * Map of default headers to add to every request.
  38. * @type {goog.structs.Map|undefined}
  39. * @private
  40. */
  41. this.headers_ = opt_headers;
  42. /**
  43. * Whether a "credentialed" requests are to be sent (ones that is aware of
  44. * cookies and authentication). This is applicable only for cross-domain
  45. * requests and more recent browsers that support this part of the HTTP Access
  46. * Control standard.
  47. *
  48. * @see http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute
  49. *
  50. * @private {boolean}
  51. */
  52. this.withCredentials_ = !!opt_withCredentials;
  53. // Must break convention of putting the super-class's constructor first. This
  54. // is because the super-class constructor calls adjustForMinMax, which calls
  55. // this class' createObject. In this class's implementation, it assumes that
  56. // there is a headers_, and will lack those if not yet present.
  57. goog.structs.PriorityPool.call(this, opt_minCount, opt_maxCount);
  58. };
  59. goog.inherits(goog.net.XhrIoPool, goog.structs.PriorityPool);
  60. /**
  61. * Creates an instance of an XhrIo object to use in the pool.
  62. * @return {!goog.net.XhrIo} The created object.
  63. * @override
  64. */
  65. goog.net.XhrIoPool.prototype.createObject = function() {
  66. var xhrIo = new goog.net.XhrIo();
  67. var headers = this.headers_;
  68. if (headers) {
  69. headers.forEach(function(value, key) { xhrIo.headers.set(key, value); });
  70. }
  71. if (this.withCredentials_) {
  72. xhrIo.setWithCredentials(true);
  73. }
  74. return xhrIo;
  75. };
  76. /**
  77. * Determine if an object has become unusable and should not be used.
  78. * @param {Object} obj The object to test.
  79. * @return {boolean} Whether the object can be reused, which is true if the
  80. * object is not disposed and not active.
  81. * @override
  82. */
  83. goog.net.XhrIoPool.prototype.objectCanBeReused = function(obj) {
  84. // An active XhrIo object should never be used.
  85. var xhr = /** @type {goog.net.XhrIo} */ (obj);
  86. return !xhr.isDisposed() && !xhr.isActive();
  87. };