28f7545e132035cc3186926237279e8c2e10f742
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / events / EventTarget.h
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/ThreadLocalEventNames.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 Node;
61 class Notification;
62 class SVGElementInstance;
63 class ExecutionContext;
64 class ScriptProcessorNode;
65 class SharedWorker;
66 class SharedWorkerGlobalScope;
67 class TextTrack;
68 class TextTrackCue;
69 class WebSocket;
70 class Worker;
71 class XMLHttpRequest;
72 class XMLHttpRequestUpload;
73
74 struct FiringEventIterator {
75     FiringEventIterator(const AtomicString& eventType, size_t& iterator, size_t& end)
76         : eventType(eventType)
77         , iterator(iterator)
78         , end(end)
79     {
80     }
81
82     const AtomicString& eventType;
83     size_t& iterator;
84     size_t& end;
85 };
86 typedef Vector<FiringEventIterator, 1> FiringEventIteratorVector;
87
88 struct EventTargetData {
89     WTF_MAKE_NONCOPYABLE(EventTargetData); WTF_MAKE_FAST_ALLOCATED;
90 public:
91     EventTargetData();
92     ~EventTargetData();
93
94     EventListenerMap eventListenerMap;
95     OwnPtr<FiringEventIteratorVector> firingEventIterators;
96 };
97
98 class EventTarget {
99 public:
100     void ref() { refEventTarget(); }
101     void deref() { derefEventTarget(); }
102
103     virtual const AtomicString& interfaceName() const = 0;
104     virtual ExecutionContext* executionContext() const = 0;
105
106     virtual Node* toNode();
107     virtual DOMWindow* toDOMWindow();
108     virtual MessagePort* toMessagePort();
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>);
119     EventListener* getAttributeEventListener(const AtomicString& eventType);
120
121     bool hasEventListeners() const;
122     bool hasEventListeners(const AtomicString& eventType) const;
123     bool hasCapturingEventListeners(const AtomicString& eventType);
124     const EventListenerVector& getEventListeners(const AtomicString& eventType);
125     Vector<AtomicString> eventTypes();
126
127     bool fireEventListeners(Event*);
128
129 protected:
130     virtual ~EventTarget();
131
132     // Subclasses should likely not override these themselves; instead, they should subclass EventTargetWithInlineData.
133     virtual EventTargetData* eventTargetData() = 0;
134     virtual EventTargetData& ensureEventTargetData() = 0;
135
136 private:
137     // Subclasses should likely not override these themselves; instead, they should use the REFCOUNTED_EVENT_TARGET() macro.
138     virtual void refEventTarget() = 0;
139     virtual void derefEventTarget() = 0;
140
141     DOMWindow* executingWindow();
142     void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
143     void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
144
145     bool clearAttributeEventListener(const AtomicString& eventType);
146
147     friend class EventListenerIterator;
148 };
149
150 class EventTargetWithInlineData : public EventTarget {
151 protected:
152     virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
153     virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
154 private:
155     EventTargetData m_eventTargetData;
156 };
157
158 // FIXME: These macros should be split into separate DEFINE and DECLARE
159 // macros to avoid causing so many header includes.
160 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
161     EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
162     void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
163
164 #define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
165     static EventListener* on##attribute(EventTarget* eventTarget) { return eventTarget->getAttributeEventListener(EventTypeNames::attribute); } \
166     static void setOn##attribute(EventTarget* eventTarget, PassRefPtr<EventListener> listener) { eventTarget->setAttributeEventListener(EventTypeNames::attribute, listener); } \
167
168 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
169     EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
170     void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
171
172 #define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
173     static EventListener* on##attribute(EventTarget* eventTarget) { \
174         if (Node* node = eventTarget->toNode()) \
175             return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
176         ASSERT(eventTarget->toDOMWindow()); \
177         return eventTarget->getAttributeEventListener(EventTypeNames::attribute); \
178     } \
179     static void setOn##attribute(EventTarget* eventTarget, PassRefPtr<EventListener> listener) { \
180         if (Node* node = eventTarget->toNode()) \
181             node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
182         else { \
183             ASSERT(eventTarget->toDOMWindow()); \
184             eventTarget->setAttributeEventListener(EventTypeNames::attribute, listener); \
185         } \
186     }
187
188 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
189     EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
190     void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
191
192 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
193     EventListener* on##attribute(); \
194     void setOn##attribute(PassRefPtr<EventListener> listener);
195
196 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
197     EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
198     void type::setOn##attribute(PassRefPtr<EventListener> listener) \
199     { \
200         if (recipient) \
201             recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
202     }
203
204 inline bool EventTarget::hasEventListeners() const
205 {
206     // FIXME: We should have a const version of eventTargetData.
207     if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
208         return !d->eventListenerMap.isEmpty();
209     return false;
210 }
211
212 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
213 {
214     // FIXME: We should have const version of eventTargetData.
215     if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
216         return d->eventListenerMap.contains(eventType);
217     return false;
218 }
219
220 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
221 {
222     EventTargetData* d = eventTargetData();
223     if (!d)
224         return false;
225     return d->eventListenerMap.containsCapturing(eventType);
226 }
227
228 } // namespace WebCore
229
230 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
231 public: \
232     using baseClass::ref; \
233     using baseClass::deref; \
234 private: \
235     virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
236     virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
237     typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
238
239 // Use this macro if your EventTarget subclass is also a subclass of WTF::RefCounted.
240 // A ref-counted class that uses a different method of refcounting should use DEFINE_EVENT_TARGET_REFCOUNTING directly.
241 // Both of these macros are meant to be placed just before the "public:" section of the class declaration.
242 #define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING(RefCounted<className>)
243
244 #endif // EventTarget_h