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