GenericEventQueue.cpp [plain text]
#include "config.h"
#include "GenericEventQueue.h"
#include "Event.h"
#include "EventTarget.h"
namespace WebCore {
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;
}
void GenericEventQueue::timerFired(Timer&)
{
ASSERT(!m_timer.isActive());
ASSERT(!m_pendingEvents.isEmpty());
Vector<RefPtr<Event>> pendingEvents;
m_pendingEvents.swap(pendingEvents);
Ref<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();
}
}