Path: blob/trunk/third_party/closure/goog/net/xhriopool.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Creates a pool of XhrIo objects to use. This allows multiple16* XhrIo objects to be grouped together and requests will use next available17* XhrIo object.18*19*/2021goog.provide('goog.net.XhrIoPool');2223goog.require('goog.net.XhrIo');24goog.require('goog.structs.PriorityPool');25262728/**29* A pool of XhrIo objects.30* @param {goog.structs.Map=} opt_headers Map of default headers to add to every31* request.32* @param {number=} opt_minCount Minimum number of objects (Default: 0).33* @param {number=} opt_maxCount Maximum number of objects (Default: 10).34* @param {boolean=} opt_withCredentials Add credentials to every request35* (Default: false).36* @constructor37* @extends {goog.structs.PriorityPool}38*/39goog.net.XhrIoPool = function(40opt_headers, opt_minCount, opt_maxCount, opt_withCredentials) {41/**42* Map of default headers to add to every request.43* @type {goog.structs.Map|undefined}44* @private45*/46this.headers_ = opt_headers;4748/**49* Whether a "credentialed" requests are to be sent (ones that is aware of50* cookies and authentication). This is applicable only for cross-domain51* requests and more recent browsers that support this part of the HTTP Access52* Control standard.53*54* @see http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute55*56* @private {boolean}57*/58this.withCredentials_ = !!opt_withCredentials;5960// Must break convention of putting the super-class's constructor first. This61// is because the super-class constructor calls adjustForMinMax, which calls62// this class' createObject. In this class's implementation, it assumes that63// there is a headers_, and will lack those if not yet present.64goog.structs.PriorityPool.call(this, opt_minCount, opt_maxCount);65};66goog.inherits(goog.net.XhrIoPool, goog.structs.PriorityPool);676869/**70* Creates an instance of an XhrIo object to use in the pool.71* @return {!goog.net.XhrIo} The created object.72* @override73*/74goog.net.XhrIoPool.prototype.createObject = function() {75var xhrIo = new goog.net.XhrIo();76var headers = this.headers_;77if (headers) {78headers.forEach(function(value, key) { xhrIo.headers.set(key, value); });79}80if (this.withCredentials_) {81xhrIo.setWithCredentials(true);82}83return xhrIo;84};858687/**88* Determine if an object has become unusable and should not be used.89* @param {Object} obj The object to test.90* @return {boolean} Whether the object can be reused, which is true if the91* object is not disposed and not active.92* @override93*/94goog.net.XhrIoPool.prototype.objectCanBeReused = function(obj) {95// An active XhrIo object should never be used.96var xhr = /** @type {goog.net.XhrIo} */ (obj);97return !xhr.isDisposed() && !xhr.isActive();98};99100101