Upstream version 7.36.149.0
[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 "platform/heap/Handle.h"
38 #include "wtf/Forward.h"
39
40 namespace WebCore {
41
42 class ApplicationCache;
43 class AudioContext;
44 class DOMWindow;
45 class DedicatedWorkerGlobalScope;
46 class Event;
47 class EventListener;
48 class EventSource;
49 class ExceptionState;
50 class FileReader;
51 class FileWriter;
52 class IDBDatabase;
53 class IDBRequest;
54 class IDBTransaction;
55 class MIDIAccess;
56 class MIDIInput;
57 class MIDIPort;
58 class MediaController;
59 class MediaStream;
60 class MessagePort;
61 class Node;
62 class Notification;
63 class SVGElementInstance;
64 class ExecutionContext;
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 ExecutionContext* executionContext() const = 0;
106
107     virtual Node* toNode();
108     virtual DOMWindow* toDOMWindow();
109     virtual MessagePort* toMessagePort();
110
111     // FIXME: first 2 args to addEventListener and removeEventListener should
112     // be required (per spec), but throwing TypeError breaks legacy content.
113     // http://crbug.com/353484
114     bool addEventListener() { return false; }
115     bool addEventListener(const AtomicString& eventType) { return false; }
116     virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false);
117     bool removeEventListener() { return false; }
118     bool removeEventListener(const AtomicString& eventType) { return false; }
119     virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture = false);
120     virtual void removeAllEventListeners();
121     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>);
122     bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>, ExceptionState&); // DOM API
123     virtual void uncaughtExceptionInEventHandler();
124
125     // Used for legacy "onEvent" attribute APIs.
126     bool setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener>);
127     EventListener* getAttributeEventListener(const AtomicString& eventType);
128
129     bool hasEventListeners() const;
130     bool hasEventListeners(const AtomicString& eventType) const;
131     bool hasCapturingEventListeners(const AtomicString& eventType);
132     const EventListenerVector& getEventListeners(const AtomicString& eventType);
133     Vector<AtomicString> eventTypes();
134
135     bool fireEventListeners(Event*);
136
137 protected:
138     virtual ~EventTarget();
139
140     // Subclasses should likely not override these themselves; instead, they should subclass EventTargetWithInlineData.
141     virtual EventTargetData* eventTargetData() = 0;
142     virtual EventTargetData& ensureEventTargetData() = 0;
143
144 private:
145     // Subclasses should likely not override these themselves; instead, they should use the REFCOUNTED_EVENT_TARGET() macro.
146     virtual void refEventTarget() = 0;
147     virtual void derefEventTarget() = 0;
148
149     DOMWindow* executingWindow();
150     void fireEventListeners(Event*, EventTargetData*, EventListenerVector&);
151     void countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector*, EventListenerVector*);
152
153     bool clearAttributeEventListener(const AtomicString& eventType);
154
155     friend class EventListenerIterator;
156 };
157
158 class EventTargetWithInlineData : public EventTarget {
159 protected:
160     virtual EventTargetData* eventTargetData() OVERRIDE FINAL { return &m_eventTargetData; }
161     virtual EventTargetData& ensureEventTargetData() OVERRIDE FINAL { return m_eventTargetData; }
162 private:
163     EventTargetData m_eventTargetData;
164 };
165
166 // FIXME: These macros should be split into separate DEFINE and DECLARE
167 // macros to avoid causing so many header includes.
168 #define DEFINE_ATTRIBUTE_EVENT_LISTENER(attribute) \
169     EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::attribute); } \
170     void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::attribute, listener); } \
171
172 #define DEFINE_STATIC_ATTRIBUTE_EVENT_LISTENER(attribute) \
173     static EventListener* on##attribute(EventTarget& eventTarget) { return eventTarget.getAttributeEventListener(EventTypeNames::attribute); } \
174     static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); } \
175
176 #define DEFINE_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
177     EventListener* on##attribute() { return document().getWindowAttributeEventListener(EventTypeNames::attribute); } \
178     void setOn##attribute(PassRefPtr<EventListener> listener) { document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); } \
179
180 #define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(attribute) \
181     static EventListener* on##attribute(EventTarget& eventTarget) { \
182         if (Node* node = eventTarget.toNode()) \
183             return node->document().getWindowAttributeEventListener(EventTypeNames::attribute); \
184         ASSERT(eventTarget.toDOMWindow()); \
185         return eventTarget.getAttributeEventListener(EventTypeNames::attribute); \
186     } \
187     static void setOn##attribute(EventTarget& eventTarget, PassRefPtr<EventListener> listener) { \
188         if (Node* node = eventTarget.toNode()) \
189             node->document().setWindowAttributeEventListener(EventTypeNames::attribute, listener); \
190         else { \
191             ASSERT(eventTarget.toDOMWindow()); \
192             eventTarget.setAttributeEventListener(EventTypeNames::attribute, listener); \
193         } \
194     }
195
196 #define DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(attribute, eventName) \
197     EventListener* on##attribute() { return getAttributeEventListener(EventTypeNames::eventName); } \
198     void setOn##attribute(PassRefPtr<EventListener> listener) { setAttributeEventListener(EventTypeNames::eventName, listener); } \
199
200 #define DECLARE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(recipient, attribute) \
201     EventListener* on##attribute(); \
202     void setOn##attribute(PassRefPtr<EventListener> listener);
203
204 #define DEFINE_FORWARDING_ATTRIBUTE_EVENT_LISTENER(type, recipient, attribute) \
205     EventListener* type::on##attribute() { return recipient ? recipient->getAttributeEventListener(EventTypeNames::attribute) : 0; } \
206     void type::setOn##attribute(PassRefPtr<EventListener> listener) \
207     { \
208         if (recipient) \
209             recipient->setAttributeEventListener(EventTypeNames::attribute, listener); \
210     }
211
212 inline bool EventTarget::hasEventListeners() const
213 {
214     // FIXME: We should have a const version of eventTargetData.
215     if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
216         return !d->eventListenerMap.isEmpty();
217     return false;
218 }
219
220 inline bool EventTarget::hasEventListeners(const AtomicString& eventType) const
221 {
222     // FIXME: We should have const version of eventTargetData.
223     if (const EventTargetData* d = const_cast<EventTarget*>(this)->eventTargetData())
224         return d->eventListenerMap.contains(eventType);
225     return false;
226 }
227
228 inline bool EventTarget::hasCapturingEventListeners(const AtomicString& eventType)
229 {
230     EventTargetData* d = eventTargetData();
231     if (!d)
232         return false;
233     return d->eventListenerMap.containsCapturing(eventType);
234 }
235
236 } // namespace WebCore
237
238 #define DEFINE_EVENT_TARGET_REFCOUNTING(baseClass) \
239 public: \
240     using baseClass::ref; \
241     using baseClass::deref; \
242 private: \
243     virtual void refEventTarget() OVERRIDE FINAL { ref(); } \
244     virtual void derefEventTarget() OVERRIDE FINAL { deref(); } \
245     typedef int thisIsHereToForceASemiColonAfterThisEventTargetMacro
246
247 // Use this macro if your EventTarget subclass is also a subclass of WTF::RefCounted.
248 // A ref-counted class that uses a different method of refcounting should use DEFINE_EVENT_TARGET_REFCOUNTING directly.
249 // Both of these macros are meant to be placed just before the "public:" section of the class declaration.
250 #define REFCOUNTED_EVENT_TARGET(className) DEFINE_EVENT_TARGET_REFCOUNTING(RefCounted<className>)
251
252 #endif // EventTarget_h