NetworkSendQueue.cpp [plain text]
#include "config.h"
#include "NetworkSendQueue.h"
#include "BlobLoader.h"
namespace WebCore {
NetworkSendQueue::NetworkSendQueue(Document& document, WriteString&& writeString, WriteRawData&& writeRawData, ProcessError&& processError)
: m_document(makeWeakPtr(document))
, m_writeString(WTFMove(writeString))
, m_writeRawData(WTFMove(writeRawData))
, m_processError(WTFMove(processError))
{
}
NetworkSendQueue::~NetworkSendQueue() = default;
void NetworkSendQueue::enqueue(CString&& utf8)
{
if (m_queue.isEmpty()) {
m_writeString(utf8);
return;
}
m_queue.append(WTFMove(utf8));
}
void NetworkSendQueue::enqueue(const JSC::ArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength)
{
if (m_queue.isEmpty()) {
auto* data = static_cast<const char*>(binaryData.data());
m_writeRawData(data + byteOffset, byteLength);
return;
}
m_queue.append(SharedBuffer::create(static_cast<const uint8_t*>(binaryData.data()) + byteOffset, byteLength));
}
void NetworkSendQueue::enqueue(WebCore::Blob& blob)
{
auto byteLength = blob.size();
if (!byteLength) {
enqueue(JSC::ArrayBuffer::create(0U, 1), 0, 0);
return;
}
auto blobLoader = makeUniqueRef<BlobLoader>([this](BlobLoader&) {
processMessages();
});
auto* blobLoaderPtr = &blobLoader.get();
m_queue.append(WTFMove(blobLoader));
blobLoaderPtr->start(blob, m_document.get(), FileReaderLoader::ReadAsArrayBuffer);
}
void NetworkSendQueue::clear()
{
std::exchange(m_queue, { });
}
void NetworkSendQueue::processMessages()
{
while (!m_queue.isEmpty()) {
bool shouldStopProcessing = false;
switchOn(m_queue.first(), [this](const CString& utf8) {
m_writeString(utf8);
}, [this](Ref<SharedBuffer>& data) {
m_writeRawData(data->data(), data->size());
}, [this, &shouldStopProcessing](UniqueRef<BlobLoader>& loader) {
auto errorCode = loader->errorCode();
if (loader->isLoading() || (errorCode && errorCode.value() == AbortError)) {
shouldStopProcessing = true;
return;
}
if (const auto& result = loader->arrayBufferResult()) {
m_writeRawData(static_cast<const char*>(result->data()), result->byteLength());
return;
}
ASSERT(errorCode);
shouldStopProcessing = m_processError(errorCode.value()) == Continue::No;
});
if (shouldStopProcessing)
return;
m_queue.removeFirst();
}
}
}