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