Path: blob/trunk/third_party/closure/goog/net/xmlhttp.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 Low level handling of XMLHttpRequest.16* @author [email protected] (Erik Arvidsson)17* @author [email protected] (David Barrett-Kahn)18*/1920goog.provide('goog.net.DefaultXmlHttpFactory');21goog.provide('goog.net.XmlHttp');22goog.provide('goog.net.XmlHttp.OptionType');23goog.provide('goog.net.XmlHttp.ReadyState');24goog.provide('goog.net.XmlHttpDefines');2526goog.require('goog.asserts');27goog.require('goog.net.WrapperXmlHttpFactory');28goog.require('goog.net.XmlHttpFactory');293031/**32* Static class for creating XMLHttpRequest objects.33* @return {!goog.net.XhrLike.OrNative} A new XMLHttpRequest object.34*/35goog.net.XmlHttp = function() {36return goog.net.XmlHttp.factory_.createInstance();37};383940/**41* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to42* true bypasses the ActiveX probing code.43* NOTE(ruilopes): Due to the way JSCompiler works, this define *will not* strip44* out the ActiveX probing code from binaries. To achieve this, use45* {@code goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR} instead.46* TODO(ruilopes): Collapse both defines.47*/48goog.define('goog.net.XmlHttp.ASSUME_NATIVE_XHR', false);495051/** @const */52goog.net.XmlHttpDefines = {};535455/**56* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to57* true eliminates the ActiveX probing code.58*/59goog.define('goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR', false);606162/**63* Gets the options to use with the XMLHttpRequest objects obtained using64* the static methods.65* @return {Object} The options.66*/67goog.net.XmlHttp.getOptions = function() {68return goog.net.XmlHttp.factory_.getOptions();69};707172/**73* Type of options that an XmlHttp object can have.74* @enum {number}75*/76goog.net.XmlHttp.OptionType = {77/**78* Whether a goog.nullFunction should be used to clear the onreadystatechange79* handler instead of null.80*/81USE_NULL_FUNCTION: 0,8283/**84* NOTE(user): In IE if send() errors on a *local* request the readystate85* is still changed to COMPLETE. We need to ignore it and allow the86* try/catch around send() to pick up the error.87*/88LOCAL_REQUEST_ERROR: 189};909192/**93* Status constants for XMLHTTP, matches:94* https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx95* @enum {number}96*/97goog.net.XmlHttp.ReadyState = {98/**99* Constant for when xmlhttprequest.readyState is uninitialized100*/101UNINITIALIZED: 0,102103/**104* Constant for when xmlhttprequest.readyState is loading.105*/106LOADING: 1,107108/**109* Constant for when xmlhttprequest.readyState is loaded.110*/111LOADED: 2,112113/**114* Constant for when xmlhttprequest.readyState is in an interactive state.115*/116INTERACTIVE: 3,117118/**119* Constant for when xmlhttprequest.readyState is completed120*/121COMPLETE: 4122};123124125/**126* The global factory instance for creating XMLHttpRequest objects.127* @type {goog.net.XmlHttpFactory}128* @private129*/130goog.net.XmlHttp.factory_;131132133/**134* Sets the factories for creating XMLHttpRequest objects and their options.135* @param {Function} factory The factory for XMLHttpRequest objects.136* @param {Function} optionsFactory The factory for options.137* @deprecated Use setGlobalFactory instead.138*/139goog.net.XmlHttp.setFactory = function(factory, optionsFactory) {140goog.net.XmlHttp.setGlobalFactory(141new goog.net.WrapperXmlHttpFactory(142goog.asserts.assert(factory), goog.asserts.assert(optionsFactory)));143};144145146/**147* Sets the global factory object.148* @param {!goog.net.XmlHttpFactory} factory New global factory object.149*/150goog.net.XmlHttp.setGlobalFactory = function(factory) {151goog.net.XmlHttp.factory_ = factory;152};153154155156/**157* Default factory to use when creating xhr objects. You probably shouldn't be158* instantiating this directly, but rather using it via goog.net.XmlHttp.159* @extends {goog.net.XmlHttpFactory}160* @constructor161*/162goog.net.DefaultXmlHttpFactory = function() {163goog.net.XmlHttpFactory.call(this);164};165goog.inherits(goog.net.DefaultXmlHttpFactory, goog.net.XmlHttpFactory);166167168/** @override */169goog.net.DefaultXmlHttpFactory.prototype.createInstance = function() {170var progId = this.getProgId_();171if (progId) {172return new ActiveXObject(progId);173} else {174return new XMLHttpRequest();175}176};177178179/** @override */180goog.net.DefaultXmlHttpFactory.prototype.internalGetOptions = function() {181var progId = this.getProgId_();182var options = {};183if (progId) {184options[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] = true;185options[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] = true;186}187return options;188};189190191/**192* The ActiveX PROG ID string to use to create xhr's in IE. Lazily initialized.193* @type {string|undefined}194* @private195*/196goog.net.DefaultXmlHttpFactory.prototype.ieProgId_;197198199/**200* Initialize the private state used by other functions.201* @return {string} The ActiveX PROG ID string to use to create xhr's in IE.202* @private203*/204goog.net.DefaultXmlHttpFactory.prototype.getProgId_ = function() {205if (goog.net.XmlHttp.ASSUME_NATIVE_XHR ||206goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR) {207return '';208}209210// The following blog post describes what PROG IDs to use to create the211// XMLHTTP object in Internet Explorer:212// http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx213// However we do not (yet) fully trust that this will be OK for old versions214// of IE on Win9x so we therefore keep the last 2.215if (!this.ieProgId_ && typeof XMLHttpRequest == 'undefined' &&216typeof ActiveXObject != 'undefined') {217// Candidate Active X types.218var ACTIVE_X_IDENTS = [219'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP',220'Microsoft.XMLHTTP'221];222for (var i = 0; i < ACTIVE_X_IDENTS.length; i++) {223var candidate = ACTIVE_X_IDENTS[i];224225try {226new ActiveXObject(candidate);227// NOTE(user): cannot assign progid and return candidate in one line228// because JSCompiler complaings: BUG 658126229this.ieProgId_ = candidate;230return candidate;231} catch (e) {232// do nothing; try next choice233}234}235236// couldn't find any matches237throw Error(238'Could not create ActiveXObject. ActiveX might be disabled,' +239' or MSXML might not be installed');240}241242return /** @type {string} */ (this.ieProgId_);243};244245246// Set the global factory to an instance of the default factory.247goog.net.XmlHttp.setGlobalFactory(new goog.net.DefaultXmlHttpFactory());248249250