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 "platform/heap/Handle.h"
38 #include "wtf/Forward.h"
42 class ApplicationCache;
45 class DedicatedWorkerGlobalScope;
58 class MediaController;
63 class SVGElementInstance;
64 class ExecutionContext;
65 class ScriptProcessorNode;
67 class SharedWorkerGlobalScope;
73 class XMLHttpRequestUpload;
75 struct FiringEventIterator {
76 FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
77 : eventType(eventType)
83 const AtomicString& eventType;
87 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
89 struct EventTargetData {
90 WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
95 EventListenerMap eventListenerMap;
96 OwnPtr<FiringEventIteratorVector> firingEventIterators;
101 void ref() { refEventTarget(); }
102 void deref() { derefEventTarget(); }
104 virtual const AtomicString& interfaceName() const = 0;
105 virtual ExecutionContext* executionContext() const = 0;
107 virtual Node* toNode();
108 virtual DOMWindow* toDOMWindow();
109 virtual MessagePort* toMessagePort();
111 // FIXME: first 2 args to addEventListener and removeEventListener should
112 // be required (per spec), but throwing TypeError breaks legacy content.
113 // http://crbug.com/353484
114 bool addEventListener() { return false; }
115 bool addEventListener(const AtomicString& eventType) { return false; }
116 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false);
117 bool removeEventListener() { return false; }
118 bool removeEventListener(const AtomicString& eventType) { return false; }
119 virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture = false);
120 virtual void removeAllEventListeners();
121 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>);
122 bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>, ExceptionState&); // DOM API
123 virtual void uncaughtExceptionInEventHandler();
125 // Used for legacy "onEvent" attribute APIs.
126 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
127 EventListener* getAttributeEventListener(const AtomicString& eventType);
129 bool hasEventListeners() const;
130 bool hasEventListeners(const AtomicString& eventType) const;
131 bool hasCapturingEventListeners(const AtomicString& eventType);
132 const EventListenerVector& getEventListeners(const AtomicString& eventType);
133 Vector<AtomicString> eventTypes();
135 bool fireEventListeners(Event*);
138 virtual ~EventTarget();
140 // Subclasses should likely not override these themselves; instead, they should subclass EventTargetWithInlineData.
141 virtual EventTargetData* eventTargetData() = 0;
142 virtual EventTargetData& ensureEventTargetData() = 0;
145 // Subclasses should likely not override these themselves; instead, they should use the REFCOUNTED_EVENT_TARGET() macro.
146 virtual void refEventTarget() = 0;
147 virtual void derefEventTarget() = 0;
149 DOMWindow* executingWindow();
150 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
151 void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
153 bool clearAttributeEventListener(const AtomicString& eventType);
155 friend class EventListenerIterator;
158 class EventTargetWithInlineData : public EventTarget {
160 virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
161 virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
163 EventTargetData m_eventTargetData;
166 // FIXME: These macros should be split into separate DEFINE and DECLARE
167 // macros to avoid causing so many header includes.
168 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
169 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
170 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
172 #define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
173 static EventListener* on##attribute(EventTarget& eventTarget) { return eventTarget.getAttributeEventListener(EventTypeNames::attribute); } \
174 static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); } \
176 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
177 EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
178 void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
180 #define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
181 static EventListener* on##attribute(EventTarget& eventTarget) { \
182 if (Node* node = eventTarget.toNode()) \
183 return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
184 ASSERT(eventTarget.toDOMWindow()); \
185 return eventTarget.getAttributeEventListener(EventTypeNames::attribute); \
187 static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { \
188 if (Node* node = eventTarget.toNode()) \
189 node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
191 ASSERT(eventTarget.toDOMWindow()); \
192 eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); \
196 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
197 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
198 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
200 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
201 EventListener* on##attribute(); \
202 void setOn##attribute(PassRefPtr<EventListener> listener);
204 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
205 EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
206 void type::setOn##attribute(PassRefPtr<EventListener> listener) \
209 recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
212 inline bool EventTarget::hasEventListeners() const
214 // FIXME: We should have a const version of eventTargetData.
215 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
216 return !d->eventListenerMap.isEmpty();
220 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
222 // FIXME: We should have const version of eventTargetData.
223 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
224 return d->eventListenerMap.contains(eventType);
228 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
230 EventTargetData* d = eventTargetData();
233 return d->eventListenerMap.containsCapturing(eventType);
236 } // namespace WebCore
238 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
240 using baseClass::ref; \
241 using baseClass::deref; \
243 virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
244 virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
245 typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
247 // Use this macro if your EventTarget subclass is also a subclass of WTF::RefCounted.
248 // A ref-counted class that uses a different method of refcounting should use DEFINE_EVENT_TARGET_REFCOUNTING directly.
249 // Both of these macros are meant to be placed just before the "public:" section of the class declaration.
250 #define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING(RefCounted<className>)
252 #endif // EventTarget_h