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 "core/fileapi/FileReaderLoader.h" | |
14 #include "core/fileapi/FileReaderLoaderClient.h" | |
15 #include "modules/ModulesExport.h" | 13 #include "modules/ModulesExport.h" |
| 14 #include "modules/fetch/FetchDataConsumerHandle.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 FileReaderLoaderClient { | 33 , public FetchDataLoader::Client { |
33 DEFINE_WRAPPERTYPEINFO(); | 34 DEFINE_WRAPPERTYPEINFO(); |
34 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(Body); | 35 USING_GARBAGE_COLLECTED_MIXIN(Body); |
35 public: | 36 public: |
36 class ReadableStreamSource; | 37 class ReadableStreamSource; |
37 enum ResponseType { | 38 enum ResponseType { |
38 ResponseUnknown, | 39 ResponseUnknown, |
39 ResponseAsArrayBuffer, | 40 ResponseAsArrayBuffer, |
40 ResponseAsBlob, | 41 ResponseAsBlob, |
41 ResponseAsFormData, | 42 ResponseAsFormData, |
42 ResponseAsJSON, | 43 ResponseAsJSON, |
43 ResponseAsText | 44 ResponseAsText |
44 }; | 45 }; |
45 enum LockBodyOption { | 46 enum LockBodyOption { |
46 LockBodyOptionNone, | 47 LockBodyOptionNone, |
47 // Setting "body passed" flag in addition to acquiring a lock. | 48 // Setting "body passed" flag in addition to acquiring a lock. |
48 PassBody, | 49 PassBody, |
49 }; | 50 }; |
50 explicit Body(ExecutionContext*); | 51 explicit Body(ExecutionContext*); |
51 virtual ~Body() { } | 52 virtual ~Body() { } |
52 | 53 |
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; | 69 virtual void stop() override; |
82 virtual bool hasPendingActivity() const override; | 70 virtual bool hasPendingActivity() const override; |
83 | 71 |
84 ReadableStreamSource* createBodySource(PassRefPtr<BlobDataHandle>); | |
85 ReadableStreamSource* createBodySource(BodyStreamBuffer*); | |
86 | |
87 DECLARE_VIRTUAL_TRACE(); | 72 DECLARE_VIRTUAL_TRACE(); |
88 | 73 |
89 BodyStreamBuffer* bufferForTest() const { return buffer(); } | 74 protected: |
| 75 // Sets |m_stream| to a newly created stream from |buffer|. |
| 76 // |buffer| can be null. |
| 77 // This is called when the underlying buffer is set/modified. |
| 78 // TODO(hiroshige): Merge FetchRequest/ResponseData::buffer() and |
| 79 // integrate Body::setBody(), Request/Response::refreshBody(), |
| 80 // FetchRequestData::setBuffer() and |
| 81 // FetchResponseData::replaceBodyStreamBuffer(). |
| 82 void setBody(BodyStreamBuffer* /* buffer */); |
90 | 83 |
91 private: | 84 private: |
92 class BlobHandleReceiver; | |
93 | |
94 void pullSource(); | 85 void pullSource(); |
95 void readAllFromStream(); | 86 void readAllFromStream(); |
96 ScriptPromise readAsync(ScriptState*, ResponseType); | 87 ScriptPromise readAsync(ScriptState*, ResponseType); |
97 void readAsyncFromBlob(PassRefPtr<BlobDataHandle>); | 88 void resolveWithEmptyDataSynchronously(); |
| 89 void readAsyncFromDrainingBodyStreamBuffer(PassOwnPtr<DrainingBodyStreamBuff
er>, const String& mimeType); |
98 void resolveJSON(const String&); | 90 void resolveJSON(const String&); |
99 | 91 |
100 // FileReaderLoaderClient functions. | 92 void didFetchDataLoadFinishedFromDrainingStream(); |
101 virtual void didStartLoading() override; | |
102 virtual void didReceiveData() override; | |
103 virtual void didFinishLoading() override; | |
104 virtual void didFail(FileError::ErrorCode) override; | |
105 | 93 |
106 void didBlobHandleReceiveError(DOMException*); | 94 // FetchDataLoader::Client functions. |
| 95 void didFetchDataLoadFailed() override; |
| 96 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle>) override; |
| 97 void didFetchDataLoadedArrayBuffer(PassRefPtr<DOMArrayBuffer>) override; |
| 98 void didFetchDataLoadedString(const String&) override; |
107 | 99 |
108 // We use BlobDataHandle or BodyStreamBuffer as data container of the Body. | |
109 // BodyStreamBuffer is used only when the Response object is created by | |
110 // fetch() API. | |
111 // FIXME: We should seek a cleaner way to handle the data. | |
112 virtual PassRefPtr<BlobDataHandle> blobDataHandle() const = 0; | |
113 virtual BodyStreamBuffer* buffer() const = 0; | |
114 virtual String mimeType() const = 0; | 100 virtual String mimeType() const = 0; |
115 | 101 |
116 void didFinishLoadingViaStream(PassRefPtr<DOMArrayBuffer>); | |
117 | |
118 OwnPtr<FileReaderLoader> m_loader; | |
119 bool m_bodyUsed; | 102 bool m_bodyUsed; |
120 ResponseType m_responseType; | 103 ResponseType m_responseType; |
121 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; | 104 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; |
122 Member<ReadableStreamSource> m_streamSource; | 105 Member<ReadableStreamSource> m_streamSource; |
123 Member<ReadableByteStream> m_stream; | 106 Member<ReadableByteStream> m_stream; |
124 }; | 107 }; |
125 | 108 |
126 } // namespace blink | 109 } // namespace blink |
127 | 110 |
128 #endif // Body_h | 111 #endif // Body_h |
OLD | NEW |