1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 6 * Copyright (C) 2006 Alexey Proskuryakov (ap (at) webkit.org) 7 * (C) 2007, 2008 Nikolas Zimmermann <zimmermann (at) kde.org> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 17 * 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. 29 * 30 */ 31 32 #ifndef EventTarget_h 33 #define EventTarget_h 34 35 #include "core/dom/EventListenerMap.h" 36 #include "core/dom/EventNames.h" 37 #include "wtf/Forward.h" 38 39 namespace WebCore { 40 41 class AudioContext; 42 class DOMApplicationCache; 43 class DOMWindow; 44 class DedicatedWorkerGlobalScope; 45 class Event; 46 class EventListener; 47 class EventSource; 48 class ExceptionState; 49 class FileReader; 50 class FileWriter; 51 class IDBDatabase; 52 class IDBRequest; 53 class IDBTransaction; 54 class MIDIAccess; 55 class MIDIInput; 56 class MIDIPort; 57 class MediaController; 58 class MediaStream; 59 class MessagePort; 60 class NamedFlow; 61 class Node; 62 class Notification; 63 class SVGElementInstance; 64 class ScriptExecutionContext; 65 class ScriptProcessorNode; 66 class SharedWorker; 67 class SharedWorkerGlobalScope; 68 class TextTrack; 69 class TextTrackCue; 70 class WebSocket; 71 class Worker; 72 class XMLHttpRequest; 73 class XMLHttpRequestUpload; 74 75 struct FiringEventIterator { 76 FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end) 77 : eventType(eventType) 78 , iterator(iterator) 79 , end(end) 80 { 81 } 82 83 const AtomicString& eventType; 84 size_t& iterator; 85 size_t& end; 86 }; 87 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector; 88 89 struct EventTargetData { 90 WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED; 91 public: 92 EventTargetData(); 93 ~EventTargetData(); 94 95 EventListenerMap eventListenerMap; 96 OwnPtr<FiringEventIteratorVector> firingEventIterators; 97 }; 98 99 class EventTarget { 100 public: 101 void ref() { refEventTarget(); } 102 void deref() { derefEventTarget(); } 103 104 virtual const AtomicString& interfaceName() const = 0; 105 virtual ScriptExecutionContext* scriptExecutionContext() const = 0; 106 107 virtual Node* toNode(); 108 virtual DOMWindow* toDOMWindow(); 109 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(); 116 117 // Used for legacy "onEvent" attribute APIs. 118 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, DOMWrapperWorld* isolatedWorld = 0); 119 EventListener* getAttributeEventListener(const AtomicString& eventType, DOMWrapperWorld* isolatedWorld = 0); 120 121 bool hasEventListeners(); 122 bool hasEventListeners(const AtomicString& eventType); 123 bool hasCapturingEventListeners(const AtomicString& eventType); 124 const EventListenerVector& getEventListeners(const AtomicString& eventType); 125 126 bool fireEventListeners(Event*); 127 bool isFiringEventListeners(); 128 129 protected: 130 virtual ~EventTarget(); 131 132 virtual EventTargetData* eventTargetData() = 0; 133 virtual EventTargetData* ensureEventTargetData() = 0; 134 135 private: 136 virtual void refEventTarget() = 0; 137 virtual void derefEventTarget() = 0; 138 139 DOMWindow* executingWindow(); 140 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&); 141 142 bool clearAttributeEventListener(const AtomicString& eventType, DOMWrapperWorld* isolatedWorld); 143 144 friend class EventListenerIterator; 145 }; 146 147 // FIXME: These macros should be split into separate DEFINE and DECLARE 148 // macros to avoid causing so many header includes. 149 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \ 150 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return getAttributeEventListener(eventNames().attribute##Event, isolatedWorld); } \ 151 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld = 0) { setAttributeEventListener(eventNames().attribute##Event, listener, isolatedWorld); } \ 152 153 #define DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(attribute) \ 154 virtual EventListener* on##attribute(DOMWrapperWorld* isolatedWorld); \ 155 virtual void setOn##attribute(PassRefPtr<EventListener>, DOMWrapperWorld* isolatedWorld); \ 156 157 #define DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(type, attribute) \ 158 EventListener* type::on##attribute(DOMWrapperWorld* isolatedWorld) { return getAttributeEventListener(eventNames().attribute##Event, isolatedWorld); } \ 159 void type::setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld) { setAttributeEventListener(eventNames().attribute##Event, listener, isolatedWorld); } \ 160 161 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \ 162 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return document()->getWindowAttributeEventListener(eventNames().attribute##Event, isolatedWorld); } \ 163 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld) { document()->setWindowAttributeEventListener(eventNames().attribute##Event, listener, isolatedWorld); } \ 164 165 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \ 166 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return getAttributeEventListener(eventNames().eventName##Event, isolatedWorld); } \ 167 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld) { setAttributeEventListener(eventNames().eventName##Event, listener, isolatedWorld); } \ 168 169 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \ 170 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld); \ 171 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld); 172 173 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \ 174 EventListener* type::on##attribute(DOMWrapperWorld* isolatedWorld) { return recipient ? recipient->getAttributeEventListener(eventNames().attribute##Event, isolatedWorld) : 0; } \ 175 void type::setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorld* isolatedWorld) { if (recipient) recipient->setAttributeEventListener(eventNames().attribute##Event, listener, isolatedWorld); } 176 177 inline bool EventTarget::isFiringEventListeners() 178 { 179 EventTargetData* d = eventTargetData(); 180 if (!d) 181 return false; 182 return d->firingEventIterators && !d->firingEventIterators->isEmpty(); 183 } 184 185 inline bool EventTarget::hasEventListeners() 186 { 187 EventTargetData* d = eventTargetData(); 188 if (!d) 189 return false; 190 return !d->eventListenerMap.isEmpty(); 191 } 192 193 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) 194 { 195 EventTargetData* d = eventTargetData(); 196 if (!d) 197 return false; 198 return d->eventListenerMap.contains(eventType); 199 } 200 201 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType) 202 { 203 EventTargetData* d = eventTargetData(); 204 if (!d) 205 return false; 206 return d->eventListenerMap.containsCapturing(eventType); 207 } 208 209 } // namespace WebCore 210 211 #endif // EventTarget_h 212