#ifndef EventTarget_h
#define EventTarget_h
#include "core/events/EventListenerMap.h"
#include "core/events/ThreadLocalEventNames.h"
#include "heap/Handle.h"
#include "wtf/Forward.h"
namespace WebCore {
class ApplicationCache;
class AudioContext;
class DOMWindow;
class DedicatedWorkerGlobalScope;
class Event;
class EventListener;
class EventSource;
class ExceptionState;
class FileReader;
class FileWriter;
class IDBDatabase;
class IDBRequest;
class IDBTransaction;
class MIDIAccess;
class MIDIInput;
class MIDIPort;
class MediaController;
class MediaStream;
class MessagePort;
class Node;
class Notification;
class SVGElementInstance;
class ExecutionContext;
class ScriptProcessorNode;
class SharedWorker;
class SharedWorkerGlobalScope;
class TextTrack;
class TextTrackCue;
class WebSocket;
class Worker;
class XMLHttpRequest;
class XMLHttpRequestUpload;
struct FiringEventIterator {
FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
: eventType(eventType)
, iterator(iterator)
, end(end)
{
}
const AtomicString& eventType;
size_t& iterator;
size_t& end;
};
typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
struct EventTargetData {
WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
public:
EventTargetData();
~EventTargetData();
EventListenerMap eventListenerMap;
OwnPtr<FiringEventIteratorVector> firingEventIterators;
};
class EventTarget {
public:
void ref() { refEventTarget(); }
void deref() { derefEventTarget(); }
virtual const AtomicString& interfaceName() const = 0;
virtual ExecutionContext* executionContext() const = 0;
virtual Node* toNode();
virtual DOMWindow* toDOMWindow();
virtual MessagePort* toMessagePort();
bool addEventListener() { return false; }
bool addEventListener(const AtomicString& eventType) { return false; }
virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false);
bool removeEventListener() { return false; }
bool removeEventListener(const AtomicString& eventType) { return false; }
virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture = false);
virtual void removeAllEventListeners();
virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>);
bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>, ExceptionState&);
virtual void uncaughtExceptionInEventHandler();
bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
EventListener* getAttributeEventListener(const AtomicString& eventType);
bool hasEventListeners() const;
bool hasEventListeners(const AtomicString& eventType) const;
bool hasCapturingEventListeners(const AtomicString& eventType);
const EventListenerVector& getEventListeners(const AtomicString& eventType);
Vector<AtomicString> eventTypes();
bool fireEventListeners(Event*);
protected:
virtual ~EventTarget();
virtual EventTargetData* eventTargetData() = 0;
virtual EventTargetData& ensureEventTargetData() = 0;
private:
virtual void refEventTarget() = 0;
virtual void derefEventTarget() = 0;
DOMWindow* executingWindow();
void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
bool clearAttributeEventListener(const AtomicString& eventType);
friend class EventListenerIterator;
};
class EventTargetWithInlineData : public EventTarget {
protected:
virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
private:
EventTargetData m_eventTargetData;
};
#define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
#define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
static EventListener* on##attribute(EventTarget& eventTarget) { return eventTarget.getAttributeEventListener(EventTypeNames::attribute); } \
static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); } \
#define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
#define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
static EventListener* on##attribute(EventTarget& eventTarget) { \
if (Node* node = eventTarget.toNode()) \
return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
ASSERT(eventTarget.toDOMWindow()); \
return eventTarget.getAttributeEventListener(EventTypeNames::attribute); \
} \
static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { \
if (Node* node = eventTarget.toNode()) \
node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
else { \
ASSERT(eventTarget.toDOMWindow()); \
eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); \
} \
}
#define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
#define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
EventListener* on##attribute(); \
void setOn##attribute(PassRefPtr<EventListener> listener);
#define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
void type::setOn##attribute(PassRefPtr<EventListener> listener) \
{ \
if (recipient) \
recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
}
inline bool EventTarget::hasEventListeners() const
{
if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
return !d->eventListenerMap.isEmpty();
return false;
}
inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
{
if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
return d->eventListenerMap.contains(eventType);
return false;
}
inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
{
EventTargetData* d = eventTargetData();
if (!d)
return false;
return d->eventListenerMap.containsCapturing(eventType);
}
}
#define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
public: \
using baseClass::ref; \
using baseClass::deref; \
private: \
virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
#define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING(RefCounted<className>)
#endif