Path: blob/trunk/third_party/closure/goog/net/xmlhttp.js
4509 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Low level handling of XMLHttpRequest.8*/910goog.provide('goog.net.DefaultXmlHttpFactory');11goog.provide('goog.net.XmlHttp');12goog.provide('goog.net.XmlHttp.OptionType');13goog.provide('goog.net.XmlHttp.ReadyState');14goog.provide('goog.net.XmlHttpDefines');1516goog.require('goog.asserts');17goog.require('goog.net.WrapperXmlHttpFactory');18goog.require('goog.net.XmlHttpFactory');19goog.requireType('goog.net.XhrLike');202122/**23* Static class for creating XMLHttpRequest objects.24* @return {!goog.net.XhrLike.OrNative} A new XMLHttpRequest object.25*/26goog.net.XmlHttp = function() {27'use strict';28return goog.net.XmlHttp.factory_.createInstance();29};303132/**33* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to34* true bypasses the ActiveX probing code.35* NOTE(ruilopes): Due to the way JSCompiler works, this define *will not* strip36* out the ActiveX probing code from binaries. To achieve this, use37* `goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR` instead.38* TODO(ruilopes): Collapse both defines.39*/40goog.net.XmlHttp.ASSUME_NATIVE_XHR =41goog.define('goog.net.XmlHttp.ASSUME_NATIVE_XHR', false);424344/** @const */45goog.net.XmlHttpDefines = {};464748/**49* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to50* true eliminates the ActiveX probing code.51*/52goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR =53goog.define('goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR', false);545556/**57* Gets the options to use with the XMLHttpRequest objects obtained using58* the static methods.59* @return {Object} The options.60*/61goog.net.XmlHttp.getOptions = function() {62'use strict';63return goog.net.XmlHttp.factory_.getOptions();64};656667/**68* Type of options that an XmlHttp object can have.69* @enum {number}70*/71goog.net.XmlHttp.OptionType = {72/**73* Whether a no-op function should be used to clear the onreadystatechange74* handler instead of null.75*/76USE_NULL_FUNCTION: 0,7778/**79* NOTE(user): In IE if send() errors on a *local* request the readystate80* is still changed to COMPLETE. We need to ignore it and allow the81* try/catch around send() to pick up the error.82*/83LOCAL_REQUEST_ERROR: 1,84};858687/**88* Status constants for XMLHTTP, matches:89* https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx90* @enum {number}91*/92goog.net.XmlHttp.ReadyState = {93/**94* Constant for when xmlhttprequest.readyState is uninitialized95*/96UNINITIALIZED: 0,9798/**99* Constant for when xmlhttprequest.readyState is loading.100*/101LOADING: 1,102103/**104* Constant for when xmlhttprequest.readyState is loaded.105*/106LOADED: 2,107108/**109* Constant for when xmlhttprequest.readyState is in an interactive state.110*/111INTERACTIVE: 3,112113/**114* Constant for when xmlhttprequest.readyState is completed115*/116COMPLETE: 4,117};118119120/**121* The global factory instance for creating XMLHttpRequest objects.122* @type {goog.net.XmlHttpFactory}123* @private124*/125goog.net.XmlHttp.factory_;126127128/**129* Sets the factories for creating XMLHttpRequest objects and their options.130* @param {Function} factory The factory for XMLHttpRequest objects.131* @param {Function} optionsFactory The factory for options.132* @deprecated Use setGlobalFactory instead.133*/134goog.net.XmlHttp.setFactory = function(factory, optionsFactory) {135'use strict';136goog.net.XmlHttp.setGlobalFactory(new goog.net.WrapperXmlHttpFactory(137goog.asserts.assert(factory), goog.asserts.assert(optionsFactory)));138};139140141/**142* Sets the global factory object.143* @param {!goog.net.XmlHttpFactory} factory New global factory object.144*/145goog.net.XmlHttp.setGlobalFactory = function(factory) {146'use strict';147goog.net.XmlHttp.factory_ = factory;148};149150151152/**153* Default factory to use when creating xhr objects. You probably shouldn't be154* instantiating this directly, but rather using it via goog.net.XmlHttp.155* @extends {goog.net.XmlHttpFactory}156* @constructor157*/158goog.net.DefaultXmlHttpFactory = function() {159'use strict';160goog.net.XmlHttpFactory.call(this);161};162goog.inherits(goog.net.DefaultXmlHttpFactory, goog.net.XmlHttpFactory);163164165/** @override */166goog.net.DefaultXmlHttpFactory.prototype.createInstance = function() {167'use strict';168const progId = this.getProgId_();169if (progId) {170return new ActiveXObject(progId);171} else {172return new XMLHttpRequest();173}174};175176177/** @override */178goog.net.DefaultXmlHttpFactory.prototype.internalGetOptions = function() {179'use strict';180const progId = this.getProgId_();181const options = {};182if (progId) {183options[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] = true;184options[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] = true;185}186return options;187};188189190/**191* The ActiveX PROG ID string to use to create xhr's in IE. Lazily initialized.192* @type {string|undefined}193* @private194*/195goog.net.DefaultXmlHttpFactory.prototype.ieProgId_;196197198/**199* Initialize the private state used by other functions.200* @return {string} The ActiveX PROG ID string to use to create xhr's in IE.201* @private202*/203goog.net.DefaultXmlHttpFactory.prototype.getProgId_ = function() {204'use strict';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.218const ACTIVE_X_IDENTS = [219'MSXML2.XMLHTTP.6.0',220'MSXML2.XMLHTTP.3.0',221'MSXML2.XMLHTTP',222'Microsoft.XMLHTTP',223];224for (let i = 0; i < ACTIVE_X_IDENTS.length; i++) {225const candidate = ACTIVE_X_IDENTS[i];226227try {228new ActiveXObject(candidate);229// NOTE(user): cannot assign progid and return candidate in one line230// because JSCompiler complaings: BUG 658126231this.ieProgId_ = candidate;232return candidate;233} catch (e) {234// do nothing; try next choice235}236}237238// couldn't find any matches239throw new Error(240'Could not create ActiveXObject. ActiveX might be disabled,' +241' or MSXML might not be installed');242}243244return /** @type {string} */ (this.ieProgId_);245};246247248// Set the global factory to an instance of the default factory.249goog.net.XmlHttp.setGlobalFactory(new goog.net.DefaultXmlHttpFactory());250251252