XMLHttpRequestProgressEventThrottle.cpp [plain text]
#include "config.h"
#include "XMLHttpRequestProgressEventThrottle.h"
#include "EventTarget.h"
#include "XMLHttpRequestProgressEvent.h"
namespace WebCore {
const double XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchingIntervalInSeconds = .05;
XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(EventTarget* target)
: m_target(target)
, m_loaded(0)
, m_total(0)
, m_suspended(false)
{
ASSERT(target);
}
XMLHttpRequestProgressEventThrottle::~XMLHttpRequestProgressEventThrottle()
{
}
void XMLHttpRequestProgressEventThrottle::dispatchProgressEvent(bool lengthComputable, unsigned loaded, unsigned total)
{
ASSERT(!suspended());
if (!isActive()) {
ASSERT(!m_loaded);
ASSERT(!m_total);
dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, lengthComputable, loaded, total));
startRepeating(minimumProgressEventDispatchingIntervalInSeconds);
return;
}
m_lengthComputable = lengthComputable;
m_loaded = loaded;
m_total = total;
}
void XMLHttpRequestProgressEventThrottle::dispatchEvent(PassRefPtr<Event> event, ProgressEventAction progressEventAction)
{
ASSERT(!suspended());
ASSERT(!m_pausedEvent);
if (progressEventAction == FlushProgressEvent)
flushProgressEvent();
m_target->dispatchEvent(event);
}
void XMLHttpRequestProgressEventThrottle::flushProgressEvent()
{
if (!hasEventToDispatch())
return;
PassRefPtr<Event> event = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total);
m_loaded = 0;
m_total = 0;
stop();
m_target->dispatchEvent(event);
}
void XMLHttpRequestProgressEventThrottle::dispatchPausedEvent()
{
ASSERT(!suspended());
if (!m_pausedEvent)
return;
m_target->dispatchEvent(m_pausedEvent);
m_pausedEvent = 0;
}
void XMLHttpRequestProgressEventThrottle::fired()
{
ASSERT(isActive());
ASSERT(!suspended());
ASSERT(!m_pausedEvent);
if (!hasEventToDispatch()) {
stop();
return;
}
m_target->dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total));
m_total = 0;
m_loaded = 0;
}
bool XMLHttpRequestProgressEventThrottle::hasEventToDispatch() const
{
return (m_total || m_loaded) && isActive();
}
void XMLHttpRequestProgressEventThrottle::suspend()
{
ASSERT(!m_pausedEvent);
m_suspended = true;
if (hasEventToDispatch()) {
m_pausedEvent = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total);
m_total = 0;
m_loaded = 0;
}
stop();
}
void XMLHttpRequestProgressEventThrottle::resume()
{
ASSERT(!m_loaded);
ASSERT(!m_total);
m_suspended = false;
dispatchPausedEvent();
}
}