#include "config.h"
#include "DOMTimer.h"
#include "InspectorInstrumentation.h"
#include "ScheduledAction.h"
#include "ScriptExecutionContext.h"
#include <wtf/CurrentTime.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
using namespace std;
namespace WebCore {
static const int maxIntervalForUserGestureForwarding = 1000; static const int maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
static int timerNestingLevel = 0;
static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
{
return UserGestureIndicator::processingUserGesture()
&& interval <= maxIntervalForUserGestureForwarding
&& nestingLevel == 1; }
DOMTimer::DOMTimer(ScriptExecutionContext* context, PassOwnPtr<ScheduledAction> action, int interval, bool singleShot)
: SuspendableTimer(context)
, m_nestingLevel(timerNestingLevel + 1)
, m_action(action)
, m_originalInterval(interval)
{
if (shouldForwardUserGesture(interval, m_nestingLevel))
m_userGestureToken = UserGestureIndicator::currentToken();
do {
m_timeoutId = context->circularSequentialID();
} while (!context->addTimeout(m_timeoutId, this));
double intervalMilliseconds = intervalClampedToMinimum(interval, context->minimumTimerInterval());
if (singleShot)
startOneShot(intervalMilliseconds);
else
startRepeating(intervalMilliseconds);
}
DOMTimer::~DOMTimer()
{
if (scriptExecutionContext())
scriptExecutionContext()->removeTimeout(m_timeoutId);
}
int DOMTimer::install(ScriptExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
{
DOMTimer* timer = new DOMTimer(context, action, timeout, singleShot);
timer->suspendIfNeeded();
InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, singleShot);
return timer->m_timeoutId;
}
void DOMTimer::removeById(ScriptExecutionContext* context, int timeoutId)
{
if (timeoutId <= 0)
return;
InspectorInstrumentation::didRemoveTimer(context, timeoutId);
delete context->findTimeout(timeoutId);
}
void DOMTimer::fired()
{
ScriptExecutionContext* context = scriptExecutionContext();
timerNestingLevel = m_nestingLevel;
ASSERT(!context->activeDOMObjectsAreSuspended());
UserGestureIndicator gestureIndicator(m_userGestureToken.release());
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutId);
if (isActive()) {
double minimumInterval = context->minimumTimerInterval();
if (repeatInterval() && repeatInterval() < minimumInterval) {
m_nestingLevel++;
if (m_nestingLevel >= maxTimerNestingLevel)
augmentRepeatInterval(minimumInterval - repeatInterval());
}
m_action->execute(context);
InspectorInstrumentation::didFireTimer(cookie);
return;
}
OwnPtr<ScheduledAction> action = m_action.release();
delete this;
action->execute(context);
InspectorInstrumentation::didFireTimer(cookie);
timerNestingLevel = 0;
}
void DOMTimer::contextDestroyed()
{
SuspendableTimer::contextDestroyed();
delete this;
}
void DOMTimer::stop()
{
SuspendableTimer::stop();
m_action.clear();
}
void DOMTimer::adjustMinimumTimerInterval(double oldMinimumTimerInterval)
{
if (m_nestingLevel < maxTimerNestingLevel)
return;
double newMinimumInterval = scriptExecutionContext()->minimumTimerInterval();
double newClampedInterval = intervalClampedToMinimum(m_originalInterval, newMinimumInterval);
if (repeatInterval()) {
augmentRepeatInterval(newClampedInterval - repeatInterval());
return;
}
double previousClampedInterval = intervalClampedToMinimum(m_originalInterval, oldMinimumTimerInterval);
augmentFireInterval(newClampedInterval - previousClampedInterval);
}
double DOMTimer::intervalClampedToMinimum(int timeout, double minimumTimerInterval) const
{
double intervalMilliseconds = max(oneMillisecond, timeout * oneMillisecond);
if (intervalMilliseconds < minimumTimerInterval && m_nestingLevel >= maxTimerNestingLevel)
intervalMilliseconds = minimumTimerInterval;
return intervalMilliseconds;
}
double DOMTimer::alignedFireTime(double fireTime) const
{
double alignmentInterval = scriptExecutionContext()->timerAlignmentInterval();
if (alignmentInterval) {
double currentTime = monotonicallyIncreasingTime();
if (fireTime <= currentTime)
return fireTime;
double alignedTime = ceil(fireTime / alignmentInterval) * alignmentInterval;
return alignedTime;
}
return fireTime;
}
}