#ifndef MessagePort_h
#define MessagePort_h
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ActiveDOMObject.h"
#include "core/events/EventListener.h"
#include "core/events/EventTarget.h"
#include "public/platform/WebMessagePortChannel.h"
#include "public/platform/WebMessagePortChannelClient.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include "wtf/WeakPtr.h"
namespace WebCore {
class Event;
class ExceptionState;
class LocalFrame;
class MessagePort;
class ExecutionContext;
class SerializedScriptValue;
typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
typedef Vector<OwnPtr<blink::WebMessagePortChannel>, 1> MessagePortChannelArray;
class MessagePort FINAL : public RefCounted<MessagePort>
, public ActiveDOMObject
, public EventTargetWithInlineData
, public ScriptWrappable
, public blink::WebMessagePortChannelClient {
REFCOUNTED_EVENT_TARGET(MessagePort);
public:
static PassRefPtr<MessagePort> create(ExecutionContext&);
virtual ~MessagePort();
void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, ExceptionState&);
void start();
void close();
void entangle(PassOwnPtr<blink::WebMessagePortChannel>);
PassOwnPtr<blink::WebMessagePortChannel> disentangle();
static PassOwnPtr<blink::WebMessagePortChannelArray> toWebMessagePortChannelArray(PassOwnPtr<MessagePortChannelArray>);
static PassOwnPtr<MessagePortArray> toMessagePortArray(ExecutionContext*, const blink::WebMessagePortChannelArray&);
static PassOwnPtr<MessagePortChannelArray> disentanglePorts(const MessagePortArray*, ExceptionState&);
static PassOwnPtr<MessagePortArray> entanglePorts(ExecutionContext&, PassOwnPtr<MessagePortChannelArray>);
bool started() const { return m_started; }
virtual const AtomicString& interfaceName() const OVERRIDE;
virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
virtual MessagePort* toMessagePort() OVERRIDE { return this; }
virtual bool hasPendingActivity() const OVERRIDE;
virtual void stop() OVERRIDE { close(); }
void setOnmessage(PassRefPtr<EventListener> listener)
{
setAttributeEventListener(EventTypeNames::message, listener);
start();
}
EventListener* onmessage() { return getAttributeEventListener(EventTypeNames::message); }
bool isEntangled() const { return !m_closed && !isNeutered(); }
bool isNeutered() const { return !m_entangledChannel; }
private:
explicit MessagePort(ExecutionContext&);
virtual void messageAvailable() OVERRIDE;
void dispatchMessages();
OwnPtr<blink::WebMessagePortChannel> m_entangledChannel;
bool m_started;
bool m_closed;
WeakPtrFactory<MessagePort> m_weakFactory;
};
}
#endif