| OLD | NEW |
| (Empty) |
| 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 U
SA | |
| 20 */ | |
| 21 | |
| 22 #ifndef XMLHttpRequest_h | |
| 23 #define XMLHttpRequest_h | |
| 24 | |
| 25 #include "bindings/core/v8/ScriptString.h" | |
| 26 #include "core/dom/ActiveDOMObject.h" | |
| 27 #include "core/dom/DocumentParserClient.h" | |
| 28 #include "core/events/EventListener.h" | |
| 29 #include "core/loader/ThreadableLoaderClient.h" | |
| 30 #include "core/streams/ReadableStreamImpl.h" | |
| 31 #include "core/xml/XMLHttpRequestEventTarget.h" | |
| 32 #include "core/xml/XMLHttpRequestProgressEventThrottle.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/Forward.h" | |
| 38 #include "wtf/OwnPtr.h" | |
| 39 #include "wtf/text/AtomicStringHash.h" | |
| 40 #include "wtf/text/StringBuilder.h" | |
| 41 | |
| 42 namespace blink { | |
| 43 | |
| 44 class Blob; | |
| 45 class BlobDataHandle; | |
| 46 class DOMArrayBuffer; | |
| 47 class DOMFormData; | |
| 48 class Document; | |
| 49 class DocumentParser; | |
| 50 class ExceptionState; | |
| 51 class ResourceRequest; | |
| 52 class SecurityOrigin; | |
| 53 class SharedBuffer; | |
| 54 class Stream; | |
| 55 class TextResourceDecoder; | |
| 56 class ThreadableLoader; | |
| 57 class UnderlyingSource; | |
| 58 class XMLHttpRequestUpload; | |
| 59 | |
| 60 typedef int ExceptionCode; | |
| 61 | |
| 62 class XMLHttpRequest final | |
| 63 : public RefCountedWillBeGarbageCollectedFinalized<XMLHttpRequest> | |
| 64 , public XMLHttpRequestEventTarget | |
| 65 , private ThreadableLoaderClient | |
| 66 , public DocumentParserClient | |
| 67 , public ActiveDOMObject { | |
| 68 DEFINE_WRAPPERTYPEINFO(); | |
| 69 REFCOUNTED_EVENT_TARGET(XMLHttpRequest); | |
| 70 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(XMLHttpRequest); | |
| 71 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED; | |
| 72 public: | |
| 73 static PassRefPtrWillBeRawPtr<XMLHttpRequest> create(ExecutionContext*, Pass
RefPtr<SecurityOrigin> = nullptr); | |
| 74 virtual ~XMLHttpRequest(); | |
| 75 | |
| 76 // These exact numeric values are important because JS expects them. | |
| 77 enum State { | |
| 78 UNSENT = 0, | |
| 79 OPENED = 1, | |
| 80 HEADERS_RECEIVED = 2, | |
| 81 LOADING = 3, | |
| 82 DONE = 4 | |
| 83 }; | |
| 84 | |
| 85 enum ResponseTypeCode { | |
| 86 ResponseTypeDefault, | |
| 87 ResponseTypeText, | |
| 88 ResponseTypeJSON, | |
| 89 ResponseTypeDocument, | |
| 90 ResponseTypeBlob, | |
| 91 ResponseTypeArrayBuffer, | |
| 92 ResponseTypeLegacyStream, | |
| 93 ResponseTypeStream, | |
| 94 }; | |
| 95 | |
| 96 // ActiveDOMObject | |
| 97 virtual void contextDestroyed() override; | |
| 98 virtual ExecutionContext* executionContext() const override; | |
| 99 virtual bool hasPendingActivity() const override; | |
| 100 virtual void suspend() override; | |
| 101 virtual void resume() override; | |
| 102 virtual void stop() override; | |
| 103 | |
| 104 // XMLHttpRequestEventTarget | |
| 105 virtual const AtomicString& interfaceName() const override; | |
| 106 | |
| 107 // JavaScript attributes and methods | |
| 108 const KURL& url() const { return m_url; } | |
| 109 String statusText() const; | |
| 110 int status() const; | |
| 111 State readyState() const; | |
| 112 bool withCredentials() const { return m_includeCredentials; } | |
| 113 void setWithCredentials(bool, ExceptionState&); | |
| 114 void open(const AtomicString& method, const KURL&, ExceptionState&); | |
| 115 void open(const AtomicString& method, const KURL&, bool async, ExceptionStat
e&); | |
| 116 void open(const AtomicString& method, const KURL&, bool async, const String&
user, ExceptionState&); | |
| 117 void open(const AtomicString& method, const KURL&, bool async, const String&
user, const String& password, ExceptionState&); | |
| 118 void send(ExceptionState&); | |
| 119 void send(Document*, ExceptionState&); | |
| 120 void send(const String&, ExceptionState&); | |
| 121 void send(Blob*, ExceptionState&); | |
| 122 void send(DOMFormData*, ExceptionState&); | |
| 123 void send(ArrayBuffer*, ExceptionState&); | |
| 124 void send(ArrayBufferView*, ExceptionState&); | |
| 125 void abort(); | |
| 126 void setRequestHeader(const AtomicString& name, const AtomicString& value, E
xceptionState&); | |
| 127 void overrideMimeType(const AtomicString& override, ExceptionState&); | |
| 128 String getAllResponseHeaders() const; | |
| 129 const AtomicString& getResponseHeader(const AtomicString&) const; | |
| 130 ScriptString responseText(ExceptionState&); | |
| 131 ScriptString responseJSONSource(); | |
| 132 Document* responseXML(ExceptionState&); | |
| 133 Blob* responseBlob(); | |
| 134 DOMArrayBuffer* responseArrayBuffer(); | |
| 135 Stream* responseLegacyStream(); | |
| 136 ReadableStream* responseStream(); | |
| 137 unsigned long timeout() const { return m_timeoutMilliseconds; } | |
| 138 void setTimeout(unsigned long timeout, ExceptionState&); | |
| 139 ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; } | |
| 140 String responseType(); | |
| 141 void setResponseType(const String&, ExceptionState&); | |
| 142 String responseURL(); | |
| 143 | |
| 144 // For Inspector. | |
| 145 void sendForInspectorXHRReplay(PassRefPtr<FormData>, ExceptionState&); | |
| 146 | |
| 147 // Expose HTTP validation methods for other untrusted requests. | |
| 148 static AtomicString uppercaseKnownHTTPMethod(const AtomicString&); | |
| 149 | |
| 150 XMLHttpRequestUpload* upload(); | |
| 151 | |
| 152 DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange); | |
| 153 | |
| 154 virtual void trace(Visitor*) override; | |
| 155 | |
| 156 private: | |
| 157 class BlobLoader; | |
| 158 XMLHttpRequest(ExecutionContext*, PassRefPtr<SecurityOrigin>); | |
| 159 | |
| 160 Document* document() const; | |
| 161 SecurityOrigin* securityOrigin() const; | |
| 162 | |
| 163 virtual void didSendData(unsigned long long bytesSent, unsigned long long to
talBytesToBeSent) override; | |
| 164 virtual void didReceiveResponse(unsigned long identifier, const ResourceResp
onse&) override; | |
| 165 virtual void didReceiveData(const char* data, unsigned dataLength) override; | |
| 166 // When responseType is set to "blob", didDownloadData() is called instead | |
| 167 // of didReceiveData(). | |
| 168 virtual void didDownloadData(int dataLength) override; | |
| 169 virtual void didFinishLoading(unsigned long identifier, double finishTime) o
verride; | |
| 170 virtual void didFail(const ResourceError&) override; | |
| 171 virtual void didFailRedirectCheck() override; | |
| 172 | |
| 173 // BlobLoader notifications. | |
| 174 void didFinishLoadingInternal(); | |
| 175 void didFinishLoadingFromBlob(); | |
| 176 void didFailLoadingFromBlob(); | |
| 177 | |
| 178 PassRefPtr<BlobDataHandle> createBlobDataHandleFromResponse(); | |
| 179 | |
| 180 // DocumentParserClient | |
| 181 virtual void notifyParserStopped() override; | |
| 182 | |
| 183 void endLoading(); | |
| 184 | |
| 185 // Returns the MIME type part of m_mimeTypeOverride if present and | |
| 186 // successfully parsed, or returns one of the "Content-Type" header value | |
| 187 // of the received response. | |
| 188 // | |
| 189 // This method is named after the term "final MIME type" defined in the | |
| 190 // spec but doesn't convert the result to ASCII lowercase as specified in | |
| 191 // the spec. Must be lowered later or compared using case insensitive | |
| 192 // comparison functions if required. | |
| 193 AtomicString finalResponseMIMEType() const; | |
| 194 // The same as finalResponseMIMEType() but fallbacks to "text/xml" if | |
| 195 // finalResponseMIMEType() returns an empty string. | |
| 196 AtomicString finalResponseMIMETypeWithFallback() const; | |
| 197 bool responseIsXML() const; | |
| 198 bool responseIsHTML() const; | |
| 199 | |
| 200 PassOwnPtr<TextResourceDecoder> createDecoder() const; | |
| 201 | |
| 202 void initResponseDocument(); | |
| 203 void parseDocumentChunk(const char* data, unsigned dataLength); | |
| 204 | |
| 205 bool areMethodAndURLValidForSend(); | |
| 206 | |
| 207 bool initSend(ExceptionState&); | |
| 208 void sendBytesData(const void*, size_t, ExceptionState&); | |
| 209 | |
| 210 const AtomicString& getRequestHeader(const AtomicString& name) const; | |
| 211 void setRequestHeaderInternal(const AtomicString& name, const AtomicString&
value); | |
| 212 | |
| 213 void trackProgress(long long dataLength); | |
| 214 // Changes m_state and dispatches a readyStateChange event if new m_state | |
| 215 // value is different from last one. | |
| 216 void changeState(State newState); | |
| 217 void dispatchReadyStateChangeEvent(); | |
| 218 | |
| 219 // Clears variables used only while the resource is being loaded. | |
| 220 void clearVariablesForLoading(); | |
| 221 // Returns false iff reentry happened and a new load is started. | |
| 222 // | |
| 223 // This method may invoke V8 GC with m_loader unset. If you touch the | |
| 224 // XMLHttpRequest instance after internalAbort() call, you must hold a | |
| 225 // refcount on it to prevent it from destroyed. | |
| 226 bool internalAbort(); | |
| 227 // Clears variables holding response header and body data. | |
| 228 void clearResponse(); | |
| 229 void clearRequest(); | |
| 230 | |
| 231 void createRequest(PassRefPtr<FormData>, ExceptionState&); | |
| 232 | |
| 233 // Dispatches a response ProgressEvent. | |
| 234 void dispatchProgressEvent(const AtomicString&, long long, long long); | |
| 235 // Dispatches a response ProgressEvent using values sampled from | |
| 236 // m_receivedLength and m_response. | |
| 237 void dispatchProgressEventFromSnapshot(const AtomicString&); | |
| 238 | |
| 239 // Handles didFail() call not caused by cancellation or timeout. | |
| 240 void handleNetworkError(); | |
| 241 // Handles didFail() call for cancellations. For example, the | |
| 242 // ResourceLoader handling the load notifies m_loader of an error | |
| 243 // cancellation when the frame containing the XHR navigates away. | |
| 244 void handleDidCancel(); | |
| 245 // Handles didFail() call for timeout. | |
| 246 void handleDidTimeout(); | |
| 247 | |
| 248 void handleRequestError(ExceptionCode, const AtomicString&, long long, long
long); | |
| 249 | |
| 250 OwnPtrWillBeMember<XMLHttpRequestUpload> m_upload; | |
| 251 | |
| 252 KURL m_url; | |
| 253 AtomicString m_method; | |
| 254 HTTPHeaderMap m_requestHeaders; | |
| 255 // Not converted to ASCII lowercase. Must be lowered later or compared | |
| 256 // using case insensitive comparison functions if needed. | |
| 257 AtomicString m_mimeTypeOverride; | |
| 258 unsigned long m_timeoutMilliseconds; | |
| 259 PersistentWillBeMember<Blob> m_responseBlob; | |
| 260 RefPtrWillBeMember<Stream> m_responseLegacyStream; | |
| 261 PersistentWillBeMember<ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMA
rrayBuffer> > > m_responseStream; | |
| 262 PersistentWillBeMember<UnderlyingSource> m_streamSource; | |
| 263 | |
| 264 RefPtr<ThreadableLoader> m_loader; | |
| 265 unsigned long m_loaderIdentifier; | |
| 266 State m_state; | |
| 267 | |
| 268 ResourceResponse m_response; | |
| 269 String m_finalResponseCharset; | |
| 270 | |
| 271 OwnPtr<TextResourceDecoder> m_decoder; | |
| 272 | |
| 273 ScriptString m_responseText; | |
| 274 RefPtrWillBeMember<Document> m_responseDocument; | |
| 275 RefPtrWillBeMember<DocumentParser> m_responseDocumentParser; | |
| 276 | |
| 277 RefPtr<SharedBuffer> m_binaryResponseBuilder; | |
| 278 long long m_lengthDownloadedToFile; | |
| 279 | |
| 280 RefPtr<DOMArrayBuffer> m_responseArrayBuffer; | |
| 281 | |
| 282 // Used for onprogress tracking | |
| 283 long long m_receivedLength; | |
| 284 | |
| 285 // An exception to throw in synchronous mode. It's set when failure | |
| 286 // notification is received from m_loader and thrown at the end of send() if | |
| 287 // any. | |
| 288 ExceptionCode m_exceptionCode; | |
| 289 | |
| 290 XMLHttpRequestProgressEventThrottle m_progressEventThrottle; | |
| 291 | |
| 292 // An enum corresponding to the allowed string values for the responseType a
ttribute. | |
| 293 ResponseTypeCode m_responseTypeCode; | |
| 294 RefPtr<SecurityOrigin> m_securityOrigin; | |
| 295 | |
| 296 // This blob loader will be used if |m_downloadingToFile| is true and | |
| 297 // |m_responseTypeCode| is NOT ResponseTypeBlob. | |
| 298 OwnPtrWillBeMember<BlobLoader> m_blobLoader; | |
| 299 | |
| 300 bool m_async; | |
| 301 bool m_includeCredentials; | |
| 302 // Used to skip m_responseDocument creation if it's done previously. We need | |
| 303 // this separate flag since m_responseDocument can be 0 for some cases. | |
| 304 bool m_parsedResponse; | |
| 305 bool m_error; | |
| 306 bool m_uploadEventsAllowed; | |
| 307 bool m_uploadComplete; | |
| 308 bool m_sameOriginRequest; | |
| 309 // True iff the ongoing resource loading is using the downloadToFile | |
| 310 // option. | |
| 311 bool m_downloadingToFile; | |
| 312 }; | |
| 313 | |
| 314 } // namespace blink | |
| 315 | |
| 316 #endif // XMLHttpRequest_h | |
| OLD | NEW |