File: RTCDataChannelRemoteManager.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (249 lines) | stat: -rw-r--r-- 10,990 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2021 Apple Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are 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 APPLE 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 APPLE INC. OR 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 "RTCDataChannelRemoteManager.h"

#if ENABLE(WEB_RTC)

#include "Connection.h"
#include "NetworkConnectionToWebProcessMessages.h"
#include "NetworkProcessConnection.h"
#include "RTCDataChannelRemoteManagerMessages.h"
#include "RTCDataChannelRemoteManagerProxyMessages.h"
#include "WebProcess.h"
#include <WebCore/RTCDataChannel.h>
#include <WebCore/RTCError.h>
#include <WebCore/ScriptExecutionContext.h>

namespace WebKit {

RTCDataChannelRemoteManager& RTCDataChannelRemoteManager::singleton()
{
    static NeverDestroyed<Ref<RTCDataChannelRemoteManager>> sharedManager = [] {
        Ref instance = adoptRef(*new RTCDataChannelRemoteManager);
        instance->initialize();
        return instance;
    }();
    return sharedManager.get();
}

RTCDataChannelRemoteManager::RTCDataChannelRemoteManager()
    : m_queue(WorkQueue::create("RTCDataChannelRemoteManager"_s))
    , m_connection(&WebProcess::singleton().ensureNetworkProcessConnection().connection())
{
}

void RTCDataChannelRemoteManager::initialize()
{
    // FIXME: If the network process crashes, all RTC data will be misdelivered for the web process.
    // https://bugs.webkit.org/show_bug.cgi?id=245062
    m_connection->addMessageReceiver(m_queue, *this, Messages::RTCDataChannelRemoteManager::messageReceiverName());
}

bool RTCDataChannelRemoteManager::connectToRemoteSource(WebCore::RTCDataChannelIdentifier localIdentifier, WebCore::RTCDataChannelIdentifier remoteIdentifier)
{
    ASSERT(WebCore::Process::identifier() == localIdentifier.processIdentifier());
    if (WebCore::Process::identifier() != localIdentifier.processIdentifier())
        return false;

    auto handler = WebCore::RTCDataChannel::handlerFromIdentifier(localIdentifier.object());
    if (!handler)
        return false;

    auto iterator = m_sources.add(remoteIdentifier.object(), makeUniqueRef<WebCore::RTCDataChannelRemoteSource>(remoteIdentifier, makeUniqueRefFromNonNullUniquePtr(WTFMove(handler)), remoteSourceConnection()));
    return iterator.isNewEntry;
}

WebCore::RTCDataChannelRemoteHandlerConnection& RTCDataChannelRemoteManager::remoteHandlerConnection()
{
    if (!m_remoteHandlerConnection)
        m_remoteHandlerConnection = RemoteHandlerConnection::create(m_queue.copyRef());
    return *m_remoteHandlerConnection;
}

WebCore::RTCDataChannelRemoteSourceConnection& RTCDataChannelRemoteManager::remoteSourceConnection()
{
    if (!m_remoteSourceConnection)
        m_remoteSourceConnection = RemoteSourceConnection::create();
    return *m_remoteSourceConnection;
}

void RTCDataChannelRemoteManager::postTaskToHandler(WebCore::RTCDataChannelIdentifier handlerIdentifier, Function<void(WebCore::RTCDataChannelRemoteHandler&)>&& function)
{
    ASSERT(WebCore::Process::identifier() == handlerIdentifier.processIdentifier());
    if (WebCore::Process::identifier() != handlerIdentifier.processIdentifier())
        return;

    auto iterator = m_handlers.find(handlerIdentifier.object());
    if (iterator == m_handlers.end())
        return;
    auto& remoteHandler = iterator->value;

    WebCore::ScriptExecutionContext::postTaskTo(*remoteHandler.contextIdentifier, [handler = remoteHandler.handler, function = WTFMove(function)](auto&) mutable {
        if (handler)
            function(*handler);
    });
}

WebCore::RTCDataChannelRemoteSource* RTCDataChannelRemoteManager::sourceFromIdentifier(WebCore::RTCDataChannelIdentifier sourceIdentifier)
{
    ASSERT(WebCore::Process::identifier() == sourceIdentifier.processIdentifier());
    if (WebCore::Process::identifier() != sourceIdentifier.processIdentifier())
        return nullptr;

    return m_sources.get(sourceIdentifier.object());
}

void RTCDataChannelRemoteManager::sendData(WebCore::RTCDataChannelIdentifier sourceIdentifier, bool isRaw, std::span<const uint8_t> data)
{
    if (auto* source = sourceFromIdentifier(sourceIdentifier)) {
        if (isRaw)
            source->sendRawData(data);
        else
            source->sendStringData(CString(data));
    }
}

void RTCDataChannelRemoteManager::close(WebCore::RTCDataChannelIdentifier sourceIdentifier)
{
    if (auto* source = sourceFromIdentifier(sourceIdentifier))
        source->close();
}

void RTCDataChannelRemoteManager::changeReadyState(WebCore::RTCDataChannelIdentifier handlerIdentifier, WebCore::RTCDataChannelState state)
{
    postTaskToHandler(handlerIdentifier, [state](auto& handler) {
        handler.didChangeReadyState(state);
    });
}

void RTCDataChannelRemoteManager::receiveData(WebCore::RTCDataChannelIdentifier handlerIdentifier, bool isRaw, std::span<const uint8_t> data)
{
    Vector<uint8_t> buffer;
    String text;
    if (isRaw)
        buffer = Vector(data);
    else
        text = String::fromUTF8(data);

    postTaskToHandler(handlerIdentifier, [isRaw, text = WTFMove(text).isolatedCopy(), buffer = WTFMove(buffer)](auto& handler) mutable {
        if (isRaw)
            handler.didReceiveRawData(buffer.span());
        else
            handler.didReceiveStringData(WTFMove(text));
    });
}

void RTCDataChannelRemoteManager::detectError(WebCore::RTCDataChannelIdentifier handlerIdentifier, WebCore::RTCErrorDetailType detail, String&& message)
{
    postTaskToHandler(handlerIdentifier, [detail, message = WTFMove(message)](auto& handler) mutable {
        handler.didDetectError(WebCore::RTCError::create(detail, WTFMove(message)));
    });
}

void RTCDataChannelRemoteManager::bufferedAmountIsDecreasing(WebCore::RTCDataChannelIdentifier handlerIdentifier, size_t amount)
{
    postTaskToHandler(handlerIdentifier, [amount](auto& handler) {
        handler.bufferedAmountIsDecreasing(amount);
    });
}

Ref<RTCDataChannelRemoteManager::RemoteHandlerConnection> RTCDataChannelRemoteManager::RemoteHandlerConnection::create(Ref<WorkQueue>&& queue)
{
    return adoptRef(*new RemoteHandlerConnection(WTFMove(queue)));
}

RTCDataChannelRemoteManager::RemoteHandlerConnection::RemoteHandlerConnection(Ref<WorkQueue>&& queue)
    : m_connection(WebProcess::singleton().ensureNetworkProcessConnection().connection())
    , m_queue(WTFMove(queue))
{
}

void RTCDataChannelRemoteManager::RemoteHandlerConnection::connectToSource(WebCore::RTCDataChannelRemoteHandler& handler, std::optional<WebCore::ScriptExecutionContextIdentifier> contextIdentifier, WebCore::RTCDataChannelIdentifier localIdentifier, WebCore::RTCDataChannelIdentifier remoteIdentifier)
{
    m_queue->dispatch([handler = WeakPtr { handler }, contextIdentifier, localIdentifier]() mutable {
        RTCDataChannelRemoteManager::singleton().m_handlers.add(localIdentifier.object(), RemoteHandler { WTFMove(handler), *contextIdentifier });
    });
    m_connection->sendWithAsyncReply(Messages::NetworkConnectionToWebProcess::ConnectToRTCDataChannelRemoteSource { localIdentifier, remoteIdentifier }, [localIdentifier](auto&& result) {
        RTCDataChannelRemoteManager::singleton().postTaskToHandler(localIdentifier, [result](auto& handler) {
            if (!result || !*result) {
                handler.didDetectError(WebCore::RTCError::create(WebCore::RTCErrorDetailType::DataChannelFailure, "Unable to find data channel"_s));
                return;
            }
            handler.readyToSend();
        });
    }, 0);
}

void RTCDataChannelRemoteManager::RemoteHandlerConnection::sendData(WebCore::RTCDataChannelIdentifier identifier, bool isRaw, std::span<const uint8_t> data)
{
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::SendData { identifier, isRaw, data }, 0);
}

void RTCDataChannelRemoteManager::RemoteHandlerConnection::close(WebCore::RTCDataChannelIdentifier identifier)
{
    // FIXME: We need to wait to send this message until RTCDataChannelRemoteManagerProxy::ConnectToSource is actually sent.
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::Close { identifier }, 0);
}

Ref<RTCDataChannelRemoteManager::RemoteSourceConnection> RTCDataChannelRemoteManager::RemoteSourceConnection::create()
{
    return adoptRef(*new RemoteSourceConnection);
}

RTCDataChannelRemoteManager::RemoteSourceConnection::RemoteSourceConnection()
    : m_connection(WebProcess::singleton().ensureNetworkProcessConnection().connection())
{
}

void RTCDataChannelRemoteManager::RemoteSourceConnection::didChangeReadyState(WebCore::RTCDataChannelIdentifier identifier, WebCore::RTCDataChannelState state)
{
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::ChangeReadyState { identifier, state }, 0);
}

void RTCDataChannelRemoteManager::RemoteSourceConnection::didReceiveStringData(WebCore::RTCDataChannelIdentifier identifier, const String& string)
{
    auto text = string.utf8();
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::ReceiveData { identifier, false, byteCast<uint8_t>(text.span()) }, 0);
}

void RTCDataChannelRemoteManager::RemoteSourceConnection::didReceiveRawData(WebCore::RTCDataChannelIdentifier identifier, std::span<const uint8_t> data)
{
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::ReceiveData { identifier, true, data }, 0);
}

void RTCDataChannelRemoteManager::RemoteSourceConnection::didDetectError(WebCore::RTCDataChannelIdentifier identifier, WebCore::RTCErrorDetailType type, const String& message)
{
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::DetectError { identifier, type, message }, 0);
}

void RTCDataChannelRemoteManager::RemoteSourceConnection::bufferedAmountIsDecreasing(WebCore::RTCDataChannelIdentifier identifier, size_t amount)
{
    m_connection->send(Messages::RTCDataChannelRemoteManagerProxy::BufferedAmountIsDecreasing { identifier, amount }, 0);
}

}

#endif