OLD | NEW |
| (Empty) |
1 /* | |
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> | |
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/events/EventListenerMap.h" | |
36 #include "core/events/EventNames.h" | |
37 #include "wtf/Forward.h" | |
38 | |
39 namespace WebCore { | |
40 | |
41 class ApplicationCache; | |
42 class AudioContext; | |
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, siz
e_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 virtual MessagePort* toMessagePort(); | |
110 | |
111 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<
EventListener>, bool useCapture); | |
112 virtual bool removeEventListener(const AtomicString& eventType, EventLis
tener*, bool useCapture); | |
113 virtual void removeAllEventListeners(); | |
114 virtual bool dispatchEvent(PassRefPtr<Event>); | |
115 bool dispatchEvent(PassRefPtr<Event>, ExceptionState&); // DOM API | |
116 virtual void uncaughtExceptionInEventHandler(); | |
117 | |
118 // Used for legacy "onEvent" attribute APIs. | |
119 bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr
<EventListener>, DOMWrapperWorld* isolatedWorld = 0); | |
120 EventListener* getAttributeEventListener(const AtomicString& eventType,
DOMWrapperWorld* isolatedWorld = 0); | |
121 | |
122 bool hasEventListeners() const; | |
123 bool hasEventListeners(const AtomicString& eventType) const; | |
124 bool hasCapturingEventListeners(const AtomicString& eventType); | |
125 const EventListenerVector& getEventListeners(const AtomicString& eventTy
pe); | |
126 | |
127 bool fireEventListeners(Event*); | |
128 bool isFiringEventListeners(); | |
129 | |
130 protected: | |
131 virtual ~EventTarget(); | |
132 | |
133 virtual EventTargetData* eventTargetData() = 0; | |
134 virtual EventTargetData* ensureEventTargetData() = 0; | |
135 | |
136 private: | |
137 virtual void refEventTarget() = 0; | |
138 virtual void derefEventTarget() = 0; | |
139 | |
140 DOMWindow* executingWindow(); | |
141 void fireEventListeners(Event*, EventTargetData*, EventListenerVector&); | |
142 void countLegacyEvents(const AtomicString& legacyTypeName, EventListener
Vector*, EventListenerVector*); | |
143 | |
144 bool clearAttributeEventListener(const AtomicString& eventType, DOMWrapp
erWorld* isolatedWorld); | |
145 | |
146 friend class EventListenerIterator; | |
147 }; | |
148 | |
149 // FIXME: These macros should be split into separate DEFINE and DECLARE | |
150 // macros to avoid causing so many header includes. | |
151 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \ | |
152 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return ge
tAttributeEventListener(eventNames().attribute##Event, isolatedWorld); } \ | |
153 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorl
d* isolatedWorld = 0) { setAttributeEventListener(eventNames().attribute##Event,
listener, isolatedWorld); } \ | |
154 | |
155 #define DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(attribute) \ | |
156 virtual EventListener* on##attribute(DOMWrapperWorld* isolatedWorld); \ | |
157 virtual void setOn##attribute(PassRefPtr<EventListener>, DOMWrapperWorld
* isolatedWorld); \ | |
158 | |
159 #define DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(type, attribute) \ | |
160 EventListener* type::on##attribute(DOMWrapperWorld* isolatedWorld) { ret
urn getAttributeEventListener(eventNames().attribute##Event, isolatedWorld); } \ | |
161 void type::setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapp
erWorld* isolatedWorld) { setAttributeEventListener(eventNames().attribute##Even
t, listener, isolatedWorld); } \ | |
162 | |
163 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \ | |
164 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return do
cument().getWindowAttributeEventListener(eventNames().attribute##Event, isolated
World); } \ | |
165 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorl
d* isolatedWorld) { document().setWindowAttributeEventListener(eventNames().attr
ibute##Event, listener, isolatedWorld); } \ | |
166 | |
167 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \ | |
168 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld) { return ge
tAttributeEventListener(eventNames().eventName##Event, isolatedWorld); } \ | |
169 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorl
d* isolatedWorld) { setAttributeEventListener(eventNames().eventName##Event, lis
tener, isolatedWorld); } \ | |
170 | |
171 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \ | |
172 EventListener* on##attribute(DOMWrapperWorld* isolatedWorld); \ | |
173 void setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapperWorl
d* isolatedWorld); | |
174 | |
175 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribut
e) \ | |
176 EventListener* type::on##attribute(DOMWrapperWorld* isolatedWorld) { ret
urn recipient ? recipient->getAttributeEventListener(eventNames().attribute##Eve
nt, isolatedWorld) : 0; } \ | |
177 void type::setOn##attribute(PassRefPtr<EventListener> listener, DOMWrapp
erWorld* isolatedWorld) { if (recipient) recipient->setAttributeEventListener(ev
entNames().attribute##Event, listener, isolatedWorld); } | |
178 | |
179 inline bool EventTarget::isFiringEventListeners() | |
180 { | |
181 EventTargetData* d = eventTargetData(); | |
182 if (!d) | |
183 return false; | |
184 return d->firingEventIterators && !d->firingEventIterators->isEmpty(); | |
185 } | |
186 | |
187 inline bool EventTarget::hasEventListeners() const | |
188 { | |
189 // FIXME: We should have a const version of eventTargetData. | |
190 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTarg
etData()) | |
191 return !d->eventListenerMap.isEmpty(); | |
192 return false; | |
193 } | |
194 | |
195 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) co
nst | |
196 { | |
197 // FIXME: We should have const version of eventTargetData. | |
198 if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTarg
etData()) | |
199 return d->eventListenerMap.contains(eventType); | |
200 return false; | |
201 } | |
202 | |
203 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& even
tType) | |
204 { | |
205 EventTargetData* d = eventTargetData(); | |
206 if (!d) | |
207 return false; | |
208 return d->eventListenerMap.containsCapturing(eventType); | |
209 } | |
210 | |
211 } // namespace WebCore | |
212 | |
213 #endif // EventTarget_h | |
OLD | NEW |