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.
5 * Copyright (C) 2012 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef XMLHttpRequest_h
23 #define XMLHttpRequest_h
25 #include "bindings/v8/ScriptString.h"
26 #include "bindings/v8/ScriptWrappable.h"
27 #include "core/dom/ActiveDOMObject.h"
28 #include "core/events/EventListener.h"
29 #include "core/loader/ThreadableLoaderClient.h"
30 #include "core/xml/XMLHttpRequestEventTarget.h"
31 #include "core/xml/XMLHttpRequestProgressEventThrottle.h"
32 #include "platform/AsyncMethodRunner.h"
33 #include "platform/heap/Handle.h"
34 #include "platform/network/FormData.h"
35 #include "platform/network/ResourceResponse.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/text/AtomicStringHash.h"
39 #include "wtf/text/StringBuilder.h"
47 class ResourceRequest;
51 class TextResourceDecoder;
52 class ThreadableLoader;
54 typedef int ExceptionCode;
56 class XMLHttpRequest FINAL : public RefCountedWillBeRefCountedGarbageCollected<XMLHttpRequest>, public ScriptWrappable, public XMLHttpRequestEventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
57 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
58 DEFINE_EVENT_TARGET_REFCOUNTING(RefCountedWillBeRefCountedGarbageCollected<XMLHttpRequest>);
60 static PassRefPtrWillBeRawPtr<XMLHttpRequest> create(ExecutionContext*, PassRefPtr<SecurityOrigin> = nullptr);
61 virtual ~XMLHttpRequest();
63 // These exact numeric values are important because JS expects them.
72 enum ResponseTypeCode {
78 ResponseTypeArrayBuffer,
87 virtual void contextDestroyed() OVERRIDE;
88 virtual void suspend() OVERRIDE;
89 virtual void resume() OVERRIDE;
90 virtual void stop() OVERRIDE;
92 virtual const AtomicString& interfaceName() const OVERRIDE;
93 virtual ExecutionContext* executionContext() const OVERRIDE;
95 const KURL& url() const { return m_url; }
96 String statusText() const;
98 State readyState() const;
99 bool withCredentials() const { return m_includeCredentials; }
100 void setWithCredentials(bool, ExceptionState&);
101 void open(const AtomicString& method, const KURL&, ExceptionState&);
102 void open(const AtomicString& method, const KURL&, bool async, ExceptionState&);
103 void open(const AtomicString& method, const KURL&, bool async, const String& user, ExceptionState&);
104 void open(const AtomicString& method, const KURL&, bool async, const String& user, const String& password, ExceptionState&);
105 void send(ExceptionState&);
106 void send(Document*, ExceptionState&);
107 void send(const String&, ExceptionState&);
108 void send(Blob*, ExceptionState&);
109 void send(DOMFormData*, ExceptionState&);
110 void send(ArrayBuffer*, ExceptionState&);
111 void send(ArrayBufferView*, ExceptionState&);
113 void setRequestHeader(const AtomicString& name, const AtomicString& value, ExceptionState&);
114 void overrideMimeType(const AtomicString& override);
115 String getAllResponseHeaders() const;
116 const AtomicString& getResponseHeader(const AtomicString&) const;
117 ScriptString responseText(ExceptionState&);
118 ScriptString responseJSONSource();
119 Document* responseXML(ExceptionState&);
120 Blob* responseBlob();
121 Stream* responseStream();
122 unsigned long timeout() const { return m_timeoutMilliseconds; }
123 void setTimeout(unsigned long timeout, ExceptionState&);
125 void sendForInspectorXHRReplay(PassRefPtr<FormData>, ExceptionState&);
127 // Expose HTTP validation methods for other untrusted requests.
128 static bool isAllowedHTTPMethod(const String&);
129 static AtomicString uppercaseKnownHTTPMethod(const AtomicString&);
130 static bool isAllowedHTTPHeader(const String&);
132 void setResponseType(const String&, ExceptionState&);
133 String responseType();
134 ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
136 // response attribute has custom getter.
137 ArrayBuffer* responseArrayBuffer();
139 void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
140 void setLastSendURL(const String& url) { m_lastSendURL = url; }
142 XMLHttpRequestUpload* upload();
144 DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange);
146 void trace(Visitor*);
149 XMLHttpRequest(ExecutionContext*, PassRefPtr<SecurityOrigin>);
151 Document* document() const;
152 SecurityOrigin* securityOrigin() const;
154 virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
155 virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&) OVERRIDE;
156 virtual void didReceiveData(const char* data, int dataLength) OVERRIDE;
157 virtual void didFinishLoading(unsigned long identifier, double finishTime) OVERRIDE;
158 virtual void didFail(const ResourceError&) OVERRIDE;
159 virtual void didFailRedirectCheck() OVERRIDE;
161 AtomicString responseMIMEType() const;
162 bool responseIsXML() const;
164 bool areMethodAndURLValidForSend();
166 bool initSend(ExceptionState&);
167 void sendBytesData(const void*, size_t, ExceptionState&);
169 const AtomicString& getRequestHeader(const AtomicString& name) const;
170 void setRequestHeaderInternal(const AtomicString& name, const AtomicString& value);
172 void trackProgress(int dataLength);
173 // Changes m_state and dispatches a readyStateChange event if new m_state
174 // value is different from last one.
175 void changeState(State newState);
176 void dispatchReadyStateChangeEvent();
178 void dropProtectionSoon();
179 void dropProtection();
180 // Clears variables used only while the resource is being loaded.
181 void clearVariablesForLoading();
182 // Returns false iff reentry happened and a new load is started.
183 bool internalAbort(DropProtection = DropProtectionSync);
184 // Clears variables holding response header and body data.
185 void clearResponse();
188 void createRequest(ExceptionState&);
190 // Dispatches an event of the specified type to m_progressEventThrottle.
191 void dispatchEventAndLoadEnd(const AtomicString&, long long, long long);
193 // Dispatches a response progress event to m_progressEventThrottle.
194 void dispatchThrottledProgressEvent(const AtomicString&, long long, long long);
196 // Dispatches a response progress event using values sampled from
197 // m_receivedLength and m_response.
198 void dispatchThrottledProgressEventSnapshot(const AtomicString&);
200 // Does clean up common for all kind of didFail() call.
201 void handleDidFailGeneric();
202 // Handles didFail() call not caused by cancellation or timeout.
203 void handleNetworkError();
204 // Handles didFail() call triggered by m_loader->cancel().
205 void handleDidCancel();
206 // Handles didFail() call for timeout.
207 void handleDidTimeout();
209 void handleRequestError(ExceptionCode, const AtomicString&, long long, long long);
211 OwnPtr<XMLHttpRequestUpload> m_upload;
214 AtomicString m_method;
215 HTTPHeaderMap m_requestHeaders;
216 RefPtr<FormData> m_requestEntityBody;
217 AtomicString m_mimeTypeOverride;
219 bool m_includeCredentials;
220 unsigned long m_timeoutMilliseconds;
221 RefPtrWillBeMember<Blob> m_responseBlob;
222 RefPtrWillBeMember<Stream> m_responseStream;
224 RefPtr<ThreadableLoader> m_loader;
227 ResourceResponse m_response;
228 String m_responseEncoding;
230 OwnPtr<TextResourceDecoder> m_decoder;
232 ScriptString m_responseText;
233 // Used to skip m_responseDocument creation if it's done previously. We need
234 // this separate flag since m_responseDocument can be 0 for some cases.
235 bool m_createdDocument;
236 RefPtr<Document> m_responseDocument;
238 RefPtr<SharedBuffer> m_binaryResponseBuilder;
239 RefPtr<ArrayBuffer> m_responseArrayBuffer;
243 bool m_uploadEventsAllowed;
244 bool m_uploadComplete;
246 bool m_sameOriginRequest;
248 // Used for onprogress tracking
249 long long m_receivedLength;
251 unsigned m_lastSendLineNumber;
252 String m_lastSendURL;
253 // An exception to throw in synchronous mode. It's set when failure
254 // notification is received from m_loader and thrown at the end of send() if
256 ExceptionCode m_exceptionCode;
258 XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
260 // An enum corresponding to the allowed string values for the responseType attribute.
261 ResponseTypeCode m_responseTypeCode;
262 AsyncMethodRunner<XMLHttpRequest> m_dropProtectionRunner;
263 RefPtr<SecurityOrigin> m_securityOrigin;
266 } // namespace WebCore
268 #endif // XMLHttpRequest_h