blob: eaf1f36f2015bcf4172273bb6be04268f046b287 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8/**
9 * @fileoverview A bunch of XML HTTP recipes used to do RPC from JavaScript
10 */
11
12
13/**
14 * The active x identifier used for ie.
15 * @type String
16 * @private
17 */
18var XH_ieProgId_;
19
20
21// Domain for XMLHttpRequest readyState
22var XML_READY_STATE_UNINITIALIZED = 0;
23var XML_READY_STATE_LOADING = 1;
24var XML_READY_STATE_LOADED = 2;
25var XML_READY_STATE_INTERACTIVE = 3;
26var XML_READY_STATE_COMPLETED = 4;
27
28
29/**
30 * Initialize the private state used by other functions.
31 * @private
32 */
33function XH_XmlHttpInit_() {
34 // The following blog post describes what PROG IDs to use to create the
35 // XMLHTTP object in Internet Explorer:
36 // http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
37 // However we do not (yet) fully trust that this will be OK for old versions
38 // of IE on Win9x so we therefore keep the last 2.
39 // Versions 4 and 5 have been removed because 3.0 is the preferred "fallback"
40 // per the article above.
41 // - Version 5 was built for Office applications and is not recommended for
42 // web applications.
43 // - Version 4 has been superseded by 6 and is only intended for legacy apps.
44 // - Version 3 has a wide install base and is serviced regularly with the OS.
45
46 /**
47 * Candidate Active X types.
48 * @type Array.<String>
49 * @private
50 */
51 let XH_ACTIVE_X_IDENTS = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0',
52 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
53
54 if (typeof XMLHttpRequest == 'undefined' &&
55 typeof ActiveXObject != 'undefined') {
56 for (let i = 0; i < XH_ACTIVE_X_IDENTS.length; i++) {
57 let candidate = XH_ACTIVE_X_IDENTS[i];
58
59 try {
60 new ActiveXObject(candidate);
61 XH_ieProgId_ = candidate;
62 break;
63 } catch (e) {
64 // do nothing; try next choice
65 }
66 }
67
68 // couldn't find any matches
69 if (!XH_ieProgId_) {
70 throw Error('Could not create ActiveXObject. ActiveX might be disabled,' +
71 ' or MSXML might not be installed.');
72 }
73 }
74}
75
76
77XH_XmlHttpInit_();
78
79
80/**
81 * Create and return an xml http request object that can be passed to
82 * {@link #XH_XmlHttpGET} or {@link #XH_XmlHttpPOST}.
83 */
84function XH_XmlHttpCreate() {
85 if (XH_ieProgId_) {
86 return new ActiveXObject(XH_ieProgId_);
87 } else {
88 return new XMLHttpRequest();
89 }
90}
91
92
93/**
94 * Send a get request.
95 * @param {XMLHttpRequest} xmlHttp as from {@link XH_XmlHttpCreate}.
96 * @param {string} url the service to contact
97 * @param {Function} handler function called when the response is received.
98 */
99function XH_XmlHttpGET(xmlHttp, url, handler) {
100 xmlHttp.open('GET', url, true);
101 xmlHttp.onreadystatechange = handler;
102 XH_XmlHttpSend(xmlHttp, null);
103}
104
105/**
106 * Send a post request.
107 * @param {XMLHttpRequest} xmlHttp as from {@link XH_XmlHttpCreate}.
108 * @param {string} url the service to contact
109 * @param {string} data the request content.
110 * @param {Function} handler function called when the response is received.
111 */
112function XH_XmlHttpPOST(xmlHttp, url, data, handler) {
113 xmlHttp.open('POST', url, true);
114 xmlHttp.onreadystatechange = handler;
115 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
116 XH_XmlHttpSend(xmlHttp, data);
117}
118
119/**
120 * Calls 'send' on the XMLHttpRequest object and calls a function called 'log'
121 * if any error occured.
122 *
123 * @deprecated This dependes on a function called 'log'. You are better off
124 * handling your errors on application level.
125 *
126 * @param {XMLHttpRequest} xmlHttp as from {@link XH_XmlHttpCreate}.
127 * @param {string|null} data the request content.
128 */
129function XH_XmlHttpSend(xmlHttp, data) {
130 try {
131 xmlHttp.send(data);
132 } catch (e) {
133 // You may want to log/debug this error one that you should be aware of is
134 // e.number == -2146697208, which occurs when the 'Languages...' setting in
135 // IE is empty.
136 // This is not entirely true. The same error code is used when the user is
137 // off line.
138 console.log('XMLHttpSend failed ' + e.toString() + '<br>' + e.stack);
139 throw e;
140 }
141}