aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/webcore/BunBroadcastChannelRegistry.cpp
blob: 258d297170bded30ffed0d5491258683795964f9 (plain) (blame)
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
#include "config.h"

#include "BunBroadcastChannelRegistry.h"
#include "webcore/BroadcastChannel.h"
#include "webcore/MessageWithMessagePorts.h"
#include <wtf/CallbackAggregator.h>

namespace WebCore {

void BunBroadcastChannelRegistry::registerChannel(const String& name, BroadcastChannelIdentifier identifier)
{
    auto& channels = m_channelsForName.ensure(name, [] { return Vector<BroadcastChannelIdentifier> {}; }).iterator->value;
    channels.append(identifier);
}

void BunBroadcastChannelRegistry::unregisterChannel(const String& name, BroadcastChannelIdentifier identifier)
{
    auto channels = m_channelsForName.find(name);
    if (channels == m_channelsForName.end())
        return;

    auto& channelIds = channels->value;
    channelIds.removeFirst(identifier);
}

void BunBroadcastChannelRegistry::postMessage(const String& name, BroadcastChannelIdentifier source, Ref<SerializedScriptValue>&& message)
{
    postMessageLocally(name, source, message.copyRef());
}

void BunBroadcastChannelRegistry::postMessageLocally(const String& name, BroadcastChannelIdentifier sourceInProcess, Ref<SerializedScriptValue>&& message)
{
    auto channels = m_channelsForName.find(name);
    if (channels == m_channelsForName.end())
        return;

    auto& channelIds = channels->value;
    for (auto& channelId : channelIds) {
        if (channelId == sourceInProcess)
            continue;

        BroadcastChannel::dispatchMessageTo(channelId, message.copyRef());
    }
}

void BunBroadcastChannelRegistry::postMessageToRemote(const String& name, MessageWithMessagePorts&& message)
{
    // auto callbackAggregator = CallbackAggregator::create(WTFMove(completionHandler));
    // PartitionedSecurityOrigin origin { clientOrigin.topOrigin.securityOrigin(), clientOrigin.clientOrigin.securityOrigin() };
    // postMessageLocally(origin, name, std::nullopt, *message.message, callbackAggregator.copyRef());
}
}