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 inline bool shouldForwardUserGesture(int interval
, int nestingLevel
)
50 return UserGestureIndicator::processingUserGesture()
51 && interval
<= maxIntervalForUserGestureForwarding
52 && nestingLevel
== 1; // Gestures should not be forwarded to nested timers.
55 double DOMTimer::hiddenPageAlignmentInterval()
57 // Timers on hidden pages are aligned so that they fire once per
62 double DOMTimer::visiblePageAlignmentInterval()
64 // Alignment does not apply to timers on visible pages.
68 int DOMTimer::install(ExecutionContext
* context
, PassOwnPtrWillBeRawPtr
<ScheduledAction
> action
, int timeout
, bool singleShot
)
70 int timeoutID
= context
->timers()->installNewTimeout(context
, action
, timeout
, singleShot
);
71 TRACE_EVENT_INSTANT1("devtools.timeline", "TimerInstall", TRACE_EVENT_SCOPE_THREAD
, "data", InspectorTimerInstallEvent::data(context
, timeoutID
, timeout
, singleShot
));
72 InspectorInstrumentation::didInstallTimer(context
, timeoutID
, timeout
, singleShot
);
76 void DOMTimer::removeByID(ExecutionContext
* context
, int timeoutID
)
78 context
->timers()->removeTimeoutByID(timeoutID
);
79 TRACE_EVENT_INSTANT1("devtools.timeline", "TimerRemove", TRACE_EVENT_SCOPE_THREAD
, "data", InspectorTimerRemoveEvent::data(context
, timeoutID
));
80 InspectorInstrumentation::didRemoveTimer(context
, timeoutID
);
83 DOMTimer::DOMTimer(ExecutionContext
* context
, PassOwnPtrWillBeRawPtr
<ScheduledAction
> action
, int interval
, bool singleShot
, int timeoutID
)
84 : SuspendableTimer(context
)
85 , m_timeoutID(timeoutID
)
86 , m_nestingLevel(context
->timers()->timerNestingLevel() + 1)
89 ASSERT(timeoutID
> 0);
90 if (shouldForwardUserGesture(interval
, m_nestingLevel
))
91 m_userGestureToken
= UserGestureIndicator::currentToken();
93 double intervalMilliseconds
= std::max(oneMillisecond
, interval
* oneMillisecond
);
94 if (intervalMilliseconds
< minimumInterval
&& m_nestingLevel
>= maxTimerNestingLevel
)
95 intervalMilliseconds
= minimumInterval
;
97 startOneShot(intervalMilliseconds
, FROM_HERE
);
99 startRepeating(intervalMilliseconds
, FROM_HERE
);
102 DOMTimer::~DOMTimer()
106 void DOMTimer::disposeTimer()
109 m_userGestureToken
= nullptr;
113 void DOMTimer::fired()
115 ExecutionContext
* context
= executionContext();
117 context
->timers()->setTimerNestingLevel(m_nestingLevel
);
118 ASSERT(!context
->activeDOMObjectsAreSuspended());
119 // Only the first execution of a multi-shot timer should get an affirmative user gesture indicator.
120 UserGestureIndicator
gestureIndicator(m_userGestureToken
.release());
122 TRACE_EVENT1("devtools.timeline", "TimerFire", "data", InspectorTimerFireEvent::data(context
, m_timeoutID
));
123 InspectorInstrumentationCookie cookie
= InspectorInstrumentation::willFireTimer(context
, m_timeoutID
);
125 // Simple case for non-one-shot timers.
127 if (repeatInterval() && repeatInterval() < minimumInterval
) {
129 if (m_nestingLevel
>= maxTimerNestingLevel
)
130 augmentRepeatInterval(minimumInterval
- repeatInterval());
133 // No access to member variables after this point, it can delete the timer.
134 m_action
->execute(context
);
136 InspectorInstrumentation::didFireTimer(cookie
);
141 RefPtrWillBeRawPtr
<DOMTimer
> protect(this);
143 // Unregister the timer from ExecutionContext before executing the action
144 // for one-shot timers.
145 OwnPtrWillBeRawPtr
<ScheduledAction
> action
= m_action
.release();
146 context
->timers()->removeTimeoutByID(m_timeoutID
);
148 action
->execute(context
);
150 InspectorInstrumentation::didFireTimer(cookie
);
151 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", TRACE_EVENT_SCOPE_THREAD
, "data", InspectorUpdateCountersEvent::data());
153 // ExecutionContext might be already gone when we executed action->execute().
154 if (executionContext())
155 executionContext()->timers()->setTimerNestingLevel(0);
158 void DOMTimer::stop()
160 SuspendableTimer::stop();
161 // Need to release JS objects potentially protected by ScheduledAction
162 // because they can form circular references back to the ExecutionContext
163 // which will cause a memory leak.
167 double DOMTimer::alignedFireTime(double fireTime
) const
169 double alignmentInterval
= executionContext()->timerAlignmentInterval();
170 if (alignmentInterval
) {
171 double currentTime
= monotonicallyIncreasingTime();
172 if (fireTime
<= currentTime
)
175 // When a repeating timer is scheduled for exactly the
176 // background page alignment interval, because it's impossible
177 // for the timer to be rescheduled instantaneously, it misses
178 // every other fire time. Avoid this by looking at the next
179 // fire time rounded both down and up.
181 double alignedTimeRoundedDown
= floor(fireTime
/ alignmentInterval
) * alignmentInterval
;
182 double alignedTimeRoundedUp
= ceil(fireTime
/ alignmentInterval
) * alignmentInterval
;
184 // If the version rounded down is in the past, discard it
187 if (alignedTimeRoundedDown
<= currentTime
)
188 return alignedTimeRoundedUp
;
190 // Only use the rounded-down time if it's within a certain
191 // tolerance of the fire time. This avoids speeding up timers
192 // on background pages in the common case.
194 if (fireTime
- alignedTimeRoundedDown
< minimumInterval
)
195 return alignedTimeRoundedDown
;
197 return alignedTimeRoundedUp
;
203 DEFINE_TRACE(DOMTimer
)
205 visitor
->trace(m_action
);
206 SuspendableTimer::trace(visitor
);