Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/net/xmlhttp.js
2868 views
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 Low level handling of XMLHttpRequest.
17
* @author [email protected] (Erik Arvidsson)
18
* @author [email protected] (David Barrett-Kahn)
19
*/
20
21
goog.provide('goog.net.DefaultXmlHttpFactory');
22
goog.provide('goog.net.XmlHttp');
23
goog.provide('goog.net.XmlHttp.OptionType');
24
goog.provide('goog.net.XmlHttp.ReadyState');
25
goog.provide('goog.net.XmlHttpDefines');
26
27
goog.require('goog.asserts');
28
goog.require('goog.net.WrapperXmlHttpFactory');
29
goog.require('goog.net.XmlHttpFactory');
30
31
32
/**
33
* Static class for creating XMLHttpRequest objects.
34
* @return {!goog.net.XhrLike.OrNative} A new XMLHttpRequest object.
35
*/
36
goog.net.XmlHttp = function() {
37
return goog.net.XmlHttp.factory_.createInstance();
38
};
39
40
41
/**
42
* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to
43
* true bypasses the ActiveX probing code.
44
* NOTE(ruilopes): Due to the way JSCompiler works, this define *will not* strip
45
* out the ActiveX probing code from binaries. To achieve this, use
46
* {@code goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR} instead.
47
* TODO(ruilopes): Collapse both defines.
48
*/
49
goog.define('goog.net.XmlHttp.ASSUME_NATIVE_XHR', false);
50
51
52
/** @const */
53
goog.net.XmlHttpDefines = {};
54
55
56
/**
57
* @define {boolean} Whether to assume XMLHttpRequest exists. Setting this to
58
* true eliminates the ActiveX probing code.
59
*/
60
goog.define('goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR', false);
61
62
63
/**
64
* Gets the options to use with the XMLHttpRequest objects obtained using
65
* the static methods.
66
* @return {Object} The options.
67
*/
68
goog.net.XmlHttp.getOptions = function() {
69
return goog.net.XmlHttp.factory_.getOptions();
70
};
71
72
73
/**
74
* Type of options that an XmlHttp object can have.
75
* @enum {number}
76
*/
77
goog.net.XmlHttp.OptionType = {
78
/**
79
* Whether a goog.nullFunction should be used to clear the onreadystatechange
80
* handler instead of null.
81
*/
82
USE_NULL_FUNCTION: 0,
83
84
/**
85
* NOTE(user): In IE if send() errors on a *local* request the readystate
86
* is still changed to COMPLETE. We need to ignore it and allow the
87
* try/catch around send() to pick up the error.
88
*/
89
LOCAL_REQUEST_ERROR: 1
90
};
91
92
93
/**
94
* Status constants for XMLHTTP, matches:
95
* https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx
96
* @enum {number}
97
*/
98
goog.net.XmlHttp.ReadyState = {
99
/**
100
* Constant for when xmlhttprequest.readyState is uninitialized
101
*/
102
UNINITIALIZED: 0,
103
104
/**
105
* Constant for when xmlhttprequest.readyState is loading.
106
*/
107
LOADING: 1,
108
109
/**
110
* Constant for when xmlhttprequest.readyState is loaded.
111
*/
112
LOADED: 2,
113
114
/**
115
* Constant for when xmlhttprequest.readyState is in an interactive state.
116
*/
117
INTERACTIVE: 3,
118
119
/**
120
* Constant for when xmlhttprequest.readyState is completed
121
*/
122
COMPLETE: 4
123
};
124
125
126
/**
127
* The global factory instance for creating XMLHttpRequest objects.
128
* @type {goog.net.XmlHttpFactory}
129
* @private
130
*/
131
goog.net.XmlHttp.factory_;
132
133
134
/**
135
* Sets the factories for creating XMLHttpRequest objects and their options.
136
* @param {Function} factory The factory for XMLHttpRequest objects.
137
* @param {Function} optionsFactory The factory for options.
138
* @deprecated Use setGlobalFactory instead.
139
*/
140
goog.net.XmlHttp.setFactory = function(factory, optionsFactory) {
141
goog.net.XmlHttp.setGlobalFactory(
142
new goog.net.WrapperXmlHttpFactory(
143
goog.asserts.assert(factory), goog.asserts.assert(optionsFactory)));
144
};
145
146
147
/**
148
* Sets the global factory object.
149
* @param {!goog.net.XmlHttpFactory} factory New global factory object.
150
*/
151
goog.net.XmlHttp.setGlobalFactory = function(factory) {
152
goog.net.XmlHttp.factory_ = factory;
153
};
154
155
156
157
/**
158
* Default factory to use when creating xhr objects. You probably shouldn't be
159
* instantiating this directly, but rather using it via goog.net.XmlHttp.
160
* @extends {goog.net.XmlHttpFactory}
161
* @constructor
162
*/
163
goog.net.DefaultXmlHttpFactory = function() {
164
goog.net.XmlHttpFactory.call(this);
165
};
166
goog.inherits(goog.net.DefaultXmlHttpFactory, goog.net.XmlHttpFactory);
167
168
169
/** @override */
170
goog.net.DefaultXmlHttpFactory.prototype.createInstance = function() {
171
var progId = this.getProgId_();
172
if (progId) {
173
return new ActiveXObject(progId);
174
} else {
175
return new XMLHttpRequest();
176
}
177
};
178
179
180
/** @override */
181
goog.net.DefaultXmlHttpFactory.prototype.internalGetOptions = function() {
182
var progId = this.getProgId_();
183
var options = {};
184
if (progId) {
185
options[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] = true;
186
options[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] = true;
187
}
188
return options;
189
};
190
191
192
/**
193
* The ActiveX PROG ID string to use to create xhr's in IE. Lazily initialized.
194
* @type {string|undefined}
195
* @private
196
*/
197
goog.net.DefaultXmlHttpFactory.prototype.ieProgId_;
198
199
200
/**
201
* Initialize the private state used by other functions.
202
* @return {string} The ActiveX PROG ID string to use to create xhr's in IE.
203
* @private
204
*/
205
goog.net.DefaultXmlHttpFactory.prototype.getProgId_ = function() {
206
if (goog.net.XmlHttp.ASSUME_NATIVE_XHR ||
207
goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR) {
208
return '';
209
}
210
211
// The following blog post describes what PROG IDs to use to create the
212
// XMLHTTP object in Internet Explorer:
213
// http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
214
// However we do not (yet) fully trust that this will be OK for old versions
215
// of IE on Win9x so we therefore keep the last 2.
216
if (!this.ieProgId_ && typeof XMLHttpRequest == 'undefined' &&
217
typeof ActiveXObject != 'undefined') {
218
// Candidate Active X types.
219
var ACTIVE_X_IDENTS = [
220
'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP',
221
'Microsoft.XMLHTTP'
222
];
223
for (var i = 0; i < ACTIVE_X_IDENTS.length; i++) {
224
var candidate = ACTIVE_X_IDENTS[i];
225
226
try {
227
new ActiveXObject(candidate);
228
// NOTE(user): cannot assign progid and return candidate in one line
229
// because JSCompiler complaings: BUG 658126
230
this.ieProgId_ = candidate;
231
return candidate;
232
} catch (e) {
233
// do nothing; try next choice
234
}
235
}
236
237
// couldn't find any matches
238
throw Error(
239
'Could not create ActiveXObject. ActiveX might be disabled,' +
240
' or MSXML might not be installed');
241
}
242
243
return /** @type {string} */ (this.ieProgId_);
244
};
245
246
247
// Set the global factory to an instance of the default factory.
248
goog.net.XmlHttp.setGlobalFactory(new goog.net.DefaultXmlHttpFactory());
249
250