| 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 "ActiveDOMObject.h" |
| 32 | #include "FetchBody.h" |
| 33 | #include "FetchBodySource.h" |
| 34 | #include "FetchHeaders.h" |
| 35 | #include "FetchLoader.h" |
| 36 | #include "FetchLoaderClient.h" |
| 37 | #include "ResourceError.h" |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | class FetchBodyOwner : public RefCounted<FetchBodyOwner>, public ActiveDOMObject { |
| 42 | public: |
| 43 | FetchBodyOwner(ScriptExecutionContext&, Optional<FetchBody>&&, Ref<FetchHeaders>&&); |
| 44 | ~FetchBodyOwner(); |
| 45 | |
| 46 | bool bodyUsed() const { return isDisturbed(); } |
| 47 | void arrayBuffer(Ref<DeferredPromise>&&); |
| 48 | void blob(Ref<DeferredPromise>&&); |
| 49 | void formData(Ref<DeferredPromise>&&); |
| 50 | void json(Ref<DeferredPromise>&&); |
| 51 | void text(Ref<DeferredPromise>&&); |
| 52 | |
| 53 | bool isDisturbed() const; |
| 54 | bool isDisturbedOrLocked() const; |
| 55 | |
| 56 | void loadBlob(const Blob&, FetchBodyConsumer*); |
| 57 | |
| 58 | bool isActive() const { return !!m_blobLoader; } |
| 59 | |
| 60 | RefPtr<ReadableStream> readableStream(JSC::ExecState&); |
| 61 | bool hasReadableStreamBody() const { return m_body && m_body->hasReadableStream(); } |
| 62 | |
| 63 | #if ENABLE(STREAMS_API) |
| 64 | virtual void consumeBodyAsStream(); |
| 65 | virtual void feedStream() { } |
| 66 | virtual void cancel() { } |
| 67 | #endif |
| 68 | |
| 69 | bool hasLoadingError() const; |
| 70 | ResourceError loadingError() const; |
| 71 | Optional<Exception> loadingException() const; |
| 72 | |
| 73 | protected: |
| 74 | const FetchBody& body() const { return *m_body; } |
| 75 | FetchBody& body() { return *m_body; } |
| 76 | bool isBodyNull() const { return !m_body; } |
| 77 | bool isBodyNullOrOpaque() const { return !m_body || m_isBodyOpaque; } |
| 78 | void cloneBody(FetchBodyOwner&); |
| 79 | |
| 80 | void extractBody(ScriptExecutionContext&, FetchBody::Init&&); |
| 81 | void updateContentType(); |
| 82 | void consumeOnceLoadingFinished(FetchBodyConsumer::Type, Ref<DeferredPromise>&&); |
| 83 | |
| 84 | void setBody(FetchBody&& body) { m_body = WTFMove(body); } |
| 85 | void createReadableStream(JSC::ExecState&); |
| 86 | |
| 87 | // ActiveDOMObject API |
| 88 | void stop() override; |
| 89 | |
| 90 | void setDisturbed() { m_isDisturbed = true; } |
| 91 | |
| 92 | void setBodyAsOpaque() { m_isBodyOpaque = true; } |
| 93 | bool isBodyOpaque() const { return m_isBodyOpaque; } |
| 94 | |
| 95 | void setLoadingError(Exception&&); |
| 96 | void setLoadingError(ResourceError&&); |
| 97 | |
| 98 | private: |
| 99 | // Blob loading routines |
| 100 | void blobChunk(const char*, size_t); |
| 101 | void blobLoadingSucceeded(); |
| 102 | void blobLoadingFailed(); |
| 103 | void finishBlobLoading(); |
| 104 | |
| 105 | struct BlobLoader final : FetchLoaderClient { |
| 106 | BlobLoader(FetchBodyOwner&); |
| 107 | |
| 108 | // FetchLoaderClient API |
| 109 | void didReceiveResponse(const ResourceResponse&) final; |
| 110 | void didReceiveData(const char* data, size_t size) final { owner.blobChunk(data, size); } |
| 111 | void didFail(const ResourceError&) final; |
| 112 | void didSucceed() final { owner.blobLoadingSucceeded(); } |
| 113 | |
| 114 | FetchBodyOwner& owner; |
| 115 | std::unique_ptr<FetchLoader> loader; |
| 116 | }; |
| 117 | |
| 118 | protected: |
| 119 | Optional<FetchBody> m_body; |
| 120 | String m_contentType; |
| 121 | bool m_isDisturbed { false }; |
| 122 | #if ENABLE(STREAMS_API) |
| 123 | RefPtr<FetchBodySource> m_readableStreamSource; |
| 124 | #endif |
| 125 | Ref<FetchHeaders> m_headers; |
| 126 | |
| 127 | private: |
| 128 | Optional<BlobLoader> m_blobLoader; |
| 129 | bool m_isBodyOpaque { false }; |
| 130 | |
| 131 | Variant<std::nullptr_t, Exception, ResourceError> m_loadingError; |
| 132 | }; |
| 133 | |
| 134 | } // namespace WebCore |
| 135 | |