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 "bindings/core/v8/ScriptWrappable.h"
36 #include "core/events/EventListenerMap.h"
37 #include "core/events/ThreadLocalEventNames.h"
38 #include "platform/heap/Handle.h"
50 class XMLHttpRequestUpload;
52 struct FiringEventIterator {
53 FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
54 : eventType(eventType)
60 const AtomicString& eventType;
64 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
66 struct EventTargetData {
67 WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
72 EventListenerMap eventListenerMap;
73 OwnPtr<FiringEventIteratorVector> firingEventIterators;
76 // This is the base class for all DOM event targets. To make your class an
77 // EventTarget, follow these steps:
78 // - Make your IDL interface inherit from EventTarget.
79 // Optionally add "attribute EventHandler onfoo;" attributes.
80 // - Inherit from EventTargetWithInlineData (only in rare cases should you use
81 // EventTarget directly).
82 // - Figure out if you now need to inherit from ActiveDOMObject as well.
83 // - In your class declaration, you will typically use
84 // REFCOUNTED_EVENT_TARGET(YourClassName) and
85 // WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(YourClassName). Make sure to include
86 // this header file in your .h file, or you will get very strange compiler
88 // - If you added an onfoo attribute, use DEFINE_ATTRIBUTE_EVENT_LISTENER(foo)
89 // in your class declaration.
90 // - Override EventTarget::interfaceName() and executionContext(). The former
91 // will typically return EventTargetNames::YourClassName. The latter will
92 // return ActiveDOMObject::executionContext (if you are an ActiveDOMObject)
93 // or the document you're in.
94 // - Your trace() method will need to call EventTargetWithInlineData::trace.
96 // Optionally, add a FooEvent.idl class, but that's outside the scope of this
97 // comment (and much more straightforward).
98 class EventTarget : public WillBeGarbageCollectedMixin, public ScriptWrappable {
99 DEFINE_WRAPPERTYPEINFO();
102 void ref() { refEventTarget(); }
103 void deref() { derefEventTarget(); }
106 virtual const AtomicString& interfaceName() const = 0;
107 virtual ExecutionContext* executionContext() const = 0;
109 virtual Node* toNode();
110 virtual LocalDOMWindow* toDOMWindow();
111 virtual MessagePort* toMessagePort();
113 // FIXME: first 2 args to addEventListener and removeEventListener should
114 // be required (per spec), but throwing TypeError breaks legacy content.
115 // http://crbug.com/353484
116 bool addEventListener() { return false; }
117 bool addEventListener(const AtomicString& eventType) { return false; }
118 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false);
119 bool removeEventListener() { return false; }
120 bool removeEventListener(const AtomicString& eventType) { return false; }
121 virtual bool removeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false);
122 virtual void removeAllEventListeners();
123 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>);
124 bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>, ExceptionState&); // DOM API
125 virtual void uncaughtExceptionInEventHandler();
127 // Used for legacy "onEvent" attribute APIs.
128 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
129 EventListener* getAttributeEventListener(const AtomicString& eventType);
131 bool hasEventListeners() const;
132 bool hasEventListeners(const AtomicString& eventType) const;
133 bool hasCapturingEventListeners(const AtomicString& eventType);
134 const EventListenerVector& getEventListeners(const AtomicString& eventType);
135 Vector<AtomicString> eventTypes();
137 bool fireEventListeners(Event*);
139 virtual void trace(Visitor*) { }
141 virtual bool keepEventInNode(Event*) { return false; };
145 virtual ~EventTarget();
147 // Subclasses should likely not override these themselves; instead, they should subclass EventTargetWithInlineData.
148 virtual EventTargetData* eventTargetData() = 0;
149 virtual EventTargetData& ensureEventTargetData() = 0;
153 // Subclasses should likely not override these themselves; instead, they should use the REFCOUNTED_EVENT_TARGET() macro.
154 virtual void refEventTarget() = 0;
155 virtual void derefEventTarget() = 0;
158 LocalDOMWindow* executingWindow();
159 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
160 void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
162 bool clearAttributeEventListener(const AtomicString& eventType);
164 friend class EventListenerIterator;
167 class EventTargetWithInlineData : public EventTarget {
169 virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
170 virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
172 EventTargetData m_eventTargetData;
175 // FIXME: These macros should be split into separate DEFINE and DECLARE
176 // macros to avoid causing so many header includes.
177 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
178 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
179 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
181 #define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
182 static EventListener* on##attribute(EventTarget& eventTarget) { return eventTarget.getAttributeEventListener(EventTypeNames::attribute); } \
183 static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); } \
185 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
186 EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
187 void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
189 #define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
190 static EventListener* on##attribute(EventTarget& eventTarget) { \
191 if (Node* node = eventTarget.toNode()) \
192 return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
193 ASSERT(eventTarget.toDOMWindow()); \
194 return eventTarget.getAttributeEventListener(EventTypeNames::attribute); \
196 static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { \
197 if (Node* node = eventTarget.toNode()) \
198 node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
200 ASSERT(eventTarget.toDOMWindow()); \
201 eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); \
205 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
206 EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
207 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
209 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
210 EventListener* on##attribute(); \
211 void setOn##attribute(PassRefPtr<EventListener> listener);
213 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
214 EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
215 void type::setOn##attribute(PassRefPtr<EventListener> listener) \
218 recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
221 inline bool EventTarget::hasEventListeners() const
223 // FIXME: We should have a const version of eventTargetData.
224 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
225 return !d->eventListenerMap.isEmpty();
229 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
231 // FIXME: We should have const version of eventTargetData.
232 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
233 return d->eventListenerMap.contains(eventType);
237 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
239 EventTargetData* d = eventTargetData();
242 return d->eventListenerMap.containsCapturing(eventType);
248 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
250 using baseClass::ref; \
251 using baseClass::deref; \
253 typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
254 #define DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(baseClass)
256 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
258 using baseClass::ref; \
259 using baseClass::deref; \
261 virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
262 virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
263 typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
264 #define DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(baseClass) DEFINE_EVENT_TARGET_REFCOUNTING(baseClass)
267 // Use this macro if your EventTarget subclass is also a subclass of WTF::RefCounted.
268 // A ref-counted class that uses a different method of refcounting should use DEFINE_EVENT_TARGET_REFCOUNTING directly.
269 // Both of these macros are meant to be placed just before the "public:" section of the class declaration.
270 #define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCounted<className>)
272 #endif // EventTarget_h