Skip to content

Truly independent web browser

LibWeb: Keep the callback of a timer in a GC::Function

Andreas Klingf2fe68788456
diff --git a/Libraries/LibWeb/HTML/Timer.cpp b/Libraries/LibWeb/HTML/Timer.cpp
index 8f7498961a4..f5e75ea1626 100644
--- a/Libraries/LibWeb/HTML/Timer.cpp
+++ b/Libraries/LibWeb/HTML/Timer.cpp
@@ -12,27 +12,30 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(Timer);
-GC::Ref<Timer> Timer::create(i32 milliseconds, Function<void()> callback, i32 id, Repeating repeating, TimerThrottlingClass throttling_class, double deadline)
+GC::Ref<Timer> Timer::create(i32 milliseconds, Callback callback, i32 id, Repeating repeating, TimerThrottlingClass throttling_class, double deadline)
{
- return GC::Heap::the().allocate<Timer>(milliseconds, move(callback), id, repeating, throttling_class, deadline);
+ return GC::Heap::the().allocate<Timer>(milliseconds, callback, id, repeating, throttling_class, deadline);
}
-Timer::Timer(i32 milliseconds, Function<void()> callback, i32 id, Repeating repeating, TimerThrottlingClass throttling_class, double deadline)
- : m_id(id)
+Timer::Timer(i32 milliseconds, Callback callback, i32 id, Repeating repeating, TimerThrottlingClass throttling_class, double deadline)
+ : m_callback(callback)
+ , m_id(id)
, m_throttling_class(throttling_class)
, m_deadline(deadline)
, m_earliest_firing(MonotonicTime::now() + AK::Duration::from_milliseconds(milliseconds))
{
+ // NB: The timer belongs to this cell, which outlives it, as it is never finalized while the timer is active.
+ auto on_timeout = [this] { (*m_callback)(); };
if (repeating == Repeating::Yes)
- m_timer = Core::Timer::create_repeating(AK::Duration::from_milliseconds(milliseconds), move(callback));
+ m_timer = Core::Timer::create_repeating(AK::Duration::from_milliseconds(milliseconds), move(on_timeout));
else
- m_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(milliseconds), move(callback));
+ m_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(milliseconds), move(on_timeout));
}
void Timer::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
- visitor.visit_possible_values(m_timer->on_timeout.raw_capture_range());
+ visitor.visit(m_callback);
}
void Timer::finalize()
@@ -53,7 +56,7 @@ void Timer::stop()
void Timer::fire_now()
{
- m_timer->on_timeout();
+ (*m_callback)();
}
void Timer::restart(i32 milliseconds)
@@ -79,11 +82,6 @@ bool Timer::is_active() const
return m_timer->is_active();
}
-void Timer::set_callback(Function<void()> callback)
-{
- m_timer->on_timeout = move(callback);
-}
-
void Timer::set_interval(i32 milliseconds)
{
if (m_timer->interval() != AK::Duration::from_milliseconds(milliseconds))
diff --git a/Libraries/LibWeb/HTML/Timer.h b/Libraries/LibWeb/HTML/Timer.h
index f6e1c53d100..5634233bc70 100644
--- a/Libraries/LibWeb/HTML/Timer.h
+++ b/Libraries/LibWeb/HTML/Timer.h
@@ -37,7 +37,9 @@ public:
Yes,
};
- static GC::Ref<Timer> create(i32 milliseconds, Function<void()> callback, i32 id, Repeating, TimerThrottlingClass, double deadline);
+ using Callback = GC::Ref<GC::Function<void()>>;
+
+ static GC::Ref<Timer> create(i32 milliseconds, Callback, i32 id, Repeating, TimerThrottlingClass, double deadline);
void start();
void stop();
@@ -47,7 +49,7 @@ public:
// Runs what the timer runs when it fires, without waiting for the clock.
void fire_now();
- void set_callback(Function<void()>);
+ void set_callback(Callback callback) { m_callback = callback; }
void set_interval(i32 milliseconds);
TimerThrottlingClass throttling_class() const { return m_throttling_class; }
@@ -65,12 +67,13 @@ public:
bool has_queued_firing() const { return m_has_queued_firing; }
private:
- Timer(i32 milliseconds, Function<void()> callback, i32 id, Repeating, TimerThrottlingClass, double deadline);
+ Timer(i32 milliseconds, Callback, i32 id, Repeating, TimerThrottlingClass, double deadline);
virtual void visit_edges(Cell::Visitor&) override;
virtual void finalize() override;
RefPtr<Core::Timer> m_timer;
+ Callback m_callback;
i32 m_id { 0 };
TimerThrottlingClass m_throttling_class { TimerThrottlingClass::Delayed };
double m_deadline { 0 };
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index a432ef9f7b5..2be8d393baa 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -1547,15 +1547,14 @@ void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout,
// NB: The intensive tier paces chained timers by their wake-ups (see throttled_timer_delay()), so note when one
// runs while the document is hidden.
- Function<void()> timer_callback;
+ Timer::Callback timer_callback = completion_step;
if (throttling_class == TimerThrottlingClass::Chained) {
- timer_callback = [this, completion_step] {
- if (timers_are_throttled())
- m_last_chained_timer_wake_up = HighResolutionTime::unsafe_shared_current_time();
- completion_step->function()();
- };
- } else {
- timer_callback = [completion_step] { completion_step->function()(); };
+ timer_callback = GC::create_function(GC::Heap::the(), GC::captures(this_impl(), completion_step), [](auto& global, auto& completion_step) {
+ auto& scope = relevant_window_or_worker_global_scope(global);
+ if (scope.timers_are_throttled())
+ scope.m_last_chained_timer_wake_up = HighResolutionTime::unsafe_shared_current_time();
+ (*completion_step)();
+ });
}
// NB: We deviate from the spec here slightly by reusing existing timers if a timer_key is provided.
@@ -1564,7 +1563,7 @@ void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout,
auto result = m_timers.get(timer_key.value());
if (result.has_value()) {
existing_timer = result.value().ptr();
- existing_timer->set_callback(move(timer_callback));
+ existing_timer->set_callback(timer_callback);
existing_timer->set_throttling_class(throttling_class);
existing_timer->set_deadline(deadline);
// A throttled delay runs from now to the wake-up, so the timer restarts on it; unthrottled, a repeating
@@ -1586,7 +1585,7 @@ void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout,
// completed. The task callback still calls run_timer_initialization_steps to update nesting
// levels and potentially clamp the interval.
auto repeating = repeat == Repeat::Yes ? Timer::Repeating::Yes : Timer::Repeating::No;
- auto timer = existing_timer ? GC::Ref { *existing_timer } : Timer::create(throttled_delay.value_or(timeout), move(timer_callback), timer_key.value(), repeating, throttling_class, deadline);
+ auto timer = existing_timer ? GC::Ref { *existing_timer } : Timer::create(throttled_delay.value_or(timeout), timer_callback, timer_key.value(), repeating, throttling_class, deadline);
// FIXME: 4. Set global's map of active timers[timerKey] to startTime plus milliseconds.
m_timers.set(timer_key.value(), timer);