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
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 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 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.
27 #include "GenericEventQueue.h"
30 #include "EventTarget.h"
32 #include <wtf/MainThread.h>
33 #include <wtf/NeverDestroyed.h>
37 GenericEventQueue::GenericEventQueue(EventTarget& owner)
39 , m_weakPtrFactory(this)
44 GenericEventQueue::~GenericEventQueue()
48 void GenericEventQueue::enqueueEvent(RefPtr<Event>&& event)
53 if (event->target() == &m_owner)
54 event->setTarget(nullptr);
56 m_pendingEvents.append(WTFMove(event));
61 pendingQueues().append(m_weakPtrFactory.createWeakPtr());
62 if (!sharedTimer().isActive())
63 sharedTimer().startOneShot(0);
66 Timer& GenericEventQueue::sharedTimer()
68 ASSERT(isMainThread());
69 static NeverDestroyed<Timer> timer(GenericEventQueue::sharedTimerFired);
73 void GenericEventQueue::sharedTimerFired()
75 ASSERT(!sharedTimer().isActive());
76 ASSERT(!pendingQueues().isEmpty());
78 while (!pendingQueues().isEmpty()) {
79 WeakPtr<GenericEventQueue> queue = pendingQueues().takeFirst();
82 queue->dispatchOneEvent();
85 if (sharedTimer().isActive())
89 Deque<WeakPtr<GenericEventQueue>>& GenericEventQueue::pendingQueues()
91 ASSERT(isMainThread());
92 static NeverDestroyed<Deque<WeakPtr<GenericEventQueue>>> queues;
96 void GenericEventQueue::dispatchOneEvent()
98 ASSERT(!m_pendingEvents.isEmpty());
100 Ref<EventTarget> protect(m_owner);
101 RefPtr<Event> event = m_pendingEvents.takeFirst();
102 EventTarget& target = event->target() ? *event->target() : m_owner;
103 target.dispatchEvent(*event);
106 void GenericEventQueue::close()
110 m_weakPtrFactory.revokeAll();
111 m_pendingEvents.clear();
114 void GenericEventQueue::cancelAllEvents()
116 m_weakPtrFactory.revokeAll();
117 m_pendingEvents.clear();
120 bool GenericEventQueue::hasPendingEvents() const
122 return !m_pendingEvents.isEmpty();
125 void GenericEventQueue::suspend()
127 ASSERT(!m_isSuspended);
128 m_isSuspended = true;
129 m_weakPtrFactory.revokeAll();
132 void GenericEventQueue::resume()
137 m_isSuspended = false;
139 if (m_pendingEvents.isEmpty())
142 for (unsigned i = 0; i < m_pendingEvents.size(); ++i)
143 pendingQueues().append(m_weakPtrFactory.createWeakPtr());
145 if (!sharedTimer().isActive())
146 sharedTimer().startOneShot(0);