Truly independent web browser
LibWeb: Stop rooting active platform timers
Shannon Booth766e96cc73e9
diff --git a/Libraries/LibWeb/Platform/Timer.cpp b/Libraries/LibWeb/Platform/Timer.cppindex 44905fc3923..5fbcb4418c8 100644--- a/Libraries/LibWeb/Platform/Timer.cpp+++ b/Libraries/LibWeb/Platform/Timer.cpp@@ -19,8 +19,6 @@ Timer::Timer()m_timer->on_timeout = [this] {if (on_timeout)on_timeout->function()();- if (!is_active())- m_activity_root.release();};}@@ -65,40 +63,31 @@ GC::Ref<Timer> Timer::create_single_shot(GC::Heap& heap, AK::Duration interval,void Timer::start(){m_timer->start();- m_activity_root.take(*this);}void Timer::start(AK::Duration interval){m_timer->start(interval);- m_activity_root.take(*this);}void Timer::restart(){m_timer->restart();- m_activity_root.take(*this);}void Timer::restart(AK::Duration interval){m_timer->restart(interval);- m_activity_root.take(*this);}void Timer::stop(){m_timer->stop();- m_activity_root.release();}void Timer::set_active(bool active){m_timer->set_active(active);- if (active)- m_activity_root.take(*this);- else- m_activity_root.release();}bool Timer::is_active() constdiff --git a/Libraries/LibWeb/Platform/Timer.h b/Libraries/LibWeb/Platform/Timer.hindex 4e6d23bca4a..b68904e3db4 100644--- a/Libraries/LibWeb/Platform/Timer.h+++ b/Libraries/LibWeb/Platform/Timer.h@@ -9,7 +9,6 @@#include <AK/NonnullRefPtr.h>#include <AK/Time.h>#include <LibCore/Forward.h>-#include <LibGC/ActivityRoot.h>#include <LibGC/CellAllocator.h>#include <LibJS/Heap/Cell.h>#include <LibWeb/Export.h>@@ -43,8 +42,6 @@ public:bool is_single_shot() const;void set_single_shot(bool);- bool has_activity_root() const { return m_activity_root.is_taken(); }-GC::Ptr<GC::Function<void()>> on_timeout;private:@@ -53,7 +50,6 @@ private:virtual void visit_edges(JS::Cell::Visitor&) override;NonnullRefPtr<Core::Timer> m_timer;- GC::ActivityRoot m_activity_root;};}diff --git a/Tests/LibWeb/TestWrapperWorld.cpp b/Tests/LibWeb/TestWrapperWorld.cppindex 109bd1d39ef..b6a41ce624d 100644--- a/Tests/LibWeb/TestWrapperWorld.cpp+++ b/Tests/LibWeb/TestWrapperWorld.cpp@@ -872,23 +872,32 @@ TEST_CASE(cross_origin_descriptor_cache_does_not_root_realm_after_wrapper_dies)EXPECT(!weak_realm);}-TEST_CASE(single_shot_timer_restarted_from_callback_keeps_activity_root)+// An active timer lives only as long as its owner reaches it. One that nothing reaches is collected, and must be+// stopped then rather than fire into a callback that was collected with it.+TEST_CASE(unreachable_active_timer_is_stopped_when_collected){IGNORE_USE_IN_ESCAPING_LAMBDA Core::EventLoop event_loop;auto vm = JS::VM::create();- IGNORE_USE_IN_ESCAPING_LAMBDA GC::Root<Web::Platform::Timer> timer;- timer = Web::Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), nullptr);- timer->on_timeout = GC::create_function(GC::Heap::the(), [&] {- timer->restart(AK::Duration::from_milliseconds(1000));+ IGNORE_USE_IN_ESCAPING_LAMBDA bool fired = false;+ GC::Weak<Web::Platform::Timer> weak_timer;+ [&] {+ auto timer = Web::Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), GC::create_function(GC::Heap::the(), [&] {+ fired = true;+ }));+ timer->start();+ weak_timer = *timer;+ }();++ vm->heap().collect_garbage(GC::Heap::CollectionType::CollectEverything);+ EXPECT(!weak_timer);++ auto quit_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(10), [&] {event_loop.quit(0);});-- timer->start();+ quit_timer->start();event_loop.exec();- EXPECT(timer->is_active());- EXPECT(timer->has_activity_root());- timer->stop();+ EXPECT(!fired);}TEST_CASE(extension_world_hash_cache_scales)