Skip to content

Truly independent web browser

LibWeb+LibWebView: Run finalize step 2 as its document is activated

Shannon Boothf5157531007e
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index b85b167b1c0..06a835a64ed 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -5391,11 +5391,6 @@ void Document::set_ready_for_post_load_tasks(bool ready)
m_ready_for_post_load_tasks = ready;
if (ready) {
if (auto navigable = this->navigable()) {
- // AD-HOC: Clear the navigation load event guard now that the document is ready.
- // This guard was set in finalize_a_cross_document_navigation to prevent the parent's
- // load event from firing while the about:blank was still the active document.
- navigable->clear_navigation_load_event_guard();
-
if (auto container = navigable->container())
container->document().schedule_html_parser_end_check();
else
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
index eaaf0eecf06..c7e1b2b8e43 100644
--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp
+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
@@ -732,6 +732,15 @@ void HistoryExecutor::apply_changing_navigable_history_step_continuation_impl(GC
Optional<HostedNavigableState> activated_navigable_state;
if (!update_only) {
navigable->activate_history_entry(*target_entry, *resolved_document);
+
+ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation
+ // 2. Set navigable's is delaying load events to false.
+ // AD-HOC: Not until the navigation's document is active. Before, the container document sees the displayed
+ // document instead, and an initial about:blank is ready for post-load tasks, so nothing would delay
+ // the container's load event.
+ if (continuation->pending_document)
+ navigable->set_delaying_load_events(false);
+
activated_navigable_state = navigable->hosted_state();
}
auto previous_entry_persisted_state = !update_only && previous_entry
@@ -817,9 +826,16 @@ void HistoryExecutor::run_ui_changing_navigable_history_job(CrossProcessId opera
}
auto navigable = local_navigable_with_id(navigable_id);
- if (pending_document && local_target_entry && (!navigable || !prepare_to_finalize_a_cross_document_navigation(*navigable, *pending_document))) {
- on_complete->function()(ChangingNavigableHistoryStepJobDisposition::Stale, UnloadDisplayedDocument::No);
- return;
+
+ // AD-HOC: The navigable can be destroyed, or its page be closing, before the job finalizing a navigation runs.
+ // Destruction may then have detached the pending document, or destroyed the active document, before
+ // marking the navigable destroyed.
+ if (pending_document && local_target_entry) {
+ GC::Ptr<DOM::Document> active_document = navigable ? navigable->active_document() : nullptr;
+ if (!navigable || navigable->has_been_destroyed() || pending_document->has_been_destroyed() || !pending_document->browsing_context() || !active_document || active_document->has_been_destroyed()) {
+ on_complete->function()(ChangingNavigableHistoryStepJobDisposition::Stale, UnloadDisplayedDocument::No);
+ return;
+ }
}
if (!navigable) {
on_complete->function()(ChangingNavigableHistoryStepJobDisposition::Skipped, UnloadDisplayedDocument::No);
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 46c068ae32b..1a257e7f26b 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -870,16 +870,6 @@ void LocalNavigable::stop_delaying_load_events_for_navigation(Utf16String const&
set_delaying_load_events(false);
}
-void LocalNavigable::set_navigation_load_event_guard(DOM::Document& parent_doc)
-{
- m_navigation_load_event_guard.emplace(parent_doc);
-}
-
-void LocalNavigable::clear_navigation_load_event_guard()
-{
- m_navigation_load_event_guard.clear();
-}
-
RefPtr<SessionHistoryEntry> LocalNavigable::active_session_history_entry() const
{
return m_active_session_history_entry;
@@ -1182,11 +1172,8 @@ void LocalNavigable::activate_history_entry(RefPtr<SessionHistoryEntry> entry, G
// their loading lifecycle before being activated (when they had no navigable).
// Re-trigger the post-load steps that were skipped:
// - completely_finish_loading: fires the iframe load event on the container
- // - clear_navigation_load_event_guard: clears the parent's load event delayer
- // that was set in finalize_a_cross_document_navigation
// - schedule_html_parser_end_check: allows the parent's parser end state to progress
if (new_document->ready_for_post_load_tasks()) {
- clear_navigation_load_event_guard();
if (auto nav_container = container())
nav_container->document().schedule_html_parser_end_check();
}
@@ -4315,33 +4302,6 @@ bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& docu
return url.scheme() == "javascript"sv || document.is_initial_about_blank();
}
-// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation
-// NB: The UI process runs these steps. Its job activating historyEntry's document begins here, with the parts that need
-// the live navigable and Document.
-bool prepare_to_finalize_a_cross_document_navigation(LocalNavigable& navigable, DOM::Document& pending_document)
-{
- // The steps can run after the navigable was destroyed, or after its page has started closing. In that case the
- // navigable may not have been marked destroyed yet, while destruction has already detached the pending document
- // from its browsing context or destroyed the active document. There is no live navigation left to finalize.
- auto active_document = navigable.active_document();
- if (navigable.has_been_destroyed() || pending_document.has_been_destroyed() || !pending_document.browsing_context() || !active_document || active_document->has_been_destroyed()) {
- navigable.set_delaying_load_events(false);
- return false;
- }
-
- // AD-HOC: Without this guard, decrementing the navigable's delay counter triggers schedule_load_event_delay_check
- // on the parent, which can see the about:blank (ready_for_post_load_tasks=true) before the session
- // history traversal activates the new document. The guard is cleared when the new document becomes ready
- // for post-load tasks (via set_ready_for_post_load_tasks).
- if (auto container_document = navigable.container_document())
- navigable.set_navigation_load_event_guard(*container_document);
-
- // 2. Set navigable's is delaying load events to false.
- navigable.set_delaying_load_events(false);
-
- return true;
-}
-
class CheckUnloadingCanceledState : public GC::Cell {
GC_CELL(CheckUnloadingCanceledState, GC::Cell);
GC_DECLARE_ALLOCATOR(CheckUnloadingCanceledState);
@@ -4615,7 +4575,12 @@ void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable> navigable, His
.pending_document = pending_document,
.local_target_navigable_id = navigable->id(),
.local_target_entry = history_entry,
- .on_complete = GC::create_function(navigable->heap(), [navigable](HistoryStepResult) {
+ .on_complete = GC::create_function(navigable->heap(), [navigable, navigation_id, has_document = !!pending_document](HistoryStepResult) {
+ // AD-HOC: Step 2 runs as the navigation's document is activated. When the steps end without activating
+ // it, the navigation is dropped, and stops delaying load events.
+ if (has_document && !navigable->has_been_destroyed() && navigable->is_delaying_load_events())
+ navigable->stop_delaying_load_events_for_navigation(navigation_id);
+
// AD-HOC: Trigger a relayout in the container document for size negotiation with SVG documents.
if (auto container = navigable->container())
container->set_needs_layout_update(DOM::SetNeedsLayoutReason::FinalizeACrossDocumentNavigation);
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index d4c97091bbb..e53698c1123 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -117,9 +117,6 @@ public:
void stop_delaying_load_events_for_navigation(Utf16String const& navigation_id);
bool is_delaying_load_events() const { return m_is_delaying_load_events; }
- void set_navigation_load_event_guard(DOM::Document& parent_doc);
- void clear_navigation_load_event_guard();
-
RefPtr<SessionHistoryEntry> active_session_history_entry() const;
void set_active_session_history_entry(RefPtr<SessionHistoryEntry>);
RefPtr<SessionHistoryEntry> current_session_history_entry() const;
@@ -575,9 +572,6 @@ private:
bool m_is_delaying_load_events { false };
Optional<DOM::DocumentLoadEventDelayer> m_container_document_load_event_delayer;
- // AD-HOC: Guards the parent document's load event delay count during cross-document navigation.
- Optional<DOM::DocumentLoadEventDelayer> m_navigation_load_event_guard;
-
NavigationObserver::NavigationObserversList m_navigation_observers;
bool m_has_been_destroyed { false };
@@ -724,7 +718,6 @@ WEB_API HashTable<GC::RawRef<LocalNavigable>>& all_local_navigables();
WEB_API GC::Ptr<LocalNavigable> local_navigable_with_id(CrossProcessId);
bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& document);
-bool prepare_to_finalize_a_cross_document_navigation(LocalNavigable&, DOM::Document& pending_document);
void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable>, HistoryHandlingBehavior, UserNavigationInvolvement, NonnullRefPtr<SessionHistoryEntry>, GC::Ptr<DOM::Document> pending_document, Utf16String navigation_id);
WEB_API void check_if_unloading_is_canceled(Vector<GC::Root<LocalNavigable>> navigables_that_need_before_unload, GC::Ptr<LocalTraversableNavigable> traversable, RefPtr<SessionHistoryEntry> target_entry, Optional<UserNavigationInvolvement> user_involvement_for_navigate_events, UnloadPromptShown, GC::Ref<GC::Function<void(CheckIfUnloadingIsCanceledResult, UnloadPromptShown)>> callback);
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cpp
index 8521dc1dad9..08aeb538787 100644
--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp
+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp
@@ -320,12 +320,6 @@ void NavigableContainer::destroy_the_child_navigable()
// block the parent document's load event indefinitely.
local_navigable->set_delaying_load_events(false);
- // AD-HOC: Clear the navigation load event guard that may have been set by
- // finalize_a_cross_document_navigation. Without this, the guard's
- // DocumentLoadEventDelayer on the parent document persists until GC,
- // blocking the parent's load event indefinitely.
- local_navigable->clear_navigation_load_event_guard();
-
// 4. Inform the navigation API about child navigable destruction given navigable.
local_navigable->inform_the_navigation_api_about_child_navigable_destruction();
@@ -434,7 +428,6 @@ void NavigableContainer::swap_content_navigable_to_remote(Badge<Page>, Replicate
local_navigable.set_container({}, nullptr);
local_navigable.set_delaying_load_events(false);
- local_navigable.clear_navigation_load_event_guard();
local_navigable.set_has_been_destroyed();
local_navigable.remove_from_all_local_navigables();
}
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index 32cedda99ae..01ea36d3730 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -2656,7 +2656,7 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation
// 2. Set navigable's is delaying load events to false.
// NB: The page that populated historyEntry's document performs this step: when it reports that there is no
- // document, or as its job for these steps begins.
+ // document, or as it activates the document.
// 3. If historyEntry's document is null, then return.
// NB: The document populated for historyEntry waits in its document state until historyEntry is activated.