2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "core/frame/DOMTimer.h"
30 #include "core/dom/ExecutionContext.h"
31 #include "core/inspector/InspectorInstrumentation.h"
32 #include "core/inspector/InspectorTraceEvents.h"
33 #include "platform/TraceEvent.h"
34 #include "wtf/CurrentTime.h"
38 static const int maxIntervalForUserGestureForwarding = 1000; // One second matches Gecko.
39 static const int maxTimerNestingLevel = 5;
40 static const double oneMillisecond = 0.001;
41 // Chromium uses a minimum timer interval of 4ms. We'd like to go
42 // lower; however, there are poorly coded websites out there which do
43 // create CPU-spinning loops. Using 4ms prevents the CPU from
44 // spinning too busily and provides a balance between CPU spinning and
45 // the smallest possible interval timer.
46 static const double minimumInterval = 0.004;
48 static int timerNestingLevel = 0;
50 static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
52 return UserGestureIndicator::processingUserGesture()
53 && interval <= maxIntervalForUserGestureForwarding
54 && nestingLevel == 1; // Gestures should not be forwarded to nested timers.
57 double DOMTimer::hiddenPageAlignmentInterval()
59 // Timers on hidden pages are aligned so that they fire once per
64 double DOMTimer::visiblePageAlignmentInterval()
66 // Alignment does not apply to timers on visible pages.
70 int DOMTimer::install(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
72 int timeoutID = context->installNewTimeout(action, timeout, singleShot);
73 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerInstall", "data", InspectorTimerInstallEvent::data(context, timeoutID, timeout, singleShot));
74 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
75 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
76 InspectorInstrumentation::didInstallTimer(context, timeoutID, timeout, singleShot);
80 void DOMTimer::removeByID(ExecutionContext* context, int timeoutID)
82 context->removeTimeoutByID(timeoutID);
83 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerRemove", "data", InspectorTimerRemoveEvent::data(context, timeoutID));
84 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
85 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
86 InspectorInstrumentation::didRemoveTimer(context, timeoutID);
89 DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
90 : SuspendableTimer(context)
91 , m_timeoutID(timeoutID)
92 , m_nestingLevel(timerNestingLevel + 1)
95 ASSERT(timeoutID > 0);
96 if (shouldForwardUserGesture(interval, m_nestingLevel))
97 m_userGestureToken = UserGestureIndicator::currentToken();
99 double intervalMilliseconds = std::max(oneMillisecond, interval * oneMillisecond);
100 if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
101 intervalMilliseconds = minimumInterval;
103 startOneShot(intervalMilliseconds, FROM_HERE);
105 startRepeating(intervalMilliseconds, FROM_HERE);
108 DOMTimer::~DOMTimer()
112 int DOMTimer::timeoutID() const
117 void DOMTimer::fired()
119 ExecutionContext* context = executionContext();
120 timerNestingLevel = m_nestingLevel;
121 ASSERT(!context->activeDOMObjectsAreSuspended());
122 // Only the first execution of a multi-shot timer should get an affirmative user gesture indicator.
123 UserGestureIndicator gestureIndicator(m_userGestureToken.release());
125 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimerFire", "data", InspectorTimerFireEvent::data(context, m_timeoutID));
126 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
127 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutID);
129 // Simple case for non-one-shot timers.
131 if (repeatInterval() && repeatInterval() < minimumInterval) {
133 if (m_nestingLevel >= maxTimerNestingLevel)
134 augmentRepeatInterval(minimumInterval - repeatInterval());
137 // No access to member variables after this point, it can delete the timer.
138 m_action->execute(context);
140 InspectorInstrumentation::didFireTimer(cookie);
145 // Delete timer before executing the action for one-shot timers.
146 OwnPtr<ScheduledAction> action = m_action.release();
148 // This timer is being deleted; no access to member variables allowed after this point.
149 context->removeTimeoutByID(m_timeoutID);
151 action->execute(context);
153 InspectorInstrumentation::didFireTimer(cookie);
154 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
156 timerNestingLevel = 0;
159 void DOMTimer::contextDestroyed()
161 SuspendableTimer::contextDestroyed();
164 void DOMTimer::stop()
166 SuspendableTimer::stop();
167 // Need to release JS objects potentially protected by ScheduledAction
168 // because they can form circular references back to the ExecutionContext
169 // which will cause a memory leak.
173 double DOMTimer::alignedFireTime(double fireTime) const
175 double alignmentInterval = executionContext()->timerAlignmentInterval();
176 if (alignmentInterval) {
177 double currentTime = monotonicallyIncreasingTime();
178 if (fireTime <= currentTime)
181 // When a repeating timer is scheduled for exactly the
182 // background page alignment interval, because it's impossible
183 // for the timer to be rescheduled instantaneously, it misses
184 // every other fire time. Avoid this by looking at the next
185 // fire time rounded both down and up.
187 double alignedTimeRoundedDown = floor(fireTime / alignmentInterval) * alignmentInterval;
188 double alignedTimeRoundedUp = ceil(fireTime / alignmentInterval) * alignmentInterval;
190 // If the version rounded down is in the past, discard it
193 if (alignedTimeRoundedDown <= currentTime)
194 return alignedTimeRoundedUp;
196 // Only use the rounded-down time if it's within a certain
197 // tolerance of the fire time. This avoids speeding up timers
198 // on background pages in the common case.
200 if (fireTime - alignedTimeRoundedDown < minimumInterval)
201 return alignedTimeRoundedDown;
203 return alignedTimeRoundedUp;