~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/PasteboardCustomData.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 "PasteboardCustomData.h"
 
28
 
 
29
#include "SharedBuffer.h"
 
30
#include <wtf/URLParser.h>
 
31
#include <wtf/persistence/PersistentCoders.h>
 
32
#include <wtf/text/StringHash.h>
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
static Variant<String, Ref<SharedBuffer>> copyPlatformData(const Variant<String, Ref<SharedBuffer>>& other)
 
37
{
 
38
    if (WTF::holds_alternative<String>(other))
 
39
        return { WTF::get<String>(other) };
 
40
 
 
41
    if (WTF::holds_alternative<Ref<SharedBuffer>>(other))
 
42
        return { WTF::get<Ref<SharedBuffer>>(other).copyRef() };
 
43
 
 
44
    return { };
 
45
}
 
46
 
 
47
PasteboardCustomData::Entry::Entry(const Entry& entry)
 
48
    : type(entry.type)
 
49
    , customData(entry.customData)
 
50
    , platformData(copyPlatformData(entry.platformData))
 
51
{
 
52
}
 
53
 
 
54
PasteboardCustomData::Entry::Entry(const String& dataType)
 
55
    : type(dataType)
 
56
{
 
57
}
 
58
 
 
59
PasteboardCustomData::Entry::Entry() = default;
 
60
PasteboardCustomData::Entry::Entry(Entry&&) = default;
 
61
 
 
62
PasteboardCustomData::Entry& PasteboardCustomData::Entry::operator=(const Entry& entry)
 
63
{
 
64
    type = entry.type;
 
65
    customData = entry.customData;
 
66
    platformData = copyPlatformData(entry.platformData);
 
67
    return *this;
 
68
}
 
69
 
 
70
PasteboardCustomData::Entry& PasteboardCustomData::Entry::operator=(Entry&&) = default;
 
71
 
 
72
PasteboardCustomData::PasteboardCustomData() = default;
 
73
PasteboardCustomData::PasteboardCustomData(const PasteboardCustomData&) = default;
 
74
PasteboardCustomData::PasteboardCustomData(PasteboardCustomData&&) = default;
 
75
PasteboardCustomData::~PasteboardCustomData() = default;
 
76
 
 
77
PasteboardCustomData::PasteboardCustomData(String&& origin, Vector<Entry>&& data)
 
78
    : m_origin(WTFMove(origin))
 
79
    , m_data(WTFMove(data))
 
80
{
 
81
}
 
82
 
 
83
Ref<SharedBuffer> PasteboardCustomData::createSharedBuffer() const
 
84
{
 
85
    constexpr unsigned currentCustomDataSerializationVersion = 1;
 
86
 
 
87
    WTF::Persistence::Encoder encoder;
 
88
    encoder << currentCustomDataSerializationVersion;
 
89
    encoder << m_origin;
 
90
    encoder << sameOriginCustomStringData();
 
91
    encoder << orderedTypes();
 
92
    return SharedBuffer::create(encoder.buffer(), encoder.bufferSize());
 
93
}
 
94
 
 
95
PasteboardCustomData PasteboardCustomData::fromSharedBuffer(const SharedBuffer& buffer)
 
96
{
 
97
    constexpr unsigned maxSupportedDataSerializationVersionNumber = 1;
 
98
 
 
99
    PasteboardCustomData result;
 
100
    auto decoder = buffer.decoder();
 
101
    unsigned version;
 
102
    if (!decoder.decode(version) || version > maxSupportedDataSerializationVersionNumber)
 
103
        return { };
 
104
 
 
105
    if (!decoder.decode(result.m_origin))
 
106
        return { };
 
107
 
 
108
    HashMap<String, String> sameOriginCustomStringData;
 
109
    if (!decoder.decode(sameOriginCustomStringData))
 
110
        return { };
 
111
 
 
112
    Vector<String> orderedTypes;
 
113
    if (!decoder.decode(orderedTypes))
 
114
        return { };
 
115
 
 
116
    for (auto& type : orderedTypes)
 
117
        result.writeStringInCustomData(type, sameOriginCustomStringData.get(type));
 
118
 
 
119
    return result;
 
120
}
 
121
 
 
122
void PasteboardCustomData::writeString(const String& type, const String& value)
 
123
{
 
124
    addOrMoveEntryToEnd(type).platformData = { value };
 
125
}
 
126
 
 
127
void PasteboardCustomData::writeData(const String& type, Ref<SharedBuffer>&& data)
 
128
{
 
129
    addOrMoveEntryToEnd(type).platformData = { WTFMove(data) };
 
130
}
 
131
 
 
132
void PasteboardCustomData::writeStringInCustomData(const String& type, const String& value)
 
133
{
 
134
    addOrMoveEntryToEnd(type).customData = value;
 
135
}
 
136
 
 
137
PasteboardCustomData::Entry& PasteboardCustomData::addOrMoveEntryToEnd(const String& type)
 
138
{
 
139
    auto index = m_data.findMatching([&] (auto& entry) {
 
140
        return entry.type == type;
 
141
    });
 
142
    auto entry = index == notFound ? Entry(type) : m_data[index];
 
143
    if (index != notFound)
 
144
        m_data.remove(index);
 
145
    m_data.append(WTFMove(entry));
 
146
    return m_data.last();
 
147
}
 
148
 
 
149
void PasteboardCustomData::clear()
 
150
{
 
151
    m_data.clear();
 
152
}
 
153
 
 
154
void PasteboardCustomData::clear(const String& type)
 
155
{
 
156
    m_data.removeFirstMatching([&] (auto& entry) {
 
157
        return entry.type == type;
 
158
    });
 
159
}
 
160
 
 
161
PasteboardCustomData& PasteboardCustomData::operator=(const PasteboardCustomData& other)
 
162
{
 
163
    m_origin = other.origin();
 
164
    m_data = other.m_data;
 
165
    return *this;
 
166
}
 
167
 
 
168
Vector<String> PasteboardCustomData::orderedTypes() const
 
169
{
 
170
    return m_data.map([&] (auto& entry) {
 
171
        return entry.type;
 
172
    });
 
173
}
 
174
 
 
175
bool PasteboardCustomData::hasData() const
 
176
{
 
177
    return !m_data.isEmpty();
 
178
}
 
179
 
 
180
bool PasteboardCustomData::hasSameOriginCustomData() const
 
181
{
 
182
    return notFound != m_data.findMatching([&] (auto& entry) {
 
183
        return !entry.customData.isNull();
 
184
    });
 
185
}
 
186
 
 
187
HashMap<String, String> PasteboardCustomData::sameOriginCustomStringData() const
 
188
{
 
189
    HashMap<String, String> customData;
 
190
    for (auto& entry : m_data)
 
191
        customData.set(entry.type, entry.customData);
 
192
    return customData;
 
193
}
 
194
 
 
195
RefPtr<SharedBuffer> PasteboardCustomData::readBuffer(const String& type) const
 
196
{
 
197
    for (auto& entry : m_data) {
 
198
        if (entry.type != type)
 
199
            continue;
 
200
 
 
201
        if (WTF::holds_alternative<Ref<SharedBuffer>>(entry.platformData))
 
202
            return makeRefPtr(WTF::get<Ref<SharedBuffer>>(entry.platformData).get());
 
203
 
 
204
        return nullptr;
 
205
    }
 
206
    return nullptr;
 
207
}
 
208
 
 
209
String PasteboardCustomData::readString(const String& type) const
 
210
{
 
211
    for (auto& entry : m_data) {
 
212
        if (entry.type != type)
 
213
            continue;
 
214
 
 
215
        if (WTF::holds_alternative<String>(entry.platformData))
 
216
            return WTF::get<String>(entry.platformData);
 
217
 
 
218
        return { };
 
219
    }
 
220
    return { };
 
221
}
 
222
 
 
223
String PasteboardCustomData::readStringInCustomData(const String& type) const
 
224
{
 
225
    for (auto& entry : m_data) {
 
226
        if (entry.type == type)
 
227
            return entry.customData;
 
228
    }
 
229
    return { };
 
230
}
 
231
 
 
232
void PasteboardCustomData::forEachType(Function<void(const String&)>&& function) const
 
233
{
 
234
    for (auto& entry : m_data)
 
235
        function(entry.type);
 
236
}
 
237
 
 
238
void PasteboardCustomData::forEachPlatformString(Function<void(const String& type, const String& data)>&& function) const
 
239
{
 
240
    for (auto& entry : m_data) {
 
241
        if (!WTF::holds_alternative<String>(entry.platformData))
 
242
            continue;
 
243
 
 
244
        auto string = WTF::get<String>(entry.platformData);
 
245
        if (!string.isNull())
 
246
            function(entry.type, string);
 
247
    }
 
248
}
 
249
 
 
250
void PasteboardCustomData::forEachCustomString(Function<void(const String& type, const String& data)>&& function) const
 
251
{
 
252
    for (auto& entry : m_data) {
 
253
        if (!entry.customData.isNull())
 
254
            function(entry.type, entry.customData);
 
255
    }
 
256
}
 
257
 
 
258
void PasteboardCustomData::forEachPlatformStringOrBuffer(Function<void(const String& type, const Variant<String, Ref<SharedBuffer>>& data)>&& function) const
 
259
{
 
260
    for (auto& entry : m_data) {
 
261
        auto& data = entry.platformData;
 
262
        if ((WTF::holds_alternative<String>(data) && !WTF::get<String>(data).isNull()) || WTF::holds_alternative<Ref<SharedBuffer>>(data))
 
263
            function(entry.type, data);
 
264
    }
 
265
}
 
266
 
 
267
} // namespace WebCore