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/TraceEvent.h"
34 #include "wtf/CurrentTime.h"
35
36 namespace WebCore {
37
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;
47
48 static int timerNestingLevel = 0;
49
shouldForwardUserGesture(int interval,int nestingLevel)50 static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
51 {
52 return UserGestureIndicator::processingUserGesture()
53 && interval <= maxIntervalForUserGestureForwarding
54 && nestingLevel == 1; // Gestures should not be forwarded to nested timers.
55 }
56
hiddenPageAlignmentInterval()57 double DOMTimer::hiddenPageAlignmentInterval()
58 {
59 // Timers on hidden pages are aligned so that they fire once per
60 // second at most.
61 return 1.0;
62 }
63
visiblePageAlignmentInterval()64 double DOMTimer::visiblePageAlignmentInterval()
65 {
66 // Alignment does not apply to timers on visible pages.
67 return 0;
68 }
69
install(ExecutionContext * context,PassOwnPtr<ScheduledAction> action,int timeout,bool singleShot)70 int DOMTimer::install(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
71 {
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);
77 return timeoutID;
78 }
79
removeByID(ExecutionContext * context,int timeoutID)80 void DOMTimer::removeByID(ExecutionContext* context, int timeoutID)
81 {
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);
87 }
88
DOMTimer(ExecutionContext * context,PassOwnPtr<ScheduledAction> action,int interval,bool singleShot,int 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)
93 , m_action(action)
94 {
95 ASSERT(timeoutID > 0);
96 if (shouldForwardUserGesture(interval, m_nestingLevel))
97 m_userGestureToken = UserGestureIndicator::currentToken();
98
99 double intervalMilliseconds = std::max(oneMillisecond, interval * oneMillisecond);
100 if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
101 intervalMilliseconds = minimumInterval;
102 if (singleShot)
103 startOneShot(intervalMilliseconds, FROM_HERE);
104 else
105 startRepeating(intervalMilliseconds, FROM_HERE);
106 }
107
~DOMTimer()108 DOMTimer::~DOMTimer()
109 {
110 }
111
timeoutID() const112 int DOMTimer::timeoutID() const
113 {
114 return m_timeoutID;
115 }
116
fired()117 void DOMTimer::fired()
118 {
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());
124
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);
128
129 // Simple case for non-one-shot timers.
130 if (isActive()) {
131 if (repeatInterval() && repeatInterval() < minimumInterval) {
132 m_nestingLevel++;
133 if (m_nestingLevel >= maxTimerNestingLevel)
134 augmentRepeatInterval(minimumInterval - repeatInterval());
135 }
136
137 // No access to member variables after this point, it can delete the timer.
138 m_action->execute(context);
139
140 InspectorInstrumentation::didFireTimer(cookie);
141
142 return;
143 }
144
145 // Delete timer before executing the action for one-shot timers.
146 OwnPtr<ScheduledAction> action = m_action.release();
147
148 // This timer is being deleted; no access to member variables allowed after this point.
149 context->removeTimeoutByID(m_timeoutID);
150
151 action->execute(context);
152
153 InspectorInstrumentation::didFireTimer(cookie);
154 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
155
156 timerNestingLevel = 0;
157 }
158
contextDestroyed()159 void DOMTimer::contextDestroyed()
160 {
161 SuspendableTimer::contextDestroyed();
162 }
163
stop()164 void DOMTimer::stop()
165 {
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.
170 m_action.clear();
171 }
172
alignedFireTime(double fireTime) const173 double DOMTimer::alignedFireTime(double fireTime) const
174 {
175 double alignmentInterval = executionContext()->timerAlignmentInterval();
176 if (alignmentInterval) {
177 double currentTime = monotonicallyIncreasingTime();
178 if (fireTime <= currentTime)
179 return fireTime;
180
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.
186
187 double alignedTimeRoundedDown = floor(fireTime / alignmentInterval) * alignmentInterval;
188 double alignedTimeRoundedUp = ceil(fireTime / alignmentInterval) * alignmentInterval;
189
190 // If the version rounded down is in the past, discard it
191 // immediately.
192
193 if (alignedTimeRoundedDown <= currentTime)
194 return alignedTimeRoundedUp;
195
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.
199
200 if (fireTime - alignedTimeRoundedDown < minimumInterval)
201 return alignedTimeRoundedDown;
202
203 return alignedTimeRoundedUp;
204 }
205
206 return fireTime;
207 }
208
209 } // namespace WebCore
210