2 * Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@nypop.com>
4 * Copyright (C) 2011 Google Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef XMLHttpRequest_h
22 #define XMLHttpRequest_h
24 #include "ActiveDOMObject.h"
25 #include "EventListener.h"
26 #include "EventNames.h"
27 #include "EventTarget.h"
29 #include "ResourceResponse.h"
30 #include "ThreadableLoaderClient.h"
31 #include "XMLHttpRequestProgressEventThrottle.h"
32 #include <wtf/OwnPtr.h>
33 #include <wtf/text/AtomicStringHash.h>
34 #include <wtf/text/StringBuilder.h>
42 class ResourceRequest;
45 class TextResourceDecoder;
46 class ThreadableLoader;
48 class XMLHttpRequest : public RefCounted<XMLHttpRequest>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
49 WTF_MAKE_FAST_ALLOCATED;
51 static PassRefPtr<XMLHttpRequest> create(ScriptExecutionContext*, PassRefPtr<SecurityOrigin> = 0);
54 // These exact numeric values are important because JS expects them.
63 enum ResponseTypeCode {
68 ResponseTypeArrayBuffer
71 virtual XMLHttpRequest* toXMLHttpRequest() { return this; }
73 virtual void contextDestroyed();
74 virtual bool canSuspend() const;
75 virtual void suspend(ReasonForSuspension);
76 virtual void resume();
79 virtual ScriptExecutionContext* scriptExecutionContext() const;
81 const KURL& url() const { return m_url; }
82 String statusText(ExceptionCode&) const;
83 int status(ExceptionCode&) const;
84 State readyState() const;
85 bool withCredentials() const { return m_includeCredentials; }
86 void setWithCredentials(bool, ExceptionCode&);
87 #if ENABLE(XHR_RESPONSE_BLOB)
88 bool asBlob() const { return responseTypeCode() == ResponseTypeBlob; }
89 void setAsBlob(bool, ExceptionCode&);
91 void open(const String& method, const KURL&, ExceptionCode&);
92 void open(const String& method, const KURL&, bool async, ExceptionCode&);
93 void open(const String& method, const KURL&, bool async, const String& user, ExceptionCode&);
94 void open(const String& method, const KURL&, bool async, const String& user, const String& password, ExceptionCode&);
95 void send(ExceptionCode&);
96 void send(Document*, ExceptionCode&);
97 void send(const String&, ExceptionCode&);
98 void send(Blob*, ExceptionCode&);
99 void send(DOMFormData*, ExceptionCode&);
100 void send(ArrayBuffer*, ExceptionCode&);
102 void setRequestHeader(const AtomicString& name, const String& value, ExceptionCode&);
103 void overrideMimeType(const String& override);
104 String getAllResponseHeaders(ExceptionCode&) const;
105 String getResponseHeader(const AtomicString& name, ExceptionCode&) const;
106 String responseText(ExceptionCode&);
107 Document* responseXML(ExceptionCode&);
108 Document* optionalResponseXML() const { return m_responseXML.get(); }
109 #if ENABLE(XHR_RESPONSE_BLOB)
110 Blob* responseBlob(ExceptionCode&) const;
111 Blob* optionalResponseBlob() const { return m_responseBlob.get(); }
114 // Expose HTTP validation methods for other untrusted requests.
115 static bool isAllowedHTTPMethod(const String&);
116 static String uppercaseKnownHTTPMethod(const String&);
117 static bool isAllowedHTTPHeader(const String&);
119 void setResponseType(const String&, ExceptionCode&);
120 String responseType();
121 ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
123 // response attribute has custom getter.
124 ArrayBuffer* responseArrayBuffer(ExceptionCode&);
125 ArrayBuffer* optionalResponseArrayBuffer() const { return m_responseArrayBuffer.get(); }
127 void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
128 void setLastSendURL(const String& url) { m_lastSendURL = url; }
130 XMLHttpRequestUpload* upload();
131 XMLHttpRequestUpload* optionalUpload() const { return m_upload.get(); }
133 DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange);
134 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
135 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
136 DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
137 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
138 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
140 using RefCounted<XMLHttpRequest>::ref;
141 using RefCounted<XMLHttpRequest>::deref;
144 XMLHttpRequest(ScriptExecutionContext*, PassRefPtr<SecurityOrigin>);
146 virtual void refEventTarget() { ref(); }
147 virtual void derefEventTarget() { deref(); }
148 virtual EventTargetData* eventTargetData();
149 virtual EventTargetData* ensureEventTargetData();
151 Document* document() const;
152 SecurityOrigin* securityOrigin() const;
154 #if ENABLE(DASHBOARD_SUPPORT)
155 bool usesDashboardBackwardCompatibilityMode() const;
158 virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
159 virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&);
160 virtual void didReceiveData(const char* data, int dataLength);
161 virtual void didFinishLoading(unsigned long identifier, double finishTime);
162 virtual void didFail(const ResourceError&);
163 virtual void didFailRedirectCheck();
165 String responseMIMEType() const;
166 bool responseIsXML() const;
168 bool initSend(ExceptionCode&);
170 String getRequestHeader(const AtomicString& name) const;
171 void setRequestHeaderInternal(const AtomicString& name, const String& value);
173 void changeState(State newState);
174 void callReadyStateChangeListener();
175 void dropProtection();
176 void internalAbort();
177 void clearResponse();
178 void clearResponseBuffers();
181 void createRequest(ExceptionCode&);
187 OwnPtr<XMLHttpRequestUpload> m_upload;
191 HTTPHeaderMap m_requestHeaders;
192 RefPtr<FormData> m_requestEntityBody;
193 String m_mimeTypeOverride;
195 bool m_includeCredentials;
196 #if ENABLE(XHR_RESPONSE_BLOB)
197 RefPtr<Blob> m_responseBlob;
200 RefPtr<ThreadableLoader> m_loader;
203 ResourceResponse m_response;
204 String m_responseEncoding;
206 RefPtr<TextResourceDecoder> m_decoder;
208 StringBuilder m_responseBuilder;
209 mutable bool m_createdDocument;
210 mutable RefPtr<Document> m_responseXML;
212 RefPtr<SharedBuffer> m_binaryResponseBuilder;
213 mutable RefPtr<ArrayBuffer> m_responseArrayBuffer;
217 bool m_uploadEventsAllowed;
218 bool m_uploadComplete;
220 bool m_sameOriginRequest;
222 // Used for onprogress tracking
223 long long m_receivedLength;
225 unsigned m_lastSendLineNumber;
226 String m_lastSendURL;
227 ExceptionCode m_exceptionCode;
229 EventTargetData m_eventTargetData;
231 XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
233 // An enum corresponding to the allowed string values for the responseType attribute.
234 ResponseTypeCode m_responseTypeCode;
236 RefPtr<SecurityOrigin> m_securityOrigin;
239 } // namespace WebCore
241 #endif // XMLHttpRequest_h