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