blob: 21a59a2aefc3fae2838c75ac899faab812224c9a [file] [log] [blame]
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +00001/*
2 * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
3 *
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.
12 *
mjs@apple.com92047332014-03-15 04:08:27 +000013 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mjs@apple.com92047332014-03-15 04:08:27 +000016 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000017 * 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.
24 */
25
26#include "config.h"
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000027#include "GenericEventQueue.h"
28
29#include "Event.h"
darin@apple.com7ba1e572013-09-09 16:58:20 +000030#include "EventTarget.h"
jer.noble@apple.comc5f81242014-11-12 18:40:45 +000031#include "Timer.h"
32#include <wtf/MainThread.h>
33#include <wtf/NeverDestroyed.h>
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000034
35namespace WebCore {
36
darin@apple.com7ba1e572013-09-09 16:58:20 +000037GenericEventQueue::GenericEventQueue(EventTarget& owner)
inferno@chromium.org5fc64962012-03-30 00:33:10 +000038 : m_owner(owner)
jer.noble@apple.comc5f81242014-11-12 18:40:45 +000039 , m_weakPtrFactory(this)
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000040 , m_isClosed(false)
41{
42}
43
44GenericEventQueue::~GenericEventQueue()
45{
46}
47
akling@apple.com01a29de2015-07-20 20:16:18 +000048void GenericEventQueue::enqueueEvent(PassRefPtr<Event> event)
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000049{
50 if (m_isClosed)
akling@apple.com01a29de2015-07-20 20:16:18 +000051 return;
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000052
darin@apple.com7ba1e572013-09-09 16:58:20 +000053 if (event->target() == &m_owner)
inferno@chromium.org5fc64962012-03-30 00:33:10 +000054 event->setTarget(0);
55
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000056 m_pendingEvents.append(event);
akling@apple.com01a29de2015-07-20 20:16:18 +000057
58 if (m_isSuspended)
59 return;
60
jer.noble@apple.comc5f81242014-11-12 18:40:45 +000061 pendingQueues().append(m_weakPtrFactory.createWeakPtr());
62 if (!sharedTimer().isActive())
63 sharedTimer().startOneShot(0);
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000064}
65
jer.noble@apple.comc5f81242014-11-12 18:40:45 +000066Timer& GenericEventQueue::sharedTimer()
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000067{
jer.noble@apple.comc5f81242014-11-12 18:40:45 +000068 ASSERT(isMainThread());
69 static NeverDestroyed<Timer> timer(GenericEventQueue::sharedTimerFired);
70 return timer.get();
71}
72
73void GenericEventQueue::sharedTimerFired()
74{
75 ASSERT(!sharedTimer().isActive());
76 ASSERT(!pendingQueues().isEmpty());
77
78 while (!pendingQueues().isEmpty()) {
79 WeakPtr<GenericEventQueue> queue = pendingQueues().takeFirst();
80 if (!queue)
81 continue;
82 queue->dispatchOneEvent();
83 }
84
85 if (sharedTimer().isActive())
86 sharedTimer().stop();
87}
88
89Deque<WeakPtr<GenericEventQueue>>& GenericEventQueue::pendingQueues()
90{
91 ASSERT(isMainThread());
92 static NeverDestroyed<Deque<WeakPtr<GenericEventQueue>>> queues;
93 return queues.get();
94}
95
96void GenericEventQueue::dispatchOneEvent()
97{
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +000098 ASSERT(!m_pendingEvents.isEmpty());
99
akling@apple.coma34e5222013-09-10 03:44:05 +0000100 Ref<EventTarget> protect(m_owner);
jer.noble@apple.comc5f81242014-11-12 18:40:45 +0000101 RefPtr<Event> event = m_pendingEvents.takeFirst();
102 EventTarget& target = event->target() ? *event->target() : m_owner;
103 target.dispatchEvent(event.release());
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +0000104}
105
106void GenericEventQueue::close()
107{
108 m_isClosed = true;
109
jer.noble@apple.comc5f81242014-11-12 18:40:45 +0000110 m_weakPtrFactory.revokeAll();
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +0000111 m_pendingEvents.clear();
112}
113
114void GenericEventQueue::cancelAllEvents()
115{
jer.noble@apple.comc5f81242014-11-12 18:40:45 +0000116 m_weakPtrFactory.revokeAll();
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +0000117 m_pendingEvents.clear();
118}
119
120bool GenericEventQueue::hasPendingEvents() const
121{
jer.noble@apple.comc5f81242014-11-12 18:40:45 +0000122 return !m_pendingEvents.isEmpty();
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +0000123}
124
akling@apple.com01a29de2015-07-20 20:16:18 +0000125void GenericEventQueue::suspend()
126{
127 ASSERT(!m_isSuspended);
128 m_isSuspended = true;
129 m_weakPtrFactory.revokeAll();
130}
131
132void GenericEventQueue::resume()
133{
akling@apple.com8f5b18b2015-07-20 21:28:45 +0000134 if (!m_isSuspended)
135 return;
136
akling@apple.com01a29de2015-07-20 20:16:18 +0000137 m_isSuspended = false;
138
139 if (m_pendingEvents.isEmpty())
140 return;
141
142 for (unsigned i = 0; i < m_pendingEvents.size(); ++i)
143 pendingQueues().append(m_weakPtrFactory.createWeakPtr());
144
145 if (!sharedTimer().isActive())
146 sharedTimer().startOneShot(0);
147}
148
eric.carlson@apple.com077b1c72012-01-27 22:56:23 +0000149}