OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Simple utilities for making XHRs more pleasant. | 7 * Simple utilities for making XHRs more pleasant. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** Namespace for XHR functions */ | 15 /** Namespace for XHR functions */ |
16 /** @type {Object} */ | 16 /** @type {Object} */ |
17 remoting.xhr = remoting.xhr || {}; | 17 remoting.xhr = remoting.xhr || {}; |
18 | 18 |
19 /** | 19 /** |
20 * Takes an associative array of parameters and urlencodes it. | 20 * Takes an associative array of parameters and urlencodes it. |
21 * | 21 * |
22 * @param {Object.<string>} paramHash The parameter key/value pairs. | 22 * @param {Object<string>} paramHash The parameter key/value pairs. |
23 * @return {string} URLEncoded version of paramHash. | 23 * @return {string} URLEncoded version of paramHash. |
24 */ | 24 */ |
25 remoting.xhr.urlencodeParamHash = function(paramHash) { | 25 remoting.xhr.urlencodeParamHash = function(paramHash) { |
26 var paramArray = []; | 26 var paramArray = []; |
27 for (var key in paramHash) { | 27 for (var key in paramHash) { |
28 paramArray.push(encodeURIComponent(key) + | 28 paramArray.push(encodeURIComponent(key) + |
29 '=' + encodeURIComponent(paramHash[key])); | 29 '=' + encodeURIComponent(paramHash[key])); |
30 } | 30 } |
31 if (paramArray.length > 0) { | 31 if (paramArray.length > 0) { |
32 return paramArray.join('&'); | 32 return paramArray.join('&'); |
33 } | 33 } |
34 return ''; | 34 return ''; |
35 }; | 35 }; |
36 | 36 |
37 /** | 37 /** |
38 * Execute an XHR GET asynchronously. | 38 * Execute an XHR GET asynchronously. |
39 * | 39 * |
40 * @param {string} url The base URL to GET, excluding parameters. | 40 * @param {string} url The base URL to GET, excluding parameters. |
41 * @param {function(XMLHttpRequest):void} onDone The function to call on | 41 * @param {function(XMLHttpRequest):void} onDone The function to call on |
42 * completion. | 42 * completion. |
43 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 43 * @param {(string|Object<string>)=} opt_parameters The request parameters, |
44 * either as an associative array, or a string. If it is a string, do | 44 * either as an associative array, or a string. If it is a string, do |
45 * not include the ? and be sure it is correctly URLEncoded. | 45 * not include the ? and be sure it is correctly URLEncoded. |
46 * @param {Object.<string>=} opt_headers Additional headers to include on the | 46 * @param {Object<string>=} opt_headers Additional headers to include on the |
47 * request. | 47 * request. |
48 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 48 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
49 * XHR. | 49 * XHR. |
50 * @return {XMLHttpRequest} The request object. | 50 * @return {XMLHttpRequest} The request object. |
51 */ | 51 */ |
52 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, | 52 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, |
53 opt_withCredentials) { | 53 opt_withCredentials) { |
54 return remoting.xhr.doMethod('GET', url, onDone, opt_parameters, | 54 return remoting.xhr.doMethod('GET', url, onDone, opt_parameters, |
55 opt_headers, opt_withCredentials); | 55 opt_headers, opt_withCredentials); |
56 }; | 56 }; |
57 | 57 |
58 /** | 58 /** |
59 * Execute an XHR POST asynchronously. | 59 * Execute an XHR POST asynchronously. |
60 * | 60 * |
61 * @param {string} url The base URL to POST, excluding parameters. | 61 * @param {string} url The base URL to POST, excluding parameters. |
62 * @param {function(XMLHttpRequest):void} onDone The function to call on | 62 * @param {function(XMLHttpRequest):void} onDone The function to call on |
63 * completion. | 63 * completion. |
64 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 64 * @param {(string|Object<string>)=} opt_parameters The request parameters, |
65 * either as an associative array, or a string. If it is a string, be | 65 * either as an associative array, or a string. If it is a string, be |
66 * sure it is correctly URLEncoded. | 66 * sure it is correctly URLEncoded. |
67 * @param {Object.<string>=} opt_headers Additional headers to include on the | 67 * @param {Object<string>=} opt_headers Additional headers to include on the |
68 * request. | 68 * request. |
69 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 69 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
70 * XHR. | 70 * XHR. |
71 * @return {XMLHttpRequest} The request object. | 71 * @return {XMLHttpRequest} The request object. |
72 */ | 72 */ |
73 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers, | 73 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers, |
74 opt_withCredentials) { | 74 opt_withCredentials) { |
75 return remoting.xhr.doMethod('POST', url, onDone, opt_parameters, | 75 return remoting.xhr.doMethod('POST', url, onDone, opt_parameters, |
76 opt_headers, opt_withCredentials); | 76 opt_headers, opt_withCredentials); |
77 }; | 77 }; |
78 | 78 |
79 /** | 79 /** |
80 * Execute an XHR DELETE asynchronously. | 80 * Execute an XHR DELETE asynchronously. |
81 * | 81 * |
82 * @param {string} url The base URL to DELETE, excluding parameters. | 82 * @param {string} url The base URL to DELETE, excluding parameters. |
83 * @param {function(XMLHttpRequest):void} onDone The function to call on | 83 * @param {function(XMLHttpRequest):void} onDone The function to call on |
84 * completion. | 84 * completion. |
85 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 85 * @param {(string|Object<string>)=} opt_parameters The request parameters, |
86 * either as an associative array, or a string. If it is a string, be | 86 * either as an associative array, or a string. If it is a string, be |
87 * sure it is correctly URLEncoded. | 87 * sure it is correctly URLEncoded. |
88 * @param {Object.<string>=} opt_headers Additional headers to include on the | 88 * @param {Object<string>=} opt_headers Additional headers to include on the |
89 * request. | 89 * request. |
90 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 90 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
91 * XHR. | 91 * XHR. |
92 * @return {XMLHttpRequest} The request object. | 92 * @return {XMLHttpRequest} The request object. |
93 */ | 93 */ |
94 remoting.xhr.remove = function(url, onDone, opt_parameters, opt_headers, | 94 remoting.xhr.remove = function(url, onDone, opt_parameters, opt_headers, |
95 opt_withCredentials) { | 95 opt_withCredentials) { |
96 return remoting.xhr.doMethod('DELETE', url, onDone, opt_parameters, | 96 return remoting.xhr.doMethod('DELETE', url, onDone, opt_parameters, |
97 opt_headers, opt_withCredentials); | 97 opt_headers, opt_withCredentials); |
98 }; | 98 }; |
99 | 99 |
100 /** | 100 /** |
101 * Execute an XHR PUT asynchronously. | 101 * Execute an XHR PUT asynchronously. |
102 * | 102 * |
103 * @param {string} url The base URL to PUT, excluding parameters. | 103 * @param {string} url The base URL to PUT, excluding parameters. |
104 * @param {function(XMLHttpRequest):void} onDone The function to call on | 104 * @param {function(XMLHttpRequest):void} onDone The function to call on |
105 * completion. | 105 * completion. |
106 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 106 * @param {(string|Object<string>)=} opt_parameters The request parameters, |
107 * either as an associative array, or a string. If it is a string, be | 107 * either as an associative array, or a string. If it is a string, be |
108 * sure it is correctly URLEncoded. | 108 * sure it is correctly URLEncoded. |
109 * @param {Object.<string>=} opt_headers Additional headers to include on the | 109 * @param {Object<string>=} opt_headers Additional headers to include on the |
110 * request. | 110 * request. |
111 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 111 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
112 * XHR. | 112 * XHR. |
113 * @return {XMLHttpRequest} The request object. | 113 * @return {XMLHttpRequest} The request object. |
114 */ | 114 */ |
115 remoting.xhr.put = function(url, onDone, opt_parameters, opt_headers, | 115 remoting.xhr.put = function(url, onDone, opt_parameters, opt_headers, |
116 opt_withCredentials) { | 116 opt_withCredentials) { |
117 return remoting.xhr.doMethod('PUT', url, onDone, opt_parameters, | 117 return remoting.xhr.doMethod('PUT', url, onDone, opt_parameters, |
118 opt_headers, opt_withCredentials); | 118 opt_headers, opt_withCredentials); |
119 }; | 119 }; |
120 | 120 |
121 /** | 121 /** |
122 * Execute an arbitrary HTTP method asynchronously. | 122 * Execute an arbitrary HTTP method asynchronously. |
123 * | 123 * |
124 * @param {string} methodName The HTTP method name, e.g. "GET", "POST" etc. | 124 * @param {string} methodName The HTTP method name, e.g. "GET", "POST" etc. |
125 * @param {string} url The base URL, excluding parameters. | 125 * @param {string} url The base URL, excluding parameters. |
126 * @param {function(XMLHttpRequest):void} onDone The function to call on | 126 * @param {function(XMLHttpRequest):void} onDone The function to call on |
127 * completion. | 127 * completion. |
128 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 128 * @param {(string|Object<string>)=} opt_parameters The request parameters, |
129 * either as an associative array, or a string. If it is a string, be | 129 * either as an associative array, or a string. If it is a string, be |
130 * sure it is correctly URLEncoded. | 130 * sure it is correctly URLEncoded. |
131 * @param {Object.<string>=} opt_headers Additional headers to include on the | 131 * @param {Object<string>=} opt_headers Additional headers to include on the |
132 * request. | 132 * request. |
133 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 133 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
134 * XHR. | 134 * XHR. |
135 * @return {XMLHttpRequest} The XMLHttpRequest object. | 135 * @return {XMLHttpRequest} The XMLHttpRequest object. |
136 */ | 136 */ |
137 remoting.xhr.doMethod = function(methodName, url, onDone, | 137 remoting.xhr.doMethod = function(methodName, url, onDone, |
138 opt_parameters, opt_headers, | 138 opt_parameters, opt_headers, |
139 opt_withCredentials) { | 139 opt_withCredentials) { |
140 /** @type {XMLHttpRequest} */ | 140 /** @type {XMLHttpRequest} */ |
141 var xhr = new XMLHttpRequest(); | 141 var xhr = new XMLHttpRequest(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 var error = | 202 var error = |
203 remoting.Error.fromHttpStatus(/** @type {number} */ (xhr.status)); | 203 remoting.Error.fromHttpStatus(/** @type {number} */ (xhr.status)); |
204 if (error == remoting.Error.NONE) { | 204 if (error == remoting.Error.NONE) { |
205 onDone(); | 205 onDone(); |
206 } else { | 206 } else { |
207 onError(error); | 207 onError(error); |
208 } | 208 } |
209 }; | 209 }; |
210 return result; | 210 return result; |
211 }; | 211 }; |
OLD | NEW |