Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / xml / XMLHttpRequest.h
1 /*
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
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #ifndef XMLHttpRequest_h
23 #define XMLHttpRequest_h
24
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"
40
41 namespace WebCore {
42
43 class Blob;
44 class DOMFormData;
45 class Document;
46 class ExceptionState;
47 class ResourceRequest;
48 class SecurityOrigin;
49 class SharedBuffer;
50 class Stream;
51 class TextResourceDecoder;
52 class ThreadableLoader;
53
54 typedef int ExceptionCode;
55
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>);
59 public:
60     static PassRefPtrWillBeRawPtr<XMLHttpRequest> create(ExecutionContext*, PassRefPtr<SecurityOrigin> = nullptr);
61     virtual ~XMLHttpRequest();
62
63     // These exact numeric values are important because JS expects them.
64     enum State {
65         UNSENT = 0,
66         OPENED = 1,
67         HEADERS_RECEIVED = 2,
68         LOADING = 3,
69         DONE = 4
70     };
71
72     enum ResponseTypeCode {
73         ResponseTypeDefault,
74         ResponseTypeText,
75         ResponseTypeJSON,
76         ResponseTypeDocument,
77         ResponseTypeBlob,
78         ResponseTypeArrayBuffer,
79         ResponseTypeStream
80     };
81
82     enum DropProtection {
83         DropProtectionSync,
84         DropProtectionAsync,
85     };
86
87     virtual void contextDestroyed() OVERRIDE;
88     virtual void suspend() OVERRIDE;
89     virtual void resume() OVERRIDE;
90     virtual void stop() OVERRIDE;
91
92     virtual const AtomicString& interfaceName() const OVERRIDE;
93     virtual ExecutionContext* executionContext() const OVERRIDE;
94
95     const KURL& url() const { return m_url; }
96     String statusText() const;
97     int status() 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&);
112     void abort();
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&);
124
125     void sendForInspectorXHRReplay(PassRefPtr<FormData>, ExceptionState&);
126
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&);
131
132     void setResponseType(const String&, ExceptionState&);
133     String responseType();
134     ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
135
136     // response attribute has custom getter.
137     ArrayBuffer* responseArrayBuffer();
138
139     void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
140     void setLastSendURL(const String& url) { m_lastSendURL = url; }
141
142     XMLHttpRequestUpload* upload();
143
144     DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange);
145
146     void trace(Visitor*);
147
148 private:
149     XMLHttpRequest(ExecutionContext*, PassRefPtr<SecurityOrigin>);
150
151     Document* document() const;
152     SecurityOrigin* securityOrigin() const;
153
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;
160
161     AtomicString responseMIMEType() const;
162     bool responseIsXML() const;
163
164     bool areMethodAndURLValidForSend();
165
166     bool initSend(ExceptionState&);
167     void sendBytesData(const void*, size_t, ExceptionState&);
168
169     const AtomicString& getRequestHeader(const AtomicString& name) const;
170     void setRequestHeaderInternal(const AtomicString& name, const AtomicString& value);
171
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();
177
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();
186     void clearRequest();
187
188     void createRequest(ExceptionState&);
189
190     // Dispatches an event of the specified type to m_progressEventThrottle.
191     void dispatchEventAndLoadEnd(const AtomicString&, long long, long long);
192
193     // Dispatches a response progress event to m_progressEventThrottle.
194     void dispatchThrottledProgressEvent(const AtomicString&, long long, long long);
195
196     // Dispatches a response progress event using values sampled from
197     // m_receivedLength and m_response.
198     void dispatchThrottledProgressEventSnapshot(const AtomicString&);
199
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();
208
209     void handleRequestError(ExceptionCode, const AtomicString&, long long, long long);
210
211     OwnPtr<XMLHttpRequestUpload> m_upload;
212
213     KURL m_url;
214     AtomicString m_method;
215     HTTPHeaderMap m_requestHeaders;
216     RefPtr<FormData> m_requestEntityBody;
217     AtomicString m_mimeTypeOverride;
218     bool m_async;
219     bool m_includeCredentials;
220     unsigned long m_timeoutMilliseconds;
221     RefPtrWillBeMember<Blob> m_responseBlob;
222     RefPtrWillBeMember<Stream> m_responseStream;
223
224     RefPtr<ThreadableLoader> m_loader;
225     State m_state;
226
227     ResourceResponse m_response;
228     String m_responseEncoding;
229
230     OwnPtr<TextResourceDecoder> m_decoder;
231
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;
237
238     RefPtr<SharedBuffer> m_binaryResponseBuilder;
239     RefPtr<ArrayBuffer> m_responseArrayBuffer;
240
241     bool m_error;
242
243     bool m_uploadEventsAllowed;
244     bool m_uploadComplete;
245
246     bool m_sameOriginRequest;
247
248     // Used for onprogress tracking
249     long long m_receivedLength;
250
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
255     // any.
256     ExceptionCode m_exceptionCode;
257
258     XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
259
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;
264 };
265
266 } // namespace WebCore
267
268 #endif // XMLHttpRequest_h