From ae71e5f99b58573c84642f827fe70c3c2257a9d7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 Oct 2021 16:07:21 +0200 Subject: [PATCH] LibWeb: Let HTML::EventLoop keep track of live DOM::Document objects This will be used by the event loop processing model. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 6 +++++ .../LibWeb/HTML/EventLoop/EventLoop.cpp | 22 +++++++++++++++++++ .../LibWeb/HTML/EventLoop/EventLoop.h | 8 +++++++ 3 files changed, 36 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 393c7f3ddb..57a6f25e24 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +71,8 @@ Document::Document(const AK::URL& url) , m_implementation(DOMImplementation::create(*this)) , m_history(HTML::History::create(*this)) { + HTML::main_thread_event_loop().register_document({}, *this); + m_style_update_timer = Core::Timer::create_single_shot(0, [this] { update_style(); }); @@ -131,6 +134,9 @@ void Document::removed_last_ref() m_in_removed_last_ref = false; m_deletion_has_begun = true; + + HTML::main_thread_event_loop().unregister_document({}, *this); + delete this; } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 3989fc4b0d..8a83844d55 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace Web::HTML { @@ -242,4 +243,25 @@ void EventLoop::perform_a_microtask_checkpoint() m_performing_a_microtask_checkpoint = false; } +NonnullRefPtrVector EventLoop::documents_in_this_event_loop() const +{ + NonnullRefPtrVector documents; + for (auto& document : m_documents) { + VERIFY(document); + documents.append(*document); + } + return documents; +} + +void EventLoop::register_document(Badge, DOM::Document& document) +{ + m_documents.append(&document); +} + +void EventLoop::unregister_document(Badge, DOM::Document& document) +{ + bool did_remove = m_documents.remove_first_matching([&](auto& entry) { return entry.ptr() == &document; }); + VERIFY(did_remove); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h index 50b9246554..b3ebdd5e07 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -49,6 +50,11 @@ public: void perform_a_microtask_checkpoint(); + void register_document(Badge, DOM::Document&); + void unregister_document(Badge, DOM::Document&); + + NonnullRefPtrVector documents_in_this_event_loop() const; + private: Type m_type { Type::Window }; @@ -64,6 +70,8 @@ private: // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint bool m_performing_a_microtask_checkpoint { false }; + + Vector> m_documents; }; EventLoop& main_thread_event_loop();