blob: 9edb3af31ecff55a077250c5df9dbc9d3451c8d2 [file] [log] [blame]
weinig681a5172006-06-19 22:58:36 +00001/*
weinig681a5172006-06-19 22:58:36 +00002 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
benjamin@webkit.orgac28bde2013-06-19 22:41:17 +00005 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
weinig681a5172006-06-19 22:58:36 +00006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
weinig681a5172006-06-19 22:58:36 +000021 *
22 */
23
commit-queue@webkit.orgd862d772016-10-31 22:07:53 +000024#pragma once
weinig681a5172006-06-19 22:58:36 +000025
commit-queue@webkit.org78873c92010-11-09 04:14:36 +000026#include "DOMTimeStamp.h"
cdumez@apple.coma7d6ccf2016-10-09 00:03:14 +000027#include "EventInit.h"
weinig@apple.comd2bf6492013-09-22 04:15:10 +000028#include "EventInterfaces.h"
cdumez@apple.com68c6eb52016-10-26 20:17:24 +000029#include "ExceptionOr.h"
eric@webkit.orgd0219bf2012-11-16 22:02:21 +000030#include "ScriptWrappable.h"
ggaren@apple.com58ea1c12007-11-14 02:05:26 +000031#include <wtf/RefCounted.h>
cdumez@apple.com434faff2014-10-01 22:29:14 +000032#include <wtf/TypeCasts.h>
barraclough@apple.combbb3cd42010-08-10 17:45:41 +000033#include <wtf/text/AtomicString.h>
weinig681a5172006-06-19 22:58:36 +000034
35namespace WebCore {
36
ap@apple.com236ec762014-04-08 22:06:16 +000037class DataTransfer;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000038class EventPath;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000039class EventTarget;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000040class HTMLIFrameElement;
cdumez@apple.com68c6eb52016-10-26 20:17:24 +000041class ScriptExecutionContext;
weinig950051b2007-06-13 02:40:00 +000042
weinig@apple.comd2bf6492013-09-22 04:15:10 +000043enum EventInterface {
44
45#define DOM_EVENT_INTERFACE_DECLARE(name) name##InterfaceType,
46DOM_EVENT_INTERFACES_FOR_EACH(DOM_EVENT_INTERFACE_DECLARE)
47#undef DOM_EVENT_INTERFACE_DECLARE
48
49};
50
eric@webkit.orgd0219bf2012-11-16 22:02:21 +000051class Event : public ScriptWrappable, public RefCounted<Event> {
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000052public:
cdumez@apple.coma7d6ccf2016-10-09 00:03:14 +000053 enum class IsTrusted { No, Yes };
54
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000055 enum PhaseType {
56 NONE = 0,
57 CAPTURING_PHASE = 1,
58 AT_TARGET = 2,
59 BUBBLING_PHASE = 3
weinig@apple.com8dc04032011-08-28 23:58:25 +000060 };
61
ddkilzer@apple.com63a5f972015-02-14 21:52:34 +000062 static Ref<Event> create(const AtomicString& type, bool canBubble, bool cancelable)
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000063 {
akling@apple.comb6c8c7c2014-12-14 12:23:31 +000064 return adoptRef(*new Event(type, canBubble, cancelable));
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000065 }
66
jiewen_tan@apple.comcb979e92016-02-11 00:03:11 +000067 static Ref<Event> createForBindings()
68 {
69 return adoptRef(*new Event);
70 }
71
cdumez@apple.comf998ad22016-10-19 02:26:50 +000072 static Ref<Event> create(const AtomicString& type, const EventInit& initializer, IsTrusted isTrusted = IsTrusted::No)
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000073 {
cdumez@apple.comf998ad22016-10-19 02:26:50 +000074 return adoptRef(*new Event(type, initializer, isTrusted));
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000075 }
76
77 virtual ~Event();
78
achristensen@apple.com760090a2016-08-22 15:51:58 +000079 WEBCORE_EXPORT void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
cdumez@apple.com68c6eb52016-10-26 20:17:24 +000080
cdumez@apple.comff1aad82015-09-06 20:41:48 +000081 bool isInitialized() const { return m_isInitialized; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000082
83 const AtomicString& type() const { return m_type; }
alexis@webkit.orgfab33032013-05-17 03:26:58 +000084 void setType(const AtomicString& type) { m_type = type; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000085
86 EventTarget* target() const { return m_target.get(); }
cdumez@apple.com32216e42015-11-12 07:28:57 +000087 void setTarget(RefPtr<EventTarget>&&);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000088
bfulgham@apple.combc488af2017-02-10 02:21:09 +000089 EventTarget* currentTarget() const { return m_currentTarget.get(); }
90 void setCurrentTarget(EventTarget*);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000091
92 unsigned short eventPhase() const { return m_eventPhase; }
93 void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
94
95 bool bubbles() const { return m_canBubble; }
96 bool cancelable() const { return m_cancelable; }
achristensen@apple.com760090a2016-08-22 15:51:58 +000097 WEBCORE_EXPORT bool composed() const;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000098
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000099 DOMTimeStamp timeStamp() const { return m_createTime; }
100
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +0000101 void setEventPath(const EventPath& path) { m_eventPath = &path; }
102 void clearEventPath() { m_eventPath = nullptr; }
rniwa@webkit.orgea6b41a2016-05-09 17:47:01 +0000103 Vector<EventTarget*> composedPath() const;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +0000104
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000105 void stopPropagation() { m_propagationStopped = true; }
106 void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
jiewen_tan@apple.com4028c652016-02-12 23:25:02 +0000107
108 bool isTrusted() const { return m_isTrusted; }
109 void setUntrusted() { m_isTrusted = false; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000110
111 // IE Extensions
112 EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
113
ch.dumez@sisa.samsung.com231386f2013-09-09 18:40:04 +0000114 bool legacyReturnValue() const { return !defaultPrevented(); }
115 void setLegacyReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000116
weinig@apple.comd2bf6492013-09-22 04:15:10 +0000117 virtual EventInterface eventInterface() const;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000118
119 // These events are general classes of events.
120 virtual bool isUIEvent() const;
121 virtual bool isMouseEvent() const;
haraken@chromium.org4c2d69b2013-02-07 08:30:00 +0000122 virtual bool isFocusEvent() const;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000123 virtual bool isKeyboardEvent() const;
wenson_hsieh@apple.com96830b22016-10-06 04:05:35 +0000124 virtual bool isInputEvent() const;
rniwa@webkit.org4c110022016-07-08 01:57:44 +0000125 virtual bool isCompositionEvent() const;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000126 virtual bool isTouchEvent() const;
127
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000128 // These events lack a DOM interface.
129 virtual bool isClipboardEvent() const;
130 virtual bool isBeforeTextInsertedEvent() const;
131
ch.dumez@sisa.samsung.com231386f2013-09-09 18:40:04 +0000132 virtual bool isBeforeUnloadEvent() const;
133
akling@apple.com3a14a922014-02-16 02:01:04 +0000134 virtual bool isErrorEvent() const;
135 virtual bool isTextEvent() const;
136 virtual bool isWheelEvent() const;
137
beidson@apple.com691c3f82015-12-31 05:43:33 +0000138#if ENABLE(INDEXED_DATABASE)
139 virtual bool isVersionChangeEvent() const { return false; }
140#endif
141
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000142 bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
143 bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
144
cdumez@apple.com9d5fb892016-08-19 15:36:55 +0000145 void resetPropagationFlags();
146
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000147 bool defaultPrevented() const { return m_defaultPrevented; }
148 void preventDefault()
149 {
cdumez@apple.com613bbf32016-06-07 17:53:15 +0000150 if (m_cancelable && !m_isExecutingPassiveEventListener)
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000151 m_defaultPrevented = true;
152 }
153 void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
154
155 bool defaultHandled() const { return m_defaultHandled; }
156 void setDefaultHandled() { m_defaultHandled = true; }
157
cdumez@apple.com613bbf32016-06-07 17:53:15 +0000158 void setInPassiveListener(bool value) { m_isExecutingPassiveEventListener = value; }
159
cdumez@apple.com86cd0922017-01-03 23:51:39 +0000160 bool cancelBubble() const { return propagationStopped(); }
161 void setCancelBubble(bool);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000162
163 Event* underlyingEvent() const { return m_underlyingEvent.get(); }
gyuyoung.kim@webkit.org5c6cefe2016-01-31 12:08:32 +0000164 void setUnderlyingEvent(Event*);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000165
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000166 bool isBeingDispatched() const { return eventPhase(); }
167
rniwa@webkit.orgd0fec7e2013-10-09 20:05:13 +0000168 virtual EventTarget* relatedTarget() const { return nullptr; }
169
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000170protected:
cdumez@apple.comdf8c1e42016-10-09 03:46:39 +0000171 Event(IsTrusted = IsTrusted::No);
achristensen@apple.com1d463762015-02-10 23:07:47 +0000172 WEBCORE_EXPORT Event(const AtomicString& type, bool canBubble, bool cancelable);
benjamin@webkit.orgac28bde2013-06-19 22:41:17 +0000173 Event(const AtomicString& type, bool canBubble, bool cancelable, double timestamp);
cdumez@apple.comf998ad22016-10-19 02:26:50 +0000174 Event(const AtomicString& type, const EventInit&, IsTrusted);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000175
176 virtual void receivedTarget();
177 bool dispatched() const { return m_target; }
178
179private:
180 AtomicString m_type;
rniwa@webkit.org4c110022016-07-08 01:57:44 +0000181
182 bool m_isInitialized { false };
hs85.jeong@samsung.coma08a0ad2015-11-03 18:24:34 +0000183 bool m_canBubble { false };
184 bool m_cancelable { false };
rniwa@webkit.org4c110022016-07-08 01:57:44 +0000185 bool m_composed { false };
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000186
hs85.jeong@samsung.coma08a0ad2015-11-03 18:24:34 +0000187 bool m_propagationStopped { false };
188 bool m_immediatePropagationStopped { false };
189 bool m_defaultPrevented { false };
190 bool m_defaultHandled { false };
jiewen_tan@apple.com4028c652016-02-12 23:25:02 +0000191 bool m_isTrusted { false };
cdumez@apple.com613bbf32016-06-07 17:53:15 +0000192 bool m_isExecutingPassiveEventListener { false };
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000193
hs85.jeong@samsung.coma08a0ad2015-11-03 18:24:34 +0000194 unsigned short m_eventPhase { 0 };
bfulgham@apple.combc488af2017-02-10 02:21:09 +0000195 RefPtr<EventTarget> m_currentTarget;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +0000196 const EventPath* m_eventPath { nullptr };
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000197 RefPtr<EventTarget> m_target;
198 DOMTimeStamp m_createTime;
199
200 RefPtr<Event> m_underlyingEvent;
201};
202
cdumez@apple.com9d5fb892016-08-19 15:36:55 +0000203inline void Event::resetPropagationFlags()
204{
205 m_propagationStopped = false;
206 m_immediatePropagationStopped = false;
207}
208
cdumez@apple.com86cd0922017-01-03 23:51:39 +0000209inline void Event::setCancelBubble(bool cancel)
210{
211 if (cancel)
212 m_propagationStopped = true;
213}
214
weinig681a5172006-06-19 22:58:36 +0000215} // namespace WebCore
216
cdumez@apple.com434faff2014-10-01 22:29:14 +0000217#define SPECIALIZE_TYPE_TRAITS_EVENT(ToValueTypeName) \
218SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \
219 static bool isType(const WebCore::Event& event) { return event.is##ToValueTypeName(); } \
220SPECIALIZE_TYPE_TRAITS_END()