Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / events / Event.cpp
blob05c35a686ddbe70d07317c570f31dbe14db7f3da
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, 2005, 2006, 2008 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "config.h"
24 #include "core/events/Event.h"
26 #include "core/dom/StaticNodeList.h"
27 #include "core/events/EventDispatchMediator.h"
28 #include "core/events/EventTarget.h"
29 #include "core/frame/OriginsUsingFeatures.h"
30 #include "core/frame/UseCounter.h"
31 #include "core/svg/SVGElement.h"
32 #include "wtf/CurrentTime.h"
34 namespace blink {
36 Event::Event()
37 : Event("", false, false)
41 Event::Event(const AtomicString& eventType, bool canBubbleArg, bool cancelableArg)
42 : m_type(eventType)
43 , m_canBubble(canBubbleArg)
44 , m_cancelable(cancelableArg)
45 , m_propagationStopped(false)
46 , m_immediatePropagationStopped(false)
47 , m_defaultPrevented(false)
48 , m_defaultHandled(false)
49 , m_cancelBubble(false)
50 , m_isTrusted(false)
51 , m_eventPhase(0)
52 , m_currentTarget(nullptr)
53 , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
54 , m_uiCreateTime(0)
58 Event::Event(const AtomicString& eventType, const EventInit& initializer)
59 : Event(eventType, initializer.bubbles(), initializer.cancelable())
63 Event::~Event()
67 void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg)
69 if (dispatched())
70 return;
72 m_propagationStopped = false;
73 m_immediatePropagationStopped = false;
74 m_defaultPrevented = false;
75 m_isTrusted = false;
77 m_type = eventTypeArg;
78 m_canBubble = canBubbleArg;
79 m_cancelable = cancelableArg;
82 bool Event::legacyReturnValue(ExecutionContext* executionContext) const
84 bool returnValue = !defaultPrevented();
85 if (returnValue)
86 UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
87 else
88 UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
89 return returnValue;
92 void Event::setLegacyReturnValue(ExecutionContext* executionContext, bool returnValue)
94 if (returnValue)
95 UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
96 else
97 UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
98 setDefaultPrevented(!returnValue);
101 const AtomicString& Event::interfaceName() const
103 return EventNames::Event;
106 bool Event::hasInterface(const AtomicString& name) const
108 return interfaceName() == name;
111 bool Event::isUIEvent() const
113 return false;
116 bool Event::isMouseEvent() const
118 return false;
121 bool Event::isFocusEvent() const
123 return false;
126 bool Event::isKeyboardEvent() const
128 return false;
131 bool Event::isTouchEvent() const
133 return false;
136 bool Event::isGestureEvent() const
138 return false;
141 bool Event::isWheelEvent() const
143 return false;
146 bool Event::isRelatedEvent() const
148 return false;
151 bool Event::isPointerEvent() const
153 return false;
156 bool Event::isDragEvent() const
158 return false;
161 bool Event::isClipboardEvent() const
163 return false;
166 bool Event::isBeforeTextInsertedEvent() const
168 return false;
171 bool Event::isBeforeUnloadEvent() const
173 return false;
176 void Event::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
178 if (m_target == target)
179 return;
181 m_target = target;
182 if (m_target)
183 receivedTarget();
186 void Event::receivedTarget()
190 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event> ue)
192 // Prohibit creation of a cycle -- just do nothing in that case.
193 for (Event* e = ue.get(); e; e = e->underlyingEvent())
194 if (e == this)
195 return;
196 m_underlyingEvent = ue;
199 void Event::initEventPath(Node& node)
201 if (!m_eventPath) {
202 m_eventPath = adoptPtrWillBeNoop(new EventPath(node, this));
203 } else {
204 m_eventPath->initializeWith(node, this);
208 WillBeHeapVector<RefPtrWillBeMember<EventTarget>> Event::path(ScriptState* scriptState) const
210 if (m_target)
211 OriginsUsingFeatures::countOriginOrIsolatedWorldHumanReadableName(scriptState, *m_target, OriginsUsingFeatures::Feature::EventPath);
213 if (!m_currentTarget) {
214 ASSERT(m_eventPhase == Event::NONE);
215 if (!m_eventPath) {
216 // Before dispatching the event
217 return WillBeHeapVector<RefPtrWillBeMember<EventTarget>>();
219 ASSERT(!m_eventPath->isEmpty());
220 // After dispatching the event
221 return m_eventPath->last().treeScopeEventContext().ensureEventPath(*m_eventPath);
224 if (Node* node = m_currentTarget->toNode()) {
225 ASSERT(m_eventPath);
226 size_t eventPathSize = m_eventPath->size();
227 for (size_t i = 0; i < eventPathSize; ++i) {
228 if (node == (*m_eventPath)[i].node()) {
229 return (*m_eventPath)[i].treeScopeEventContext().ensureEventPath(*m_eventPath);
232 ASSERT_NOT_REACHED();
235 // Returns [window] for events that are directly dispatched to the window object;
236 // e.g., window.load, pageshow, etc.
237 if (LocalDOMWindow* window = m_currentTarget->toDOMWindow())
238 return WillBeHeapVector<RefPtrWillBeMember<EventTarget>>(1, window);
240 return WillBeHeapVector<RefPtrWillBeMember<EventTarget>>();
243 PassRefPtrWillBeRawPtr<EventDispatchMediator> Event::createMediator()
245 return EventDispatchMediator::create(this);
248 EventTarget* Event::currentTarget() const
250 if (!m_currentTarget)
251 return nullptr;
252 Node* node = m_currentTarget->toNode();
253 if (node && node->isSVGElement()) {
254 if (SVGElement* svgElement = toSVGElement(node)->correspondingElement())
255 return svgElement;
257 return m_currentTarget.get();
260 DEFINE_TRACE(Event)
262 visitor->trace(m_currentTarget);
263 visitor->trace(m_target);
264 visitor->trace(m_underlyingEvent);
265 visitor->trace(m_eventPath);
268 } // namespace blink