#include "config.h"
#if ENABLE(SVG)
#include "TimeScheduler.h"
#include "Document.h"
#include "SystemTime.h"
#include "SVGTimer.h"
namespace WebCore {
const double staticTimerInterval = 0.050;
TimeScheduler::TimeScheduler(Document* document)
: m_creationTime(currentTime())
, m_savedTime(0)
, m_document(document)
{
m_intervalTimer = new SVGTimer(this, staticTimerInterval, false);
}
TimeScheduler::~TimeScheduler()
{
deleteAllValues(m_timerSet);
delete m_intervalTimer;
}
void TimeScheduler::addTimer(SVGAnimationElement* element, unsigned ms)
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
SVGTimer* svgTimer = new SVGTimer(this, ms * 0.001, true);
svgTimer->addNotify(element, true);
m_timerSet.add(svgTimer);
m_intervalTimer->addNotify(element, false);
#endif
}
void TimeScheduler::connectIntervalTimer(SVGAnimationElement* element)
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
m_intervalTimer->addNotify(element, true);
#endif
}
void TimeScheduler::disconnectIntervalTimer(SVGAnimationElement* element)
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
m_intervalTimer->removeNotify(element);
#endif
}
void TimeScheduler::startAnimations()
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
m_creationTime = currentTime();
SVGTimerSet::iterator end = m_timerSet.end();
for (SVGTimerSet::iterator it = m_timerSet.begin(); it != end; ++it) {
SVGTimer* svgTimer = *it;
if (svgTimer && !svgTimer->isActive())
svgTimer->start();
}
#endif
}
void TimeScheduler::toggleAnimations()
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
if (m_intervalTimer->isActive()) {
m_intervalTimer->stop();
m_savedTime = currentTime();
} else {
if (m_savedTime != 0) {
m_creationTime += currentTime() - m_savedTime;
m_savedTime = 0;
}
m_intervalTimer->start();
}
#endif
}
bool TimeScheduler::animationsPaused() const
{
return !m_intervalTimer->isActive();
}
void TimeScheduler::timerFired(Timer<TimeScheduler>* baseTimer)
{
#if ENABLE(SVG_EXPERIMENTAL_FEATURES)
RefPtr<Document> doc = m_document;
SVGTimer* timer = SVGTimer::downcast(baseTimer);
timer->notifyAll();
if (timer != m_intervalTimer) {
ASSERT(!timer->isActive());
ASSERT(m_timerSet.contains(timer));
m_timerSet.remove(timer);
delete timer;
if (!m_intervalTimer->isActive())
m_intervalTimer->start();
}
doc->updateRendering();
#endif
}
double TimeScheduler::elapsed() const
{
return currentTime() - m_creationTime;
}
}
#endif // ENABLE(SVG)