eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 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. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 27 | #include "GenericEventQueue.h" |
| 28 | |
| 29 | #include "Event.h" |
darin@apple.com | 7ba1e57 | 2013-09-09 16:58:20 +0000 | [diff] [blame] | 30 | #include "EventTarget.h" |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 31 | #include "Timer.h" |
| 32 | #include <wtf/MainThread.h> |
| 33 | #include <wtf/NeverDestroyed.h> |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 34 | |
| 35 | namespace WebCore { |
| 36 | |
darin@apple.com | 7ba1e57 | 2013-09-09 16:58:20 +0000 | [diff] [blame] | 37 | GenericEventQueue::GenericEventQueue(EventTarget& owner) |
inferno@chromium.org | 5fc6496 | 2012-03-30 00:33:10 +0000 | [diff] [blame] | 38 | : m_owner(owner) |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 39 | , m_weakPtrFactory(this) |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 40 | , m_isClosed(false) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | GenericEventQueue::~GenericEventQueue() |
| 45 | { |
| 46 | } |
| 47 | |
akling@apple.com | 01a29de | 2015-07-20 20:16:18 +0000 | [diff] [blame] | 48 | void GenericEventQueue::enqueueEvent(PassRefPtr<Event> event) |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 49 | { |
| 50 | if (m_isClosed) |
akling@apple.com | 01a29de | 2015-07-20 20:16:18 +0000 | [diff] [blame] | 51 | return; |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 52 | |
darin@apple.com | 7ba1e57 | 2013-09-09 16:58:20 +0000 | [diff] [blame] | 53 | if (event->target() == &m_owner) |
inferno@chromium.org | 5fc6496 | 2012-03-30 00:33:10 +0000 | [diff] [blame] | 54 | event->setTarget(0); |
| 55 | |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 56 | m_pendingEvents.append(event); |
akling@apple.com | 01a29de | 2015-07-20 20:16:18 +0000 | [diff] [blame] | 57 | |
| 58 | if (m_isSuspended) |
| 59 | return; |
| 60 | |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 61 | pendingQueues().append(m_weakPtrFactory.createWeakPtr()); |
| 62 | if (!sharedTimer().isActive()) |
| 63 | sharedTimer().startOneShot(0); |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 64 | } |
| 65 | |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 66 | Timer& GenericEventQueue::sharedTimer() |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 67 | { |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 68 | ASSERT(isMainThread()); |
| 69 | static NeverDestroyed<Timer> timer(GenericEventQueue::sharedTimerFired); |
| 70 | return timer.get(); |
| 71 | } |
| 72 | |
| 73 | void 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 | |
| 89 | Deque<WeakPtr<GenericEventQueue>>& GenericEventQueue::pendingQueues() |
| 90 | { |
| 91 | ASSERT(isMainThread()); |
| 92 | static NeverDestroyed<Deque<WeakPtr<GenericEventQueue>>> queues; |
| 93 | return queues.get(); |
| 94 | } |
| 95 | |
| 96 | void GenericEventQueue::dispatchOneEvent() |
| 97 | { |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 98 | ASSERT(!m_pendingEvents.isEmpty()); |
| 99 | |
akling@apple.com | a34e522 | 2013-09-10 03:44:05 +0000 | [diff] [blame] | 100 | Ref<EventTarget> protect(m_owner); |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 101 | RefPtr<Event> event = m_pendingEvents.takeFirst(); |
| 102 | EventTarget& target = event->target() ? *event->target() : m_owner; |
| 103 | target.dispatchEvent(event.release()); |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void GenericEventQueue::close() |
| 107 | { |
| 108 | m_isClosed = true; |
| 109 | |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 110 | m_weakPtrFactory.revokeAll(); |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 111 | m_pendingEvents.clear(); |
| 112 | } |
| 113 | |
| 114 | void GenericEventQueue::cancelAllEvents() |
| 115 | { |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 116 | m_weakPtrFactory.revokeAll(); |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 117 | m_pendingEvents.clear(); |
| 118 | } |
| 119 | |
| 120 | bool GenericEventQueue::hasPendingEvents() const |
| 121 | { |
jer.noble@apple.com | c5f8124 | 2014-11-12 18:40:45 +0000 | [diff] [blame] | 122 | return !m_pendingEvents.isEmpty(); |
eric.carlson@apple.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 123 | } |
| 124 | |
akling@apple.com | 01a29de | 2015-07-20 20:16:18 +0000 | [diff] [blame] | 125 | void GenericEventQueue::suspend() |
| 126 | { |
| 127 | ASSERT(!m_isSuspended); |
| 128 | m_isSuspended = true; |
| 129 | m_weakPtrFactory.revokeAll(); |
| 130 | } |
| 131 | |
| 132 | void GenericEventQueue::resume() |
| 133 | { |
akling@apple.com | 8f5b18b | 2015-07-20 21:28:45 +0000 | [diff] [blame] | 134 | if (!m_isSuspended) |
| 135 | return; |
| 136 | |
akling@apple.com | 01a29de | 2015-07-20 20:16:18 +0000 | [diff] [blame] | 137 | 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.com | 077b1c7 | 2012-01-27 22:56:23 +0000 | [diff] [blame] | 149 | } |