| 1 | /* |
| 2 | * Copyright (C) 2016 Apple 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 Apple 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 APPLE 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 APPLE 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 "FetchBodySource.h" |
| 32 | #include "JSDOMPromiseDeferred.h" |
| 33 | #include "ReadableStreamSink.h" |
| 34 | #include "SharedBuffer.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class Blob; |
| 39 | class FetchBodySource; |
| 40 | class ReadableStream; |
| 41 | |
| 42 | class FetchBodyConsumer { |
| 43 | public: |
| 44 | enum class Type { None, ArrayBuffer, Blob, JSON, Text }; |
| 45 | |
| 46 | FetchBodyConsumer(Type type) : m_type(type) { } |
| 47 | |
| 48 | void append(const char* data, unsigned); |
| 49 | void append(const unsigned char* data, unsigned); |
| 50 | |
| 51 | bool hasData() const { return !!m_buffer; } |
| 52 | const SharedBuffer* data() const { return m_buffer.get(); } |
| 53 | void setData(Ref<SharedBuffer>&& data) { m_buffer = WTFMove(data); } |
| 54 | |
| 55 | RefPtr<SharedBuffer> takeData(); |
| 56 | RefPtr<JSC::ArrayBuffer> takeAsArrayBuffer(); |
| 57 | Ref<Blob> takeAsBlob(); |
| 58 | String takeAsText(); |
| 59 | |
| 60 | void setContentType(const String& contentType) { m_contentType = contentType; } |
| 61 | void setType(Type type) { m_type = type; } |
| 62 | |
| 63 | void clean(); |
| 64 | |
| 65 | void extract(ReadableStream&, ReadableStreamToSharedBufferSink::Callback&&); |
| 66 | void resolve(Ref<DeferredPromise>&&, ReadableStream*); |
| 67 | void resolveWithData(Ref<DeferredPromise>&&, const unsigned char*, unsigned); |
| 68 | |
| 69 | void loadingFailed(const Exception&); |
| 70 | void loadingSucceeded(); |
| 71 | |
| 72 | void setConsumePromise(Ref<DeferredPromise>&&); |
| 73 | void setSource(Ref<FetchBodySource>&&); |
| 74 | |
| 75 | void setAsLoading() { m_isLoading = true; } |
| 76 | |
| 77 | private: |
| 78 | Type m_type; |
| 79 | String m_contentType; |
| 80 | RefPtr<SharedBuffer> m_buffer; |
| 81 | RefPtr<DeferredPromise> m_consumePromise; |
| 82 | RefPtr<ReadableStreamToSharedBufferSink> m_sink; |
| 83 | RefPtr<FetchBodySource> m_source; |
| 84 | bool m_isLoading { false }; |
| 85 | }; |
| 86 | |
| 87 | } // namespace WebCore |
| 88 | |