| 1 | /* |
| 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) |
| 5 | * Copyright (C) 2003-2017 Apple Inc. All rights reserved. |
| 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 |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "Event.h" |
| 25 | |
| 26 | #include "DOMWindow.h" |
| 27 | #include "Document.h" |
| 28 | #include "EventNames.h" |
| 29 | #include "EventPath.h" |
| 30 | #include "EventTarget.h" |
| 31 | #include "InspectorInstrumentation.h" |
| 32 | #include "Performance.h" |
| 33 | #include "UserGestureIndicator.h" |
| 34 | #include "WorkerGlobalScope.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | ALWAYS_INLINE Event::Event(MonotonicTime createTime, const AtomicString& type, IsTrusted isTrusted, CanBubble canBubble, IsCancelable cancelable, IsComposed composed) |
| 39 | : m_isInitialized { !type.isNull() } |
| 40 | , m_canBubble { canBubble == CanBubble::Yes } |
| 41 | , m_cancelable { cancelable == IsCancelable::Yes } |
| 42 | , m_composed { composed == IsComposed::Yes } |
| 43 | , m_propagationStopped { false } |
| 44 | , m_immediatePropagationStopped { false } |
| 45 | , m_wasCanceled { false } |
| 46 | , m_defaultHandled { false } |
| 47 | , m_isDefaultEventHandlerIgnored { false } |
| 48 | , m_isTrusted { isTrusted == IsTrusted::Yes } |
| 49 | , m_isExecutingPassiveEventListener { false } |
| 50 | , m_eventPhase { NONE } |
| 51 | , m_type { type } |
| 52 | , m_createTime { createTime } |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | Event::Event(IsTrusted isTrusted) |
| 57 | : Event { MonotonicTime::now(), { }, isTrusted, CanBubble::No, IsCancelable::No, IsComposed::No } |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | Event::Event(const AtomicString& eventType, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed) |
| 62 | : Event { MonotonicTime::now(), eventType, IsTrusted::Yes, canBubble, isCancelable, isComposed } |
| 63 | { |
| 64 | ASSERT(!eventType.isNull()); |
| 65 | } |
| 66 | |
| 67 | Event::Event(const AtomicString& eventType, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed, MonotonicTime timestamp, IsTrusted isTrusted) |
| 68 | : Event { timestamp, eventType, isTrusted, canBubble, isCancelable, isComposed } |
| 69 | { |
| 70 | ASSERT(!eventType.isNull()); |
| 71 | } |
| 72 | |
| 73 | Event::Event(const AtomicString& eventType, const EventInit& initializer, IsTrusted isTrusted) |
| 74 | : Event { MonotonicTime::now(), eventType, isTrusted, |
| 75 | initializer.bubbles ? CanBubble::Yes : CanBubble::No, |
| 76 | initializer.cancelable ? IsCancelable::Yes : IsCancelable::No, |
| 77 | initializer.composed ? IsComposed::Yes : IsComposed::No } |
| 78 | { |
| 79 | ASSERT(!eventType.isNull()); |
| 80 | } |
| 81 | |
| 82 | Event::~Event() = default; |
| 83 | |
| 84 | Ref<Event> Event::create(const AtomicString& type, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed) |
| 85 | { |
| 86 | return adoptRef(*new Event(type, canBubble, isCancelable, isComposed)); |
| 87 | } |
| 88 | |
| 89 | Ref<Event> Event::createForBindings() |
| 90 | { |
| 91 | return adoptRef(*new Event); |
| 92 | } |
| 93 | |
| 94 | Ref<Event> Event::create(const AtomicString& type, const EventInit& initializer, IsTrusted isTrusted) |
| 95 | { |
| 96 | return adoptRef(*new Event(type, initializer, isTrusted)); |
| 97 | } |
| 98 | |
| 99 | void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg) |
| 100 | { |
| 101 | if (isBeingDispatched()) |
| 102 | return; |
| 103 | |
| 104 | m_isInitialized = true; |
| 105 | m_propagationStopped = false; |
| 106 | m_immediatePropagationStopped = false; |
| 107 | m_wasCanceled = false; |
| 108 | m_isTrusted = false; |
| 109 | m_target = nullptr; |
| 110 | m_type = eventTypeArg; |
| 111 | m_canBubble = canBubbleArg; |
| 112 | m_cancelable = cancelableArg; |
| 113 | |
| 114 | m_underlyingEvent = nullptr; |
| 115 | } |
| 116 | |
| 117 | void Event::setTarget(RefPtr<EventTarget>&& target) |
| 118 | { |
| 119 | if (m_target == target) |
| 120 | return; |
| 121 | |
| 122 | m_target = WTFMove(target); |
| 123 | if (m_target) |
| 124 | receivedTarget(); |
| 125 | } |
| 126 | |
| 127 | void Event::setCurrentTarget(EventTarget* currentTarget) |
| 128 | { |
| 129 | m_currentTarget = currentTarget; |
| 130 | } |
| 131 | |
| 132 | Vector<EventTarget*> Event::composedPath() const |
| 133 | { |
| 134 | if (!m_eventPath) |
| 135 | return Vector<EventTarget*>(); |
| 136 | return m_eventPath->computePathUnclosedToTarget(*m_currentTarget); |
| 137 | } |
| 138 | |
| 139 | void Event::setUnderlyingEvent(Event* underlyingEvent) |
| 140 | { |
| 141 | // Prohibit creation of a cycle by doing nothing if a cycle would be created. |
| 142 | for (Event* event = underlyingEvent; event; event = event->underlyingEvent()) { |
| 143 | if (event == this) |
| 144 | return; |
| 145 | } |
| 146 | m_underlyingEvent = underlyingEvent; |
| 147 | } |
| 148 | |
| 149 | DOMHighResTimeStamp Event::timeStampForBindings(ScriptExecutionContext& context) const |
| 150 | { |
| 151 | Performance* performance = nullptr; |
| 152 | if (is<WorkerGlobalScope>(context)) |
| 153 | performance = &downcast<WorkerGlobalScope>(context).performance(); |
| 154 | else if (auto* window = downcast<Document>(context).domWindow()) |
| 155 | performance = &window->performance(); |
| 156 | |
| 157 | if (!performance) |
| 158 | return 0; |
| 159 | |
| 160 | return std::max(performance->relativeTimeFromTimeOriginInReducedResolution(m_createTime), 0.); |
| 161 | } |
| 162 | |
| 163 | void Event::resetBeforeDispatch() |
| 164 | { |
| 165 | m_defaultHandled = false; |
| 166 | } |
| 167 | |
| 168 | void Event::resetAfterDispatch() |
| 169 | { |
| 170 | m_eventPath = nullptr; |
| 171 | m_currentTarget = nullptr; |
| 172 | m_eventPhase = NONE; |
| 173 | m_propagationStopped = false; |
| 174 | m_immediatePropagationStopped = false; |
| 175 | |
| 176 | InspectorInstrumentation::eventDidResetAfterDispatch(*this); |
| 177 | } |
| 178 | |
| 179 | } // namespace WebCore |
| 180 | |