From de1a805898a4210dd7632aa7706d9e498811e726 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Wed, 30 Oct 2024 21:42:05 +1300 Subject: [PATCH] LibWeb: Use HeapFunction for Platform::Timer --- .../LibWeb/CSS/StyleValues/ImageStyleValue.cpp | 2 +- .../Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 4 ++-- Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp | 4 ++-- Userland/Libraries/LibWeb/Platform/Timer.cpp | 11 +++++++++-- Userland/Libraries/LibWeb/Platform/Timer.h | 9 ++++++--- Userland/Libraries/LibWeb/Platform/TimerSerenity.cpp | 4 ++-- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 10 +++++----- Userland/Services/WebContent/ConnectionFromClient.cpp | 2 +- 8 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp index 2d91e53c3d1..1c4460c168d 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp @@ -63,7 +63,7 @@ void ImageStyleValue::load_any_resources(DOM::Document& document) if (image_data->is_animated() && image_data->frame_count() > 1) { m_timer = Platform::Timer::create(m_document->heap()); m_timer->set_interval(image_data->frame_duration(0)); - m_timer->on_timeout = [this] { animate(); }; + m_timer->on_timeout = JS::create_heap_function(m_document->heap(), [this] { animate(); }); m_timer->start(); } }, diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 7ff71dd1c5f..4ce29d19347 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -54,9 +54,9 @@ void EventLoop::visit_edges(Visitor& visitor) void EventLoop::schedule() { if (!m_system_event_loop_timer) { - m_system_event_loop_timer = Platform::Timer::create_single_shot(heap(), 0, [this] { + m_system_event_loop_timer = Platform::Timer::create_single_shot(heap(), 0, JS::create_heap_function(heap(), [this] { process(); - }); + })); } if (!m_system_event_loop_timer->is_active()) diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp index 3db78ee00aa..1ffb8a769e7 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -415,12 +415,12 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback if (timeout.has_value() && timeout.value() > 0) { auto timer = Platform::Timer::create_single_shot(m_heap, timeout.value(), nullptr); - timer->on_timeout = [timer, protocol_request, timeout_callback = move(timeout_callback)] { + timer->on_timeout = JS::create_heap_function(m_heap, [timer = JS::make_handle(timer), protocol_request, timeout_callback = move(timeout_callback)] { (void)timer; protocol_request->stop(); if (timeout_callback) timeout_callback(); - }; + }); timer->start(); } diff --git a/Userland/Libraries/LibWeb/Platform/Timer.cpp b/Userland/Libraries/LibWeb/Platform/Timer.cpp index dd1da7aa725..5f7c05f2666 100644 --- a/Userland/Libraries/LibWeb/Platform/Timer.cpp +++ b/Userland/Libraries/LibWeb/Platform/Timer.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include @@ -11,12 +12,18 @@ namespace Web::Platform { Timer::~Timer() = default; +void Timer::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(on_timeout); +} + JS::NonnullGCPtr Timer::create(JS::Heap& heap) { return EventLoopPlugin::the().create_timer(heap); } -JS::NonnullGCPtr Timer::create_repeating(JS::Heap& heap, int interval_ms, JS::SafeFunction&& timeout_handler) +JS::NonnullGCPtr Timer::create_repeating(JS::Heap& heap, int interval_ms, JS::GCPtr> timeout_handler) { auto timer = EventLoopPlugin::the().create_timer(heap); timer->set_single_shot(false); @@ -25,7 +32,7 @@ JS::NonnullGCPtr Timer::create_repeating(JS::Heap& heap, int interval_ms, return timer; } -JS::NonnullGCPtr Timer::create_single_shot(JS::Heap& heap, int interval_ms, JS::SafeFunction&& timeout_handler) +JS::NonnullGCPtr Timer::create_single_shot(JS::Heap& heap, int interval_ms, JS::GCPtr> timeout_handler) { auto timer = EventLoopPlugin::the().create_timer(heap); timer->set_single_shot(true); diff --git a/Userland/Libraries/LibWeb/Platform/Timer.h b/Userland/Libraries/LibWeb/Platform/Timer.h index f01c8a38fa6..77895b9c334 100644 --- a/Userland/Libraries/LibWeb/Platform/Timer.h +++ b/Userland/Libraries/LibWeb/Platform/Timer.h @@ -17,8 +17,8 @@ class Timer : public JS::Cell { public: static JS::NonnullGCPtr create(JS::Heap&); - static JS::NonnullGCPtr create_repeating(JS::Heap&, int interval_ms, JS::SafeFunction&& timeout_handler); - static JS::NonnullGCPtr create_single_shot(JS::Heap&, int interval_ms, JS::SafeFunction&& timeout_handler); + static JS::NonnullGCPtr create_repeating(JS::Heap&, int interval_ms, JS::GCPtr> timeout_handler); + static JS::NonnullGCPtr create_single_shot(JS::Heap&, int interval_ms, JS::GCPtr> timeout_handler); virtual ~Timer(); @@ -37,7 +37,10 @@ public: virtual bool is_single_shot() const = 0; virtual void set_single_shot(bool) = 0; - JS::SafeFunction on_timeout; + JS::GCPtr> on_timeout; + +protected: + virtual void visit_edges(JS::Cell::Visitor&) override; }; } diff --git a/Userland/Libraries/LibWeb/Platform/TimerSerenity.cpp b/Userland/Libraries/LibWeb/Platform/TimerSerenity.cpp index 81ecf2064f2..5bbc3cf5706 100644 --- a/Userland/Libraries/LibWeb/Platform/TimerSerenity.cpp +++ b/Userland/Libraries/LibWeb/Platform/TimerSerenity.cpp @@ -5,9 +5,9 @@ */ #include "TimerSerenity.h" -#include #include #include +#include namespace Web::Platform { @@ -21,7 +21,7 @@ TimerSerenity::TimerSerenity() { m_timer->on_timeout = [this] { if (on_timeout) - on_timeout(); + on_timeout->function()(); }; } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index fac578dcf88..200ebfc91ec 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -879,14 +879,14 @@ WebIDL::ExceptionOr XMLHttpRequest::send(Optionalon_timeout = [this, request, timer]() { + // NOTE: `this` and `request` is kept alive by Platform::Timer using a Handle. + timer->on_timeout = JS::create_heap_function(heap(), [this, request, timer = JS::make_handle(timer)]() { (void)timer; if (!request->done()) { m_timed_out = true; m_fetch_controller->terminate(); } - }; + }); timer->start(); } @@ -933,10 +933,10 @@ WebIDL::ExceptionOr XMLHttpRequest::send(Optionalon_timeout = [timer, &did_time_out]() { + timer->on_timeout = JS::create_heap_function(heap(), [timer = JS::make_handle(timer), &did_time_out]() { (void)timer; did_time_out = true; - }; + }); timer->start(); } diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index b8d3e231d35..a6ad03e77cc 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -58,7 +58,7 @@ ConnectionFromClient::ConnectionFromClient(JS::Heap& heap, IPC::Transport transp , m_heap(heap) , m_page_host(PageHost::create(*this)) { - m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(m_heap, 0, [this] { process_next_input_event(); }); + m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(m_heap, 0, JS::create_heap_function(heap, [this] { process_next_input_event(); })); } ConnectionFromClient::~ConnectionFromClient() = default;