| 1 | /* |
| 2 | * Copyright (C) 2016 Canon Inc. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted, provided that the following conditions |
| 6 | * are required to be met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Canon Inc. nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR |
| 21 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include "DOMFormData.h" |
| 32 | #include "FetchBodyConsumer.h" |
| 33 | #include "FormData.h" |
| 34 | #include "JSDOMPromiseDeferred.h" |
| 35 | #include "ReadableStream.h" |
| 36 | #include "URLSearchParams.h" |
| 37 | #include <wtf/Variant.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | class FetchBodyOwner; |
| 42 | class FetchBodySource; |
| 43 | class ScriptExecutionContext; |
| 44 | |
| 45 | class FetchBody { |
| 46 | public: |
| 47 | void arrayBuffer(FetchBodyOwner&, Ref<DeferredPromise>&&); |
| 48 | void blob(FetchBodyOwner&, Ref<DeferredPromise>&&, const String&); |
| 49 | void json(FetchBodyOwner&, Ref<DeferredPromise>&&); |
| 50 | void text(FetchBodyOwner&, Ref<DeferredPromise>&&); |
| 51 | void formData(FetchBodyOwner&, Ref<DeferredPromise>&& promise) { promise.get().reject(NotSupportedError); } |
| 52 | |
| 53 | #if ENABLE(STREAMS_API) |
| 54 | void consumeAsStream(FetchBodyOwner&, FetchBodySource&); |
| 55 | #endif |
| 56 | |
| 57 | using Init = Variant<RefPtr<Blob>, RefPtr<ArrayBufferView>, RefPtr<ArrayBuffer>, RefPtr<DOMFormData>, RefPtr<URLSearchParams>, RefPtr<ReadableStream>, String>; |
| 58 | static FetchBody extract(ScriptExecutionContext&, Init&&, String&); |
| 59 | FetchBody() = default; |
| 60 | |
| 61 | WEBCORE_EXPORT static Optional<FetchBody> fromFormData(FormData&); |
| 62 | |
| 63 | void loadingFailed(const Exception&); |
| 64 | void loadingSucceeded(); |
| 65 | |
| 66 | RefPtr<FormData> bodyAsFormData(ScriptExecutionContext&) const; |
| 67 | |
| 68 | using TakenData = Variant<std::nullptr_t, Ref<FormData>, Ref<SharedBuffer>>; |
| 69 | TakenData take(); |
| 70 | |
| 71 | void setAsFormData(Ref<FormData>&& data) { m_data = WTFMove(data); } |
| 72 | FetchBodyConsumer& consumer() { return m_consumer; } |
| 73 | |
| 74 | void consumeOnceLoadingFinished(FetchBodyConsumer::Type, Ref<DeferredPromise>&&, const String&); |
| 75 | void cleanConsumer() { m_consumer.clean(); } |
| 76 | |
| 77 | FetchBody clone(); |
| 78 | |
| 79 | bool hasReadableStream() const { return !!m_readableStream; } |
| 80 | const ReadableStream* readableStream() const { return m_readableStream.get(); } |
| 81 | ReadableStream* readableStream() { return m_readableStream.get(); } |
| 82 | void setReadableStream(Ref<ReadableStream>&& stream) |
| 83 | { |
| 84 | ASSERT(!m_readableStream); |
| 85 | m_readableStream = WTFMove(stream); |
| 86 | } |
| 87 | |
| 88 | bool isBlob() const { return WTF::holds_alternative<Ref<const Blob>>(m_data); } |
| 89 | bool isFormData() const { return WTF::holds_alternative<Ref<FormData>>(m_data); } |
| 90 | |
| 91 | private: |
| 92 | explicit FetchBody(Ref<const Blob>&& data) : m_data(WTFMove(data)) { } |
| 93 | explicit FetchBody(Ref<const ArrayBuffer>&& data) : m_data(WTFMove(data)) { } |
| 94 | explicit FetchBody(Ref<const ArrayBufferView>&& data) : m_data(WTFMove(data)) { } |
| 95 | explicit FetchBody(Ref<FormData>&& data) : m_data(WTFMove(data)) { } |
| 96 | explicit FetchBody(String&& data) : m_data(WTFMove(data)) { } |
| 97 | explicit FetchBody(Ref<const URLSearchParams>&& data) : m_data(WTFMove(data)) { } |
| 98 | explicit FetchBody(const FetchBodyConsumer& consumer) : m_consumer(consumer) { } |
| 99 | explicit FetchBody(Ref<ReadableStream>&& stream) : m_readableStream(WTFMove(stream)) { } |
| 100 | |
| 101 | void consume(FetchBodyOwner&, Ref<DeferredPromise>&&); |
| 102 | |
| 103 | void consumeArrayBuffer(Ref<DeferredPromise>&&); |
| 104 | void consumeArrayBufferView(Ref<DeferredPromise>&&); |
| 105 | void consumeText(Ref<DeferredPromise>&&, const String&); |
| 106 | void consumeBlob(FetchBodyOwner&, Ref<DeferredPromise>&&); |
| 107 | |
| 108 | bool isArrayBuffer() const { return WTF::holds_alternative<Ref<const ArrayBuffer>>(m_data); } |
| 109 | bool isArrayBufferView() const { return WTF::holds_alternative<Ref<const ArrayBufferView>>(m_data); } |
| 110 | bool isURLSearchParams() const { return WTF::holds_alternative<Ref<const URLSearchParams>>(m_data); } |
| 111 | bool isText() const { return WTF::holds_alternative<String>(m_data); } |
| 112 | |
| 113 | const Blob& blobBody() const { return WTF::get<Ref<const Blob>>(m_data).get(); } |
| 114 | FormData& formDataBody() { return WTF::get<Ref<FormData>>(m_data).get(); } |
| 115 | const FormData& formDataBody() const { return WTF::get<Ref<FormData>>(m_data).get(); } |
| 116 | const ArrayBuffer& arrayBufferBody() const { return WTF::get<Ref<const ArrayBuffer>>(m_data).get(); } |
| 117 | const ArrayBufferView& arrayBufferViewBody() const { return WTF::get<Ref<const ArrayBufferView>>(m_data).get(); } |
| 118 | String& textBody() { return WTF::get<String>(m_data); } |
| 119 | const String& textBody() const { return WTF::get<String>(m_data); } |
| 120 | const URLSearchParams& urlSearchParamsBody() const { return WTF::get<Ref<const URLSearchParams>>(m_data).get(); } |
| 121 | |
| 122 | using Data = Variant<std::nullptr_t, Ref<const Blob>, Ref<FormData>, Ref<const ArrayBuffer>, Ref<const ArrayBufferView>, Ref<const URLSearchParams>, String>; |
| 123 | Data m_data { nullptr }; |
| 124 | |
| 125 | FetchBodyConsumer m_consumer { FetchBodyConsumer::Type::None }; |
| 126 | RefPtr<ReadableStream> m_readableStream; |
| 127 | }; |
| 128 | |
| 129 | } // namespace WebCore |
| 130 | |