| 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 | * |
| 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. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "GenericEventQueue.h" |
| 28 | |
| 29 | #include "Document.h" |
| 30 | #include "Event.h" |
| 31 | #include "EventTarget.h" |
| 32 | #include "Node.h" |
| 33 | #include "ScriptExecutionContext.h" |
| 34 | #include "Timer.h" |
| 35 | #include <wtf/MainThread.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | GenericEventQueue::GenericEventQueue(EventTarget& owner) |
| 40 | : m_owner(owner) |
| 41 | , m_isClosed(false) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | GenericEventQueue::~GenericEventQueue() = default; |
| 46 | |
| 47 | void GenericEventQueue::enqueueEvent(RefPtr<Event>&& event) |
| 48 | { |
| 49 | if (m_isClosed) |
| 50 | return; |
| 51 | |
| 52 | if (event->target() == &m_owner) |
| 53 | event->setTarget(nullptr); |
| 54 | |
| 55 | m_pendingEvents.append(WTFMove(event)); |
| 56 | |
| 57 | if (m_isSuspended) |
| 58 | return; |
| 59 | |
| 60 | m_taskQueue.enqueueTask(std::bind(&GenericEventQueue::dispatchOneEvent, this)); |
| 61 | } |
| 62 | |
| 63 | void GenericEventQueue::dispatchOneEvent() |
| 64 | { |
| 65 | ASSERT(!m_pendingEvents.isEmpty()); |
| 66 | |
| 67 | Ref<EventTarget> protect(m_owner); |
| 68 | RefPtr<Event> event = m_pendingEvents.takeFirst(); |
| 69 | EventTarget& target = event->target() ? *event->target() : m_owner; |
| 70 | ASSERT_WITH_MESSAGE(!target.scriptExecutionContext()->activeDOMObjectsAreStopped(), |
| 71 | "An attempt to dispatch an event on a stopped target by EventTargetInterface=%d (nodeName=%s target=%p owner=%p)" , |
| 72 | m_owner.eventTargetInterface(), m_owner.isNode() ? static_cast<Node&>(m_owner).nodeName().ascii().data() : "" , &target, &m_owner); |
| 73 | target.dispatchEvent(*event); |
| 74 | } |
| 75 | |
| 76 | void GenericEventQueue::close() |
| 77 | { |
| 78 | m_isClosed = true; |
| 79 | |
| 80 | m_taskQueue.close(); |
| 81 | m_pendingEvents.clear(); |
| 82 | } |
| 83 | |
| 84 | void GenericEventQueue::cancelAllEvents() |
| 85 | { |
| 86 | m_taskQueue.cancelAllTasks(); |
| 87 | m_pendingEvents.clear(); |
| 88 | } |
| 89 | |
| 90 | bool GenericEventQueue::hasPendingEvents() const |
| 91 | { |
| 92 | return !m_pendingEvents.isEmpty(); |
| 93 | } |
| 94 | |
| 95 | bool GenericEventQueue::hasPendingEventsOfType(const AtomicString& type) const |
| 96 | { |
| 97 | for (auto& event : m_pendingEvents) { |
| 98 | if (event->type() == type) |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | void GenericEventQueue::suspend() |
| 106 | { |
| 107 | ASSERT(!m_isSuspended); |
| 108 | m_isSuspended = true; |
| 109 | m_taskQueue.cancelAllTasks(); |
| 110 | } |
| 111 | |
| 112 | void GenericEventQueue::resume() |
| 113 | { |
| 114 | if (!m_isSuspended) |
| 115 | return; |
| 116 | |
| 117 | m_isSuspended = false; |
| 118 | |
| 119 | for (unsigned i = 0; i < m_pendingEvents.size(); ++i) |
| 120 | m_taskQueue.enqueueTask(std::bind(&GenericEventQueue::dispatchOneEvent, this)); |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |