2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23
* THE POSSIBILITY OF SUCH DAMAGE.
27
#include "NetworkSendQueue.h"
29
#include "BlobLoader.h"
33
NetworkSendQueue::NetworkSendQueue(Document& document, WriteString&& writeString, WriteRawData&& writeRawData, ProcessError&& processError)
34
: m_document(makeWeakPtr(document))
35
, m_writeString(WTFMove(writeString))
36
, m_writeRawData(WTFMove(writeRawData))
37
, m_processError(WTFMove(processError))
41
NetworkSendQueue::~NetworkSendQueue() = default;
43
void NetworkSendQueue::enqueue(const String& data)
45
if (m_queue.isEmpty()) {
52
void NetworkSendQueue::enqueue(const JSC::ArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength)
54
if (m_queue.isEmpty()) {
55
auto* data = static_cast<const char*>(binaryData.data());
56
m_writeRawData(data + byteOffset, byteLength);
59
m_queue.append(SharedBuffer::create(static_cast<const uint8_t*>(binaryData.data()) + byteOffset, byteLength));
62
void NetworkSendQueue::enqueue(WebCore::Blob& blob)
64
auto byteLength = blob.size();
66
enqueue(JSC::ArrayBuffer::create(0U, 1), 0, 0);
69
m_queue.append(makeUniqueRef<BlobLoader>(m_document.get(), blob, [this] {
74
void NetworkSendQueue::clear()
79
void NetworkSendQueue::processMessages()
81
while (!m_queue.isEmpty()) {
82
bool shouldStopProcessing = false;
83
switchOn(m_queue.first(), [this](const String& message) {
84
m_writeString(message);
85
}, [this](Ref<SharedBuffer>& data) {
86
m_writeRawData(data->data(), data->size());
87
}, [this, &shouldStopProcessing](UniqueRef<BlobLoader>& loader) {
88
if (loader->isLoading() || loader->errorCode() == FileError::ABORT_ERR) {
89
shouldStopProcessing = true;
93
if (const auto& result = loader->result()) {
94
m_writeRawData(static_cast<const char*>(result->data()), result->byteLength());
97
ASSERT(loader->errorCode());
98
shouldStopProcessing = m_processError(loader->errorCode()) == Continue::No;
100
if (shouldStopProcessing)
102
m_queue.removeFirst();
107
} // namespace WebCore