Truly independent web browser
LibWeb+LibWebView: Navigate a child before the UI admits its creation
Shannon Boothf0c8ccc9e444
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 350a901304c..ba0e46f9c15 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1245,7 +1245,7 @@ WebIDL::ExceptionOr<Document*> Document::open(Optional<Utf16String> const&, Optireturn this;// 8. If document's node navigable is non-null and document's node navigable's ongoing navigation is a navigation ID, then stop loading document's node navigable.- // AD-HOC: Pending navigations can also sit in m_pending_navigations, so we need to cancel those too.+ // AD-HOC: A navigation can also wait for a traversal or for its population, so we need to cancel those too.if (auto navigable = this->navigable()) {navigable->clear_pending_navigations();if (navigable->ongoing_navigation().has<Utf16String>())diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cppindex 46ed8295b20..ab314ea10ba 100644--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp@@ -274,14 +274,6 @@ bool HistoryExecutor::run_changing_navigable_history_step_job_impl(ChangingNavigon_complete->function()({ ChangingNavigableHistoryStepJobDisposition::Skipped, nullptr });return false;}- // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-child-navigable- // NB: The creation/destruction update is the bookkeeping step after the child's nested history has been attached to its canonical- // parent document state. If the container's requested navigation has already started, it owns the ongoing navigation ID and- // eventual document activation.- if (!job.navigation_type.has_value() && navigable->ongoing_navigation().has<Utf16String>()) {- on_complete->function()({ ChangingNavigableHistoryStepJobDisposition::Skipped, nullptr });- return false;- }// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.// NB: The UI coordinator already ran that step against canonical history and sent the selected entry with this@@ -306,17 +298,18 @@ bool HistoryExecutor::run_changing_navigable_history_step_job_impl(ChangingNavig// navigable was created or destroyed — which no other engine does. So we skip such applying-the-target-// entry-would-cross-documents navigables here.// https://github.com/whatwg/html/issues/12724+ // Nor do we take a navigable from a navigation that has already started: its container's requested+ // navigation owns the ongoing navigation ID and eventual document activation.// A navigable still awaiting the update for its creation has only its initial about:blank, and populates- // the entry its nested history kept.+ // the entry its nested history kept, which supersedes its container's navigation.if (!job.navigation_type.has_value()) {bool would_cross_documents = claimed_target_entry->document_state()->document_id() != navigable->active_document_id()|| claimed_target_entry->document_state()->reload_pending();- if (would_cross_documents && navigable->has_session_history_entry_and_ready_for_navigation()) {+ bool restores_kept_entry = would_cross_documents && !navigable->has_session_history_entry_and_ready_for_navigation();+ if (!restores_kept_entry && (would_cross_documents || navigable->ongoing_navigation().has<Utf16String>())) {on_complete->function()({ ChangingNavigableHistoryStepJobDisposition::Skipped, nullptr });return false;}- if (would_cross_documents)- navigable->drop_container_navigation_for_restored_entry();}// https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-traverse-navigate-eventdiff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 0466ea345c3..72ff67d2b5c 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -783,8 +783,8 @@ void LocalNavigable::visit_edges(Cell::Visitor& visitor)visitor.visit(m_input_method_composition_node);m_event_handler.visit_edges(visitor);- for (auto& navigation : m_pending_navigations)- navigation.visit_edges(visitor);+ if (m_navigation_waiting_for_traversal.has_value())+ m_navigation_waiting_for_traversal->visit_edges(visitor);for (auto& parked : m_navigations_parked_for_population) {if (parked.navigation.has_value())parked.navigation->visit_edges(visitor);@@ -1660,11 +1660,10 @@ void LocalNavigable::set_ongoing_navigation_without_informing_navigation_api(Varnavigation_observer.ongoing_navigation_changed()->function()();}- // AD-HOC: If we just finished a traversal and there are navigations that were deferred because the traversal was- // ongoing, process them now. A freshly-created child navigable can also have pending navigations while- // its initial session history entry is being installed, so only drain once both gates are open.- if (was_traversal && !ongoing_navigation.has<Traversal>() && m_has_session_history_entry_and_ready_for_navigation)- process_pending_navigations();+ // AD-HOC: If we just finished a traversal and a navigation was deferred because the traversal was ongoing, begin+ // it now.+ if (was_traversal && !ongoing_navigation.has<Traversal>())+ begin_navigation_waiting_for_traversal();}void LocalNavigable::clear_ongoing_history_traversal()@@ -1677,16 +1676,9 @@ void LocalNavigable::clear_ongoing_history_traversal()set_ongoing_navigation_without_informing_navigation_api({});}-void LocalNavigable::queue_pending_navigation(PreparedNavigation navigation, PendingNavigationBehavior behavior)-{- if (behavior == PendingNavigationBehavior::Replace)- m_pending_navigations.clear();- m_pending_navigations.append(move(navigation));-}-void LocalNavigable::clear_pending_navigations(){- m_pending_navigations.clear();+ m_navigation_waiting_for_traversal.clear();if (m_navigations_parked_for_population.is_empty())return;m_navigations_parked_for_population.clear();@@ -1721,20 +1713,11 @@ Optional<LocalNavigable::NavigationParkedForPopulation> LocalNavigable::take_navreturn m_navigations_parked_for_population.take(*index);}-void LocalNavigable::process_pending_navigations()+void LocalNavigable::begin_navigation_waiting_for_traversal(){- if (!m_has_session_history_entry_and_ready_for_navigation || ongoing_navigation().has<Traversal>())+ if (!m_navigation_waiting_for_traversal.has_value() || ongoing_navigation().has<Traversal>())return;-- while (!m_pending_navigations.is_empty())- begin_navigation(m_pending_navigations.take_first());-}--// The entry a child navigable's nested history kept is restored in place of the navigation its container started.-void LocalNavigable::drop_container_navigation_for_restored_entry()-{- if (!m_pending_navigations.is_empty())- m_pending_navigations.remove(0);+ begin_navigation(m_navigation_waiting_for_traversal.release_value());}// https://html.spec.whatwg.org/multipage/document-sequences.html#the-rules-for-choosing-a-navigable@@ -3362,17 +3345,6 @@ WebIDL::ExceptionOr<void> LocalNavigable::continue_navigation_in_active_documentif (!active_window())return {};- // AD-HOC: A child navigable's session history entry exists canonically only once the UI process has admitted- // the creation operation, so navigations that arrive before that acknowledgment queue until it lands.- // Top-level traversables are marked ready at creation and never queue here. Keep the values snapshotted- // by steps 1-7 so the eventual continuation starts at step 8.- // A javascript: URL runs against the active document, and the UI process orders any document it creates- // after the creation operation, so it queues its task now unless an earlier navigation is still queued.- if (!m_has_session_history_entry_and_ready_for_navigation && (navigation.url.scheme() != "javascript"sv || has_pending_navigations())) {- queue_pending_navigation(move(navigation), PendingNavigationBehavior::Append);- return {};- }-begin_navigation(move(navigation));return {};}@@ -3653,7 +3625,7 @@ void LocalNavigable::begin_navigation(PreparedNavigation navigation)auto initiator_origin_snapshot = navigation.initiator_origin_snapshot;auto initiator_base_url_snapshot = navigation.initiator_base_url_snapshot;- // Keep the ID in the prepared navigation in case step 18 queues it behind an ongoing traversal.+ // Keep the ID in the prepared navigation in case step 18 defers it behind an ongoing traversal.auto navigation_id = navigation.navigation_id;// 9. If navigable's active document's unload counter is greater than 0,@@ -3737,7 +3709,7 @@ void LocalNavigable::begin_navigation(PreparedNavigation navigation)// instead so UI-initiated navigations that race the tail end of a previous load are not dropped.// Match Chromium, WebKit, and Gecko's observable behavior by letting the newest navigation win.// See https://github.com/whatwg/html/issues/12581.- queue_pending_navigation(move(navigation), PendingNavigationBehavior::Replace);+ m_navigation_waiting_for_traversal = move(navigation);// 2. Return.return;@@ -4831,7 +4803,7 @@ GC::Ref<LocalNavigable> LocalNavigable::create_stand_in(Badge<Page> badge, Remotnavigable->set_parent_compositor_context(as<RemoteNavigable>(*parent_navigable).compositor_context_id());}- // The UI process appended the navigable's session history entry to the traversable before choosing this process.+ // The UI process orders any history step for the navigable after the update for its creation.navigable->set_has_session_history_entry_and_ready_for_navigation();return navigable;}@@ -6727,7 +6699,6 @@ void LocalNavigable::set_has_session_history_entry_and_ready_for_navigation(){m_has_session_history_entry_and_ready_for_navigation = true;report_state_to_remote_container();- process_pending_navigations();}void LocalNavigable::clear_parent_compositor_context()diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex 9b56ce9a32d..99fc2bd14ea 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -298,10 +298,9 @@ public:void inform_the_navigation_api_about_child_navigable_destruction();- bool has_pending_navigations() const { return !m_pending_navigations.is_empty() || !m_navigations_parked_for_population.is_empty(); }+ bool has_pending_navigations() const { return m_navigation_waiting_for_traversal.has_value() || !m_navigations_parked_for_population.is_empty(); }bool has_navigation_parked_for_population(Utf16String const& navigation_id) const;void clear_pending_navigations();- void drop_container_navigation_for_restored_entry();// Commits the frame that brings the compositor context up to date to the render owner, which presents it beside the// event loop: a new display list, or what changed for the one it has. Answers whether it committed one.@@ -461,11 +460,6 @@ private:void finish_recording_in_flight(RecordingInFlight&, Layout::RustFFI::FfiRecordingLanding, Layout::RustFFI::FfiPresentation);Gfx::IntRect present_viewport_rect() const;- enum class PendingNavigationBehavior {- Append,- Replace- };-// A navigation the UI process is to continue: it checks unloading, then asks for the navigation params.struct NavigationParkedForPopulation {Utf16String navigation_id;@@ -476,10 +470,9 @@ private:void begin_navigation(PreparedNavigation);virtual WebIDL::ExceptionOr<void> continue_navigation_in_active_document_agent(PreparedNavigation) override;void continue_navigation_after_population_dispatch(PreparedNavigation, NavigationPopulationRequest);- void queue_pending_navigation(PreparedNavigation, PendingNavigationBehavior);void park_navigation_for_population(Utf16String navigation_id, Optional<PreparedNavigation>, GC::Ref<GC::Function<void(Optional<PreparedNavigation>, Optional<NavigationPopulationRequest>)>> continue_steps);Optional<NavigationParkedForPopulation> take_navigation_parked_for_population(Utf16String const& navigation_id);- void process_pending_navigations();+ void begin_navigation_waiting_for_traversal();void navigate_to_a_javascript_url(GC::Ref<Fetch::Infrastructure::Request>, HistoryHandlingBehavior, URL::Origin const& initiator_origin, UserNavigationInvolvement, ContentSecurityPolicy::Directives::Directive::NavigationType csp_navigation_type, InitialInsertion, Utf16String navigation_id);void reset_cursor_blink_cycle();@@ -610,8 +603,7 @@ private:bool m_has_session_history_entry_and_ready_for_navigation { false };- // Navigations waiting for a traversal to be over, or for the navigable's session history entry.- Vector<PreparedNavigation> m_pending_navigations;+ Optional<PreparedNavigation> m_navigation_waiting_for_traversal;Vector<NavigationParkedForPopulation> m_navigations_parked_for_population;// The navigation IDs of javascript: URL navigations whose navigate to a javascript: URL task is queued.diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cppindex c363d70e557..76352d31487 100644--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp@@ -239,8 +239,7 @@ Optional<URL::URL> NavigableContainer::shared_attribute_processing_steps_for_ifr// AD-HOC: If the content navigable already has a navigation in progress or pending, skip the initial// about:blank URL update. Without this, the URL update creates a state machine that clobbers the// navigable's ongoing_navigation, causing the real navigation to be dropped when its populate completion- // callback checks ongoing_navigation != navigation_id. Non-blank src navigations must still be processed- // here, and will be queued by LocalNavigable::navigate() until the child navigable is ready for navigation.+ // callback checks ongoing_navigation != navigation_id.if (local_navigable.has_pending_navigations() || !local_navigable.ongoing_navigation().has<Empty>())return {};diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex 43d139a303a..7799e16755f 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -765,9 +765,10 @@ ErrorOr<NonnullRefPtr<WebContentPage>> CanonicalNavigable::obtain_page_to_host_iauto current_entry_descriptor = [&] {auto current_step = traversable.session_history().current_step();VERIFY(current_step.has_value());- auto const* current_entry = traversable.session_history().get_the_target_history_entry(*this, *current_step);- VERIFY(current_entry);- return current_entry->descriptor();+ // NB: Until the update for its creation, the navigable's entry is the one initializing it created.+ if (auto const* current_entry = traversable.session_history().get_the_target_history_entry(*this, *current_step))+ return current_entry->descriptor();+ return active_session_history_entry()->descriptor();};// The host takes the navigable's node over once the document it is to display is activated; until then, the pagediff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cppindex 032b3a86f40..52691610bc7 100644--- a/Libraries/LibWebView/CanonicalTraversable.cpp+++ b/Libraries/LibWebView/CanonicalTraversable.cpp@@ -2187,6 +2187,18 @@ void CanonicalTraversable::enqueue_history_operation(Web::HTML::CrossProcessId oremove_nested_history(*parent_navigable, parameters.parent_document_state_id, parameters.navigable_id);}+ // AD-HOC: The update for a navigable's creation populates the entry its nested history kept, if a document+ // repopulated for its entry created it, in place of the navigation its container starts meanwhile. That+ // navigation's start arrives after this request, and is canceled before it fetches anything.+ if (auto const* parameters = request.get_pointer<Web::NavigableCreationHistoryOperationParameters>()) {+ auto navigable = find(parameters->navigable_id);+ auto current_step = m_session_history.current_step();+ if (navigable.has_value() && current_step.has_value() && !navigable->ongoing_navigation()+ && m_session_history.get_the_target_history_entry(*navigable, *current_step)) {+ navigable->set_ongoing_navigation_to_traversal(operation_id);+ }+ }+if (auto const* parameters = request.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>(); parameters && requesting_page)did_attempt_to_populate_the_history_entry_document(*requesting_page, *parameters);@@ -2770,6 +2782,10 @@ void CanonicalTraversable::release_history_operation(HistoryOperation& operation// stays with the operation: its completion can be what finished it.for (auto const& [navigable_id, pending_job] : operation.pending_changing_jobs)pending_job->abandon(find(navigable_id));+ if (auto const* parameters = operation.parameters.get_pointer<Web::NavigableCreationHistoryOperationParameters>()) {+ if (auto navigable = find(parameters->navigable_id); navigable.has_value())+ navigable->clear_ongoing_navigation_traversal(operation.operation_id);+ }if (auto const* parameters = operation.parameters.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>()) {if (auto navigable = find(parameters->navigable_id); navigable.has_value())navigable->did_finish_finalizing_navigation(parameters->navigation_id, result);