diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-04-21 12:13:04 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-21 20:44:47 +0100 |
commit | 6d93e03211795d8285d7bbd3a8e1b6fe458262e8 (patch) | |
tree | 1b7dfa81817e5a148595412a20f161072dbddbca | |
parent | 892470a91228e01d973230cd76327bbf5187319e (diff) | |
download | serenity-6d93e03211795d8285d7bbd3a8e1b6fe458262e8.zip |
LibWeb+Browser+Ladybird: Use JS::SafeFunction for EventLoop callbacks
This automatically protects captured objects from being GC'd before the
callback runs.
11 files changed, 19 insertions, 19 deletions
diff --git a/Ladybird/EventLoopPluginQt.cpp b/Ladybird/EventLoopPluginQt.cpp index b1c58332b2..766447cf08 100644 --- a/Ladybird/EventLoopPluginQt.cpp +++ b/Ladybird/EventLoopPluginQt.cpp @@ -18,13 +18,13 @@ namespace Ladybird { EventLoopPluginQt::EventLoopPluginQt() = default; EventLoopPluginQt::~EventLoopPluginQt() = default; -void EventLoopPluginQt::spin_until(Function<bool()> goal_condition) +void EventLoopPluginQt::spin_until(JS::SafeFunction<bool()> goal_condition) { while (!goal_condition()) QCoreApplication::processEvents(QEventLoop::ProcessEventsFlag::AllEvents | QEventLoop::ProcessEventsFlag::WaitForMoreEvents); } -void EventLoopPluginQt::deferred_invoke(Function<void()> function) +void EventLoopPluginQt::deferred_invoke(JS::SafeFunction<void()> function) { VERIFY(function); QTimer::singleShot(0, [function = move(function)] { diff --git a/Ladybird/EventLoopPluginQt.h b/Ladybird/EventLoopPluginQt.h index 1c3751fe98..db3c0300b9 100644 --- a/Ladybird/EventLoopPluginQt.h +++ b/Ladybird/EventLoopPluginQt.h @@ -15,8 +15,8 @@ public: EventLoopPluginQt(); virtual ~EventLoopPluginQt() override; - virtual void spin_until(Function<bool()> goal_condition) override; - virtual void deferred_invoke(Function<void()>) override; + virtual void spin_until(JS::SafeFunction<bool()> goal_condition) override; + virtual void deferred_invoke(JS::SafeFunction<void()>) override; virtual NonnullRefPtr<Web::Platform::Timer> create_timer() override; virtual void quit() override; }; diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index eb1dca8ff8..6880ab4f2a 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -35,7 +35,7 @@ public: struct CustomData { virtual ~CustomData() = default; - virtual void spin_event_loop_until(Function<bool()> goal_condition) = 0; + virtual void spin_event_loop_until(JS::SafeFunction<bool()> goal_condition) = 0; }; static ErrorOr<NonnullRefPtr<VM>> create(OwnPtr<CustomData> = {}); diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp index 25c7394b34..32022c560d 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -499,7 +499,7 @@ NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Fu return realm_execution_context; } -void WebEngineCustomData::spin_event_loop_until(Function<bool()> goal_condition) +void WebEngineCustomData::spin_event_loop_until(JS::SafeFunction<bool()> goal_condition) { Platform::EventLoopPlugin::the().spin_until(move(goal_condition)); } diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h index 149211f556..e3605f8f90 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h @@ -38,7 +38,7 @@ struct CustomElementReactionsStack { struct WebEngineCustomData final : public JS::VM::CustomData { virtual ~WebEngineCustomData() override = default; - virtual void spin_event_loop_until(Function<bool()> goal_condition) override; + virtual void spin_event_loop_until(JS::SafeFunction<bool()> goal_condition) override; HTML::EventLoop event_loop; diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp index 97eb40bc23..b3305128f0 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp @@ -56,9 +56,9 @@ void PendingResponse::run_callback() { VERIFY(m_callback); VERIFY(m_response); - Platform::EventLoopPlugin::the().deferred_invoke([strong_this = JS::make_handle(*this)] { - strong_this->m_callback(*strong_this->m_response); - strong_this->m_request->remove_pending_response({}, *strong_this.ptr()); + Platform::EventLoopPlugin::the().deferred_invoke([this] { + m_callback(*m_response); + m_request->remove_pending_response({}, *this); }); } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 6cddeab5f8..35f41979d1 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -51,7 +51,7 @@ EventLoop& main_thread_event_loop() } // https://html.spec.whatwg.org/multipage/webappapis.html#spin-the-event-loop -void EventLoop::spin_until(Function<bool()> goal_condition) +void EventLoop::spin_until(JS::SafeFunction<bool()> goal_condition) { // FIXME: 1. Let task be the event loop's currently running task. diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h index 34cdecfb7c..63eb03224b 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h @@ -37,7 +37,7 @@ public: TaskQueue& microtask_queue() { return m_microtask_queue; } TaskQueue const& microtask_queue() const { return m_microtask_queue; } - void spin_until(Function<bool()> goal_condition); + void spin_until(JS::SafeFunction<bool()> goal_condition); void process(); // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level diff --git a/Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h b/Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h index 9824fed4aa..6101a1728f 100644 --- a/Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h +++ b/Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Forward.h> +#include <LibJS/SafeFunction.h> #include <LibWeb/Forward.h> namespace Web::Platform { @@ -18,8 +19,8 @@ public: virtual ~EventLoopPlugin(); - virtual void spin_until(Function<bool()> goal_condition) = 0; - virtual void deferred_invoke(Function<void()>) = 0; + virtual void spin_until(JS::SafeFunction<bool()> goal_condition) = 0; + virtual void deferred_invoke(JS::SafeFunction<void()>) = 0; virtual NonnullRefPtr<Timer> create_timer() = 0; virtual void quit() = 0; }; diff --git a/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.cpp b/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.cpp index 7a2246a30c..f087f50b54 100644 --- a/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.cpp +++ b/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.cpp @@ -5,7 +5,6 @@ */ #include "EventLoopPluginSerenity.h" -#include <AK/Function.h> #include <AK/NonnullRefPtr.h> #include <LibCore/EventLoop.h> #include <LibWeb/Platform/TimerSerenity.h> @@ -15,12 +14,12 @@ namespace Web::Platform { EventLoopPluginSerenity::EventLoopPluginSerenity() = default; EventLoopPluginSerenity::~EventLoopPluginSerenity() = default; -void EventLoopPluginSerenity::spin_until(Function<bool()> goal_condition) +void EventLoopPluginSerenity::spin_until(JS::SafeFunction<bool()> goal_condition) { Core::EventLoop::current().spin_until(move(goal_condition)); } -void EventLoopPluginSerenity::deferred_invoke(Function<void()> function) +void EventLoopPluginSerenity::deferred_invoke(JS::SafeFunction<void()> function) { VERIFY(function); Core::deferred_invoke(move(function)); diff --git a/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.h b/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.h index 0479b91e5a..c79e72a074 100644 --- a/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.h +++ b/Userland/Libraries/LibWeb/Platform/EventLoopPluginSerenity.h @@ -15,8 +15,8 @@ public: EventLoopPluginSerenity(); virtual ~EventLoopPluginSerenity() override; - virtual void spin_until(Function<bool()> goal_condition) override; - virtual void deferred_invoke(Function<void()>) override; + virtual void spin_until(JS::SafeFunction<bool()> goal_condition) override; + virtual void deferred_invoke(JS::SafeFunction<void()>) override; virtual NonnullRefPtr<Timer> create_timer() override; virtual void quit() override; }; |