1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28#include "core/frame/DOMTimer.h"
29
30#include "core/dom/ExecutionContext.h"
31#include "core/inspector/InspectorInstrumentation.h"
32#include "core/inspector/InspectorTraceEvents.h"
33#include "platform/Logging.h"
34#include "platform/TraceEvent.h"
35#include "wtf/CurrentTime.h"
36
37namespace blink {
38
39static const int maxIntervalForUserGestureForwarding = 1000; // One second matches Gecko.
40static const int maxTimerNestingLevel = 5;
41static const double oneMillisecond = 0.001;
42// Chromium uses a minimum timer interval of 4ms. We'd like to go
43// lower; however, there are poorly coded websites out there which do
44// create CPU-spinning loops.  Using 4ms prevents the CPU from
45// spinning too busily and provides a balance between CPU spinning and
46// the smallest possible interval timer.
47static const double minimumInterval = 0.004;
48
49static int timerNestingLevel = 0;
50
51static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
52{
53    return UserGestureIndicator::processingUserGesture()
54        && interval <= maxIntervalForUserGestureForwarding
55        && nestingLevel == 1; // Gestures should not be forwarded to nested timers.
56}
57
58double DOMTimer::hiddenPageAlignmentInterval()
59{
60    // Timers on hidden pages are aligned so that they fire once per
61    // second at most.
62    return 1.0;
63}
64
65double DOMTimer::visiblePageAlignmentInterval()
66{
67    // Alignment does not apply to timers on visible pages.
68    return 0;
69}
70
71int DOMTimer::install(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
72{
73    int timeoutID = context->installNewTimeout(action, timeout, singleShot);
74    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerInstall", "data", InspectorTimerInstallEvent::data(context, timeoutID, timeout, singleShot));
75    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
76    // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
77    InspectorInstrumentation::didInstallTimer(context, timeoutID, timeout, singleShot);
78    WTF_LOG(Timers, "DOMTimer::install: timeoutID = %d, timeout = %d, singleShot = %d", timeoutID, timeout, singleShot ? 1 : 0);
79    return timeoutID;
80}
81
82void DOMTimer::removeByID(ExecutionContext* context, int timeoutID)
83{
84    WTF_LOG(Timers, "DOMTimer::removeByID: timeoutID = %d", timeoutID);
85    context->removeTimeoutByID(timeoutID);
86    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerRemove", "data", InspectorTimerRemoveEvent::data(context, timeoutID));
87    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
88    // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
89    InspectorInstrumentation::didRemoveTimer(context, timeoutID);
90}
91
92DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
93    : SuspendableTimer(context)
94    , m_timeoutID(timeoutID)
95    , m_nestingLevel(timerNestingLevel + 1)
96    , m_action(action)
97{
98    ASSERT(timeoutID > 0);
99    if (shouldForwardUserGesture(interval, m_nestingLevel))
100        m_userGestureToken = UserGestureIndicator::currentToken();
101
102    double intervalMilliseconds = std::max(oneMillisecond, interval * oneMillisecond);
103    if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
104        intervalMilliseconds = minimumInterval;
105    if (singleShot)
106        startOneShot(intervalMilliseconds, FROM_HERE);
107    else
108        startRepeating(intervalMilliseconds, FROM_HERE);
109}
110
111DOMTimer::~DOMTimer()
112{
113}
114
115int DOMTimer::timeoutID() const
116{
117    return m_timeoutID;
118}
119
120void DOMTimer::fired()
121{
122    ExecutionContext* context = executionContext();
123    timerNestingLevel = m_nestingLevel;
124    ASSERT(!context->activeDOMObjectsAreSuspended());
125    // Only the first execution of a multi-shot timer should get an affirmative user gesture indicator.
126    UserGestureIndicator gestureIndicator(m_userGestureToken.release());
127
128    TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerFire", "data", InspectorTimerFireEvent::data(context, m_timeoutID));
129    // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
130    InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutID);
131
132    // Simple case for non-one-shot timers.
133    if (isActive()) {
134        if (repeatInterval() && repeatInterval() < minimumInterval) {
135            m_nestingLevel++;
136            if (m_nestingLevel >= maxTimerNestingLevel)
137                augmentRepeatInterval(minimumInterval - repeatInterval());
138        }
139
140        WTF_LOG(Timers, "DOMTimer::fired: m_timeoutID = %d, repeatInterval = %f, m_action = %p", m_timeoutID, repeatInterval(), m_action.get());
141
142        // No access to member variables after this point, it can delete the timer.
143        m_action->execute(context);
144
145        InspectorInstrumentation::didFireTimer(cookie);
146
147        return;
148    }
149
150    WTF_LOG(Timers, "DOMTimer::fired: m_timeoutID = %d, one-shot, m_action = %p", m_timeoutID, m_action.get());
151
152    // Delete timer before executing the action for one-shot timers.
153    OwnPtr<ScheduledAction> action = m_action.release();
154
155    // This timer is being deleted; no access to member variables allowed after this point.
156    context->removeTimeoutByID(m_timeoutID);
157
158    action->execute(context);
159
160    InspectorInstrumentation::didFireTimer(cookie);
161    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
162
163    timerNestingLevel = 0;
164}
165
166void DOMTimer::contextDestroyed()
167{
168    SuspendableTimer::contextDestroyed();
169}
170
171void DOMTimer::stop()
172{
173    SuspendableTimer::stop();
174    // Need to release JS objects potentially protected by ScheduledAction
175    // because they can form circular references back to the ExecutionContext
176    // which will cause a memory leak.
177    m_action.clear();
178}
179
180double DOMTimer::alignedFireTime(double fireTime) const
181{
182    double alignmentInterval = executionContext()->timerAlignmentInterval();
183    if (alignmentInterval) {
184        double currentTime = monotonicallyIncreasingTime();
185        if (fireTime <= currentTime)
186            return fireTime;
187
188        // When a repeating timer is scheduled for exactly the
189        // background page alignment interval, because it's impossible
190        // for the timer to be rescheduled instantaneously, it misses
191        // every other fire time. Avoid this by looking at the next
192        // fire time rounded both down and up.
193
194        double alignedTimeRoundedDown = floor(fireTime / alignmentInterval) * alignmentInterval;
195        double alignedTimeRoundedUp = ceil(fireTime / alignmentInterval) * alignmentInterval;
196
197        // If the version rounded down is in the past, discard it
198        // immediately.
199
200        if (alignedTimeRoundedDown <= currentTime)
201            return alignedTimeRoundedUp;
202
203        // Only use the rounded-down time if it's within a certain
204        // tolerance of the fire time. This avoids speeding up timers
205        // on background pages in the common case.
206
207        if (fireTime - alignedTimeRoundedDown < minimumInterval)
208            return alignedTimeRoundedDown;
209
210        return alignedTimeRoundedUp;
211    }
212
213    return fireTime;
214}
215
216} // namespace blink
217