MessagePortChannelRegistry.cpp [plain text]
#include "config.h"
#include "MessagePortChannelRegistry.h"
#include <wtf/MainThread.h>
namespace WebCore {
MessagePortChannelRegistry::MessagePortChannelRegistry(MessagePortChannelProvider& provider)
: m_provider(provider)
{
}
MessagePortChannelRegistry::~MessagePortChannelRegistry()
{
ASSERT(m_openChannels.isEmpty());
}
void MessagePortChannelRegistry::didCreateMessagePortChannel(const MessagePortIdentifier& port1, const MessagePortIdentifier& port2)
{
LOG(MessagePorts, "Registry: Creating MessagePortChannel %p linking %s and %s", this, port1.logString().utf8().data(), port2.logString().utf8().data());
ASSERT(isMainThread());
MessagePortChannel::create(*this, port1, port2);
}
void MessagePortChannelRegistry::messagePortChannelCreated(MessagePortChannel& channel)
{
ASSERT(isMainThread());
auto result = m_openChannels.ensure(channel.port1(), [channel = &channel] {
return channel;
});
ASSERT(result.isNewEntry);
result = m_openChannels.ensure(channel.port2(), [channel = &channel] {
return channel;
});
ASSERT(result.isNewEntry);
}
void MessagePortChannelRegistry::messagePortChannelDestroyed(MessagePortChannel& channel)
{
ASSERT(isMainThread());
ASSERT(m_openChannels.get(channel.port1()) == &channel);
ASSERT(m_openChannels.get(channel.port2()) == &channel);
m_openChannels.remove(channel.port1());
m_openChannels.remove(channel.port2());
LOG(MessagePorts, "Registry: After removing channel %s there are %u channels left in the registry:", channel.logString().utf8().data(), m_openChannels.size());
}
void MessagePortChannelRegistry::didEntangleLocalToRemote(const MessagePortIdentifier& local, const MessagePortIdentifier& remote, ProcessIdentifier process)
{
ASSERT(isMainThread());
auto* channel = m_openChannels.get(local);
if (!channel)
return;
ASSERT_UNUSED(remote, channel->includesPort(remote));
channel->entanglePortWithProcess(local, process);
}
void MessagePortChannelRegistry::didDisentangleMessagePort(const MessagePortIdentifier& port)
{
ASSERT(isMainThread());
auto* channel = m_openChannels.get(port);
if (!channel)
return;
channel->disentanglePort(port);
}
void MessagePortChannelRegistry::didCloseMessagePort(const MessagePortIdentifier& port)
{
ASSERT(isMainThread());
LOG(MessagePorts, "Registry: MessagePort %s closed in registry", port.logString().utf8().data());
auto* channel = m_openChannels.get(port);
if (!channel)
return;
#ifndef NDEBUG
if (channel && channel->hasAnyMessagesPendingOrInFlight())
LOG(MessagePorts, "Registry: (Note) The channel closed for port %s had messages pending or in flight", port.logString().utf8().data());
#endif
channel->closePort(port);
}
bool MessagePortChannelRegistry::didPostMessageToRemote(MessageWithMessagePorts&& message, const MessagePortIdentifier& remoteTarget)
{
ASSERT(isMainThread());
LOG(MessagePorts, "Registry: Posting message to MessagePort %s in registry", remoteTarget.logString().utf8().data());
auto* channel = m_openChannels.get(remoteTarget);
if (!channel) {
LOG(MessagePorts, "Registry: Could not find MessagePortChannel for port %s; It was probably closed. Message will be dropped.", remoteTarget.logString().utf8().data());
return false;
}
return channel->postMessageToRemote(WTFMove(message), remoteTarget);
}
void MessagePortChannelRegistry::takeAllMessagesForPort(const MessagePortIdentifier& port, Function<void(Vector<MessageWithMessagePorts>&&, Function<void()>&&)>&& callback)
{
ASSERT(isMainThread());
LOG(MessagePorts, "Registry: Taking all messages for MessagePort %s", port.logString().utf8().data());
auto* channel = m_openChannels.get(port);
if (!channel) {
callback({ }, [] { });
return;
}
channel->takeAllMessagesForPort(port, WTFMove(callback));
}
void MessagePortChannelRegistry::checkRemotePortForActivity(const MessagePortIdentifier& remoteTarget, CompletionHandler<void(MessagePortChannelProvider::HasActivity)>&& callback)
{
ASSERT(isMainThread());
auto* channel = m_openChannels.get(remoteTarget);
if (!channel) {
callback(MessagePortChannelProvider::HasActivity::No);
return;
}
channel->checkRemotePortForActivity(remoteTarget, WTFMove(callback));
}
MessagePortChannel* MessagePortChannelRegistry::existingChannelContainingPort(const MessagePortIdentifier& port)
{
ASSERT(isMainThread());
return m_openChannels.get(port);
}
}