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 "EventNames.h"
36 #include "RegisteredEventListener.h"
37 #include <wtf/Forward.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/text/AtomicStringHash.h>
45 class DedicatedWorkerContext;
46 class DOMApplicationCache;
56 class IDBVersionChangeRequest;
57 class JavaScriptAudioNode;
61 class SVGElementInstance;
62 class ScriptExecutionContext;
64 class SharedWorkerContext;
68 class XMLHttpRequestUpload;
70 typedef int ExceptionCode;
72 struct FiringEventIterator {
73 FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
74 : eventType(eventType)
80 const AtomicString& eventType;
84 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
86 typedef Vector<RegisteredEventListener, 1> EventListenerVector;
87 typedef HashMap<AtomicString, EventListenerVector*> EventListenerMap;
89 struct EventTargetData {
90 WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
95 EventListenerMap eventListenerMap;
96 FiringEventIteratorVector firingEventIterators;
101 void ref() { refEventTarget(); }
102 void deref() { derefEventTarget(); }
104 virtual EventSource* toEventSource();
105 virtual MessagePort* toMessagePort();
106 virtual Node* toNode();
107 virtual DOMWindow* toDOMWindow();
108 virtual XMLHttpRequest* toXMLHttpRequest();
109 virtual XMLHttpRequestUpload* toXMLHttpRequestUpload();
110 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
111 virtual DOMApplicationCache* toDOMApplicationCache();
114 virtual SVGElementInstance* toSVGElementInstance();
117 virtual Worker* toWorker();
118 virtual DedicatedWorkerContext* toDedicatedWorkerContext();
120 #if ENABLE(SHARED_WORKERS)
121 virtual SharedWorker* toSharedWorker();
122 virtual SharedWorkerContext* toSharedWorkerContext();
125 #if ENABLE(WEB_AUDIO)
126 virtual AudioContext* toAudioContext();
127 virtual JavaScriptAudioNode* toJavaScriptAudioNode();
130 #if ENABLE(WEB_SOCKETS)
131 virtual WebSocket* toWebSocket();
134 #if ENABLE(NOTIFICATIONS)
135 virtual Notification* toNotification();
138 virtual FileReader* toFileReader();
140 #if ENABLE(FILE_SYSTEM)
141 virtual FileWriter* toFileWriter();
144 #if ENABLE(INDEXED_DATABASE)
145 virtual IDBDatabase* toIDBDatabase();
146 virtual IDBRequest* toIDBRequest();
147 virtual IDBTransaction* toIDBTransaction();
148 virtual IDBVersionChangeRequest* toIDBVersionChangeRequest();
151 virtual ScriptExecutionContext* scriptExecutionContext() const = 0;
153 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture);
154 virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture);
155 virtual void removeAllEventListeners();
156 virtual bool dispatchEvent(PassRefPtr<Event>);
157 bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&); // DOM API
158 virtual void uncaughtExceptionInEventHandler();
160 // Used for legacy "onEvent" attribute APIs.
161 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
162 bool clearAttributeEventListener(const AtomicString& eventType);
163 EventListener* getAttributeEventListener(const AtomicString& eventType);
165 bool hasEventListeners();
166 bool hasEventListeners(const AtomicString& eventType);
167 const EventListenerVector& getEventListeners(const AtomicString& eventType);
169 bool fireEventListeners(Event*);
170 bool isFiringEventListeners();
173 void markJSEventListeners(JSC::MarkStack&);
174 void invalidateJSEventListeners(JSC::JSObject*);
178 virtual ~EventTarget();
180 virtual EventTargetData* eventTargetData() = 0;
181 virtual EventTargetData* ensureEventTargetData() = 0;
184 virtual void refEventTarget() = 0;
185 virtual void derefEventTarget() = 0;
187 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
190 // FIXME: These macros should be split into separate DEFINE and DECLARE
191 // macros to avoid causing so many header includes.
192 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
193 EventListener* on##attribute() { return getAttributeEventListener(eventNames().attribute##Event); } \
194 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(eventNames().attribute##Event, listener); } \
196 #define DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(attribute) \
197 virtual EventListener* on##attribute(); \
198 virtual void setOn##attribute(PassRefPtr<EventListener> listener); \
200 #define DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(type, attribute) \
201 EventListener* type::on##attribute() { return getAttributeEventListener(eventNames().attribute##Event); } \
202 void type::setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(eventNames().attribute##Event, listener); } \
204 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
205 EventListener* on##attribute() { return document()->getWindowAttributeEventListener(eventNames().attribute##Event); } \
206 void setOn##attribute(PassRefPtr<EventListener> listener) { document()->setWindowAttributeEventListener(eventNames().attribute##Event, listener); } \
208 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
209 EventListener* on##attribute() { return getAttributeEventListener(eventNames().eventName##Event); } \
210 void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(eventNames().eventName##Event, listener); } \
212 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
213 EventListener* on##attribute() { return recipient ? recipient->getAttributeEventListener(eventNames().attribute##Event) : 0; } \
214 void setOn##attribute(PassRefPtr<EventListener> listener) { if (recipient) recipient->setAttributeEventListener(eventNames().attribute##Event, listener); } \
217 void forbidEventDispatch();
218 void allowEventDispatch();
219 bool eventDispatchForbidden();
221 inline void forbidEventDispatch() { }
222 inline void allowEventDispatch() { }
226 inline void EventTarget::markJSEventListeners(JSC::MarkStack& markStack)
228 EventTargetData* d = eventTargetData();
232 EventListenerMap::iterator end = d->eventListenerMap.end();
233 for (EventListenerMap::iterator it = d->eventListenerMap.begin(); it != end; ++it) {
234 EventListenerVector& entry = *it->second;
235 for (size_t i = 0; i < entry.size(); ++i)
236 entry[i].listener->markJSFunction(markStack);
241 inline bool EventTarget::isFiringEventListeners()
243 EventTargetData* d = eventTargetData();
246 return d->firingEventIterators.size() != 0;
249 inline bool EventTarget::hasEventListeners()
251 EventTargetData* d = eventTargetData();
254 return !d->eventListenerMap.isEmpty();
257 inline bool EventTarget::hasEventListeners(const AtomicString& eventType)
259 EventTargetData* d = eventTargetData();
262 return d->eventListenerMap.contains(eventType);
265 } // namespace WebCore
267 #endif // EventTarget_h