#pragma once
#include "EventTarget.h"
#include "ExceptionOr.h"
#include "MessagePortChannel.h"
namespace JSC {
class ExecState;
class JSObject;
class JSValue;
}
namespace WebCore {
class Frame;
class MessagePort final : public RefCounted<MessagePort>, public EventTargetWithInlineData {
public:
static Ref<MessagePort> create(ScriptExecutionContext& scriptExecutionContext) { return adoptRef(*new MessagePort(scriptExecutionContext)); }
virtual ~MessagePort();
ExceptionOr<void> postMessage(JSC::ExecState&, JSC::JSValue message, Vector<JSC::Strong<JSC::JSObject>>&&);
void start();
void close();
void entangle(std::unique_ptr<MessagePortChannel>&&);
static ExceptionOr<std::unique_ptr<MessagePortChannelArray>> disentanglePorts(Vector<RefPtr<MessagePort>>&&);
static Vector<RefPtr<MessagePort>> entanglePorts(ScriptExecutionContext&, std::unique_ptr<MessagePortChannelArray>&&);
void messageAvailable();
bool started() const { return m_started; }
void contextDestroyed();
ScriptExecutionContext* scriptExecutionContext() const final { return m_scriptExecutionContext; }
void dispatchMessages();
bool hasPendingActivity();
MessagePort* locallyEntangledPort();
using RefCounted::ref;
using RefCounted::deref;
private:
explicit MessagePort(ScriptExecutionContext&);
void refEventTarget() final { ref(); }
void derefEventTarget() final { deref(); }
bool isMessagePort() const final { return true; }
EventTargetInterface eventTargetInterface() const final { return MessagePortEventTargetInterfaceType; }
bool addEventListener(const AtomicString& eventType, Ref<EventListener>&&, const AddEventListenerOptions&) final;
std::unique_ptr<MessagePortChannel> disentangle();
bool isEntangled() const { return !m_closed && !isNeutered(); }
bool isNeutered() const { return !m_entangledChannel; }
std::unique_ptr<MessagePortChannel> m_entangledChannel;
bool m_started { false };
bool m_closed { false };
ScriptExecutionContext* m_scriptExecutionContext;
};
}