This source file includes following definitions.
- create
- m_weakFactory
- postMessage
- toWebMessagePortChannelArray
- toMessagePortArray
- disentangle
- messageAvailable
- start
- close
- entangle
- interfaceName
- tryGetMessageFrom
- dispatchMessages
- hasPendingActivity
- disentanglePorts
- entanglePorts
#include "config.h"
#include "core/dom/MessagePort.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "bindings/v8/SerializedScriptValue.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "core/events/MessageEvent.h"
#include "core/frame/DOMWindow.h"
#include "core/workers/WorkerGlobalScope.h"
#include "public/platform/WebString.h"
#include "wtf/Functional.h"
#include "wtf/text/AtomicString.h"
namespace WebCore {
PassRefPtr<MessagePort> MessagePort::create(ExecutionContext& executionContext)
{
RefPtr<MessagePort> port = adoptRef(new MessagePort(executionContext));
port->suspendIfNeeded();
return port.release();
}
MessagePort::MessagePort(ExecutionContext& executionContext)
: ActiveDOMObject(&executionContext)
, m_started(false)
, m_closed(false)
, m_weakFactory(this)
{
ScriptWrappable::init(this);
}
MessagePort::~MessagePort()
{
close();
}
void MessagePort::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState)
{
if (!isEntangled())
return;
ASSERT(executionContext());
ASSERT(m_entangledChannel);
OwnPtr<MessagePortChannelArray> channels;
if (ports) {
for (unsigned i = 0; i < ports->size(); ++i) {
MessagePort* dataPort = (*ports)[i].get();
if (dataPort == this) {
exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " contains the source port.");
return;
}
}
channels = MessagePort::disentanglePorts(ports, exceptionState);
if (exceptionState.hadException())
return;
}
blink::WebString messageString = message->toWireString();
OwnPtr<blink::WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArray(channels.release());
m_entangledChannel->postMessage(messageString, webChannels.leakPtr());
}
PassOwnPtr<blink::WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray(PassOwnPtr<MessagePortChannelArray> channels)
{
OwnPtr<blink::WebMessagePortChannelArray> webChannels;
if (channels && channels->size()) {
webChannels = adoptPtr(new blink::WebMessagePortChannelArray(channels->size()));
for (size_t i = 0; i < channels->size(); ++i)
(*webChannels)[i] = (*channels)[i].leakPtr();
}
return webChannels.release();
}
PassOwnPtr<MessagePortArray> MessagePort::toMessagePortArray(ExecutionContext* context, const blink::WebMessagePortChannelArray& webChannels)
{
OwnPtr<MessagePortArray> ports;
if (!webChannels.isEmpty()) {
OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
for (size_t i = 0; i < webChannels.size(); ++i)
(*channels)[i] = adoptPtr(webChannels[i]);
ports = MessagePort::entanglePorts(*context, channels.release());
}
return ports.release();
}
PassOwnPtr<blink::WebMessagePortChannel> MessagePort::disentangle()
{
ASSERT(m_entangledChannel);
m_entangledChannel->setClient(0);
return m_entangledChannel.release();
}
void MessagePort::messageAvailable()
{
ASSERT(executionContext());
executionContext()->postTask(bind(&MessagePort::dispatchMessages, m_weakFactory.createWeakPtr()));
}
void MessagePort::start()
{
if (!isEntangled())
return;
ASSERT(executionContext());
if (m_started)
return;
m_started = true;
messageAvailable();
}
void MessagePort::close()
{
if (isEntangled())
m_entangledChannel->setClient(0);
m_closed = true;
}
void MessagePort::entangle(PassOwnPtr<blink::WebMessagePortChannel> remote)
{
ASSERT(!m_entangledChannel);
ASSERT(executionContext());
m_entangledChannel = remote;
m_entangledChannel->setClient(this);
}
const AtomicString& MessagePort::interfaceName() const
{
return EventTargetNames::MessagePort;
}
static bool tryGetMessageFrom(blink::WebMessagePortChannel& webChannel, RefPtr<SerializedScriptValue>& message, OwnPtr<MessagePortChannelArray>& channels)
{
blink::WebString messageString;
blink::WebMessagePortChannelArray webChannels;
if (!webChannel.tryGetMessage(&messageString, webChannels))
return false;
if (webChannels.size()) {
channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
for (size_t i = 0; i < webChannels.size(); ++i)
(*channels)[i] = adoptPtr(webChannels[i]);
}
message = SerializedScriptValue::createFromWire(messageString);
return true;
}
void MessagePort::dispatchMessages()
{
if (!started())
return;
RefPtr<SerializedScriptValue> message;
OwnPtr<MessagePortChannelArray> channels;
while (m_entangledChannel && tryGetMessageFrom(*m_entangledChannel, message, channels)) {
if (executionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(executionContext())->isClosing())
return;
OwnPtr<MessagePortArray> ports = MessagePort::entanglePorts(*executionContext(), channels.release());
RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), message.release());
dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION);
}
}
bool MessagePort::hasPendingActivity() const
{
return m_started && isEntangled();
}
PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(const MessagePortArray* ports, ExceptionState& exceptionState)
{
if (!ports || !ports->size())
return nullptr;
HashSet<MessagePort*> portSet;
for (unsigned i = 0; i < ports->size(); ++i) {
MessagePort* port = (*ports)[i].get();
if (!port || port->isNeutered() || portSet.contains(port)) {
String type;
if (!port)
type = "null";
else if (port->isNeutered())
type = "already neutered";
else
type = "a duplicate";
exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " is " + type + ".");
return nullptr;
}
portSet.add(port);
}
OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelArray(ports->size()));
for (unsigned i = 0; i < ports->size(); ++i)
(*portArray)[i] = (*ports)[i]->disentangle();
return portArray.release();
}
PassOwnPtr<MessagePortArray> MessagePort::entanglePorts(ExecutionContext& context, PassOwnPtr<MessagePortChannelArray> channels)
{
if (!channels || !channels->size())
return nullptr;
OwnPtr<MessagePortArray> portArray = adoptPtr(new MessagePortArray(channels->size()));
for (unsigned i = 0; i < channels->size(); ++i) {
RefPtr<MessagePort> port = MessagePort::create(context);
port->entangle((*channels)[i].release());
(*portArray)[i] = port.release();
}
return portArray.release();
}
}