blob: 1bc725504a867a77f9fa25e232ed21bd66058383 [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)
darin@apple.coma92d1a62017-11-04 21:19:11 +00005 * Copyright (C) 2003-2017 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
cdumez@apple.com8e740352017-09-22 17:13:06 +000026#include "DOMHighResTimeStamp.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"
cdumez@apple.com8e740352017-09-22 17:13:06 +000031#include <wtf/MonotonicTime.h>
cdumez@apple.com434faff2014-10-01 22:29:14 +000032#include <wtf/TypeCasts.h>
darin@apple.com0ce67df2019-06-17 01:48:13 +000033#include <wtf/text/AtomString.h>
weinig681a5172006-06-19 22:58:36 +000034
35namespace WebCore {
36
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000037class EventPath;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000038class EventTarget;
cdumez@apple.com68c6eb52016-10-26 20:17:24 +000039class ScriptExecutionContext;
weinig950051b2007-06-13 02:40:00 +000040
eric@webkit.orgd0219bf2012-11-16 22:02:21 +000041class Event : public ScriptWrappable, public RefCounted<Event> {
ysuzuki@apple.come4070702019-09-18 22:10:00 +000042 WTF_MAKE_ISO_ALLOCATED(Event);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000043public:
rniwa@webkit.orgc844ee62018-08-18 00:00:25 +000044 enum class IsTrusted : uint8_t { No, Yes };
45 enum class CanBubble : uint8_t { No, Yes };
46 enum class IsCancelable : uint8_t { No, Yes };
rniwa@webkit.org06e85a92018-08-18 06:47:22 +000047 enum class IsComposed : uint8_t { No, Yes };
cdumez@apple.coma7d6ccf2016-10-09 00:03:14 +000048
yusukesuzuki@slowstart.org2e3fd132018-08-19 23:59:12 +000049 enum PhaseType : uint8_t {
darin@apple.coma92d1a62017-11-04 21:19:11 +000050 NONE = 0,
51 CAPTURING_PHASE = 1,
52 AT_TARGET = 2,
53 BUBBLING_PHASE = 3
weinig@apple.com8dc04032011-08-28 23:58:25 +000054 };
55
darin@apple.com0ce67df2019-06-17 01:48:13 +000056 WEBCORE_EXPORT static Ref<Event> create(const AtomString& type, CanBubble, IsCancelable, IsComposed = IsComposed::No);
darin@apple.coma92d1a62017-11-04 21:19:11 +000057 static Ref<Event> createForBindings();
darin@apple.com0ce67df2019-06-17 01:48:13 +000058 static Ref<Event> create(const AtomString& type, const EventInit&, IsTrusted = IsTrusted::No);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000059
60 virtual ~Event();
61
darin@apple.com0ce67df2019-06-17 01:48:13 +000062 WEBCORE_EXPORT void initEvent(const AtomString& type, bool canBubble, bool cancelable);
cdumez@apple.com68c6eb52016-10-26 20:17:24 +000063
cdumez@apple.comff1aad82015-09-06 20:41:48 +000064 bool isInitialized() const { return m_isInitialized; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000065
darin@apple.com0ce67df2019-06-17 01:48:13 +000066 const AtomString& type() const { return m_type; }
67 void setType(const AtomString& type) { m_type = type; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000068
69 EventTarget* target() const { return m_target.get(); }
cdumez@apple.com32216e42015-11-12 07:28:57 +000070 void setTarget(RefPtr<EventTarget>&&);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000071
bfulgham@apple.combc488af2017-02-10 02:21:09 +000072 EventTarget* currentTarget() const { return m_currentTarget.get(); }
73 void setCurrentTarget(EventTarget*);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000074
75 unsigned short eventPhase() const { return m_eventPhase; }
darin@apple.coma92d1a62017-11-04 21:19:11 +000076 void setEventPhase(PhaseType phase) { m_eventPhase = phase; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000077
78 bool bubbles() const { return m_canBubble; }
79 bool cancelable() const { return m_cancelable; }
rniwa@webkit.org1321fa32018-08-24 20:14:50 +000080 bool composed() const { return m_composed; }
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000081
cdumez@apple.com8e740352017-09-22 17:13:06 +000082 DOMHighResTimeStamp timeStampForBindings(ScriptExecutionContext&) const;
83 MonotonicTime timeStamp() const { return m_createTime; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000084
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000085 void setEventPath(const EventPath& path) { m_eventPath = &path; }
rniwa@webkit.orgea6b41a2016-05-09 17:47:01 +000086 Vector<EventTarget*> composedPath() const;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +000087
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000088 void stopPropagation() { m_propagationStopped = true; }
89 void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
jiewen_tan@apple.com4028c652016-02-12 23:25:02 +000090
91 bool isTrusted() const { return m_isTrusted; }
92 void setUntrusted() { m_isTrusted = false; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000093
darin@apple.com0b38bee2018-02-08 06:06:29 +000094 bool legacyReturnValue() const { return !m_wasCanceled; }
cdumez@apple.com2734f6e2018-04-16 01:01:18 +000095 void setLegacyReturnValue(bool);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000096
darin@apple.coma92d1a62017-11-04 21:19:11 +000097 virtual EventInterface eventInterface() const { return EventInterfaceType; }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +000098
darin@apple.coma92d1a62017-11-04 21:19:11 +000099 virtual bool isBeforeTextInsertedEvent() const { return false; }
100 virtual bool isBeforeUnloadEvent() const { return false; }
101 virtual bool isClipboardEvent() const { return false; }
102 virtual bool isCompositionEvent() const { return false; }
103 virtual bool isErrorEvent() const { return false; }
104 virtual bool isFocusEvent() const { return false; }
105 virtual bool isInputEvent() const { return false; }
106 virtual bool isKeyboardEvent() const { return false; }
107 virtual bool isMouseEvent() const { return false; }
dino@apple.comf6e7e052018-09-22 00:51:47 +0000108 virtual bool isPointerEvent() const { return false; }
darin@apple.coma92d1a62017-11-04 21:19:11 +0000109 virtual bool isTextEvent() const { return false; }
110 virtual bool isTouchEvent() const { return false; }
111 virtual bool isUIEvent() const { return false; }
beidson@apple.com691c3f82015-12-31 05:43:33 +0000112 virtual bool isVersionChangeEvent() const { return false; }
darin@apple.coma92d1a62017-11-04 21:19:11 +0000113 virtual bool isWheelEvent() const { return false; }
beidson@apple.com691c3f82015-12-31 05:43:33 +0000114
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000115 bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
116 bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
117
darin@apple.com0b38bee2018-02-08 06:06:29 +0000118 void resetBeforeDispatch();
darin@apple.coma92d1a62017-11-04 21:19:11 +0000119 void resetAfterDispatch();
cdumez@apple.com9d5fb892016-08-19 15:36:55 +0000120
darin@apple.com0b38bee2018-02-08 06:06:29 +0000121 bool defaultPrevented() const { return m_wasCanceled; }
darin@apple.coma92d1a62017-11-04 21:19:11 +0000122 void preventDefault();
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000123
124 bool defaultHandled() const { return m_defaultHandled; }
125 void setDefaultHandled() { m_defaultHandled = true; }
126
rniwa@webkit.orgedcf0872018-08-09 04:32:39 +0000127 bool isDefaultEventHandlerIgnored() const { return m_isDefaultEventHandlerIgnored; }
128 void setIsDefaultEventHandlerIgnored() { m_isDefaultEventHandlerIgnored = true; }
129
cdumez@apple.com613bbf32016-06-07 17:53:15 +0000130 void setInPassiveListener(bool value) { m_isExecutingPassiveEventListener = value; }
131
cdumez@apple.com86cd0922017-01-03 23:51:39 +0000132 bool cancelBubble() const { return propagationStopped(); }
133 void setCancelBubble(bool);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000134
135 Event* underlyingEvent() const { return m_underlyingEvent.get(); }
gyuyoung.kim@webkit.org5c6cefe2016-01-31 12:08:32 +0000136 void setUnderlyingEvent(Event*);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000137
cdumez@apple.come7147302017-10-23 19:36:24 +0000138 // Returns true if the dispatch flag is set.
139 // https://dom.spec.whatwg.org/#dispatch-flag
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000140 bool isBeingDispatched() const { return eventPhase(); }
141
rniwa@webkit.orgd0fec7e2013-10-09 20:05:13 +0000142 virtual EventTarget* relatedTarget() const { return nullptr; }
darin@apple.com05c7bff2017-10-07 23:44:45 +0000143 virtual void setRelatedTarget(EventTarget&) { }
rniwa@webkit.orgd0fec7e2013-10-09 20:05:13 +0000144
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000145protected:
darin@apple.coma92d1a62017-11-04 21:19:11 +0000146 explicit Event(IsTrusted = IsTrusted::No);
darin@apple.com0ce67df2019-06-17 01:48:13 +0000147 Event(const AtomString& type, CanBubble, IsCancelable, IsComposed = IsComposed::No);
148 Event(const AtomString& type, CanBubble, IsCancelable, IsComposed, MonotonicTime timestamp, IsTrusted isTrusted = IsTrusted::Yes);
149 Event(const AtomString& type, const EventInit&, IsTrusted);
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000150
darin@apple.coma92d1a62017-11-04 21:19:11 +0000151 virtual void receivedTarget() { }
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000152
153private:
darin@apple.com0ce67df2019-06-17 01:48:13 +0000154 explicit Event(MonotonicTime createTime, const AtomString& type, IsTrusted, CanBubble, IsCancelable, IsComposed);
rniwa@webkit.org4c110022016-07-08 01:57:44 +0000155
cdumez@apple.com2734f6e2018-04-16 01:01:18 +0000156 void setCanceledFlagIfPossible();
157
rniwa@webkit.org06e85a92018-08-18 06:47:22 +0000158 unsigned m_isInitialized : 1;
159 unsigned m_canBubble : 1;
160 unsigned m_cancelable : 1;
161 unsigned m_composed : 1;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000162
rniwa@webkit.org06e85a92018-08-18 06:47:22 +0000163 unsigned m_propagationStopped : 1;
164 unsigned m_immediatePropagationStopped : 1;
165 unsigned m_wasCanceled : 1;
166 unsigned m_defaultHandled : 1;
167 unsigned m_isDefaultEventHandlerIgnored : 1;
168 unsigned m_isTrusted : 1;
169 unsigned m_isExecutingPassiveEventListener : 1;
170
171 unsigned m_eventPhase : 2;
172
darin@apple.com0ce67df2019-06-17 01:48:13 +0000173 AtomString m_type;
yusukesuzuki@slowstart.org2e3fd132018-08-19 23:59:12 +0000174
bfulgham@apple.combc488af2017-02-10 02:21:09 +0000175 RefPtr<EventTarget> m_currentTarget;
rniwa@webkit.orgaaacd152016-03-12 03:11:04 +0000176 const EventPath* m_eventPath { nullptr };
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000177 RefPtr<EventTarget> m_target;
cdumez@apple.com8e740352017-09-22 17:13:06 +0000178 MonotonicTime m_createTime;
commit-queue@webkit.org2b5fd702012-09-04 22:22:40 +0000179
180 RefPtr<Event> m_underlyingEvent;
181};
182
darin@apple.coma92d1a62017-11-04 21:19:11 +0000183inline void Event::preventDefault()
184{
cdumez@apple.com2734f6e2018-04-16 01:01:18 +0000185 setCanceledFlagIfPossible();
186}
187
188inline void Event::setLegacyReturnValue(bool returnValue)
189{
190 if (!returnValue)
191 setCanceledFlagIfPossible();
192}
193
194// https://dom.spec.whatwg.org/#set-the-canceled-flag
195inline void Event::setCanceledFlagIfPossible()
196{
darin@apple.coma92d1a62017-11-04 21:19:11 +0000197 if (m_cancelable && !m_isExecutingPassiveEventListener)
darin@apple.com0b38bee2018-02-08 06:06:29 +0000198 m_wasCanceled = true;
darin@apple.coma92d1a62017-11-04 21:19:11 +0000199 // FIXME: Specification suggests we log something to the console when preventDefault is called but
200 // doesn't do anything because the event is not cancelable or is executing passive event listeners.
cdumez@apple.com9d5fb892016-08-19 15:36:55 +0000201}
202
cdumez@apple.com86cd0922017-01-03 23:51:39 +0000203inline void Event::setCancelBubble(bool cancel)
204{
205 if (cancel)
206 m_propagationStopped = true;
207}
208
weinig681a5172006-06-19 22:58:36 +0000209} // namespace WebCore
210
cdumez@apple.com434faff2014-10-01 22:29:14 +0000211#define SPECIALIZE_TYPE_TRAITS_EVENT(ToValueTypeName) \
212SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \
213 static bool isType(const WebCore::Event& event) { return event.is##ToValueTypeName(); } \
214SPECIALIZE_TYPE_TRAITS_END()