Skip to content

Commit

Permalink
Replace DOMTimer's boolean oneShot argument with an enum class for cl…
Browse files Browse the repository at this point in the history
…arity

https://bugs.webkit.org/show_bug.cgi?id=259871

Reviewed by Matt Woodrow.

Replaced the boolean by an enum class Type for clarity.

* Source/WebCore/dom/AbortSignal.cpp:
(WebCore::AbortSignal::timeout):
* Source/WebCore/page/DOMTimer.cpp:
(WebCore::DOMTimer::DOMTimer):
(WebCore::DOMTimer::install):
* Source/WebCore/page/DOMTimer.h:
* Source/WebCore/page/LocalDOMWindow.cpp:
(WebCore::LocalDOMWindow::setTimeout):
(WebCore::LocalDOMWindow::setInterval):
* Source/WebCore/workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::setTimeout):
(WebCore::WorkerGlobalScope::setInterval):

Canonical link: https://commits.webkit.org/266630@main
  • Loading branch information
rniwa committed Aug 7, 2023
1 parent 5d331a9 commit 5e90e4c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/dom/AbortSignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Ref<AbortSignal> AbortSignal::timeout(ScriptExecutionContext& context, uint64_t
Locker locker { vm.apiLock() };
signal->signalAbort(toJS(globalObject, globalObject, DOMException::create(TimeoutError)));
};
DOMTimer::install(context, WTFMove(action), Seconds::fromMilliseconds(milliseconds), true);
DOMTimer::install(context, WTFMove(action), Seconds::fromMilliseconds(milliseconds), DOMTimer::Type::SingleShot);
return signal;
}

Expand Down
18 changes: 9 additions & 9 deletions Source/WebCore/page/DOMTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,35 +159,35 @@ struct NestedTimersMap {

bool NestedTimersMap::isTrackingNestedTimers = false;

DOMTimer::DOMTimer(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds interval, bool oneShot)
DOMTimer::DOMTimer(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds interval, Type type)
: SuspendableTimerBase(&context)
, m_nestingLevel(context.timerNestingLevel())
, m_action(WTFMove(action))
, m_originalInterval(interval)
, m_throttleState(Undetermined)
, m_oneShot(oneShot)
, m_oneShot(type == Type::SingleShot)
, m_currentTimerInterval(intervalClampedToMinimum())
, m_userGestureTokenToForward(UserGestureIndicator::currentUserGesture())
{
if (oneShot)
if (m_oneShot)
startOneShot(m_currentTimerInterval);
else
startRepeating(m_originalInterval, m_currentTimerInterval);
}

DOMTimer::~DOMTimer() = default;

int DOMTimer::install(ScriptExecutionContext& context, std::unique_ptr<ScheduledAction> action, Seconds timeout, bool oneShot)
int DOMTimer::install(ScriptExecutionContext& context, std::unique_ptr<ScheduledAction> action, Seconds timeout, Type type)
{
auto actionFunction = [action = WTFMove(action)](ScriptExecutionContext& context) mutable {
action->execute(context);
};
return DOMTimer::install(context, WTFMove(actionFunction), timeout, oneShot);
return DOMTimer::install(context, WTFMove(actionFunction), timeout, type);
}

int DOMTimer::install(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds timeout, bool oneShot)
int DOMTimer::install(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds timeout, Type type)
{
Ref<DOMTimer> timer = adoptRef(*new DOMTimer(context, WTFMove(action), timeout, oneShot));
Ref<DOMTimer> timer = adoptRef(*new DOMTimer(context, WTFMove(action), timeout, type));
timer->suspendIfNeeded();
timer->makeOpportunisticTaskDeferralScopeIfPossible(context);

Expand All @@ -196,15 +196,15 @@ int DOMTimer::install(ScriptExecutionContext& context, Function<void(ScriptExecu
timer->m_timeoutId = context.circularSequentialID();
} while (!context.addTimeout(timer->m_timeoutId, timer.get()));

InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, oneShot);
InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, type == Type::SingleShot);

// Keep track of nested timer installs.
if (NestedTimersMap* nestedTimers = NestedTimersMap::instanceForContext(context))
nestedTimers->add(timer->m_timeoutId, timer.get());
#if ENABLE(CONTENT_CHANGE_OBSERVER)
if (is<Document>(context)) {
auto& document = downcast<Document>(context);
document.contentChangeObserver().didInstallDOMTimer(timer.get(), timeout, oneShot);
document.contentChangeObserver().didInstallDOMTimer(timer.get(), timeout, type == Type::SingleShot);
if (DeferDOMTimersForScope::isDeferring())
document.domTimerHoldingTank().add(timer.get());
}
Expand Down
9 changes: 4 additions & 5 deletions Source/WebCore/page/DOMTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class DOMTimer final : public RefCounted<DOMTimer>, public SuspendableTimerBase,
static Seconds nonInteractedCrossOriginFrameAlignmentInterval() { return 30_ms; }
static Seconds hiddenPageAlignmentInterval() { return 1_s; }

// Creates a new timer owned by specified ScriptExecutionContext, starts it
// and returns its Id.
static int install(ScriptExecutionContext&, std::unique_ptr<ScheduledAction>, Seconds timeout, bool singleShot);
static int install(ScriptExecutionContext&, Function<void(ScriptExecutionContext&)>&&, Seconds timeout, bool singleShot);
enum class Type : bool { SingleShot, Repeating };
static int install(ScriptExecutionContext&, std::unique_ptr<ScheduledAction>, Seconds timeout, Type);
static int install(ScriptExecutionContext&, Function<void(ScriptExecutionContext&)>&&, Seconds timeout, Type);
static void removeById(ScriptExecutionContext&, int timeoutId);

// Notify that the interval may need updating (e.g. because the minimum interval
Expand All @@ -66,7 +65,7 @@ class DOMTimer final : public RefCounted<DOMTimer>, public SuspendableTimerBase,
static void scriptDidInteractWithPlugin();

private:
DOMTimer(ScriptExecutionContext&, Function<void(ScriptExecutionContext&)>&&, Seconds interval, bool singleShot);
DOMTimer(ScriptExecutionContext&, Function<void(ScriptExecutionContext&)>&&, Seconds interval, Type);
friend class Internals;

WEBCORE_EXPORT Seconds intervalClampedToMinimum() const;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/page/LocalDOMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ ExceptionOr<int> LocalDOMWindow::setTimeout(std::unique_ptr<ScheduledAction> act

action->addArguments(WTFMove(arguments));

return DOMTimer::install(*context, WTFMove(action), Seconds::fromMilliseconds(timeout), true);
return DOMTimer::install(*context, WTFMove(action), Seconds::fromMilliseconds(timeout), DOMTimer::Type::SingleShot);
}

void LocalDOMWindow::clearTimeout(int timeoutId)
Expand All @@ -1925,7 +1925,7 @@ ExceptionOr<int> LocalDOMWindow::setInterval(std::unique_ptr<ScheduledAction> ac

action->addArguments(WTFMove(arguments));

return DOMTimer::install(*context, WTFMove(action), Seconds::fromMilliseconds(timeout), false);
return DOMTimer::install(*context, WTFMove(action), Seconds::fromMilliseconds(timeout), DOMTimer::Type::Repeating);
}

void LocalDOMWindow::clearInterval(int timeoutId)
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/workers/WorkerGlobalScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ ExceptionOr<int> WorkerGlobalScope::setTimeout(std::unique_ptr<ScheduledAction>

action->addArguments(WTFMove(arguments));

return DOMTimer::install(*this, WTFMove(action), Seconds::fromMilliseconds(timeout), true);
return DOMTimer::install(*this, WTFMove(action), Seconds::fromMilliseconds(timeout), DOMTimer::Type::SingleShot);
}

void WorkerGlobalScope::clearTimeout(int timeoutId)
Expand All @@ -353,7 +353,7 @@ ExceptionOr<int> WorkerGlobalScope::setInterval(std::unique_ptr<ScheduledAction>

action->addArguments(WTFMove(arguments));

return DOMTimer::install(*this, WTFMove(action), Seconds::fromMilliseconds(timeout), false);
return DOMTimer::install(*this, WTFMove(action), Seconds::fromMilliseconds(timeout), DOMTimer::Type::Repeating);
}

void WorkerGlobalScope::clearInterval(int timeoutId)
Expand Down

0 comments on commit 5e90e4c

Please sign in to comment.