Revert of Implement timers by posting delayed tasks (patchset #21 id:400001 of https...
[chromium-blink-merge.git] / third_party / WebKit / Source / core / events / GenericEventQueue.cpp
blob5e0de0782955f891b8c0a152fff39b3a397bc00a
1 /*
2 * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "config.h"
28 #include "core/events/GenericEventQueue.h"
30 #include "core/events/Event.h"
31 #include "core/inspector/InspectorInstrumentation.h"
32 #include "platform/TraceEvent.h"
34 namespace blink {
36 PassOwnPtrWillBeRawPtr<GenericEventQueue> GenericEventQueue::create(EventTarget* owner)
38 return adoptPtrWillBeNoop(new GenericEventQueue(owner));
41 GenericEventQueue::GenericEventQueue(EventTarget* owner)
42 : m_owner(owner)
43 , m_timer(this, &GenericEventQueue::timerFired)
44 , m_isClosed(false)
48 GenericEventQueue::~GenericEventQueue()
52 DEFINE_TRACE(GenericEventQueue)
54 visitor->trace(m_owner);
55 visitor->trace(m_pendingEvents);
56 EventQueue::trace(visitor);
59 bool GenericEventQueue::enqueueEvent(PassRefPtrWillBeRawPtr<Event> event)
61 if (m_isClosed)
62 return false;
64 if (event->target() == m_owner)
65 event->setTarget(nullptr);
67 TRACE_EVENT_ASYNC_BEGIN1("event", "GenericEventQueue:enqueueEvent", event.get(), "type", event->type().ascii());
68 InspectorInstrumentation::didEnqueueEvent(event->target() ? event->target() : m_owner.get(), event.get());
69 m_pendingEvents.append(event);
71 if (!m_timer.isActive())
72 m_timer.startOneShot(0, FROM_HERE);
74 return true;
77 bool GenericEventQueue::cancelEvent(Event* event)
79 bool found = m_pendingEvents.contains(event);
81 if (found) {
82 InspectorInstrumentation::didRemoveEvent(event->target() ? event->target() : m_owner.get(), event);
83 m_pendingEvents.remove(m_pendingEvents.find(event));
84 TRACE_EVENT_ASYNC_END2("event", "GenericEventQueue:enqueueEvent", event, "type", event->type().ascii(), "status", "cancelled");
87 if (m_pendingEvents.isEmpty())
88 m_timer.stop();
90 return found;
93 void GenericEventQueue::timerFired(Timer<GenericEventQueue>*)
95 ASSERT(!m_timer.isActive());
96 ASSERT(!m_pendingEvents.isEmpty());
98 WillBeHeapVector<RefPtrWillBeMember<Event>> pendingEvents;
99 m_pendingEvents.swap(pendingEvents);
101 RefPtrWillBeRawPtr<EventTarget> protect(m_owner.get());
102 for (const auto& pendingEvent : pendingEvents) {
103 Event* event = pendingEvent.get();
104 EventTarget* target = event->target() ? event->target() : m_owner.get();
105 CString type(event->type().ascii());
106 TRACE_EVENT_ASYNC_STEP_INTO1("event", "GenericEventQueue:enqueueEvent", event, "dispatch", "type", type);
107 target->dispatchEvent(pendingEvent);
108 TRACE_EVENT_ASYNC_END1("event", "GenericEventQueue:enqueueEvent", event, "type", type);
109 InspectorInstrumentation::didRemoveEvent(target, event);
113 void GenericEventQueue::close()
115 m_isClosed = true;
116 cancelAllEvents();
119 void GenericEventQueue::cancelAllEvents()
121 m_timer.stop();
123 for (const auto& pendingEvent : m_pendingEvents) {
124 Event* event = pendingEvent.get();
125 TRACE_EVENT_ASYNC_END2("event", "GenericEventQueue:enqueueEvent", event, "type", event->type().ascii(), "status", "cancelled");
126 InspectorInstrumentation::didRemoveEvent(event->target() ? event->target() : m_owner.get(), event);
128 m_pendingEvents.clear();
131 bool GenericEventQueue::hasPendingEvents() const
133 return m_pendingEvents.size();