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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*
* 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 COMPUTER, 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 COMPUTER, 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 "third_party/blink/renderer/core/scheduler/dom_timer.h"
#include <limits>
#include "base/check_deref.h"
#include "base/message_loop/message_pump.h"
#include "base/numerics/clamped_math.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/core_probes_inl.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/page_dismissal_scope.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/scheduler/scheduled_action.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/scheduler/public/scheduling_policy.h"
#include "third_party/blink/renderer/platform/weborigin/reporting_disposition.h"
namespace blink {
namespace {
// Step 11 of the algorithm at
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html requires
// that a timeout less than 4ms is increased to 4ms when the nesting level is
// greater than 5. Since the counters in this file start at 1 (rather than the
// 0 in the spec), we should use the value 6 here.
// (The value is still 4 until StandardizedTimerClamping has shipped.)
constexpr int kMaxTimerNestingLevel = 4;
constexpr int kSpecCompliantMaxTimerNestingLevel = 6;
constexpr base::TimeDelta kMinimumInterval = base::Milliseconds(4);
base::TimeDelta GetMaxHighResolutionInterval() {
return base::MessagePump::GetAlignWakeUpsEnabled() &&
base::FeatureList::IsEnabled(
features::kLowerHighResolutionTimerThreshold)
? base::Milliseconds(4)
: base::Milliseconds(32);
}
// Maintains a set of DOMTimers for a given ExecutionContext. Assigns IDs to
// timers; these IDs are the ones returned to web authors from setTimeout or
// setInterval. It also tracks recursive creation or iterative scheduling of
// timers, which is used as a signal for throttling repetitive timers.
class DOMTimerCoordinator : public GarbageCollected<DOMTimerCoordinator>,
public Supplement<ExecutionContext> {
public:
constexpr static const char kSupplementName[] = "DOMTimerCoordinator";
static DOMTimerCoordinator& From(ExecutionContext& context) {
CHECK(!context.IsWorkletGlobalScope());
auto* coordinator =
Supplement<ExecutionContext>::From<DOMTimerCoordinator>(context);
if (!coordinator) {
coordinator = MakeGarbageCollected<DOMTimerCoordinator>(context);
Supplement<ExecutionContext>::ProvideTo(context, coordinator);
}
return *coordinator;
}
explicit DOMTimerCoordinator(ExecutionContext& context)
: Supplement<ExecutionContext>(context) {}
int Install(DOMTimer* timer) {
int timeout_id = NextID();
timers_.insert(timeout_id, timer);
return timeout_id;
}
// Removes and disposes the timer with the specified ID, if any. This may
// destroy the timer.
DOMTimer* RemoveTimeoutByID(int timeout_id) {
if (timeout_id <= 0) {
return nullptr;
}
DOMTimer* removed_timer = timers_.Take(timeout_id);
if (removed_timer) {
removed_timer->Stop();
}
return removed_timer;
}
// Timers created during the execution of other timers, and
// repeating timers, are throttled. Timer nesting level tracks the
// number of linked timers or repetitions of a timer. See
// https://html.spec.whatwg.org/C/#timers
int TimerNestingLevel() { return timer_nesting_level_; }
// Sets the timer nesting level. Set when a timer executes so that
// any timers created while the timer is executing will incur a
// deeper timer nesting level, see DOMTimer::DOMTimer.
void SetTimerNestingLevel(int level) { timer_nesting_level_ = level; }
void Trace(Visitor* visitor) const final {
visitor->Trace(timers_);
Supplement<ExecutionContext>::Trace(visitor);
}
private:
int NextID() {
while (true) {
if (circular_sequential_id_ == std::numeric_limits<int>::max()) {
circular_sequential_id_ = 1;
} else {
++circular_sequential_id_;
}
if (!timers_.Contains(circular_sequential_id_)) {
return circular_sequential_id_;
}
}
}
HeapHashMap<int, Member<DOMTimer>> timers_;
int circular_sequential_id_ = 0;
int timer_nesting_level_ = 0;
};
bool IsAllowed(ExecutionContext& context, bool is_eval, const String& source) {
if (context.IsContextDestroyed()) {
return false;
}
if (is_eval && !context.GetContentSecurityPolicy()->AllowEval(
ReportingDisposition::kReport,
ContentSecurityPolicy::kWillNotThrowException, source)) {
return false;
}
if (auto* window = DynamicTo<LocalDOMWindow>(context);
window && PageDismissalScope::IsActive()) {
UseCounter::Count(window, window->document()->ProcessingBeforeUnload()
? WebFeature::kTimerInstallFromBeforeUnload
: WebFeature::kTimerInstallFromUnload);
}
return true;
}
} // namespace
int DOMTimer::setTimeout(ScriptState* script_state,
ExecutionContext& context,
V8Function* handler,
int timeout,
const HeapVector<ScriptValue>& arguments) {
if (!IsAllowed(context, false, g_empty_string)) {
return 0;
}
auto* action = MakeGarbageCollected<ScheduledAction>(script_state, context,
handler, arguments);
return MakeGarbageCollected<DOMTimer>(context, action,
base::Milliseconds(timeout), true)
->timeout_id_;
}
int DOMTimer::setTimeout(ScriptState* script_state,
ExecutionContext& context,
const String& handler,
int timeout,
const HeapVector<ScriptValue>&) {
if (!IsAllowed(context, true, handler)) {
return 0;
}
// Don't allow setting timeouts to run empty functions. Was historically a
// performance issue.
if (handler.empty()) {
return 0;
}
auto* action =
MakeGarbageCollected<ScheduledAction>(script_state, context, handler);
return MakeGarbageCollected<DOMTimer>(context, action,
base::Milliseconds(timeout), true)
->timeout_id_;
}
int DOMTimer::setInterval(ScriptState* script_state,
ExecutionContext& context,
V8Function* handler,
int timeout,
const HeapVector<ScriptValue>& arguments) {
if (!IsAllowed(context, false, g_empty_string)) {
return 0;
}
auto* action = MakeGarbageCollected<ScheduledAction>(script_state, context,
handler, arguments);
return MakeGarbageCollected<DOMTimer>(context, action,
base::Milliseconds(timeout), false)
->timeout_id_;
}
int DOMTimer::setInterval(ScriptState* script_state,
ExecutionContext& context,
const String& handler,
int timeout,
const HeapVector<ScriptValue>&) {
if (!IsAllowed(context, true, handler)) {
return 0;
}
// Don't allow setting timeouts to run empty functions. Was historically a
// performance issue.
if (handler.empty()) {
return 0;
}
auto* action =
MakeGarbageCollected<ScheduledAction>(script_state, context, handler);
return MakeGarbageCollected<DOMTimer>(context, action,
base::Milliseconds(timeout), false)
->timeout_id_;
}
void DOMTimer::clearTimeout(ExecutionContext& context, int timeout_id) {
RemoveByID(context, timeout_id);
}
void DOMTimer::clearInterval(ExecutionContext& context, int timeout_id) {
RemoveByID(context, timeout_id);
}
void DOMTimer::RemoveByID(ExecutionContext& context, int timeout_id) {
DEVTOOLS_TIMELINE_TRACE_EVENT_INSTANT(
"TimerRemove", inspector_timer_remove_event::Data, &context, timeout_id);
// Eagerly unregister as ExecutionContext observer.
if (DOMTimer* timer =
DOMTimerCoordinator::From(context).RemoveTimeoutByID(timeout_id)) {
// Eagerly unregister as ExecutionContext observer.
timer->SetExecutionContext(nullptr);
}
}
DOMTimer::DOMTimer(ExecutionContext& context,
ScheduledAction* action,
base::TimeDelta timeout,
bool single_shot)
: ExecutionContextLifecycleObserver(&context),
TimerBase(nullptr),
timeout_id_(DOMTimerCoordinator::From(context).Install(this)),
// Step 9:
nesting_level_(DOMTimerCoordinator::From(context).TimerNestingLevel()),
action_(action) {
DCHECK_GT(timeout_id_, 0);
// Step 10:
if (timeout.is_negative()) {
timeout = base::TimeDelta();
}
// Steps 12 and 13:
// Note: The implementation increments the nesting level before using it to
// adjust timeout, contrary to what the spec requires crbug.com/1108877.
IncrementNestingLevel();
// A timer with a long timeout probably doesn't need to run at a precise time,
// so allow some leeway on it. On the other hand, a timer with a short timeout
// may need to run on time to deliver the best user experience.
// TODO(crbug.com/1153139): Remove IsAlignWakeUpsDisabledForProcess() in M121
// once workaround is no longer needed by WebRTC apps.
bool precise = (timeout < GetMaxHighResolutionInterval()) ||
scheduler::IsAlignWakeUpsDisabledForProcess();
const int max_timer_nesting_level =
RuntimeEnabledFeatures::StandardizedTimerClampingEnabled()
? kSpecCompliantMaxTimerNestingLevel
: kMaxTimerNestingLevel;
// Step 11:
if (nesting_level_ > max_timer_nesting_level && timeout < kMinimumInterval) {
timeout = kMinimumInterval;
}
// Select TaskType based on nesting level.
TaskType task_type;
if (nesting_level_ > max_timer_nesting_level) {
task_type = TaskType::kJavascriptTimerDelayedHighNesting;
} else if (timeout.is_zero()) {
task_type = TaskType::kJavascriptTimerImmediate;
DCHECK_LE(nesting_level_, max_timer_nesting_level);
} else {
task_type = TaskType::kJavascriptTimerDelayedLowNesting;
}
MoveToNewTaskRunner(context.GetTaskRunner(task_type));
// Clamping up to 1ms for historical reasons crbug.com/402694.
if (!single_shot && !blink::features::IsSetIntervalWithoutClampEnabled()) {
timeout = std::max(timeout, base::Milliseconds(1));
}
if (single_shot) {
StartOneShot(timeout, FROM_HERE, precise);
} else {
StartRepeating(timeout, FROM_HERE, precise);
}
DEVTOOLS_TIMELINE_TRACE_EVENT_INSTANT(
"TimerInstall", inspector_timer_install_event::Data, &context,
timeout_id_, timeout, single_shot);
const char* name = single_shot ? "setTimeout" : "setInterval";
async_task_context_.Schedule(&context, name);
probe::BreakableLocation(&context, name);
}
DOMTimer::~DOMTimer() = default;
void DOMTimer::Dispose() {
Stop();
}
void DOMTimer::Stop() {
if (!action_) {
return;
}
async_task_context_.Cancel();
const bool is_interval = RepeatInterval().has_value();
probe::BreakableLocation(GetExecutionContext(),
is_interval ? "clearInterval" : "clearTimeout");
// Need to release JS objects potentially protected by ScheduledAction
// because they can form circular references back to the ExecutionContext
// which will cause a memory leak.
if (action_) {
action_->Dispose();
}
action_ = nullptr;
TimerBase::Stop();
}
void DOMTimer::ContextDestroyed() {
Stop();
}
void DOMTimer::Fired() {
ExecutionContext* context = GetExecutionContext();
DCHECK(context);
DOMTimerCoordinator::From(*context).SetTimerNestingLevel(nesting_level_);
DCHECK(!context->IsContextPaused());
// Only the first execution of a multi-shot timer should get an affirmative
// user gesture indicator.
DEVTOOLS_TIMELINE_TRACE_EVENT("TimerFire", inspector_timer_fire_event::Data,
context, timeout_id_);
const bool is_interval = RepeatInterval().has_value();
probe::UserCallback probe(context, is_interval ? "setInterval" : "setTimeout",
g_null_atom, true);
probe::InvokeCallback invoke_probe(
CHECK_DEREF(action_->GetScriptState()),
is_interval ? "TimerHandler:setInterval" : "TimerHandler:setTimeout",
action_->CallbackFunction());
probe::AsyncTask async_task(context, &async_task_context_,
is_interval ? "fired" : nullptr);
const int max_timer_nesting_level =
RuntimeEnabledFeatures::StandardizedTimerClampingEnabled()
? kSpecCompliantMaxTimerNestingLevel
: kMaxTimerNestingLevel;
// Simple case for non-one-shot timers.
if (IsActive()) {
DCHECK(is_interval);
DCHECK(RepeatInterval());
// Steps 12 and 13:
IncrementNestingLevel();
// Step 11:
// Make adjustments when the nesting level becomes > |kMaxNestingLevel|.
if (nesting_level_ == max_timer_nesting_level + 1 &&
(*RepeatInterval() < kMinimumInterval)) {
AugmentRepeatInterval(kMinimumInterval - *RepeatInterval());
}
if (nesting_level_ == max_timer_nesting_level + 1) {
// Move to the TaskType that corresponds to nesting level >=
// |kMaxNestingLevel|.
MoveToNewTaskRunner(
context->GetTaskRunner(TaskType::kJavascriptTimerDelayedHighNesting));
}
DCHECK(nesting_level_ <= max_timer_nesting_level ||
(is_interval && *RepeatInterval() >= kMinimumInterval));
// No access to member variables after this point, it can delete the timer.
action_->Execute(context);
DOMTimerCoordinator::From(*context).SetTimerNestingLevel(0);
return;
}
// Unregister the timer from ExecutionContext before executing the action
// for one-shot timers.
ScheduledAction* action = action_.Release();
DOMTimerCoordinator::From(*context).RemoveTimeoutByID(timeout_id_);
action->Execute(context);
// Eagerly clear out |action|'s resources.
action->Dispose();
// ExecutionContext might be already gone when we executed action->execute().
ExecutionContext* execution_context = GetExecutionContext();
if (!execution_context) {
return;
}
DOMTimerCoordinator::From(*execution_context).SetTimerNestingLevel(0);
// Eagerly unregister as ExecutionContext observer.
SetExecutionContext(nullptr);
}
void DOMTimer::Trace(Visitor* visitor) const {
visitor->Trace(action_);
ExecutionContextLifecycleObserver::Trace(visitor);
}
void DOMTimer::IncrementNestingLevel() {
nesting_level_ = base::ClampAdd(nesting_level_, 1);
}
} // namespace blink
|