f39236e8772b76a1e573e2c037ca2d2870c99e87
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / events / Event.cpp
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.
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 "core/events/Event.h"
25
26 #include "core/dom/StaticNodeList.h"
27 #include "core/events/EventTarget.h"
28 #include "core/frame/UseCounter.h"
29 #include "core/svg/SVGElement.h"
30 #include "wtf/CurrentTime.h"
31
32 namespace blink {
33
34 EventInit::EventInit()
35     : bubbles(false)
36     , cancelable(false)
37 {
38 }
39
40
41 Event::Event()
42     : m_canBubble(false)
43     , m_cancelable(false)
44     , m_propagationStopped(false)
45     , m_immediatePropagationStopped(false)
46     , m_defaultPrevented(false)
47     , m_defaultHandled(false)
48     , m_cancelBubble(false)
49     , m_eventPhase(0)
50     , m_currentTarget(nullptr)
51     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
52 {
53     ScriptWrappable::init(this);
54 }
55
56 Event::Event(const AtomicString& eventType, bool canBubbleArg, bool cancelableArg)
57     : m_type(eventType)
58     , m_canBubble(canBubbleArg)
59     , m_cancelable(cancelableArg)
60     , m_propagationStopped(false)
61     , m_immediatePropagationStopped(false)
62     , m_defaultPrevented(false)
63     , m_defaultHandled(false)
64     , m_cancelBubble(false)
65     , m_eventPhase(0)
66     , m_currentTarget(nullptr)
67     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
68 {
69     ScriptWrappable::init(this);
70 }
71
72 Event::Event(const AtomicString& eventType, const EventInit& initializer)
73     : m_type(eventType)
74     , m_canBubble(initializer.bubbles)
75     , m_cancelable(initializer.cancelable)
76     , m_propagationStopped(false)
77     , m_immediatePropagationStopped(false)
78     , m_defaultPrevented(false)
79     , m_defaultHandled(false)
80     , m_cancelBubble(false)
81     , m_eventPhase(0)
82     , m_currentTarget(nullptr)
83     , m_createTime(convertSecondsToDOMTimeStamp(currentTime()))
84 {
85     ScriptWrappable::init(this);
86 }
87
88 Event::~Event()
89 {
90 }
91
92 void Event::initEvent(const AtomicString& eventTypeArg, bool canBubbleArg, bool cancelableArg)
93 {
94     if (dispatched())
95         return;
96
97     m_propagationStopped = false;
98     m_immediatePropagationStopped = false;
99     m_defaultPrevented = false;
100
101     m_type = eventTypeArg;
102     m_canBubble = canBubbleArg;
103     m_cancelable = cancelableArg;
104 }
105
106 bool Event::legacyReturnValue(ExecutionContext* executionContext) const
107 {
108     bool returnValue = !defaultPrevented();
109     if (returnValue)
110         UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
111     else
112         UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
113     return returnValue;
114 }
115
116 void Event::setLegacyReturnValue(ExecutionContext* executionContext, bool returnValue)
117 {
118     if (returnValue)
119         UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
120     else
121         UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
122     setDefaultPrevented(!returnValue);
123 }
124
125 const AtomicString& Event::interfaceName() const
126 {
127     return EventNames::Event;
128 }
129
130 bool Event::hasInterface(const AtomicString& name) const
131 {
132     return interfaceName() == name;
133 }
134
135 bool Event::isUIEvent() const
136 {
137     return false;
138 }
139
140 bool Event::isMouseEvent() const
141 {
142     return false;
143 }
144
145 bool Event::isFocusEvent() const
146 {
147     return false;
148 }
149
150 bool Event::isKeyboardEvent() const
151 {
152     return false;
153 }
154
155 bool Event::isTouchEvent() const
156 {
157     return false;
158 }
159
160 bool Event::isGestureEvent() const
161 {
162     return false;
163 }
164
165 bool Event::isWheelEvent() const
166 {
167     return false;
168 }
169
170 bool Event::isRelatedEvent() const
171 {
172     return false;
173 }
174
175 bool Event::isDragEvent() const
176 {
177     return false;
178 }
179
180 bool Event::isClipboardEvent() const
181 {
182     return false;
183 }
184
185 bool Event::isBeforeTextInsertedEvent() const
186 {
187     return false;
188 }
189
190 bool Event::isBeforeUnloadEvent() const
191 {
192     return false;
193 }
194
195 void Event::setTarget(PassRefPtrWillBeRawPtr<EventTarget> target)
196 {
197     if (m_target == target)
198         return;
199
200     m_target = target;
201     if (m_target)
202         receivedTarget();
203 }
204
205 void Event::receivedTarget()
206 {
207 }
208
209 void Event::setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event> ue)
210 {
211     // Prohibit creation of a cycle -- just do nothing in that case.
212     for (Event* e = ue.get(); e; e = e->underlyingEvent())
213         if (e == this)
214             return;
215     m_underlyingEvent = ue;
216 }
217
218 EventPath& Event::ensureEventPath()
219 {
220     if (!m_eventPath)
221         m_eventPath = adoptPtrWillBeNoop(new EventPath(this));
222     return *m_eventPath;
223 }
224
225 PassRefPtrWillBeRawPtr<StaticNodeList> Event::path() const
226 {
227     if (!m_currentTarget) {
228         ASSERT(m_eventPhase == PhaseType::NONE);
229         if (!m_eventPath) {
230             // Before dispatching the event
231             return StaticNodeList::createEmpty();
232         }
233         ASSERT(!m_eventPath->isEmpty());
234         // After dispatching the event
235         return m_eventPath->last().treeScopeEventContext().ensureEventPath(*m_eventPath);
236     }
237     if (!m_currentTarget->toNode())
238         return StaticNodeList::createEmpty();
239     Node* node = m_currentTarget->toNode();
240     size_t eventPathSize = m_eventPath->size();
241     for (size_t i = 0; i < eventPathSize; ++i) {
242         if (node == (*m_eventPath)[i].node()) {
243             return (*m_eventPath)[i].treeScopeEventContext().ensureEventPath(*m_eventPath);
244         }
245     }
246     return StaticNodeList::createEmpty();
247 }
248
249 EventTarget* Event::currentTarget() const
250 {
251     if (!m_currentTarget)
252         return 0;
253     Node* node = m_currentTarget->toNode();
254     if (node && node->isSVGElement()) {
255         if (SVGElement* svgElement = toSVGElement(node)->correspondingElement())
256             return svgElement;
257     }
258     return m_currentTarget;
259 }
260
261 void Event::trace(Visitor* visitor)
262 {
263     visitor->trace(m_currentTarget);
264     visitor->trace(m_target);
265     visitor->trace(m_underlyingEvent);
266     visitor->trace(m_eventPath);
267 }
268
269 } // namespace blink