Skip to content

Truly independent web browser

LibWeb: Destroy navigation documents dropped before activation

Shannon Booth4cb23d976f7f
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 93f038a5754..7d37442d6af 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -6132,6 +6132,18 @@ void Document::decrement_number_of_pending_style_sheet_requests(Badge<DocumentLo
page().client().request_frame();
}
+// AD-HOC: A navigation can drop the document it created before activating it. Its parser and fetch go on queueing
+// tasks for it that can never run, and keep it alive. A document reusing an initial about:blank's window
+// shares that window with the active document, so it is left alone.
+void Document::destroy_if_dropped_before_activation()
+{
+ if (m_has_been_destroyed || is_active())
+ return;
+ if (m_browsing_context && m_browsing_context->active_window() == window().ptr())
+ return;
+ destroy();
+}
+
void Document::set_html_parser_end_state(GC::Ptr<HTML::HTMLParserEndState> state)
{
m_html_parser_end_state = state;
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index f457d9c3ce2..cddaf9f5e0c 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1188,6 +1188,7 @@ public:
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#destroy-a-document
void destroy();
+ void destroy_if_dropped_before_activation();
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#destroy-a-document-and-its-descendants
void destroy_a_document_and_its_descendants(GC::Ptr<GC::Function<void()>> after_all_destruction = {});
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
index 8f038f672be..9c0ef0ca7c2 100644
--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp
+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
@@ -121,6 +121,21 @@ void HistoryExecutor::complete_ui_history_operation(CrossProcessId operation_id,
auto local_target_navigable_id = operation->value.local_target_navigable_id;
auto local_target_entry = move(operation->value.local_target_entry);
auto on_complete = operation->value.on_complete;
+
+ // AD-HOC: A canceled or stale operation drops the documents it populated without activating them.
+ GC::RootVector<GC::Ref<DOM::Document>> populated_documents;
+ auto append_populated_document = [&](GC::Ptr<DOM::Document> document) {
+ if (document)
+ populated_documents.append(*document);
+ };
+ append_populated_document(operation->value.pending_document);
+ for (auto const& job : operation->value.changing_navigable_jobs) {
+ if (!job.value.continuation)
+ continue;
+ append_populated_document(job.value.continuation->pending_document);
+ if (auto population_output = job.value.continuation->population_output)
+ append_populated_document(population_output->document);
+ }
m_history_operations.remove(operation);
// AD-HOC: A canceled or stale operation can leave a claimed navigable whose continuation was never applied.
@@ -129,6 +144,9 @@ void HistoryExecutor::complete_ui_history_operation(CrossProcessId operation_id,
navigable->clear_ongoing_history_traversal();
}
+ for (auto& document : populated_documents)
+ document->destroy_if_dropped_before_activation();
+
if (target_entry_step.has_value()
&& local_target_navigable_id.has_value()
&& local_target_entry
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index dedc92d0639..f658991b63d 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -1026,6 +1026,8 @@ void LocalNavigable::continue_navigation_at_population(NavigationPopulationReque
GC::create_function(navigable->heap(), [navigable, history_entry, history_handling = request.history_handling, navigation_id = request.navigation_id, user_involvement = request.user_involvement](GC::Ptr<PopulateSessionHistoryEntryDocumentOutput> output) mutable {
// AD-HOC: This check is not in the spec but we should not continue navigation if ongoing navigation id has changed.
if (navigable->ongoing_navigation() != navigation_id) {
+ if (output && output->document)
+ output->document->destroy_if_dropped_before_activation();
navigable->stop_delaying_load_events_for_navigation(navigation_id);
navigable->page().client().navigation_population_failed(navigable->id(), navigation_id);
return;
@@ -4422,6 +4424,8 @@ void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable> navigable, His
{
// NOTE: This is not in the spec but we should not navigate destroyed navigable.
if (navigable->has_been_destroyed()) {
+ if (pending_document)
+ pending_document->destroy_if_dropped_before_activation();
navigable->set_delaying_load_events(false);
navigable->page().client().navigation_population_failed(navigable->id(), navigation_id);
return;