#include "config.h"
#include "DOMTimer.h"
#include "InspectorInstrumentation.h"
#include "ScheduledAction.h"
#include "ScriptExecutionContext.h"
#include "UserGestureIndicator.h"
#include <wtf/CurrentTime.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(IOS)
#include "Chrome.h"
#include "ChromeClient.h"
#include "Frame.h"
#include "Page.h"
#include "WKContentObservation.h"
#endif
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, std::unique_ptr<ScheduledAction> action, int interval, bool singleShot)
: SuspendableTimer(context)
, m_nestingLevel(timerNestingLevel + 1)
, m_action(WTF::move(action))
, m_originalInterval(interval)
, m_shouldForwardUserGesture(shouldForwardUserGesture(interval, m_nestingLevel))
{
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, std::unique_ptr<ScheduledAction> action, int timeout, bool singleShot)
{
DOMTimer* timer = new DOMTimer(context, WTF::move(action), timeout, singleShot);
#if PLATFORM(IOS)
if (context->isDocument()) {
Document& document = toDocument(*context);
bool didDeferTimeout = document.frame() && document.frame()->timersPaused();
if (!didDeferTimeout && timeout <= 100 && singleShot) {
WKSetObservedContentChange(WKContentIndeterminateChange);
WebThreadAddObservedContentModifier(timer); }
}
#endif
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);
#if PLATFORM(IOS)
Document* document = nullptr;
if (context->isDocument()) {
document = toDocument(context);
ASSERT(!document->frame()->timersPaused());
}
#endif
timerNestingLevel = m_nestingLevel;
ASSERT(!isSuspended());
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;
}
std::unique_ptr<ScheduledAction> action = WTF::move(m_action);
delete this;
#if PLATFORM(IOS)
bool shouldReportLackOfChanges;
bool shouldBeginObservingChanges;
if (document) {
shouldReportLackOfChanges = WebThreadCountOfObservedContentModifiers() == 1;
shouldBeginObservingChanges = WebThreadContainsObservedContentModifier(this);
} else {
shouldReportLackOfChanges = false;
shouldBeginObservingChanges = false;
}
if (shouldBeginObservingChanges) {
WKBeginObservingContentChanges(false);
WebThreadRemoveObservedContentModifier(this);
}
#endif
action->execute(context);
#if PLATFORM(IOS)
if (shouldBeginObservingChanges) {
WKStopObservingContentChanges();
if (WKObservedContentChange() == WKContentVisibilityChange || shouldReportLackOfChanges)
if (document && document->page())
document->page()->chrome().client().observedContentChange(document->frame());
}
#endif
InspectorInstrumentation::didFireTimer(cookie);
timerNestingLevel = 0;
}
void DOMTimer::contextDestroyed()
{
SuspendableTimer::contextDestroyed();
delete this;
}
void DOMTimer::didStop()
{
m_action = nullptr;
}
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 = std::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;
}
}