Skip to content

Truly independent web browser

LibWeb: Destroy a discarded page's documents before detaching them

Aliaksandr Kalenik19fac1b4aa17
diff --git a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
index d855e13c0e3..fb0fbc4379e 100644
--- a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
@@ -310,6 +310,11 @@ void LocalTraversableNavigable::run_ui_traversable_close_unload_task()
// The UI process has already unloaded every descendant subtree.
queue_a_task(Task::Source::NavigationAndTraversal, nullptr, nullptr,
GC::create_function(heap(), [this] {
+ // NB: The UI process discards the page of a tab it let go of without waiting for this task, and the page
+ // goes without the unload then.
+ if (page().has_been_discarded())
+ return;
+
// 2. Unload document, passing along newDocument if it is not null.
if (auto document = active_document())
document->unload();
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index f799cf40566..2be87874276 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1150,15 +1150,65 @@ void Page::unfullscreen_descendant_documents(Vector<GC::Root<HTML::Navigable>> c
void Page::discard()
{
+ if (m_has_been_discarded)
+ return;
+ m_has_been_discarded = true;
+
+ // Nothing answers a dialog of a closed tab anymore, and the process's event loop stays paused while it is open.
+ close_pending_dialog_of_discarded_page();
+
// A tab closed while this page still displayed a document of it: the document goes without the unload the UI
// process runs otherwise, as the documents of a top-level traversable being destroyed do.
- for (auto const& navigable : local_roots()) {
- if (auto document = navigable->active_document())
- document->destroy_a_document_and_its_descendants();
- navigable->set_has_been_destroyed();
- navigable->remove_from_all_local_navigables();
+ auto navigables = local_roots();
+ if (navigables.is_empty()) {
+ finish_discarding();
+ return;
}
+ m_local_roots_left_to_discard = navigables.size();
+ for (auto const& navigable : navigables) {
+ auto detach_navigable = GC::create_function(heap(), [this, navigable] {
+ navigable->set_has_been_destroyed();
+ navigable->remove_from_all_local_navigables();
+ if (--m_local_roots_left_to_discard == 0)
+ finish_discarding();
+ });
+
+ // NB: The task destroying a document only runs while the document is fully active, so its navigable can only
+ // be detached after that task.
+ auto document = navigable->active_document();
+ if (document && !document->has_been_destroyed() && document->is_fully_active()) {
+ document->destroy_a_document_and_its_descendants(detach_navigable);
+ continue;
+ }
+ if (document && !document->has_been_destroyed())
+ document->destroy();
+ detach_navigable->function()();
+ }
+}
+
+void Page::close_pending_dialog_of_discarded_page()
+{
+ switch (m_pending_dialog) {
+ case PendingDialog::None:
+ break;
+ case PendingDialog::Alert:
+ alert_closed();
+ break;
+ case PendingDialog::BeforeUnload:
+ before_unload_closed(true);
+ break;
+ case PendingDialog::Confirm:
+ confirm_closed(false);
+ break;
+ case PendingDialog::Prompt:
+ prompt_closed({});
+ break;
+ }
+}
+
+void Page::finish_discarding()
+{
// The navigables of other processes this page held no longer stand for anything here, and a WindowProxy a script
// of another page holds for one of them is closed.
GC::RootVector<GC::Ref<HTML::RemoteNavigable>> remote_navigables;
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 4f171decbe4..c6cb091be5c 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -176,6 +176,7 @@ public:
GC::Ptr<HTML::LocalNavigable> fullscreen_element_and_its_containers(GC::Ref<DOM::Element>, Fullscreen::RequestType, ElementIsRequestedElement);
void discard();
+ bool has_been_discarded() const { return m_has_been_discarded; }
HTML::HistoryExecutor& history_executor();
@@ -541,6 +542,8 @@ private:
void set_find_in_page_active_match(GC::Ptr<DOM::Range>);
void on_pending_dialog_closed();
+ void close_pending_dialog_of_discarded_page();
+ void finish_discarding();
GC::Ref<PageClient> m_client;
@@ -556,6 +559,8 @@ private:
GC::Ptr<HTML::Navigable> m_top_level_traversable;
OwnPtr<Compositor::CompositorContextHandle> m_retired_page_compositor_context;
Vector<GC::Ref<HTML::Navigable>> m_navigables_being_destroyed;
+ bool m_has_been_discarded { false };
+ size_t m_local_roots_left_to_discard { 0 };
HTML::VisibilityState m_system_visibility_state { HTML::VisibilityState::Hidden };