nsIXMLHttpRequest.idl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsIDOMEventTarget.idl"
  6. interface nsIChannel;
  7. interface nsIDOMDocument;
  8. interface nsIDOMEventListener;
  9. interface nsILoadGroup;
  10. interface nsIPrincipal;
  11. interface nsIScriptContext;
  12. interface nsIURI;
  13. interface nsIVariant;
  14. interface nsIGlobalObject;
  15. interface nsIInputStream;
  16. interface nsIDOMBlob;
  17. [builtinclass, uuid(88e7d2a0-2e5b-4f65-9624-a61e607a9948)]
  18. interface nsIXMLHttpRequestEventTarget : nsIDOMEventTarget {
  19. // event handler attributes
  20. };
  21. [builtinclass, uuid(d74c4dc4-bc8c-4f5d-b7f1-121a48750abe)]
  22. interface nsIXMLHttpRequestUpload : nsIXMLHttpRequestEventTarget {
  23. // for future use
  24. };
  25. /**
  26. * Mozilla's XMLHttpRequest is modelled after Microsoft's IXMLHttpRequest
  27. * object. The goal has been to make Mozilla's version match Microsoft's
  28. * version as closely as possible, but there are bound to be some differences.
  29. *
  30. * In general, Microsoft's documentation for IXMLHttpRequest can be used.
  31. * Mozilla's interface definitions provide some additional documentation. The
  32. * web page to look at is http://www.mozilla.org/xmlextras/
  33. *
  34. * Mozilla's XMLHttpRequest object can be created in JavaScript like this:
  35. * new XMLHttpRequest()
  36. * compare to Internet Explorer:
  37. * new ActiveXObject("Msxml2.XMLHTTP")
  38. *
  39. * From JavaScript, the methods and properties visible in the XMLHttpRequest
  40. * object are a combination of nsIXMLHttpRequest and nsIJSXMLHttpRequest;
  41. * there is no need to differentiate between those interfaces.
  42. *
  43. * From native code, the way to set up onload and onerror handlers is a bit
  44. * different. Here is a comment from Johnny Stenback <jst@netscape.com>:
  45. *
  46. * The mozilla implementation of nsIXMLHttpRequest implements the interface
  47. * nsIDOMEventTarget and that's how you're supported to add event listeners.
  48. * Try something like this:
  49. *
  50. * nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(myxmlhttpreq));
  51. *
  52. * target->AddEventListener(NS_LITERAL_STRING("load"), mylistener,
  53. * PR_FALSE)
  54. *
  55. * where mylistener is your event listener object that implements the
  56. * interface nsIDOMEventListener.
  57. *
  58. * The 'onload', 'onerror', and 'onreadystatechange' attributes moved to
  59. * nsIJSXMLHttpRequest, but if you're coding in C++ you should avoid using
  60. * those.
  61. *
  62. * Conclusion: Do not use event listeners on XMLHttpRequest from C++, unless
  63. * you're aware of all the security implications. And then think twice about
  64. * it.
  65. */
  66. [scriptable, uuid(6f54214c-7175-498d-9d2d-0429e38c2869)]
  67. interface nsIXMLHttpRequest : nsISupports
  68. {
  69. /**
  70. * The request uses a channel in order to perform the
  71. * request. This attribute represents the channel used
  72. * for the request. NULL if the channel has not yet been
  73. * created.
  74. *
  75. * Mozilla only. Requires elevated privileges to access.
  76. */
  77. readonly attribute nsIChannel channel;
  78. /**
  79. * The response to the request is parsed as if it were a
  80. * text/xml stream. This attributes represents the response as
  81. * a DOM Document object. NULL if the request is unsuccessful or
  82. * has not yet been sent.
  83. */
  84. readonly attribute nsIDOMDocument responseXML;
  85. /**
  86. * The response to the request as text.
  87. * NULL if the request is unsuccessful or
  88. * has not yet been sent.
  89. */
  90. readonly attribute AString responseText;
  91. /**
  92. * Determine a response format which response attribute returns.
  93. * empty string (initial value) or "text": as text.
  94. * "arraybuffer": as a typed array ArrayBuffer.
  95. * "blob": as a File API Blob.
  96. * "document": as a DOM Document object.
  97. */
  98. attribute AString responseType;
  99. /**
  100. * The response to the request as a specified format by responseType.
  101. * NULL if the request is unsuccessful or
  102. * has not yet been sent.
  103. */
  104. [implicit_jscontext] readonly attribute jsval /* any */ response;
  105. /**
  106. * The status of the response to the request for HTTP requests.
  107. */
  108. // XXX spec says unsigned short
  109. readonly attribute unsigned long status;
  110. /**
  111. * The string representing the status of the response for
  112. * HTTP requests.
  113. */
  114. readonly attribute ACString statusText;
  115. /**
  116. * If the request has been sent already, this method will
  117. * abort the request.
  118. */
  119. [binaryname(SlowAbort)] void abort();
  120. /**
  121. * Returns all of the response headers as a string for HTTP
  122. * requests.
  123. *
  124. * @returns A string containing all of the response headers.
  125. * The empty string if the response has not yet been received.
  126. */
  127. ACString getAllResponseHeaders();
  128. /**
  129. * Returns the text of the header with the specified name for
  130. * HTTP requests.
  131. *
  132. * @param header The name of the header to retrieve
  133. * @returns A string containing the text of the header specified.
  134. * NULL if the response has not yet been received or the
  135. * header does not exist in the response.
  136. */
  137. ACString getResponseHeader(in ACString header);
  138. %{C++
  139. // note this is NOT virtual so this won't muck with the vtable!
  140. inline nsresult Open(const nsACString& method, const nsACString& url,
  141. bool async, const nsAString& user,
  142. const nsAString& password) {
  143. return Open(method, url, async, user, password, 3);
  144. }
  145. %}
  146. /**
  147. * Meant to be a script-only method for initializing a request.
  148. *
  149. * If there is an "active" request (that is, if open() has been called
  150. * already), this is equivalent to calling abort() and then open().
  151. *
  152. * @param method The HTTP method - either "POST" or "GET". Ignored
  153. * if the URL is not a HTTP URL.
  154. * @param url The URL to which to send the request.
  155. * @param async (optional) Whether the request is synchronous or
  156. * asynchronous i.e. whether send returns only after
  157. * the response is received or if it returns immediately after
  158. * sending the request. In the latter case, notification
  159. * of completion is sent through the event listeners.
  160. * The default value is true.
  161. * @param user (optional) A username for authentication if necessary.
  162. * The default value is the empty string
  163. * @param password (optional) A password for authentication if necessary.
  164. * The default value is the empty string
  165. */
  166. [optional_argc] void open(in ACString method, in AUTF8String url,
  167. [optional] in boolean async,
  168. [optional,Undefined(Empty)] in DOMString user,
  169. [optional,Undefined(Empty)] in DOMString password);
  170. /**
  171. * Sends the request. If the request is asynchronous, returns
  172. * immediately after sending the request. If it is synchronous
  173. * returns only after the response has been received.
  174. *
  175. * All event listeners must be set before calling send().
  176. *
  177. * After the initial response, all event listeners will be cleared.
  178. * // XXXbz what does that mean, exactly?
  179. *
  180. * @param body Either an instance of nsIDOMDocument, nsIInputStream
  181. * or a string (nsISupportsString in the native calling
  182. * case). This is used to populate the body of the
  183. * HTTP request if the HTTP request method is "POST".
  184. * If the parameter is a nsIDOMDocument, it is serialized.
  185. * If the parameter is a nsIInputStream, then it must be
  186. * compatible with nsIUploadChannel.setUploadStream, and a
  187. * Content-Length header will be added to the HTTP request
  188. * with a value given by nsIInputStream.available. Any
  189. * headers included at the top of the stream will be
  190. * treated as part of the message body. The MIME type of
  191. * the stream should be specified by setting the Content-
  192. * Type header via the setRequestHeader method before
  193. * calling send.
  194. */
  195. void send([optional] in nsIVariant body);
  196. /**
  197. * Sets a HTTP request header for HTTP requests. You must call open
  198. * before setting the request headers.
  199. *
  200. * @param header The name of the header to set in the request.
  201. * @param value The body of the header.
  202. */
  203. void setRequestHeader(in ACString header, in ACString value);
  204. /**
  205. * The amount of milliseconds a request can take before being terminated.
  206. * Initially zero. Zero means there is no timeout.
  207. */
  208. attribute unsigned long timeout;
  209. /**
  210. * The state of the request.
  211. *
  212. * Possible values:
  213. * 0 UNSENT open() has not been called yet.
  214. * 1 OPENED send() has not been called yet.
  215. * 2 HEADERS_RECEIVED
  216. * send() has been called, headers and status are available.
  217. * 3 LOADING Downloading, responseText holds the partial data.
  218. * 4 DONE Finished with all operations.
  219. */
  220. const unsigned short UNSENT = 0;
  221. const unsigned short OPENED = 1;
  222. const unsigned short HEADERS_RECEIVED = 2;
  223. const unsigned short LOADING = 3;
  224. const unsigned short DONE = 4;
  225. readonly attribute unsigned short readyState;
  226. /**
  227. * Override the mime type returned by the server (if any). This may
  228. * be used, for example, to force a stream to be treated and parsed
  229. * as text/xml, even if the server does not report it as such. This
  230. * must be done before the <code>send</code> method is invoked.
  231. *
  232. * @param mimetype The type used to override that returned by the server
  233. * (if any).
  234. */
  235. [binaryname(SlowOverrideMimeType)] void overrideMimeType(in DOMString mimetype);
  236. /**
  237. * Set to true if this is a background service request. This will
  238. * prevent a load group being associated with the request, and
  239. * suppress any security dialogs from being shown * to the user.
  240. * In the cases where one of those dialogs would be shown, the request
  241. * will simply fail instead.
  242. */
  243. attribute boolean mozBackgroundRequest;
  244. /**
  245. * When set to true attempts to make cross-site Access-Control requests
  246. * with credentials such as cookies and authorization headers.
  247. *
  248. * Never affects same-site requests.
  249. *
  250. * Defaults to false.
  251. */
  252. attribute boolean withCredentials;
  253. /**
  254. * Initialize the object for use from C++ code with the principal, script
  255. * context, and owner window that should be used.
  256. *
  257. * @param principal The principal to use for the request. This must not be
  258. * null.
  259. * @param globalObject The associated global for the request. Can be the
  260. * outer window, a sandbox, or a backstage pass.
  261. * May be null, but then the request cannot create a
  262. * document.
  263. * @param baseURI The base URI to use when resolving relative URIs. May be
  264. * null.
  265. * @param loadGroup An optional load group to use when performing the request.
  266. * This will be used even if the global has a window with a
  267. * load group.
  268. */
  269. [noscript] void init(in nsIPrincipal principal,
  270. in nsIGlobalObject globalObject,
  271. in nsIURI baseURI,
  272. [optional] in nsILoadGroup loadGroup);
  273. /**
  274. * Upload process can be tracked by adding event listener to |upload|.
  275. */
  276. readonly attribute nsIXMLHttpRequestUpload upload;
  277. /**
  278. * Meant to be a script-only mechanism for setting a callback function.
  279. * The attribute is expected to be JavaScript function object. When the
  280. * readyState changes, the callback function will be called.
  281. * This attribute should not be used from native code!!
  282. *
  283. * After the initial response, all event listeners will be cleared.
  284. * // XXXbz what does that mean, exactly?
  285. *
  286. * Call open() before setting an onreadystatechange listener.
  287. */
  288. [implicit_jscontext] attribute jsval onreadystatechange;
  289. /**
  290. * If true, the request will be sent without cookie and authentication
  291. * headers.
  292. */
  293. readonly attribute boolean mozAnon;
  294. /**
  295. * If true, the same origin policy will not be enforced on the request.
  296. */
  297. readonly attribute boolean mozSystem;
  298. };
  299. [uuid(840d0d00-e83e-4a29-b3c7-67e96e90a499)]
  300. interface nsIXHRSendable : nsISupports {
  301. void getSendInfo(out nsIInputStream body,
  302. out uint64_t contentLength,
  303. out ACString contentType,
  304. out ACString charset);
  305. };
  306. /**
  307. * @deprecated
  308. */
  309. [scriptable, uuid(8ae70a39-edf1-40b4-a992-472d23421c25)]
  310. interface nsIJSXMLHttpRequest : nsISupports {
  311. };
  312. %{ C++
  313. #define NS_XMLHTTPREQUEST_CID \
  314. { /* d164e770-4157-11d4-9a42-000064657374 */ \
  315. 0xd164e770, 0x4157, 0x11d4, \
  316. {0x9a, 0x42, 0x00, 0x00, 0x64, 0x65, 0x73, 0x74} }
  317. #define NS_XMLHTTPREQUEST_CONTRACTID \
  318. "@mozilla.org/xmlextras/xmlhttprequest;1"
  319. %}