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 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.
24 */
25
26#include "config.h"
27
28#include "GenericEventQueue.h"
29
30#include "Event.h"
31
32namespace WebCore {
33
34PassOwnPtr<GenericEventQueue> GenericEventQueue::create(EventTarget* owner)
35{
36    return adoptPtr(new GenericEventQueue(owner));
37}
38
39GenericEventQueue::GenericEventQueue(EventTarget* owner)
40    : m_owner(owner)
41    , m_timer(this, &GenericEventQueue::timerFired)
42    , m_isClosed(false)
43{
44}
45
46GenericEventQueue::~GenericEventQueue()
47{
48}
49
50bool GenericEventQueue::enqueueEvent(PassRefPtr<Event> event)
51{
52    if (m_isClosed)
53        return false;
54
55    if (event->target() == m_owner)
56        event->setTarget(0);
57
58    m_pendingEvents.append(event);
59
60    if (!m_timer.isActive())
61        m_timer.startOneShot(0);
62
63    return true;
64}
65
66bool GenericEventQueue::cancelEvent(Event* event)
67{
68    bool found = m_pendingEvents.contains(event);
69
70    if (found)
71        m_pendingEvents.remove(m_pendingEvents.find(event));
72
73    if (m_pendingEvents.isEmpty())
74        m_timer.stop();
75
76    return found;
77}
78
79void GenericEventQueue::timerFired(Timer<GenericEventQueue>*)
80{
81    ASSERT(!m_timer.isActive());
82    ASSERT(!m_pendingEvents.isEmpty());
83
84    Vector<RefPtr<Event> > pendingEvents;
85    m_pendingEvents.swap(pendingEvents);
86
87    RefPtr<EventTarget> protect(m_owner);
88    for (unsigned i = 0; i < pendingEvents.size(); ++i) {
89        EventTarget* target = pendingEvents[i]->target() ? pendingEvents[i]->target() : m_owner;
90        target->dispatchEvent(pendingEvents[i].release());
91    }
92}
93
94void GenericEventQueue::close()
95{
96    m_isClosed = true;
97
98    m_timer.stop();
99    m_pendingEvents.clear();
100}
101
102void GenericEventQueue::cancelAllEvents()
103{
104    m_timer.stop();
105    m_pendingEvents.clear();
106}
107
108bool GenericEventQueue::hasPendingEvents() const
109{
110    return m_pendingEvents.size();
111}
112
113}
114