#include "config.h"
#include "DOMTimer.h"
#include "HTMLPlugInElement.h"
#include "InspectorInstrumentation.h"
#include "PluginViewBase.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 minIntervalForNonUserObservablePluginScriptTimers = 1000; static const int maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
struct DOMTimerFireState {
DOMTimerFireState(ScriptExecutionContext* context)
: scriptDidInteractWithNonUserObservablePlugin(false)
, scriptDidInteractWithUserObservablePlugin(false)
, shouldSetCurrent(context->isDocument())
{
if (shouldSetCurrent) {
previous = current;
current = this;
}
}
~DOMTimerFireState()
{
if (shouldSetCurrent)
current = previous;
}
static DOMTimerFireState* current;
bool scriptDidInteractWithNonUserObservablePlugin;
bool scriptDidInteractWithUserObservablePlugin;
private:
bool shouldSetCurrent;
DOMTimerFireState* previous;
};
DOMTimerFireState* DOMTimerFireState::current = nullptr;
static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
{
return UserGestureIndicator::processingUserGesture()
&& interval <= maxIntervalForUserGestureForwarding
&& !nestingLevel; }
DOMTimer::DOMTimer(ScriptExecutionContext* context, std::unique_ptr<ScheduledAction> action, int interval, bool singleShot)
: SuspendableTimer(context)
, m_nestingLevel(context->timerNestingLevel())
, m_action(WTF::move(action))
, m_originalInterval(interval)
, m_throttleState(Undetermined)
, m_currentTimerInterval(intervalClampedToMinimum())
, m_shouldForwardUserGesture(shouldForwardUserGesture(interval, m_nestingLevel))
{
RefPtr<DOMTimer> reference = adoptRef(this);
do {
m_timeoutId = context->circularSequentialID();
} while (!context->addTimeout(m_timeoutId, reference));
if (singleShot)
startOneShot(m_currentTimerInterval);
else
startRepeating(m_currentTimerInterval);
}
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);
context->removeTimeout(timeoutId);
}
void DOMTimer::scriptDidInteractWithPlugin(HTMLPlugInElement& pluginElement)
{
if (!DOMTimerFireState::current)
return;
if (pluginElement.isUserObservable())
DOMTimerFireState::current->scriptDidInteractWithUserObservablePlugin = true;
else
DOMTimerFireState::current->scriptDidInteractWithNonUserObservablePlugin = true;
}
void DOMTimer::fired()
{
RefPtr<DOMTimer> reference = this;
ScriptExecutionContext* context = scriptExecutionContext();
ASSERT(context);
DOMTimerFireState fireState(context);
#if PLATFORM(IOS)
Document* document = nullptr;
if (context->isDocument()) {
document = toDocument(context);
ASSERT(!document->frame()->timersPaused());
}
#endif
context->setTimerNestingLevel(std::min(m_nestingLevel + 1, maxTimerNestingLevel));
ASSERT(!isSuspended());
ASSERT(!context->activeDOMObjectsAreSuspended());
UserGestureIndicator gestureIndicator(m_shouldForwardUserGesture ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
m_shouldForwardUserGesture = false;
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutId);
if (isActive()) {
if (m_nestingLevel < maxTimerNestingLevel) {
m_nestingLevel++;
updateTimerIntervalIfNecessary();
}
m_action->execute(context);
InspectorInstrumentation::didFireTimer(cookie);
if (fireState.scriptDidInteractWithUserObservablePlugin && m_throttleState != ShouldNotThrottle) {
m_throttleState = ShouldNotThrottle;
updateTimerIntervalIfNecessary();
} else if (fireState.scriptDidInteractWithNonUserObservablePlugin && m_throttleState == Undetermined) {
m_throttleState = ShouldThrottle;
updateTimerIntervalIfNecessary();
}
return;
}
context->removeTimeout(m_timeoutId);
#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
m_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);
context->setTimerNestingLevel(0);
}
void DOMTimer::didStop()
{
m_action = nullptr;
}
void DOMTimer::updateTimerIntervalIfNecessary()
{
ASSERT(m_nestingLevel <= maxTimerNestingLevel);
double previousInterval = m_currentTimerInterval;
m_currentTimerInterval = intervalClampedToMinimum();
if (previousInterval == m_currentTimerInterval)
return;
if (repeatInterval()) {
ASSERT(repeatInterval() == previousInterval);
augmentRepeatInterval(m_currentTimerInterval - previousInterval);
} else
augmentFireInterval(m_currentTimerInterval - previousInterval);
}
double DOMTimer::intervalClampedToMinimum() const
{
ASSERT(scriptExecutionContext());
ASSERT(m_nestingLevel <= maxTimerNestingLevel);
double intervalInSeconds = std::max(oneMillisecond, m_originalInterval * oneMillisecond);
if (m_nestingLevel < maxTimerNestingLevel)
return intervalInSeconds;
intervalInSeconds = std::max(intervalInSeconds, scriptExecutionContext()->minimumTimerInterval());
if (m_throttleState == ShouldThrottle)
intervalInSeconds = std::max(intervalInSeconds, minIntervalForNonUserObservablePluginScriptTimers * oneMillisecond);
return intervalInSeconds;
}
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;
}
}