weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 1 | /* |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 2 | * 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.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003-2017 Apple Inc. All rights reserved. |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 6 | * |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
commit-queue@webkit.org | d862d77 | 2016-10-31 22:07:53 +0000 | [diff] [blame] | 24 | #pragma once |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 25 | |
cdumez@apple.com | 8e74035 | 2017-09-22 17:13:06 +0000 | [diff] [blame] | 26 | #include "DOMHighResTimeStamp.h" |
cdumez@apple.com | a7d6ccf | 2016-10-09 00:03:14 +0000 | [diff] [blame] | 27 | #include "EventInit.h" |
weinig@apple.com | d2bf649 | 2013-09-22 04:15:10 +0000 | [diff] [blame] | 28 | #include "EventInterfaces.h" |
cdumez@apple.com | 68c6eb5 | 2016-10-26 20:17:24 +0000 | [diff] [blame] | 29 | #include "ExceptionOr.h" |
eric@webkit.org | d0219bf | 2012-11-16 22:02:21 +0000 | [diff] [blame] | 30 | #include "ScriptWrappable.h" |
cdumez@apple.com | 8e74035 | 2017-09-22 17:13:06 +0000 | [diff] [blame] | 31 | #include <wtf/MonotonicTime.h> |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 32 | #include <wtf/TypeCasts.h> |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 33 | #include <wtf/text/AtomString.h> |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 34 | |
| 35 | namespace WebCore { |
| 36 | |
rniwa@webkit.org | aaacd15 | 2016-03-12 03:11:04 +0000 | [diff] [blame] | 37 | class EventPath; |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 38 | class EventTarget; |
cdumez@apple.com | 68c6eb5 | 2016-10-26 20:17:24 +0000 | [diff] [blame] | 39 | class ScriptExecutionContext; |
weinig | 950051b | 2007-06-13 02:40:00 +0000 | [diff] [blame] | 40 | |
eric@webkit.org | d0219bf | 2012-11-16 22:02:21 +0000 | [diff] [blame] | 41 | class Event : public ScriptWrappable, public RefCounted<Event> { |
ysuzuki@apple.com | e407070 | 2019-09-18 22:10:00 +0000 | [diff] [blame] | 42 | WTF_MAKE_ISO_ALLOCATED(Event); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 43 | public: |
rniwa@webkit.org | c844ee6 | 2018-08-18 00:00:25 +0000 | [diff] [blame] | 44 | 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.org | 06e85a9 | 2018-08-18 06:47:22 +0000 | [diff] [blame] | 47 | enum class IsComposed : uint8_t { No, Yes }; |
cdumez@apple.com | a7d6ccf | 2016-10-09 00:03:14 +0000 | [diff] [blame] | 48 | |
yusukesuzuki@slowstart.org | 2e3fd13 | 2018-08-19 23:59:12 +0000 | [diff] [blame] | 49 | enum PhaseType : uint8_t { |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 50 | NONE = 0, |
| 51 | CAPTURING_PHASE = 1, |
| 52 | AT_TARGET = 2, |
| 53 | BUBBLING_PHASE = 3 |
weinig@apple.com | 8dc0403 | 2011-08-28 23:58:25 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 56 | WEBCORE_EXPORT static Ref<Event> create(const AtomString& type, CanBubble, IsCancelable, IsComposed = IsComposed::No); |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 57 | static Ref<Event> createForBindings(); |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 58 | static Ref<Event> create(const AtomString& type, const EventInit&, IsTrusted = IsTrusted::No); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 59 | |
| 60 | virtual ~Event(); |
| 61 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 62 | WEBCORE_EXPORT void initEvent(const AtomString& type, bool canBubble, bool cancelable); |
cdumez@apple.com | 68c6eb5 | 2016-10-26 20:17:24 +0000 | [diff] [blame] | 63 | |
cdumez@apple.com | ff1aad8 | 2015-09-06 20:41:48 +0000 | [diff] [blame] | 64 | bool isInitialized() const { return m_isInitialized; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 65 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 66 | const AtomString& type() const { return m_type; } |
| 67 | void setType(const AtomString& type) { m_type = type; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 68 | |
| 69 | EventTarget* target() const { return m_target.get(); } |
cdumez@apple.com | 32216e4 | 2015-11-12 07:28:57 +0000 | [diff] [blame] | 70 | void setTarget(RefPtr<EventTarget>&&); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 71 | |
bfulgham@apple.com | bc488af | 2017-02-10 02:21:09 +0000 | [diff] [blame] | 72 | EventTarget* currentTarget() const { return m_currentTarget.get(); } |
| 73 | void setCurrentTarget(EventTarget*); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 74 | |
| 75 | unsigned short eventPhase() const { return m_eventPhase; } |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 76 | void setEventPhase(PhaseType phase) { m_eventPhase = phase; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 77 | |
| 78 | bool bubbles() const { return m_canBubble; } |
| 79 | bool cancelable() const { return m_cancelable; } |
rniwa@webkit.org | 1321fa3 | 2018-08-24 20:14:50 +0000 | [diff] [blame] | 80 | bool composed() const { return m_composed; } |
rniwa@webkit.org | aaacd15 | 2016-03-12 03:11:04 +0000 | [diff] [blame] | 81 | |
cdumez@apple.com | 8e74035 | 2017-09-22 17:13:06 +0000 | [diff] [blame] | 82 | DOMHighResTimeStamp timeStampForBindings(ScriptExecutionContext&) const; |
| 83 | MonotonicTime timeStamp() const { return m_createTime; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 84 | |
rniwa@webkit.org | aaacd15 | 2016-03-12 03:11:04 +0000 | [diff] [blame] | 85 | void setEventPath(const EventPath& path) { m_eventPath = &path; } |
rniwa@webkit.org | ea6b41a | 2016-05-09 17:47:01 +0000 | [diff] [blame] | 86 | Vector<EventTarget*> composedPath() const; |
rniwa@webkit.org | aaacd15 | 2016-03-12 03:11:04 +0000 | [diff] [blame] | 87 | |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 88 | void stopPropagation() { m_propagationStopped = true; } |
| 89 | void stopImmediatePropagation() { m_immediatePropagationStopped = true; } |
jiewen_tan@apple.com | 4028c65 | 2016-02-12 23:25:02 +0000 | [diff] [blame] | 90 | |
| 91 | bool isTrusted() const { return m_isTrusted; } |
| 92 | void setUntrusted() { m_isTrusted = false; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 93 | |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 94 | bool legacyReturnValue() const { return !m_wasCanceled; } |
cdumez@apple.com | 2734f6e | 2018-04-16 01:01:18 +0000 | [diff] [blame] | 95 | void setLegacyReturnValue(bool); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 96 | |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 97 | virtual EventInterface eventInterface() const { return EventInterfaceType; } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 98 | |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 99 | 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.com | f6e7e05 | 2018-09-22 00:51:47 +0000 | [diff] [blame] | 108 | virtual bool isPointerEvent() const { return false; } |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 109 | virtual bool isTextEvent() const { return false; } |
| 110 | virtual bool isTouchEvent() const { return false; } |
| 111 | virtual bool isUIEvent() const { return false; } |
beidson@apple.com | 691c3f8 | 2015-12-31 05:43:33 +0000 | [diff] [blame] | 112 | virtual bool isVersionChangeEvent() const { return false; } |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 113 | virtual bool isWheelEvent() const { return false; } |
beidson@apple.com | 691c3f8 | 2015-12-31 05:43:33 +0000 | [diff] [blame] | 114 | |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 115 | bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; } |
| 116 | bool immediatePropagationStopped() const { return m_immediatePropagationStopped; } |
| 117 | |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 118 | void resetBeforeDispatch(); |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 119 | void resetAfterDispatch(); |
cdumez@apple.com | 9d5fb89 | 2016-08-19 15:36:55 +0000 | [diff] [blame] | 120 | |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 121 | bool defaultPrevented() const { return m_wasCanceled; } |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 122 | void preventDefault(); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 123 | |
| 124 | bool defaultHandled() const { return m_defaultHandled; } |
| 125 | void setDefaultHandled() { m_defaultHandled = true; } |
| 126 | |
rniwa@webkit.org | edcf087 | 2018-08-09 04:32:39 +0000 | [diff] [blame] | 127 | bool isDefaultEventHandlerIgnored() const { return m_isDefaultEventHandlerIgnored; } |
| 128 | void setIsDefaultEventHandlerIgnored() { m_isDefaultEventHandlerIgnored = true; } |
| 129 | |
cdumez@apple.com | 613bbf3 | 2016-06-07 17:53:15 +0000 | [diff] [blame] | 130 | void setInPassiveListener(bool value) { m_isExecutingPassiveEventListener = value; } |
| 131 | |
cdumez@apple.com | 86cd092 | 2017-01-03 23:51:39 +0000 | [diff] [blame] | 132 | bool cancelBubble() const { return propagationStopped(); } |
| 133 | void setCancelBubble(bool); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 134 | |
| 135 | Event* underlyingEvent() const { return m_underlyingEvent.get(); } |
gyuyoung.kim@webkit.org | 5c6cefe | 2016-01-31 12:08:32 +0000 | [diff] [blame] | 136 | void setUnderlyingEvent(Event*); |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 137 | |
cdumez@apple.com | e714730 | 2017-10-23 19:36:24 +0000 | [diff] [blame] | 138 | // Returns true if the dispatch flag is set. |
| 139 | // https://dom.spec.whatwg.org/#dispatch-flag |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 140 | bool isBeingDispatched() const { return eventPhase(); } |
| 141 | |
rniwa@webkit.org | d0fec7e | 2013-10-09 20:05:13 +0000 | [diff] [blame] | 142 | virtual EventTarget* relatedTarget() const { return nullptr; } |
darin@apple.com | 05c7bff | 2017-10-07 23:44:45 +0000 | [diff] [blame] | 143 | virtual void setRelatedTarget(EventTarget&) { } |
rniwa@webkit.org | d0fec7e | 2013-10-09 20:05:13 +0000 | [diff] [blame] | 144 | |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 145 | protected: |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 146 | explicit Event(IsTrusted = IsTrusted::No); |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 147 | 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.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 150 | |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 151 | virtual void receivedTarget() { } |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 152 | |
| 153 | private: |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 154 | explicit Event(MonotonicTime createTime, const AtomString& type, IsTrusted, CanBubble, IsCancelable, IsComposed); |
rniwa@webkit.org | 4c11002 | 2016-07-08 01:57:44 +0000 | [diff] [blame] | 155 | |
cdumez@apple.com | 2734f6e | 2018-04-16 01:01:18 +0000 | [diff] [blame] | 156 | void setCanceledFlagIfPossible(); |
| 157 | |
rniwa@webkit.org | 06e85a9 | 2018-08-18 06:47:22 +0000 | [diff] [blame] | 158 | unsigned m_isInitialized : 1; |
| 159 | unsigned m_canBubble : 1; |
| 160 | unsigned m_cancelable : 1; |
| 161 | unsigned m_composed : 1; |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 162 | |
rniwa@webkit.org | 06e85a9 | 2018-08-18 06:47:22 +0000 | [diff] [blame] | 163 | 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.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 173 | AtomString m_type; |
yusukesuzuki@slowstart.org | 2e3fd13 | 2018-08-19 23:59:12 +0000 | [diff] [blame] | 174 | |
bfulgham@apple.com | bc488af | 2017-02-10 02:21:09 +0000 | [diff] [blame] | 175 | RefPtr<EventTarget> m_currentTarget; |
rniwa@webkit.org | aaacd15 | 2016-03-12 03:11:04 +0000 | [diff] [blame] | 176 | const EventPath* m_eventPath { nullptr }; |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 177 | RefPtr<EventTarget> m_target; |
cdumez@apple.com | 8e74035 | 2017-09-22 17:13:06 +0000 | [diff] [blame] | 178 | MonotonicTime m_createTime; |
commit-queue@webkit.org | 2b5fd70 | 2012-09-04 22:22:40 +0000 | [diff] [blame] | 179 | |
| 180 | RefPtr<Event> m_underlyingEvent; |
| 181 | }; |
| 182 | |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 183 | inline void Event::preventDefault() |
| 184 | { |
cdumez@apple.com | 2734f6e | 2018-04-16 01:01:18 +0000 | [diff] [blame] | 185 | setCanceledFlagIfPossible(); |
| 186 | } |
| 187 | |
| 188 | inline void Event::setLegacyReturnValue(bool returnValue) |
| 189 | { |
| 190 | if (!returnValue) |
| 191 | setCanceledFlagIfPossible(); |
| 192 | } |
| 193 | |
| 194 | // https://dom.spec.whatwg.org/#set-the-canceled-flag |
| 195 | inline void Event::setCanceledFlagIfPossible() |
| 196 | { |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 197 | if (m_cancelable && !m_isExecutingPassiveEventListener) |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 198 | m_wasCanceled = true; |
darin@apple.com | a92d1a6 | 2017-11-04 21:19:11 +0000 | [diff] [blame] | 199 | // 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.com | 9d5fb89 | 2016-08-19 15:36:55 +0000 | [diff] [blame] | 201 | } |
| 202 | |
cdumez@apple.com | 86cd092 | 2017-01-03 23:51:39 +0000 | [diff] [blame] | 203 | inline void Event::setCancelBubble(bool cancel) |
| 204 | { |
| 205 | if (cancel) |
| 206 | m_propagationStopped = true; |
| 207 | } |
| 208 | |
weinig | 681a517 | 2006-06-19 22:58:36 +0000 | [diff] [blame] | 209 | } // namespace WebCore |
| 210 | |
cdumez@apple.com | 434faff | 2014-10-01 22:29:14 +0000 | [diff] [blame] | 211 | #define SPECIALIZE_TYPE_TRAITS_EVENT(ToValueTypeName) \ |
| 212 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \ |
| 213 | static bool isType(const WebCore::Event& event) { return event.is##ToValueTypeName(); } \ |
| 214 | SPECIALIZE_TYPE_TRAITS_END() |