1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "DOMTimer.h"
#include "HTMLPlugInElement.h"
#include "InspectorInstrumentation.h"
#include "PluginViewBase.h"
#include "ScheduledAction.h"
#include "ScriptExecutionContext.h"
#include "UserGestureIndicator.h"
#include <wtf/CurrentTime.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(IOS)
#include "Chrome.h"
#include "ChromeClient.h"
#include "Frame.h"
#include "Page.h"
#include "WKContentObservation.h"
#endif
namespace WebCore {
static const int maxIntervalForUserGestureForwarding = 1000; // One second matches Gecko.
static const int minIntervalForNonUserObservablePluginScriptTimers = 1000; // Empirically determined to maximize battery life.
static const int maxTimerNestingLevel = 5;
static const double oneMillisecond = 0.001;
struct DOMTimerFireState {
DOMTimerFireState(ScriptExecutionContext* context)
: scriptDidInteractWithNonUserObservablePlugin(false)
, scriptDidInteractWithUserObservablePlugin(false)
, shouldSetCurrent(context->isDocument())
{
// For worker threads, don't update the current DOMTimerFireState.
// Setting this from workers would not be thread-safe, and its not relevant to current uses.
if (shouldSetCurrent) {
previous = current;
current = this;
}
}
~DOMTimerFireState()
{
if (shouldSetCurrent)
current = previous;
}
static DOMTimerFireState* current;
bool scriptDidInteractWithNonUserObservablePlugin;
bool scriptDidInteractWithUserObservablePlugin;
private:
bool shouldSetCurrent;
DOMTimerFireState* previous;
};
DOMTimerFireState* DOMTimerFireState::current = nullptr;
static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
{
return UserGestureIndicator::processingUserGesture()
&& interval <= maxIntervalForUserGestureForwarding
&& !nestingLevel; // Gestures should not be forwarded to nested timers.
}
DOMTimer::DOMTimer(ScriptExecutionContext* context, std::unique_ptr<ScheduledAction> action, int interval, bool singleShot)
: SuspendableTimer(context)
, m_nestingLevel(context->timerNestingLevel())
, m_action(WTF::move(action))
, m_originalInterval(interval)
, m_throttleState(Undetermined)
, m_currentTimerInterval(intervalClampedToMinimum())
, m_shouldForwardUserGesture(shouldForwardUserGesture(interval, m_nestingLevel))
{
RefPtr<DOMTimer> reference = adoptRef(this);
// Keep asking for the next id until we're given one that we don't already have.
do {
m_timeoutId = context->circularSequentialID();
} while (!context->addTimeout(m_timeoutId, reference));
if (singleShot)
startOneShot(m_currentTimerInterval);
else
startRepeating(m_currentTimerInterval);
}
int DOMTimer::install(ScriptExecutionContext* context, std::unique_ptr<ScheduledAction> action, int timeout, bool singleShot)
{
// DOMTimer constructor passes ownership of the initial ref on the object to the constructor.
// This reference will be released automatically when a one-shot timer fires, when the context
// is destroyed, or if explicitly cancelled by removeById.
DOMTimer* timer = new DOMTimer(context, WTF::move(action), timeout, singleShot);
#if PLATFORM(IOS)
if (context->isDocument()) {
Document& document = toDocument(*context);
bool didDeferTimeout = document.frame() && document.frame()->timersPaused();
if (!didDeferTimeout && timeout <= 100 && singleShot) {
WKSetObservedContentChange(WKContentIndeterminateChange);
WebThreadAddObservedContentModifier(timer); // Will only take affect if not already visibility change.
}
}
#endif
timer->suspendIfNeeded();
InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, singleShot);
return timer->m_timeoutId;
}
void DOMTimer::removeById(ScriptExecutionContext* context, int timeoutId)
{
// timeout IDs have to be positive, and 0 and -1 are unsafe to
// even look up since they are the empty and deleted value
// respectively
if (timeoutId <= 0)
return;
InspectorInstrumentation::didRemoveTimer(context, timeoutId);
context->removeTimeout(timeoutId);
}
void DOMTimer::scriptDidInteractWithPlugin(HTMLPlugInElement& pluginElement)
{
if (!DOMTimerFireState::current)
return;
if (pluginElement.isUserObservable())
DOMTimerFireState::current->scriptDidInteractWithUserObservablePlugin = true;
else
DOMTimerFireState::current->scriptDidInteractWithNonUserObservablePlugin = true;
}
void DOMTimer::fired()
{
// Retain this - if the timer is cancelled while this function is on the stack (implicitly and always
// for one-shot timers, or if removeById is called on itself from within an interval timer fire) then
// wait unit the end of this function to delete DOMTimer.
RefPtr<DOMTimer> reference = this;
ScriptExecutionContext* context = scriptExecutionContext();
ASSERT(context);
DOMTimerFireState fireState(context);
#if PLATFORM(IOS)
Document* document = nullptr;
if (context->isDocument()) {
document = toDocument(context);
ASSERT(!document->frame()->timersPaused());
}
#endif
context->setTimerNestingLevel(std::min(m_nestingLevel + 1, maxTimerNestingLevel));
ASSERT(!isSuspended());
ASSERT(!context->activeDOMObjectsAreSuspended());
UserGestureIndicator gestureIndicator(m_shouldForwardUserGesture ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
// Only the first execution of a multi-shot timer should get an affirmative user gesture indicator.
m_shouldForwardUserGesture = false;
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutId);
// Simple case for non-one-shot timers.
if (isActive()) {
if (m_nestingLevel < maxTimerNestingLevel) {
m_nestingLevel++;
updateTimerIntervalIfNecessary();
}
m_action->execute(context);
InspectorInstrumentation::didFireTimer(cookie);
if (fireState.scriptDidInteractWithUserObservablePlugin && m_throttleState != ShouldNotThrottle) {
m_throttleState = ShouldNotThrottle;
updateTimerIntervalIfNecessary();
} else if (fireState.scriptDidInteractWithNonUserObservablePlugin && m_throttleState == Undetermined) {
m_throttleState = ShouldThrottle;
updateTimerIntervalIfNecessary();
}
return;
}
context->removeTimeout(m_timeoutId);
#if PLATFORM(IOS)
bool shouldReportLackOfChanges;
bool shouldBeginObservingChanges;
if (document) {
shouldReportLackOfChanges = WebThreadCountOfObservedContentModifiers() == 1;
shouldBeginObservingChanges = WebThreadContainsObservedContentModifier(this);
} else {
shouldReportLackOfChanges = false;
shouldBeginObservingChanges = false;
}
if (shouldBeginObservingChanges) {
WKBeginObservingContentChanges(false);
WebThreadRemoveObservedContentModifier(this);
}
#endif
m_action->execute(context);
#if PLATFORM(IOS)
if (shouldBeginObservingChanges) {
WKStopObservingContentChanges();
if (WKObservedContentChange() == WKContentVisibilityChange || shouldReportLackOfChanges)
if (document && document->page())
document->page()->chrome().client().observedContentChange(document->frame());
}
#endif
InspectorInstrumentation::didFireTimer(cookie);
context->setTimerNestingLevel(0);
}
void DOMTimer::didStop()
{
// Need to release JS objects potentially protected by ScheduledAction
// because they can form circular references back to the ScriptExecutionContext
// which will cause a memory leak.
m_action = nullptr;
}
void DOMTimer::updateTimerIntervalIfNecessary()
{
ASSERT(m_nestingLevel <= maxTimerNestingLevel);
double previousInterval = m_currentTimerInterval;
m_currentTimerInterval = intervalClampedToMinimum();
if (previousInterval == m_currentTimerInterval)
return;
if (repeatInterval()) {
ASSERT(repeatInterval() == previousInterval);
augmentRepeatInterval(m_currentTimerInterval - previousInterval);
} else
augmentFireInterval(m_currentTimerInterval - previousInterval);
}
double DOMTimer::intervalClampedToMinimum() const
{
ASSERT(scriptExecutionContext());
ASSERT(m_nestingLevel <= maxTimerNestingLevel);
double intervalInSeconds = std::max(oneMillisecond, m_originalInterval * oneMillisecond);
// Only apply throttling to repeating timers.
if (m_nestingLevel < maxTimerNestingLevel)
return intervalInSeconds;
// Apply two throttles - the global (per Page) minimum, and also a per-timer throttle.
intervalInSeconds = std::max(intervalInSeconds, scriptExecutionContext()->minimumTimerInterval());
if (m_throttleState == ShouldThrottle)
intervalInSeconds = std::max(intervalInSeconds, minIntervalForNonUserObservablePluginScriptTimers * oneMillisecond);
return intervalInSeconds;
}
double DOMTimer::alignedFireTime(double fireTime) const
{
double alignmentInterval = scriptExecutionContext()->timerAlignmentInterval();
if (alignmentInterval) {
double currentTime = monotonicallyIncreasingTime();
if (fireTime <= currentTime)
return fireTime;
double alignedTime = ceil(fireTime / alignmentInterval) * alignmentInterval;
return alignedTime;
}
return fireTime;
}
} // namespace WebCore
|