This source file includes following definitions.
- shouldForwardUserGesture
- hiddenPageAlignmentInterval
- visiblePageAlignmentInterval
- install
- removeByID
- m_action
- timeoutID
- fired
- contextDestroyed
- stop
- alignedFireTime
#include "config.h"
#include "core/frame/DOMTimer.h"
#include "core/dom/ExecutionContext.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "wtf/CurrentTime.h"
using namespace std;
namespace WebCore {
static const int maxIntervalForUserGestureForwarding = 1000;
static const int maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
static const double minimumInterval = 0.004;
static int timerNestingLevel = 0;
static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
{
return UserGestureIndicator::processingUserGesture()
&& interval <= maxIntervalForUserGestureForwarding
&& nestingLevel == 1;
}
double DOMTimer::hiddenPageAlignmentInterval()
{
return 1.0;
}
double DOMTimer::visiblePageAlignmentInterval()
{
return 0;
}
int DOMTimer::install(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
{
int timeoutID = context->installNewTimeout(action, timeout, singleShot);
InspectorInstrumentation::didInstallTimer(context, timeoutID, timeout, singleShot);
return timeoutID;
}
void DOMTimer::removeByID(ExecutionContext* context, int timeoutID)
{
context->removeTimeoutByID(timeoutID);
InspectorInstrumentation::didRemoveTimer(context, timeoutID);
}
DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
: SuspendableTimer(context)
, m_timeoutID(timeoutID)
, m_nestingLevel(timerNestingLevel + 1)
, m_action(action)
{
ASSERT(timeoutID > 0);
if (shouldForwardUserGesture(interval, m_nestingLevel))
m_userGestureToken = UserGestureIndicator::currentToken();
double intervalMilliseconds = max(oneMillisecond, interval * oneMillisecond);
if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
intervalMilliseconds = minimumInterval;
if (singleShot)
startOneShot(intervalMilliseconds, FROM_HERE);
else
startRepeating(intervalMilliseconds, FROM_HERE);
}
DOMTimer::~DOMTimer()
{
}
int DOMTimer::timeoutID() const
{
return m_timeoutID;
}
void DOMTimer::fired()
{
ExecutionContext* context = executionContext();
timerNestingLevel = m_nestingLevel;
ASSERT(!context->activeDOMObjectsAreSuspended());
UserGestureIndicator gestureIndicator(m_userGestureToken.release());
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutID);
if (isActive()) {
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();
context->removeTimeoutByID(m_timeoutID);
action->execute(context);
InspectorInstrumentation::didFireTimer(cookie);
timerNestingLevel = 0;
}
void DOMTimer::contextDestroyed()
{
SuspendableTimer::contextDestroyed();
}
void DOMTimer::stop()
{
SuspendableTimer::stop();
m_action.clear();
}
double DOMTimer::alignedFireTime(double fireTime) const
{
double alignmentInterval = executionContext()->timerAlignmentInterval();
if (alignmentInterval) {
double currentTime = monotonicallyIncreasingTime();
if (fireTime <= currentTime)
return fireTime;
double alignedTimeRoundedDown = floor(fireTime / alignmentInterval) * alignmentInterval;
double alignedTimeRoundedUp = ceil(fireTime / alignmentInterval) * alignmentInterval;
if (alignedTimeRoundedDown <= currentTime)
return alignedTimeRoundedUp;
if (fireTime - alignedTimeRoundedDown < minimumInterval)
return alignedTimeRoundedDown;
return alignedTimeRoundedUp;
}
return fireTime;
}
}