#include "config.h"
#include "DOMTimer.h"
#include "InspectorTimelineAgent.h"
#include "ScheduledAction.h"
#include "ScriptExecutionContext.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 maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
double DOMTimer::s_minTimerInterval = 0.010;
static int timerNestingLevel = 0;
DOMTimer::DOMTimer(ScriptExecutionContext* context, ScheduledAction* action, int timeout, bool singleShot)
: ActiveDOMObject(context, this)
, m_action(action)
, m_nextFireInterval(0)
, m_repeatInterval(0)
#if !ASSERT_DISABLED
, m_suspended(false)
#endif
{
static int lastUsedTimeoutId = 0;
++lastUsedTimeoutId;
if (lastUsedTimeoutId <= 0)
lastUsedTimeoutId = 1;
m_timeoutId = lastUsedTimeoutId;
m_nestingLevel = timerNestingLevel + 1;
scriptExecutionContext()->addTimeout(m_timeoutId, this);
double intervalMilliseconds = max(oneMillisecond, timeout * oneMillisecond);
if (intervalMilliseconds < s_minTimerInterval && m_nestingLevel >= maxTimerNestingLevel)
intervalMilliseconds = s_minTimerInterval;
if (singleShot)
startOneShot(intervalMilliseconds);
else
startRepeating(intervalMilliseconds);
}
DOMTimer::~DOMTimer()
{
if (scriptExecutionContext())
scriptExecutionContext()->removeTimeout(m_timeoutId);
}
int DOMTimer::install(ScriptExecutionContext* context, 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); }
} else {
if (timer)
timer->suspend();
}
}
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = InspectorTimelineAgent::retrieve(context))
timelineAgent->didInstallTimer(timer->m_timeoutId, timeout, singleShot);
#endif
return timer->m_timeoutId;
}
void DOMTimer::removeById(ScriptExecutionContext* context, int timeoutId)
{
if (timeoutId <= 0)
return;
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = InspectorTimelineAgent::retrieve(context))
timelineAgent->didRemoveTimer(timeoutId);
#endif
delete context->findTimeout(timeoutId);
}
void DOMTimer::fired()
{
ScriptExecutionContext* context = scriptExecutionContext();
ASSERT(context && context->isDocument());
Document* document = static_cast<Document*>(context);
ASSERT(!document->frame()->timersPaused());
timerNestingLevel = m_nestingLevel;
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = InspectorTimelineAgent::retrieve(context))
timelineAgent->willFireTimer(m_timeoutId);
#endif
if (isActive()) {
if (repeatInterval() && repeatInterval() < s_minTimerInterval) {
m_nestingLevel++;
if (m_nestingLevel >= maxTimerNestingLevel)
augmentRepeatInterval(s_minTimerInterval - repeatInterval());
}
m_action->execute(context);
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = InspectorTimelineAgent::retrieve(context))
timelineAgent->didFireTimer();
#endif
return;
}
ScheduledAction* action = m_action.release();
delete this;
bool shouldReportLackOfChanges = WebThreadCountOfObservedContentModifiers() == 1;
bool 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());
}
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = InspectorTimelineAgent::retrieve(context))
timelineAgent->didFireTimer();
#endif
delete action;
timerNestingLevel = 0;
}
bool DOMTimer::hasPendingActivity() const
{
return isActive();
}
void DOMTimer::contextDestroyed()
{
ActiveDOMObject::contextDestroyed();
delete this;
}
void DOMTimer::stop()
{
TimerBase::stop();
m_action.clear();
}
void DOMTimer::suspend()
{
#if !ASSERT_DISABLED
ASSERT(!m_suspended);
m_suspended = true;
#endif
m_nextFireInterval = nextFireInterval();
m_repeatInterval = repeatInterval();
TimerBase::stop();
}
void DOMTimer::resume()
{
#if !ASSERT_DISABLED
ASSERT(m_suspended);
m_suspended = false;
#endif
start(m_nextFireInterval, m_repeatInterval);
}
bool DOMTimer::canSuspend() const
{
return true;
}
}