1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/*
* Copyright (C) 2016 Canon Inc.
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions
* are required to be met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FetchBodySource.h"
#include "FetchResponse.h"
#include "JSDOMPromise.h"
#include "JSDOMPromiseDeferred.h"
#include "ReadableByteStreamController.h"
namespace WebCore {
std::pair<Ref<FetchBodySource>, Ref<RefCountedReadableStreamSource>> FetchBodySource::createNonByteSource(FetchBodyOwner& bodyOwner)
{
Ref nonByteSource = NonByteSource::create(bodyOwner);
Ref source = adoptRef(*new FetchBodySource(bodyOwner, nonByteSource.ptr()));
return { WTFMove(source), WTFMove(nonByteSource) };
}
Ref<FetchBodySource> FetchBodySource::createByteSource(FetchBodyOwner& bodyOwner)
{
return adoptRef(*new FetchBodySource(bodyOwner));
}
FetchBodySource::FetchBodySource(FetchBodyOwner& bodyOwner, RefPtr<NonByteSource>&& nonByteSource)
: m_bodyOwner(bodyOwner)
, m_nonByteSource(WTFMove(nonByteSource))
{
}
FetchBodySource::~FetchBodySource() = default;
void FetchBodySource::setByteController(ReadableByteStreamController& controller)
{
ASSERT(!m_nonByteSource);
ASSERT(!m_byteController);
m_byteController = controller;
if (RefPtr bodyOwner = m_bodyOwner.get())
bodyOwner->consumeBodyAsStream();
}
Ref<DOMPromise> FetchBodySource::pull(JSDOMGlobalObject& globalObject, ReadableByteStreamController& controller)
{
ASSERT_UNUSED(controller, &controller == m_byteController.get() || !m_byteController);
auto [promise, deferred] = createPromiseAndWrapper(globalObject);
m_isPulling = true;
m_pullPromise = WTFMove(deferred);
return promise;
}
Ref<DOMPromise> FetchBodySource::cancel(JSDOMGlobalObject& globalObject, ReadableByteStreamController& controller, std::optional<JSC::JSValue>&&)
{
ASSERT_UNUSED(controller, &controller == m_byteController.get());
m_isCancelling = true;
if (RefPtr bodyOwner = m_bodyOwner.get())
bodyOwner->cancel();
auto [promise, deferred] = createPromiseAndWrapper(globalObject);
deferred->resolve();
return promise;
}
static JSDOMGlobalObject* globalObjectFromBodyOwner(RefPtr<FetchBodyOwner>&& bodyOwner)
{
RefPtr context = bodyOwner ? bodyOwner->scriptExecutionContext() : nullptr;
return JSC::jsCast<JSDOMGlobalObject*>(context->globalObject());
}
// FIXME: We should be able to take a FragmentedSharedBuffer
bool FetchBodySource::enqueue(RefPtr<JSC::ArrayBuffer>&& chunk)
{
if (m_nonByteSource)
return m_nonByteSource->enqueue(WTFMove(chunk));
if (!chunk)
return false;
RefPtr controller = m_byteController.get();
if (!controller)
return false;
auto* globalObject = globalObjectFromBodyOwner(m_bodyOwner.get());
if (!globalObject)
return false;
size_t byteLength = chunk->byteLength();
auto result = controller->enqueue(*globalObject, Uint8Array::create(chunk.releaseNonNull(), 0, byteLength));
return !result.hasException();
}
void FetchBodySource::close()
{
if (m_nonByteSource) {
m_nonByteSource->close();
return;
}
RefPtr controller = m_byteController.get();
if (!controller)
return;
auto* globalObject = globalObjectFromBodyOwner(m_bodyOwner.get());
if (!globalObject)
return;
controller->closeAndRespondToPendingPullIntos(*globalObject);
}
void FetchBodySource::error(const Exception& exception)
{
if (m_nonByteSource) {
m_nonByteSource->error(exception);
return;
}
RefPtr controller = m_byteController.get();
if (!controller)
return;
auto* globalObject = globalObjectFromBodyOwner(m_bodyOwner.get());
if (!globalObject)
return;
controller->error(*globalObject, exception);
}
bool FetchBodySource::isPulling() const
{
return m_nonByteSource ? m_nonByteSource->isPulling() : m_isPulling;
}
bool FetchBodySource::isCancelling() const
{
return m_nonByteSource ? m_nonByteSource->isCancelling() : m_isCancelling;
}
void FetchBodySource::resolvePullPromise()
{
if (m_nonByteSource) {
m_nonByteSource->resolvePullPromise();
return;
}
m_isPulling = false;
if (auto pullPromise = std::exchange(m_pullPromise, { }))
pullPromise->resolve();
}
void FetchBodySource::detach()
{
if (m_nonByteSource) {
m_nonByteSource->detach();
return;
}
m_bodyOwner = nullptr;
m_byteController = nullptr;
m_pullPromise = nullptr;
}
FetchBodySource::NonByteSource::NonByteSource(FetchBodyOwner& owner)
: m_bodyOwner(owner)
{
}
void FetchBodySource::NonByteSource::setActive()
{
ASSERT(m_bodyOwner);
ASSERT(!m_pendingActivity);
if (RefPtr bodyOwner = m_bodyOwner.get())
m_pendingActivity = bodyOwner->makePendingActivity(*bodyOwner);
}
void FetchBodySource::NonByteSource::setInactive()
{
ASSERT(m_bodyOwner);
ASSERT(m_pendingActivity);
m_pendingActivity = nullptr;
}
void FetchBodySource::NonByteSource::doStart()
{
ASSERT(m_bodyOwner);
if (RefPtr bodyOwner = m_bodyOwner.get())
bodyOwner->consumeBodyAsStream();
}
void FetchBodySource::NonByteSource::doPull()
{
ASSERT(m_bodyOwner);
if (RefPtr bodyOwner = m_bodyOwner.get())
bodyOwner->feedStream();
}
void FetchBodySource::NonByteSource::doCancel(JSC::JSValue)
{
m_isCancelling = true;
RefPtr bodyOwner = m_bodyOwner.get();
if (!bodyOwner)
return;
bodyOwner->cancel();
m_bodyOwner = nullptr;
}
void FetchBodySource::NonByteSource::close()
{
#if ASSERT_ENABLED
ASSERT(!m_isClosed);
m_isClosed = true;
#endif
controller().close();
clean();
m_bodyOwner = nullptr;
}
void FetchBodySource::NonByteSource::error(const Exception& value)
{
controller().error(value);
clean();
m_bodyOwner = nullptr;
}
} // namespace WebCore
|