PlatformMessagePortChannel.cpp [plain text]
#include "config.h"
#include "PlatformMessagePortChannel.h"
#include "MessagePort.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
PlatformMessagePortChannel::EventData::EventData(PassRefPtr<SerializedScriptValue> message, std::unique_ptr<MessagePortChannelArray> channels)
: m_message(message)
, m_channels(WTF::move(channels))
{
}
void MessagePortChannel::createChannel(PassRefPtr<MessagePort> port1, PassRefPtr<MessagePort> port2)
{
RefPtr<PlatformMessagePortChannel::MessagePortQueue> queue1 = PlatformMessagePortChannel::MessagePortQueue::create();
RefPtr<PlatformMessagePortChannel::MessagePortQueue> queue2 = PlatformMessagePortChannel::MessagePortQueue::create();
auto channel1 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue1, queue2));
auto channel2 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue2, queue1));
channel1->m_channel->m_entangledChannel = channel2->m_channel;
channel2->m_channel->m_entangledChannel = channel1->m_channel;
port1->entangle(WTF::move(channel2));
port2->entangle(WTF::move(channel1));
}
MessagePortChannel::MessagePortChannel(PassRefPtr<PlatformMessagePortChannel> channel)
: m_channel(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(0);
}
void MessagePortChannel::postMessageToRemote(PassRefPtr<SerializedScriptValue> message, std::unique_ptr<MessagePortChannelArray> channels)
{
MutexLocker lock(m_channel->m_mutex);
if (!m_channel->m_outgoingQueue)
return;
bool wasEmpty = m_channel->m_outgoingQueue->appendAndCheckEmpty(std::make_unique<PlatformMessagePortChannel::EventData>(message, WTF::move(channels)));
if (wasEmpty && m_channel->m_remotePort)
m_channel->m_remotePort->messageAvailable();
}
bool MessagePortChannel::tryGetMessageFromRemote(RefPtr<SerializedScriptValue>& message, std::unique_ptr<MessagePortChannelArray>& channels)
{
MutexLocker lock(m_channel->m_mutex);
auto result = m_channel->m_incomingQueue->tryGetMessage();
if (!result)
return false;
message = result->message();
channels = result->channels();
return true;
}
void MessagePortChannel::close()
{
RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
if (!remote)
return;
m_channel->closeInternal();
remote->closeInternal();
}
bool MessagePortChannel::isConnectedTo(MessagePort* port)
{
MutexLocker lock(m_channel->m_mutex);
return m_channel->m_remotePort == port;
}
bool MessagePortChannel::hasPendingActivity()
{
MutexLocker lock(m_channel->m_mutex);
return !m_channel->m_incomingQueue->isEmpty();
}
MessagePort* MessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
{
MutexLocker 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(PassRefPtr<MessagePortQueue> incoming, PassRefPtr<MessagePortQueue> outgoing)
{
return adoptRef(*new PlatformMessagePortChannel(incoming, outgoing));
}
PlatformMessagePortChannel::PlatformMessagePortChannel(PassRefPtr<MessagePortQueue> incoming, PassRefPtr<MessagePortQueue> outgoing)
: m_incomingQueue(incoming)
, m_outgoingQueue(outgoing)
, m_remotePort(0)
{
}
PlatformMessagePortChannel::~PlatformMessagePortChannel()
{
}
void PlatformMessagePortChannel::setRemotePort(MessagePort* port)
{
MutexLocker lock(m_mutex);
ASSERT(!port || !m_remotePort);
m_remotePort = port;
}
PassRefPtr<PlatformMessagePortChannel> PlatformMessagePortChannel::entangledChannel()
{
MutexLocker lock(m_mutex);
return m_entangledChannel;
}
void PlatformMessagePortChannel::closeInternal()
{
MutexLocker lock(m_mutex);
m_remotePort = nullptr;
m_entangledChannel = nullptr;
m_outgoingQueue = nullptr;
}
}