~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/fileapi/NetworkSendQueue.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "NetworkSendQueue.h"
 
28
 
 
29
#include "BlobLoader.h"
 
30
 
 
31
namespace WebCore {
 
32
 
 
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))
 
38
{
 
39
}
 
40
 
 
41
NetworkSendQueue::~NetworkSendQueue() = default;
 
42
 
 
43
void NetworkSendQueue::enqueue(const String& data)
 
44
{
 
45
    if (m_queue.isEmpty()) {
 
46
        m_writeString(data);
 
47
        return;
 
48
    }
 
49
    m_queue.append(data);
 
50
}
 
51
 
 
52
void NetworkSendQueue::enqueue(const JSC::ArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength)
 
53
{
 
54
    if (m_queue.isEmpty()) {
 
55
        auto* data = static_cast<const char*>(binaryData.data());
 
56
        m_writeRawData(data + byteOffset, byteLength);
 
57
        return;
 
58
    }
 
59
    m_queue.append(SharedBuffer::create(static_cast<const uint8_t*>(binaryData.data()) + byteOffset, byteLength));
 
60
}
 
61
 
 
62
void NetworkSendQueue::enqueue(WebCore::Blob& blob)
 
63
{
 
64
    auto byteLength = blob.size();
 
65
    if (!byteLength) {
 
66
        enqueue(JSC::ArrayBuffer::create(0U, 1), 0, 0);
 
67
        return;
 
68
    }
 
69
    m_queue.append(makeUniqueRef<BlobLoader>(m_document.get(), blob, [this] {
 
70
        processMessages();
 
71
    }));
 
72
}
 
73
 
 
74
void NetworkSendQueue::clear()
 
75
{
 
76
    m_queue.clear();
 
77
}
 
78
 
 
79
void NetworkSendQueue::processMessages()
 
80
{
 
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;
 
90
                return;
 
91
            }
 
92
 
 
93
            if (const auto& result = loader->result()) {
 
94
                m_writeRawData(static_cast<const char*>(result->data()), result->byteLength());
 
95
                return;
 
96
            }
 
97
            ASSERT(loader->errorCode());
 
98
            shouldStopProcessing = m_processError(loader->errorCode()) == Continue::No;
 
99
        });
 
100
        if (shouldStopProcessing)
 
101
            return;
 
102
        m_queue.removeFirst();
 
103
    }
 
104
 
 
105
}
 
106
 
 
107
} // namespace WebCore