| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef Body_h | 5 #ifndef Body_h |
| 6 #define Body_h | 6 #define Body_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptWrappable.h" | 10 #include "bindings/core/v8/ScriptWrappable.h" |
| 11 #include "core/dom/ActiveDOMObject.h" | 11 #include "core/dom/ActiveDOMObject.h" |
| 12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
| 13 #include "modules/ModulesExport.h" | 13 #include "modules/ModulesExport.h" |
| 14 #include "modules/fetch/FetchDataConsumerHandle.h" | 14 #include "modules/fetch/FetchDataConsumerHandle.h" |
| 15 #include "modules/fetch/FetchDataLoader.h" | 15 #include "modules/fetch/FetchDataLoader.h" |
| 16 #include "platform/blob/BlobData.h" | 16 #include "platform/blob/BlobData.h" |
| 17 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
| 18 #include "wtf/RefPtr.h" | 18 #include "wtf/RefPtr.h" |
| 19 | 19 |
| 20 namespace blink { | 20 namespace blink { |
| 21 | 21 |
| 22 class BodyStreamBuffer; | 22 class BodyStreamBuffer; |
| 23 class DrainingBodyStreamBuffer; |
| 23 class BodyStreamSource; | 24 class BodyStreamSource; |
| 24 class DOMException; | 25 class DOMException; |
| 25 class ReadableByteStream; | 26 class ReadableByteStream; |
| 26 class ScriptState; | 27 class ScriptState; |
| 27 | 28 |
| 28 class MODULES_EXPORT Body | 29 class MODULES_EXPORT Body |
| 29 : public GarbageCollectedFinalized<Body> | 30 : public GarbageCollectedFinalized<Body> |
| 30 , public ScriptWrappable | 31 , public ScriptWrappable |
| 31 , public ActiveDOMObject | 32 , public ActiveDOMObject |
| 32 , public FetchDataLoader::Client { | 33 , public FetchDataLoader::Client { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 ScriptPromise arrayBuffer(ScriptState*); | 54 ScriptPromise arrayBuffer(ScriptState*); |
| 54 ScriptPromise blob(ScriptState*); | 55 ScriptPromise blob(ScriptState*); |
| 55 ScriptPromise formData(ScriptState*); | 56 ScriptPromise formData(ScriptState*); |
| 56 ScriptPromise json(ScriptState*); | 57 ScriptPromise json(ScriptState*); |
| 57 ScriptPromise text(ScriptState*); | 58 ScriptPromise text(ScriptState*); |
| 58 ReadableByteStream* body(); | 59 ReadableByteStream* body(); |
| 59 | 60 |
| 60 bool bodyUsed() const; | 61 bool bodyUsed() const; |
| 61 void lockBody(LockBodyOption = LockBodyOptionNone); | 62 void lockBody(LockBodyOption = LockBodyOptionNone); |
| 62 | 63 |
| 63 // Returns true if the body stream is (possibly partially) consumed. | 64 // Creates a DrainingBodyStreamBuffer to access body data. |
| 64 bool isBodyConsumed() const; | 65 // Returns nullptr if underlying BodyStreamBuffer is null. |
| 65 // Sets |m_stream| to a newly created stream from |source|. | 66 PassOwnPtr<DrainingBodyStreamBuffer> createDrainingStream(); |
| 66 void setBody(ReadableStreamSource* /* source */); | |
| 67 | |
| 68 void setBody(PassRefPtr<BlobDataHandle> handle) | |
| 69 { | |
| 70 setBody(createBodySource(handle)); | |
| 71 } | |
| 72 void setBody(BodyStreamBuffer* buffer) | |
| 73 { | |
| 74 setBody(createBodySource(buffer)); | |
| 75 } | |
| 76 | |
| 77 // Creates a new BodyStreamBuffer to drain the data from the ReadableStream. | |
| 78 BodyStreamBuffer* createDrainingStream(); | |
| 79 | 67 |
| 80 // ActiveDOMObject override. | 68 // ActiveDOMObject override. |
| 81 virtual void stop() override; | |
| 82 virtual bool hasPendingActivity() const override; | 69 virtual bool hasPendingActivity() const override; |
| 83 | 70 |
| 84 ReadableStreamSource* createBodySource(PassRefPtr<BlobDataHandle>); | |
| 85 ReadableStreamSource* createBodySource(BodyStreamBuffer*); | |
| 86 | |
| 87 DECLARE_VIRTUAL_TRACE(); | 71 DECLARE_VIRTUAL_TRACE(); |
| 88 | 72 |
| 89 BodyStreamBuffer* bufferForTest() const { return buffer(); } | 73 protected: |
| 74 // Sets |m_stream| to a newly created stream from |buffer|. |
| 75 // |buffer| can be null. |
| 76 // This is called when the underlying buffer is set/modified. |
| 77 // TODO(hiroshige): Merge FetchRequest/ResponseData::buffer() and |
| 78 // integrate Body::setBody(), Request/Response::refreshBody(), |
| 79 // FetchRequestData::setBuffer() and |
| 80 // FetchResponseData::replaceBodyStreamBuffer(). |
| 81 void setBody(BodyStreamBuffer* /* buffer */); |
| 90 | 82 |
| 91 private: | 83 private: |
| 92 class BlobHandleReceiver; | |
| 93 | |
| 94 void pullSource(); | 84 void pullSource(); |
| 95 void readAllFromStream(); | 85 void readAllFromStream(); |
| 96 ScriptPromise readAsync(ScriptState*, ResponseType); | 86 ScriptPromise readAsync(ScriptState*, ResponseType); |
| 97 void readAsyncFromBlob(PassRefPtr<BlobDataHandle>); | 87 void resolveWithEmptyDataSynchronously(); |
| 98 void readAsyncFromFetchDataConsumerHandle(FetchDataConsumerHandle*, const St
ring& mimeType); | 88 void readAsyncFromDrainingBodyStreamBuffer(PassOwnPtr<DrainingBodyStreamBuff
er>, const String& mimeType); |
| 99 void resolveJSON(const String&); | 89 void resolveJSON(const String&); |
| 100 | 90 |
| 91 void didFetchDataLoadFinishedFromDrainingStream(); |
| 92 |
| 101 // FetchDataLoader::Client functions. | 93 // FetchDataLoader::Client functions. |
| 102 void didFetchDataLoadFailed() override; | 94 void didFetchDataLoadFailed() override; |
| 103 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle>) override; | 95 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle>) override; |
| 104 void didFetchDataLoadedArrayBuffer(PassRefPtr<DOMArrayBuffer>) override; | 96 void didFetchDataLoadedArrayBuffer(PassRefPtr<DOMArrayBuffer>) override; |
| 105 void didFetchDataLoadedString(const String&) override; | 97 void didFetchDataLoadedString(const String&) override; |
| 106 | 98 |
| 107 void didBlobHandleReceiveError(DOMException*); | |
| 108 | |
| 109 // We use BlobDataHandle or BodyStreamBuffer as data container of the Body. | |
| 110 // BodyStreamBuffer is used only when the Response object is created by | |
| 111 // fetch() API. | |
| 112 // FIXME: We should seek a cleaner way to handle the data. | |
| 113 virtual PassRefPtr<BlobDataHandle> blobDataHandle() const = 0; | |
| 114 virtual BodyStreamBuffer* buffer() const = 0; | |
| 115 virtual String mimeType() const = 0; | 99 virtual String mimeType() const = 0; |
| 116 | 100 |
| 117 void didFinishLoadingViaStream(PassRefPtr<DOMArrayBuffer>); | |
| 118 | |
| 119 Member<FetchDataLoader> m_fetchDataLoader; | |
| 120 bool m_bodyUsed; | 101 bool m_bodyUsed; |
| 121 ResponseType m_responseType; | 102 ResponseType m_responseType; |
| 122 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; | 103 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; |
| 123 Member<ReadableStreamSource> m_streamSource; | 104 Member<ReadableStreamSource> m_streamSource; |
| 124 Member<ReadableByteStream> m_stream; | 105 Member<ReadableByteStream> m_stream; |
| 125 }; | 106 }; |
| 126 | 107 |
| 127 } // namespace blink | 108 } // namespace blink |
| 128 | 109 |
| 129 #endif // Body_h | 110 #endif // Body_h |
| OLD | NEW |