GenericEventQueue.cpp [plain text]
#include "config.h"
#include "GenericEventQueue.h"
#include "Event.h"
namespace WebCore {
PassOwnPtr<GenericEventQueue> GenericEventQueue::create(EventTarget* owner)
{
return adoptPtr(new GenericEventQueue(owner));
}
GenericEventQueue::GenericEventQueue(EventTarget* owner)
: m_owner(owner)
, m_timer(this, &GenericEventQueue::timerFired)
, m_isClosed(false)
{
}
GenericEventQueue::~GenericEventQueue()
{
}
bool GenericEventQueue::enqueueEvent(PassRefPtr<Event> event)
{
if (m_isClosed)
return false;
if (event->target() == m_owner)
event->setTarget(0);
m_pendingEvents.append(event);
if (!m_timer.isActive())
m_timer.startOneShot(0);
return true;
}
bool GenericEventQueue::cancelEvent(Event* event)
{
bool found = m_pendingEvents.contains(event);
if (found)
m_pendingEvents.remove(m_pendingEvents.find(event));
if (m_pendingEvents.isEmpty())
m_timer.stop();
return found;
}
void GenericEventQueue::timerFired(Timer<GenericEventQueue>*)
{
ASSERT(!m_timer.isActive());
ASSERT(!m_pendingEvents.isEmpty());
Vector<RefPtr<Event> > pendingEvents;
m_pendingEvents.swap(pendingEvents);
RefPtr<EventTarget> protect(m_owner);
for (unsigned i = 0; i < pendingEvents.size(); ++i) {
EventTarget* target = pendingEvents[i]->target() ? pendingEvents[i]->target() : m_owner;
target->dispatchEvent(pendingEvents[i].release());
}
}
void GenericEventQueue::close()
{
m_isClosed = true;
m_timer.stop();
m_pendingEvents.clear();
}
void GenericEventQueue::cancelAllEvents()
{
m_timer.stop();
m_pendingEvents.clear();
}
bool GenericEventQueue::hasPendingEvents() const
{
return m_pendingEvents.size();
}
}