#include "config.h"
#include "DOMTimer.h"
#include "InspectorInstrumentation.h"
#include "ScheduledAction.h"
#include "ScriptExecutionContext.h"
#include "UserGestureIndicator.h"
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
#include "Chrome.h"
#include "ChromeClient.h"
#include "Frame.h"
#include "Page.h"
#include "WKContentObservation.h"
using namespace std;
namespace WebCore {
static const int maxIntervalForUserGestureForwarding = 1000; static const int maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
double DOMTimer::s_minDefaultTimerInterval = 0.010;
static int timerNestingLevel = 0;
static int timeoutId()
{
static int lastUsedTimeoutId = 0;
++lastUsedTimeoutId;
if (lastUsedTimeoutId <= 0)
lastUsedTimeoutId = 1;
return lastUsedTimeoutId;
}
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_timeoutId(timeoutId())
, m_nestingLevel(timerNestingLevel + 1)
, m_action(action)
, m_originalInterval(interval)
, m_shouldForwardUserGesture(shouldForwardUserGesture(interval, m_nestingLevel))
{
scriptExecutionContext()->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);
if (context->isDocument()) {
Document* document = static_cast<Document*>(context);
bool deferTimeout = (document->frame() && document->frame()->timersPaused());
if (!deferTimeout) {
if (timeout <= 100 && singleShot) {
WKSetObservedContentChange(WKContentIndeterminateChange);
WebThreadAddObservedContentModifier(timer); }
}
}
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();
ASSERT(context);
Document* document = NULL;
if (context->isDocument()) {
document = static_cast<Document*>(context);
ASSERT(!document->frame()->timersPaused());
}
timerNestingLevel = m_nestingLevel;
ASSERT(!context->activeDOMObjectsAreSuspended());
UserGestureIndicator gestureIndicator(m_shouldForwardUserGesture ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
m_shouldForwardUserGesture = false;
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;
bool shouldReportLackOfChanges = false;
bool shouldBeginObservingChanges = false;
if (document) {
shouldReportLackOfChanges = WebThreadCountOfObservedContentModifiers() == 1;
shouldBeginObservingChanges = WebThreadContainsObservedContentModifier(this);
}
if (shouldBeginObservingChanges) {
WKBeginObservingContentChanges(false);
WebThreadRemoveObservedContentModifier(this);
}
action->execute(context);
if (shouldBeginObservingChanges) {
WKStopObservingContentChanges();
if (WKObservedContentChange() == WKContentVisibilityChange || shouldReportLackOfChanges)
if (document && document->page())
document->page()->chrome()->client()->observedContentChange(document->frame());
}
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;
}
}