2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
7 * (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "core/events/EventListenerMap.h"
36 #include "core/events/ThreadLocalEventNames.h"
37 #include "wtf/Forward.h"
41 class ApplicationCache;
44 class DedicatedWorkerGlobalScope;
57 class MediaController;
62 class SVGElementInstance;
63 class ExecutionContext;
64 class ScriptProcessorNode;
66 class SharedWorkerGlobalScope;
72 class XMLHttpRequestUpload;
74 struct FiringEventIterator {
75 FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
76 : eventType(eventType)
82 const AtomicString& eventType;
86 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
88 struct EventTargetData {
89 WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
94 EventListenerMap eventListenerMap;
95 OwnPtr<FiringEventIteratorVector> firingEventIterators;
100 void ref() { refEventTarget(); }
101 void deref() { derefEventTarget(); }
103 virtual const AtomicString& interfaceName() const = 0;
104 virtual ExecutionContext* executionContext() const = 0;
106 virtual Node* toNode();
107 virtual DOMWindow* toDOMWindow();
108 virtual MessagePort* toMessagePort();
110 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture);
111 virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture);
112 virtual void removeAllEventListeners();
113 virtual bool dispatchEvent(PassRefPtr<Event>);
114 bool dispatchEvent(PassRefPtr<Event>, ExceptionState&); // DOM API
115 virtual void uncaughtExceptionInEventHandler();
117 // Used for legacy "onEvent" attribute APIs.
118 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
119 EventListener* getAttributeEventListener(const AtomicString& eventType);
121 bool hasEventListeners() const;
122 bool hasEventListeners(const AtomicString& eventType) const;
123 bool hasCapturingEventListeners(const AtomicString& eventType);
124 const EventListenerVector& getEventListeners(const AtomicString& eventType);
125 Vector<AtomicString> eventTypes();
127 bool fireEventListeners(Event*);
130 virtual ~EventTarget();
132 // Subclasses should likely not override these themselves; instead, they should subclass EventTargetWithInlineData.
133 virtual EventTargetData* eventTargetData() = 0;
134 virtual EventTargetData& ensureEventTargetData() = 0;
137 // Subclasses should likely not override these themselves; instead, they should use the REFCOUNTED_EVENT_TARGET() macro.
138 virtual void refEventTarget() = 0;
139 virtual void derefEventTarget() = 0;
141 DOMWindow* executingWindow();
142 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
143 void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
145 bool clearAttributeEventListener(const AtomicString& eventType);
147 friend class EventListenerIterator;
150 class EventTargetWithInlineData : public EventTarget {
152 virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
153 virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
155 EventTargetData m_eventTargetData;
158 // FIXME: These macros should be split into separate DEFINE and DECLARE
159 // macros to avoid causing so many header includes.
160 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
161 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
162 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
164 #define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
165 static EventListener* on##attribute(EventTarget* eventTarget) { return eventTarget->getAttributeEventListener(EventTypeNames::attribute); } \
166 static void setOn##attribute(EventTarget* eventTarget, PassRefPtr<EventListener> listener) { eventTarget->setAttributeEventListener(EventTypeNames::attribute, listener); } \
168 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
169 EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
170 void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
172 #define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
173 static EventListener* on##attribute(EventTarget* eventTarget) { \
174 if (Node* node = eventTarget->toNode()) \
175 return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
176 ASSERT(eventTarget->toDOMWindow()); \
177 return eventTarget->getAttributeEventListener(EventTypeNames::attribute); \
179 static void setOn##attribute(EventTarget* eventTarget, PassRefPtr<EventListener> listener) { \
180 if (Node* node = eventTarget->toNode()) \
181 node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
183 ASSERT(eventTarget->toDOMWindow()); \
184 eventTarget->setAttributeEventListener(EventTypeNames::attribute, listener); \
188 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
189 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
190 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
192 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
193 EventListener* on##attribute(); \
194 void setOn##attribute(PassRefPtr<EventListener> listener);
196 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
197 EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
198 void type::setOn##attribute(PassRefPtr<EventListener> listener) \
201 recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
204 inline bool EventTarget::hasEventListeners() const
206 // FIXME: We should have a const version of eventTargetData.
207 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
208 return !d->eventListenerMap.isEmpty();
212 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
214 // FIXME: We should have const version of eventTargetData.
215 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
216 return d->eventListenerMap.contains(eventType);
220 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
222 EventTargetData* d = eventTargetData();
225 return d->eventListenerMap.containsCapturing(eventType);
228 } // namespace WebCore
230 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
232 using baseClass::ref; \
233 using baseClass::deref; \
235 virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
236 virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
237 typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
239 // Use this macro if your EventTarget subclass is also a subclass of WTF::RefCounted.
240 // A ref-counted class that uses a different method of refcounting should use DEFINE_EVENT_TARGET_REFCOUNTING directly.
241 // Both of these macros are meant to be placed just before the "public:" section of the class declaration.
242 #define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING(RefCounted<className>)
244 #endif // EventTarget_h