initial import
[vuplus_webkit] / Source / WebCore / 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  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifndef XMLHttpRequest_h
22 #define XMLHttpRequest_h
23
24 #include "ActiveDOMObject.h"
25 #include "EventListener.h"
26 #include "EventNames.h"
27 #include "EventTarget.h"
28 #include "FormData.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>
35
36 namespace WebCore {
37
38 class ArrayBuffer;
39 class Blob;
40 class Document;
41 class DOMFormData;
42 class ResourceRequest;
43 class SecurityOrigin;
44 class SharedBuffer;
45 class TextResourceDecoder;
46 class ThreadableLoader;
47
48 class XMLHttpRequest : public RefCounted<XMLHttpRequest>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
49     WTF_MAKE_FAST_ALLOCATED;
50 public:
51     static PassRefPtr<XMLHttpRequest> create(ScriptExecutionContext*, PassRefPtr<SecurityOrigin> = 0);
52     ~XMLHttpRequest();
53
54     // These exact numeric values are important because JS expects them.
55     enum State {
56         UNSENT = 0,
57         OPENED = 1,
58         HEADERS_RECEIVED = 2,
59         LOADING = 3,
60         DONE = 4
61     };
62     
63     enum ResponseTypeCode {
64         ResponseTypeDefault,
65         ResponseTypeText, 
66         ResponseTypeDocument,
67         ResponseTypeBlob,
68         ResponseTypeArrayBuffer
69     };
70
71     virtual XMLHttpRequest* toXMLHttpRequest() { return this; }
72
73     virtual void contextDestroyed();
74     virtual bool canSuspend() const;
75     virtual void suspend(ReasonForSuspension);
76     virtual void resume();
77     virtual void stop();
78
79     virtual ScriptExecutionContext* scriptExecutionContext() const;
80
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&);
90 #endif
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&);
101     void abort();
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(); }
112 #endif
113
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&);
118
119     void setResponseType(const String&, ExceptionCode&);
120     String responseType();
121     ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
122     
123     // response attribute has custom getter.
124     ArrayBuffer* responseArrayBuffer(ExceptionCode&);
125     ArrayBuffer* optionalResponseArrayBuffer() const { return m_responseArrayBuffer.get(); }
126
127     void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
128     void setLastSendURL(const String& url) { m_lastSendURL = url; }
129
130     XMLHttpRequestUpload* upload();
131     XMLHttpRequestUpload* optionalUpload() const { return m_upload.get(); }
132
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);
139
140     using RefCounted<XMLHttpRequest>::ref;
141     using RefCounted<XMLHttpRequest>::deref;
142
143 private:
144     XMLHttpRequest(ScriptExecutionContext*, PassRefPtr<SecurityOrigin>);
145
146     virtual void refEventTarget() { ref(); }
147     virtual void derefEventTarget() { deref(); }
148     virtual EventTargetData* eventTargetData();
149     virtual EventTargetData* ensureEventTargetData();
150
151     Document* document() const;
152     SecurityOrigin* securityOrigin() const;
153
154 #if ENABLE(DASHBOARD_SUPPORT)
155     bool usesDashboardBackwardCompatibilityMode() const;
156 #endif
157
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();
164
165     String responseMIMEType() const;
166     bool responseIsXML() const;
167
168     bool initSend(ExceptionCode&);
169
170     String getRequestHeader(const AtomicString& name) const;
171     void setRequestHeaderInternal(const AtomicString& name, const String& value);
172
173     void changeState(State newState);
174     void callReadyStateChangeListener();
175     void dropProtection();
176     void internalAbort();
177     void clearResponse();
178     void clearResponseBuffers();
179     void clearRequest();
180
181     void createRequest(ExceptionCode&);
182
183     void genericError();
184     void networkError();
185     void abortError();
186
187     OwnPtr<XMLHttpRequestUpload> m_upload;
188
189     KURL m_url;
190     String m_method;
191     HTTPHeaderMap m_requestHeaders;
192     RefPtr<FormData> m_requestEntityBody;
193     String m_mimeTypeOverride;
194     bool m_async;
195     bool m_includeCredentials;
196 #if ENABLE(XHR_RESPONSE_BLOB)
197     RefPtr<Blob> m_responseBlob;
198 #endif
199
200     RefPtr<ThreadableLoader> m_loader;
201     State m_state;
202
203     ResourceResponse m_response;
204     String m_responseEncoding;
205
206     RefPtr<TextResourceDecoder> m_decoder;
207
208     StringBuilder m_responseBuilder;
209     mutable bool m_createdDocument;
210     mutable RefPtr<Document> m_responseXML;
211     
212     RefPtr<SharedBuffer> m_binaryResponseBuilder;
213     mutable RefPtr<ArrayBuffer> m_responseArrayBuffer;
214
215     bool m_error;
216
217     bool m_uploadEventsAllowed;
218     bool m_uploadComplete;
219
220     bool m_sameOriginRequest;
221
222     // Used for onprogress tracking
223     long long m_receivedLength;
224
225     unsigned m_lastSendLineNumber;
226     String m_lastSendURL;
227     ExceptionCode m_exceptionCode;
228
229     EventTargetData m_eventTargetData;
230
231     XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
232
233     // An enum corresponding to the allowed string values for the responseType attribute.
234     ResponseTypeCode m_responseTypeCode;
235
236     RefPtr<SecurityOrigin> m_securityOrigin;
237 };
238
239 } // namespace WebCore
240
241 #endif // XMLHttpRequest_h