PlatformMessagePortChannel.cpp [plain text]
#include "config.h"
#include "PlatformMessagePortChannel.h"
#include "MessagePort.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
void MessagePortChannel::createChannel(MessagePort* port1, MessagePort* port2)
{
Ref<PlatformMessagePortChannel::MessagePortQueue> queue1 = PlatformMessagePortChannel::MessagePortQueue::create();
Ref<PlatformMessagePortChannel::MessagePortQueue> queue2 = PlatformMessagePortChannel::MessagePortQueue::create();
auto channel1 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue1.ptr(), queue2.ptr()));
auto channel2 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue2.ptr(), queue1.ptr()));
channel1->m_channel->m_entangledChannel = channel2->m_channel;
channel2->m_channel->m_entangledChannel = channel1->m_channel;
port1->entangle(WTFMove(channel2));
port2->entangle(WTFMove(channel1));
}
MessagePortChannel::MessagePortChannel(RefPtr<PlatformMessagePortChannel>&& channel)
: m_channel(WTFMove(channel))
{
}
MessagePortChannel::~MessagePortChannel()
{
close();
}
bool MessagePortChannel::entangleIfOpen(MessagePort* port)
{
RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
if (!remote)
return false;
remote->setRemotePort(port);
return true;
}
void MessagePortChannel::disentangle()
{
RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
if (remote)
remote->setRemotePort(nullptr);
}
void MessagePortChannel::postMessageToRemote(Ref<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray> channels)
{
LockHolder lock(m_channel->m_mutex);
if (!m_channel->m_outgoingQueue)
return;
bool wasEmpty = m_channel->m_outgoingQueue->appendAndCheckEmpty(std::make_unique<EventData>(WTFMove(message), WTFMove(channels)));
if (wasEmpty && m_channel->m_remotePort)
m_channel->m_remotePort->messageAvailable();
}
auto MessagePortChannel::takeMessageFromRemote() -> std::unique_ptr<EventData>
{
LockHolder lock(m_channel->m_mutex);
return m_channel->m_incomingQueue->takeMessage();
}
auto MessagePortChannel::takeAllMessagesFromRemote() -> Deque<std::unique_ptr<EventData>>
{
LockHolder lock(m_channel->m_mutex);
return m_channel->m_incomingQueue->takeAllMessages();
}
void MessagePortChannel::close()
{
RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
if (!remote)
return;
m_channel->closeInternal();
remote->closeInternal();
}
bool MessagePortChannel::isConnectedTo(MessagePort* port)
{
LockHolder lock(m_channel->m_mutex);
return m_channel->m_remotePort == port;
}
bool MessagePortChannel::hasPendingActivity()
{
LockHolder lock(m_channel->m_mutex);
return !m_channel->m_incomingQueue->isEmpty();
}
MessagePort* MessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
{
LockHolder lock(m_channel->m_mutex);
if (m_channel->m_remotePort) {
ScriptExecutionContext* remoteContext = m_channel->m_remotePort->scriptExecutionContext();
if (remoteContext == context || (remoteContext && remoteContext->isDocument() && context->isDocument()))
return m_channel->m_remotePort;
}
return 0;
}
Ref<PlatformMessagePortChannel> PlatformMessagePortChannel::create(MessagePortQueue* incoming, MessagePortQueue* outgoing)
{
return adoptRef(*new PlatformMessagePortChannel(incoming, outgoing));
}
PlatformMessagePortChannel::PlatformMessagePortChannel(MessagePortQueue* incoming, MessagePortQueue* outgoing)
: m_incomingQueue(incoming)
, m_outgoingQueue(outgoing)
{
}
PlatformMessagePortChannel::~PlatformMessagePortChannel()
{
}
void PlatformMessagePortChannel::setRemotePort(MessagePort* port)
{
LockHolder lock(m_mutex);
ASSERT(!port || !m_remotePort);
m_remotePort = port;
}
RefPtr<PlatformMessagePortChannel> PlatformMessagePortChannel::entangledChannel()
{
LockHolder lock(m_mutex);
return m_entangledChannel;
}
void PlatformMessagePortChannel::closeInternal()
{
LockHolder lock(m_mutex);
m_remotePort = nullptr;
m_entangledChannel = nullptr;
m_outgoingQueue = nullptr;
}
}