File: Event.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (354 lines) | stat: -rw-r--r-- 10,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
 * Copyright (C) 2003, 2005, 2006, 2008 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "core/events/Event.h"

#include "core/dom/StaticNodeList.h"
#include "core/events/EventDispatchMediator.h"
#include "core/events/EventTarget.h"
#include "core/frame/HostsUsingFeatures.h"
#include "core/frame/UseCounter.h"
#include "core/svg/SVGElement.h"
#include "core/timing/DOMWindowPerformance.h"
#include "core/timing/Performance.h"

namespace blink {

static bool isEventTypeScopedInV0(const AtomicString& eventType) {
  // WebKit never allowed selectstart event to cross the the shadow DOM
  // boundary.  Changing this breaks existing sites.
  // See https://bugs.webkit.org/show_bug.cgi?id=52195 for details.
  return eventType == EventTypeNames::abort ||
         eventType == EventTypeNames::change ||
         eventType == EventTypeNames::error ||
         eventType == EventTypeNames::load ||
         eventType == EventTypeNames::reset ||
         eventType == EventTypeNames::resize ||
         eventType == EventTypeNames::scroll ||
         eventType == EventTypeNames::select ||
         eventType == EventTypeNames::selectstart ||
         eventType == EventTypeNames::slotchange;
}

Event::Event() : Event("", false, false) {
  m_wasInitialized = false;
}

Event::Event(const AtomicString& eventType,
             bool canBubbleArg,
             bool cancelableArg,
             TimeTicks platformTimeStamp)
    : Event(eventType,
            canBubbleArg,
            cancelableArg,
            ComposedMode::Scoped,
            platformTimeStamp) {}

Event::Event(const AtomicString& eventType,
             bool canBubbleArg,
             bool cancelableArg,
             ComposedMode composedMode)
    : Event(eventType,
            canBubbleArg,
            cancelableArg,
            composedMode,
            TimeTicks::Now()) {}

Event::Event(const AtomicString& eventType,
             bool canBubbleArg,
             bool cancelableArg,
             ComposedMode composedMode,
             TimeTicks platformTimeStamp)
    : m_type(eventType),
      m_canBubble(canBubbleArg),
      m_cancelable(cancelableArg),
      m_composed(composedMode == ComposedMode::Composed),
      m_isEventTypeScopedInV0(isEventTypeScopedInV0(eventType)),
      m_propagationStopped(false),
      m_immediatePropagationStopped(false),
      m_defaultPrevented(false),
      m_defaultHandled(false),
      m_cancelBubble(false),
      m_wasInitialized(true),
      m_isTrusted(false),
      m_preventDefaultCalledOnUncancelableEvent(false),
      m_handlingPassive(PassiveMode::NotPassiveDefault),
      m_eventPhase(0),
      m_currentTarget(nullptr),
      m_platformTimeStamp(platformTimeStamp) {}

Event::Event(const AtomicString& eventType, const EventInit& initializer)
    : Event(eventType,
            initializer.bubbles(),
            initializer.cancelable(),
            initializer.composed() ? ComposedMode::Composed
                                   : ComposedMode::Scoped,
            TimeTicks::Now()) {}

Event::~Event() {}

bool Event::isScopedInV0() const {
  return isTrusted() && m_isEventTypeScopedInV0;
}

void Event::initEvent(const AtomicString& eventTypeArg,
                      bool canBubbleArg,
                      bool cancelableArg) {
  initEvent(eventTypeArg, canBubbleArg, cancelableArg, nullptr);
}

void Event::initEvent(const AtomicString& eventTypeArg,
                      bool canBubbleArg,
                      bool cancelableArg,
                      EventTarget* relatedTarget) {
  if (isBeingDispatched())
    return;

  m_wasInitialized = true;
  m_propagationStopped = false;
  m_immediatePropagationStopped = false;
  m_defaultPrevented = false;
  m_isTrusted = false;
  m_preventDefaultCalledOnUncancelableEvent = false;

  m_type = eventTypeArg;
  m_canBubble = canBubbleArg;
  m_cancelable = cancelableArg;
}

bool Event::legacyReturnValue(ExecutionContext* executionContext) const {
  bool returnValue = !defaultPrevented();
  if (returnValue)
    UseCounter::count(executionContext, UseCounter::EventGetReturnValueTrue);
  else
    UseCounter::count(executionContext, UseCounter::EventGetReturnValueFalse);
  return returnValue;
}

void Event::setLegacyReturnValue(ExecutionContext* executionContext,
                                 bool returnValue) {
  if (returnValue)
    UseCounter::count(executionContext, UseCounter::EventSetReturnValueTrue);
  else
    UseCounter::count(executionContext, UseCounter::EventSetReturnValueFalse);
  setDefaultPrevented(!returnValue);
}

const AtomicString& Event::interfaceName() const {
  return EventNames::Event;
}

bool Event::hasInterface(const AtomicString& name) const {
  return interfaceName() == name;
}

bool Event::isUIEvent() const {
  return false;
}

bool Event::isMouseEvent() const {
  return false;
}

bool Event::isFocusEvent() const {
  return false;
}

bool Event::isKeyboardEvent() const {
  return false;
}

bool Event::isTouchEvent() const {
  return false;
}

bool Event::isGestureEvent() const {
  return false;
}

bool Event::isWheelEvent() const {
  return false;
}

bool Event::isRelatedEvent() const {
  return false;
}

bool Event::isPointerEvent() const {
  return false;
}

bool Event::isInputEvent() const {
  return false;
}

bool Event::isDragEvent() const {
  return false;
}

bool Event::isClipboardEvent() const {
  return false;
}

bool Event::isBeforeTextInsertedEvent() const {
  return false;
}

bool Event::isBeforeUnloadEvent() const {
  return false;
}

void Event::preventDefault() {
  if (m_handlingPassive != PassiveMode::NotPassive &&
      m_handlingPassive != PassiveMode::NotPassiveDefault) {
    m_preventDefaultCalledDuringPassive = true;

    const LocalDOMWindow* window =
        m_eventPath ? m_eventPath->windowEventContext().window() : 0;
    if (window && m_handlingPassive == PassiveMode::Passive) {
      window->printErrorMessage(
          "Unable to preventDefault inside passive event listener invocation.");
    }
    return;
  }

  if (m_cancelable)
    m_defaultPrevented = true;
  else
    m_preventDefaultCalledOnUncancelableEvent = true;
}

void Event::setTarget(EventTarget* target) {
  if (m_target == target)
    return;

  m_target = target;
  if (m_target)
    receivedTarget();
}

void Event::receivedTarget() {}

void Event::setUnderlyingEvent(Event* ue) {
  // Prohibit creation of a cycle -- just do nothing in that case.
  for (Event* e = ue; e; e = e->underlyingEvent())
    if (e == this)
      return;
  m_underlyingEvent = ue;
}

void Event::initEventPath(Node& node) {
  if (!m_eventPath) {
    m_eventPath = new EventPath(node, this);
  } else {
    m_eventPath->initializeWith(node, this);
  }
}

HeapVector<Member<EventTarget>> Event::path(ScriptState* scriptState) const {
  return pathInternal(scriptState, NonEmptyAfterDispatch);
}

HeapVector<Member<EventTarget>> Event::composedPath(
    ScriptState* scriptState) const {
  return pathInternal(scriptState, EmptyAfterDispatch);
}

void Event::setHandlingPassive(PassiveMode mode) {
  m_handlingPassive = mode;
  m_preventDefaultCalledDuringPassive = false;
}

HeapVector<Member<EventTarget>> Event::pathInternal(ScriptState* scriptState,
                                                    EventPathMode mode) const {
  if (m_target)
    HostsUsingFeatures::countHostOrIsolatedWorldHumanReadableName(
        scriptState, *m_target, HostsUsingFeatures::Feature::EventPath);

  if (!m_currentTarget) {
    DCHECK_EQ(Event::kNone, m_eventPhase);
    if (!m_eventPath) {
      // Before dispatching the event
      return HeapVector<Member<EventTarget>>();
    }
    DCHECK(!m_eventPath->isEmpty());
    // After dispatching the event
    if (mode == EmptyAfterDispatch)
      return HeapVector<Member<EventTarget>>();
    return m_eventPath->last().treeScopeEventContext().ensureEventPath(
        *m_eventPath);
  }

  if (Node* node = m_currentTarget->toNode()) {
    DCHECK(m_eventPath);
    for (auto& context : m_eventPath->nodeEventContexts()) {
      if (node == context.node())
        return context.treeScopeEventContext().ensureEventPath(*m_eventPath);
    }
    NOTREACHED();
  }

  if (LocalDOMWindow* window = m_currentTarget->toLocalDOMWindow()) {
    if (m_eventPath && !m_eventPath->isEmpty()) {
      return m_eventPath->topNodeEventContext()
          .treeScopeEventContext()
          .ensureEventPath(*m_eventPath);
    }
    return HeapVector<Member<EventTarget>>(1, window);
  }

  return HeapVector<Member<EventTarget>>();
}

EventDispatchMediator* Event::createMediator() {
  return EventDispatchMediator::create(this);
}

double Event::timeStamp(ScriptState* scriptState) const {
  double timeStamp = 0;
  if (scriptState && scriptState->domWindow()) {
    Performance* performance =
        DOMWindowPerformance::performance(*scriptState->domWindow());
    double timestampSeconds = (m_platformTimeStamp - TimeTicks()).InSecondsF();
    timeStamp =
        performance->monotonicTimeToDOMHighResTimeStamp(timestampSeconds);
  }

  return timeStamp;
}

void Event::setCancelBubble(ExecutionContext* context, bool cancel) {
  if (!m_cancelBubble && cancel)
    UseCounter::count(context, UseCounter::EventCancelBubbleWasChangedToTrue);
  else if (m_cancelBubble && !cancel)
    UseCounter::count(context, UseCounter::EventCancelBubbleWasChangedToFalse);
  m_cancelBubble = cancel;
}

DEFINE_TRACE(Event) {
  visitor->trace(m_currentTarget);
  visitor->trace(m_target);
  visitor->trace(m_underlyingEvent);
  visitor->trace(m_eventPath);
}

}  // namespace blink