Truly independent web browser
LibWeb+LibWebView: Finalize a navigation without asking its page first
Shannon Booth91da0b99afce
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cppindex 7f9c34377f9..c031e261cb3 100644--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp@@ -70,7 +70,6 @@ void HistoryExecutor::visit_edges(Cell::Visitor& visitor)for (auto& operation : m_history_operations) {visitor.visit(operation.value.source_snapshot_params);visitor.visit(operation.value.pending_document);- visitor.visit(operation.value.expected_ongoing_navigation_navigable);visitor.visit(operation.value.pre_steps);visitor.visit(operation.value.on_apply_complete);visitor.visit(operation.value.on_complete);@@ -81,18 +80,6 @@ void HistoryExecutor::visit_edges(Cell::Visitor& visitor)}}-bool HistoryExecutor::HistoryOperationState::expected_ongoing_navigation_was_superseded() const-{- if (!expected_ongoing_navigation_navigable || !expected_ongoing_navigation_id.has_value())- return false;- auto navigable = local_navigable_with_id(expected_ongoing_navigation_navigable->id());- if (!navigable)- return true;- if (navigable->has_been_destroyed())- return true;- return navigable->ongoing_navigation() != *expected_ongoing_navigation_id;-}-HistoryExecutor::HistoryOperationState* HistoryExecutor::find_history_operation(CrossProcessId operation_id){auto operation = m_history_operations.find(operation_id);@@ -126,15 +113,6 @@ void HistoryExecutor::handle_ui_history_operation_started(CrossProcessId operatireturn;}- // NB: A cross-document navigation can be superseded after its document has populated but before its queued- // history-step application runs. The navigate algorithm's earlier navigation ID check caught the same- // condition before requesting this operation; this re-check keeps a stale finalization from claiming- // "traversal" and canceling the newer navigation.- if (operation->expected_ongoing_navigation_was_superseded()) {- ready->function()(HistoryStepResult::Applied);- return;- }-if (operation->pre_steps) {operation->pre_steps->function()(ready);return;@@ -882,12 +860,12 @@ void HistoryExecutor::run_ui_changing_navigable_history_job(CrossProcessId operaVERIFY(operation.local_target_entry);local_target_entry = operation.local_target_entry;}- if (operation.expected_ongoing_navigation_was_superseded()) {++ 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;}-- auto navigable = local_navigable_with_id(navigable_id);if (!navigable) {on_complete->function()(ChangingNavigableHistoryStepJobDisposition::Skipped, UnloadDisplayedDocument::No);return;diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.h b/Libraries/LibWeb/HTML/HistoryExecutor.hindex 59bb35f786d..81a9e9e96c1 100644--- a/Libraries/LibWeb/HTML/HistoryExecutor.h+++ b/Libraries/LibWeb/HTML/HistoryExecutor.h@@ -45,8 +45,6 @@ public:using OnHistoryOperationPreSteps = GC::Function<void(GC::Ref<OnHistoryOperationReady>)>;struct HistoryOperationState {GC::Ptr<DOM::Document> pending_document {};- GC::Ptr<LocalNavigable> expected_ongoing_navigation_navigable {};- Optional<Utf16String> expected_ongoing_navigation_id {};GC::Ptr<SourceSnapshotParams> source_snapshot_params {};Optional<NavigationSourceSnapshot> serialized_source_snapshot_params {};Optional<CrossProcessId> local_target_navigable_id {};@@ -59,8 +57,6 @@ public:HashTable<CrossProcessId> claimed_navigables_awaiting_continuation {};HashMap<CrossProcessId, GC::Ref<ChangingNavigableContinuationState>> changing_navigable_continuations {};HashMap<CrossProcessId, GC::Ref<GC::Function<void(HistoryNavigationPopulation)>>> pending_populations {};-- bool expected_ongoing_navigation_was_superseded() const;};void request_history_operation(HistoryOperationParameters);diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 4ae398cb45c..629e0aa412b 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -4371,49 +4371,29 @@ bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& docureturn url.scheme() == "javascript"sv || document.is_initial_about_blank();}-static bool prepare_to_finalize_a_cross_document_navigation(GC::Ref<LocalNavigable> navigable, GC::Ptr<DOM::Document> pending_document, Optional<Utf16String> const& expected_ongoing_navigation_id)+// 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){- // NOTE: This is not in the spec but we should not navigate destroyed navigable.- if (navigable->has_been_destroyed()) {- navigable->set_delaying_load_events(false);- return false;- }-- // AD-HOC: This check is not in the spec but we should not continue navigation if ongoing navigation id has changed.- if (expected_ongoing_navigation_id.has_value() && navigable->ongoing_navigation() != *expected_ongoing_navigation_id) {- navigable->stop_delaying_load_events_for_navigation(*expected_ongoing_navigation_id);- return false;- }-- // The history operation can reach its queue position after its page has started closing. In that case the+ // 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 (pending_document && (pending_document->has_been_destroyed() || !pending_document->browsing_context() || !active_document || active_document->has_been_destroyed())) {- navigable->set_delaying_load_events(false);+ 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;}- // The UI process has reached this navigation's position on the session history traversal queue. Perform the- // parts of finalization that need the live navigable and Document, and let the UI process continue the algorithm- // there.- //// 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(); container_document && pending_document)- navigable->set_navigation_load_event_guard(*container_document);+ if (auto container_document = navigable.container_document())+ navigable.set_navigation_load_event_guard(*container_document);- navigable->set_delaying_load_events(false);-- if (!pending_document) {- // AD-HOC: Clear the ongoing navigation, like the "navigation must be a replace" and download cases do.- // No history step will be applied for this navigation, so nothing else clears it, and a stale- // ongoing navigation ID makes later same-document traversals consider themselves superseded.- if (expected_ongoing_navigation_id.has_value() && navigable->ongoing_navigation() == expected_ongoing_navigation_id)- navigable->set_ongoing_navigation({});- }+ // 2. Set navigable's is delaying load events to false.+ navigable.set_delaying_load_events(false);return true;}@@ -4660,8 +4640,45 @@ void check_if_unloading_is_canceled(Vector<GC::Root<LocalNavigable>> navigables_}// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation+// NB: The UI process appends the steps finalizing the navigation, and runs them, once this process reports that+// populating historyEntry's document is over.void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable> navigable, HistoryHandlingBehavior history_handling, UserNavigationInvolvement user_involvement, NonnullRefPtr<SessionHistoryEntry> history_entry, GC::Ptr<DOM::Document> pending_document, Utf16String navigation_id, Optional<Utf16String> expected_ongoing_navigation_id, GC::Ref<OnApplyHistoryStepComplete> on_complete){+ // NOTE: This is not in the spec but we should not navigate destroyed navigable.+ if (navigable->has_been_destroyed()) {+ navigable->set_delaying_load_events(false);+ navigable->page().client().navigation_population_failed(navigable->id(), navigation_id);+ return;+ }++ // AD-HOC: This check is not in the spec but we should not continue navigation if ongoing navigation id has changed.+ if (expected_ongoing_navigation_id.has_value() && navigable->ongoing_navigation() != *expected_ongoing_navigation_id) {+ navigable->stop_delaying_load_events_for_navigation(*expected_ongoing_navigation_id);+ navigable->page().client().navigation_population_failed(navigable->id(), navigation_id);+ return;+ }++ HashTable<CrossProcessId> claimed_navigables;+ if (pending_document) {+ // AD-HOC: The navigation is committed from here on, and the steps finalizing it set navigable's ongoing+ // navigation to "traversal" only once the UI process reaches them. Set it now, so that a navigation+ // this process starts meanwhile waits for them instead of crossing them on its way to the UI process.+ if (!navigable->ongoing_navigation().has<Utf16String>() || navigable->ongoing_navigation() == navigation_id) {+ navigable->set_ongoing_navigation_without_informing_navigation_api(LocalNavigable::Traversal::Tag);+ claimed_navigables.set(navigable->id());+ }+ } else {+ // 2. Set navigable's is delaying load events to false.+ // 3. If historyEntry's document is null, then return.+ navigable->set_delaying_load_events(false);++ // AD-HOC: Clear the ongoing navigation, like the "navigation must be a replace" and download cases do.+ // No history step will be applied for this navigation, so nothing else clears it, and a stale+ // ongoing navigation ID makes later same-document traversals consider themselves superseded.+ if (expected_ongoing_navigation_id.has_value() && navigable->ongoing_navigation() == expected_ongoing_navigation_id)+ navigable->set_ongoing_navigation({});+ }+navigable->page().history_executor().request_history_operation(FinalizeCrossDocumentNavigationHistoryOperationParameters {.navigable_id = navigable->id(),@@ -4674,23 +4691,15 @@ void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable> navigable, His},{.pending_document = pending_document,- .expected_ongoing_navigation_navigable = navigable,- .expected_ongoing_navigation_id = expected_ongoing_navigation_id,.local_target_navigable_id = navigable->id(),.local_target_entry = history_entry,- .pre_steps = GC::create_function(navigable->heap(), [navigable, pending_document, expected_ongoing_navigation_id](GC::Ref<HistoryExecutor::OnHistoryOperationReady> ready) {- if (!prepare_to_finalize_a_cross_document_navigation(navigable, pending_document, expected_ongoing_navigation_id)) {- ready->function()(HistoryStepResult::Applied);- return;- }- ready->function()(Empty {});- }),.on_complete = GC::create_function(navigable->heap(), [navigable, on_complete](HistoryStepResult result) {// 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);on_complete->function()(result);}),+ .claimed_navigables_awaiting_continuation = move(claimed_navigables),});}diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex 86ad739f75a..1b4861104d7 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -733,6 +733,7 @@ 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, Optional<Utf16String> expected_ongoing_navigation_id, GC::Ref<OnApplyHistoryStepComplete> on_complete);enum class CheckIfUnloadingIsCanceledResult {diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex 99fab7e1f27..6c1025effb5 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -842,15 +842,15 @@ void CanonicalNavigable::populate_document_for_ongoing_navigation(NonnullRefPtr<// NB: newDocument's URL is the active document's, as the process hosting both reports it. Whatever URL it reports,// newDocument holds no local file content and displays no WebUI unless the active document does, as its contents// come from that process.-bool CanonicalNavigable::populate_document_for_javascript_url(NonnullRefPtr<CanonicalDocumentState> document_state, NonnullRefPtr<CanonicalDocument> document)+void CanonicalNavigable::populate_document_for_javascript_url(NonnullRefPtr<CanonicalSessionHistoryEntry> history_entry, NonnullRefPtr<CanonicalDocument> document){document->determine_whether_it_is_local_file_content(blob_url_store());if (document->is_local_file_content() && !active_document().is_local_file_content())- return false;+ return;if (document->web_ui_host().has_value() && document->web_ui_host() != active_document().web_ui_host())- return false;- populate_document(move(document_state), move(document));- return true;+ return;+ ongoing_navigation()->history_entry = move(history_entry);+ populate_document_for_ongoing_navigation(move(document));}// The process hosting a document populated for the navigable created it with an origin other than its navigationdiff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.hindex dfb7a3056ba..ddb721b8804 100644--- a/Libraries/LibWebView/CanonicalNavigable.h+++ b/Libraries/LibWebView/CanonicalNavigable.h@@ -92,8 +92,8 @@ public:RefPtr<CanonicalDocument> document_with_id(Web::HTML::CrossProcessId) const;void populate_document(NonnullRefPtr<CanonicalDocumentState>, NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});void populate_document_for_ongoing_navigation(NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});- // Returns false, populating nothing, for a document that would hold access the active document lacks.- bool populate_document_for_javascript_url(NonnullRefPtr<CanonicalDocumentState>, NonnullRefPtr<CanonicalDocument>);+ // Populates nothing for a document that would hold access the active document lacks.+ void populate_document_for_javascript_url(NonnullRefPtr<CanonicalSessionHistoryEntry>, NonnullRefPtr<CanonicalDocument>);void did_create_populated_document_with_an_origin_of_its_own(WebContentPage const& host, Web::HTML::CrossProcessId document_id, Web::HTML::PopulatedDocumentOrigin, Web::HTML::EnvironmentId const& environment_id);bool take_navigation_to_finalize(Utf16String const& navigation_id, WebContentPage const&);void abandon_populated_document(CanonicalDocument const&);@@ -226,7 +226,6 @@ public:void clear_ongoing_navigation_state();void set_navigation_host(WebContentPage&);bool cancel_navigation_for_client(WebContentClient&);- CanonicalNavigation* navigation_being_finalized() { return m_navigation_being_finalized.has_value() ? &*m_navigation_being_finalized : nullptr; }CanonicalNavigation const* navigation_being_finalized() const { return m_navigation_being_finalized.has_value() ? &*m_navigation_being_finalized : nullptr; }void did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::HistoryStepResult);bool has_uncommitted_navigation() const { return ongoing_navigation(); }diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cppindex 262d41590e6..787fd4a8c2a 100644--- a/Libraries/LibWebView/CanonicalTraversable.cpp+++ b/Libraries/LibWebView/CanonicalTraversable.cpp@@ -2035,6 +2035,10 @@ void CanonicalTraversable::run_history_operation_at_queue_position(Web::HTML::Cr// The traversal queue can outlive an embedded page which appended work to it. Such a page cannot run the// preparation step or receive completion, so discard its queued operation instead of waiting forever.if (requesting_page && !requesting_page->is_open()) {+ if (auto const* parameters = request.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>()) {+ if (auto navigable = find(parameters->navigable_id); navigable.has_value())+ navigable->did_finish_finalizing_navigation(parameters->navigation_id, Web::HTML::HistoryStepResult::CanceledByMissingPage);+ }promise->resolve({});return;}@@ -2093,6 +2097,72 @@ NonnullRefPtr<CanonicalSessionHistoryEntry> CanonicalTraversable::session_historreturn entry;}+// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate+// NB: The page conducting a navigation reports historyEntry once attempting to populate its document is over. The+// completion steps, which append the steps finalizing the navigation, run here.+void CanonicalTraversable::did_attempt_to_populate_the_history_entry_document(WebContentPage& source_page, Web::FinalizeCrossDocumentNavigationHistoryOperationParameters const& parameters)+{+ auto navigable = find(parameters.navigable_id);+ if (!navigable.has_value())+ return;+ auto* navigation = navigable->ongoing_navigation();+ if (!navigation || navigation->navigation_id != parameters.navigation_id || navigation->host() != &source_page)+ return;++ // NB: Populating historyEntry's document saves state in the entry, which the page that populated it reports.+ auto history_entry_descriptor = Web::HTML::create_session_history_entry_descriptor(parameters.history_entry, 0);+ if (!navigation->state.has<CanonicalNavigation::EvaluatingJavaScriptURL>()) {+ if (navigation->history_entry && history_entry_descriptor.document_state.id == navigation->history_entry->document_state->id)+ (void)navigation->history_entry->update_from_descriptor(history_entry_descriptor);+ return;+ }++ auto history_entry_or_error = CanonicalSessionHistoryEntry::create_from_descriptor(history_entry_descriptor);+ if (history_entry_or_error.is_error())+ return;+ auto history_entry = history_entry_or_error.release_value();++ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-to-a-javascript:-url+ // NB: The process running a javascript: URL navigation evaluates it, creates newDocument in navigable's active+ // browsing context, and reports historyEntry. The UI process runs the steps that decide newDocument's origin,+ // and populates the document it holds for historyEntry's document state.+ auto const& initiator_origin = history_entry->document_state->initiator_origin;++ // 4. If initiatorOrigin is not same origin-domain with targetNavigable's active document's origin, then return.+ if (!initiator_origin.has_value() || !initiator_origin->is_same_origin_domain(navigable->active_document().origin()))+ return;++ if (!parameters.document_id.has_value())+ return;++ // 8. Assert: initiatorOrigin is newDocument's origin.+ // NB: The UI process creates its newDocument with initiatorOrigin, and with the opener policy evaluating a+ // javascript: URL gives it: targetNavigable's active document's opener policy.+ auto const& coop = navigable->active_document().opener_policy();+ NavigationLoader::ResponseDocument response_document {+ .is_inline_content = false,+ .coop_enforcement_result = { .url = history_entry->url, .origin = *initiator_origin, .opener_policy = coop },+ .response_url = history_entry->url,+ .request_current_url = {},+ .origin = *initiator_origin,+ .opener_policy = coop,+ .final_sandboxing_flag_set = navigable->active_document().active_sandboxing_flag_set(),+ .environment_id = parameters.environment_id,+ .document_id = *parameters.document_id,+ };++ // 11. Let documentState be a new document state with+ // [...]+ // initiator origin: initiatorOrigin+ // origin: initiatorOrigin+ history_entry->document_state->origin = initiator_origin;++ // 12. Let historyEntry be a new session history entry, with+ // URL: entryToReplace's URL+ // document state: documentState+ navigable->populate_document_for_javascript_url(move(history_entry), navigable->create_and_initialize_a_document(response_document));+}+void CanonicalTraversable::enqueue_history_operation(Web::HTML::CrossProcessId operation_id, Web::HistoryOperationParameters request, RefPtr<WebContentPage> requesting_page, u64 sequence_number, OnHistoryOperationComplete on_complete){// https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-child-navigable@@ -2104,6 +2174,9 @@ void CanonicalTraversable::enqueue_history_operation(Web::HTML::CrossProcessId oremove_nested_history(*parent_navigable, parameters.parent_document_state_id, parameters.navigable_id);}+ if (auto const* parameters = request.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>(); parameters && requesting_page)+ did_attempt_to_populate_the_history_entry_document(*requesting_page, *parameters);+Optional<Web::HTML::CrossProcessId> synchronous_navigation_target;RefPtr<CanonicalSessionHistoryEntry> target_entry;if (request.has<Web::FinalizeSameDocumentNavigationHistoryOperationParameters>()) {@@ -2211,13 +2284,11 @@ void CanonicalTraversable::update_for_navigable_creation_or_destruction(HistoryOapply_history_step(operation, *step, false, {}, Web::HTML::UserNavigationInvolvement::None, {});}-// Finalizing a cross-document navigation and resuming a traversal begin, at their queue position, with steps in the-// requesting process, which can find a newer navigation there. The other operations have their complete input in-// canonical state.+// Resuming a traversal begins, at its queue position, with steps in the requesting process, which can find the+// intercepted navigate event aborted there. The other operations have their complete input in canonical state.static bool history_operation_needs_preparation(Web::HistoryOperationParameters const& parameters){- return parameters.has<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>()- || parameters.has<Web::ResumeTraverseHistoryOperationParameters>();+ return parameters.has<Web::ResumeTraverseHistoryOperationParameters>();}// https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history-by-a-delta@@ -2473,6 +2544,15 @@ void CanonicalTraversable::run_direct_history_operation(HistoryOperation& operat// Flush is a queue barrier; completing at the queue position is the whole operation.finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::Applied, {});},+ [&](Web::FinalizeCrossDocumentNavigationHistoryOperationParameters const& parameters) {+ // The navigation is committed from here on. One that a newer navigation replaced meanwhile is not finalized.+ auto navigable = find(parameters.navigable_id);+ if (!navigable.has_value() || !operation.initiating_page || !navigable->take_navigation_to_finalize(parameters.navigation_id, *operation.initiating_page)) {+ finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::Applied, {});+ return;+ }+ finalize_a_cross_document_navigation(operation);+ },[&](auto const&) {VERIFY_NOT_REACHED();});@@ -2507,14 +2587,6 @@ void CanonicalTraversable::start_history_operation(HistoryOperation& operation,return;}- if (auto const* parameters = operation.parameters.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>()) {- auto navigable = find(parameters->navigable_id);- if (!navigable.has_value() || !navigable->take_navigation_to_finalize(parameters->navigation_id, *operation.initiating_page)) {- finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::Applied, {});- return;- }- }-operation.initiating_page->async_history_operation_started(operation.operation_id);}@@ -2528,68 +2600,12 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperationreturn;}- // NB: historyEntry is the navigation's, as the process that populated its document reports it: populating saves- // state in the entry.- auto& navigation = *navigable->navigation_being_finalized();- CanonicalSessionHistoryEntry::DocumentStates document_states;- if (navigation.history_entry)- document_states.set(navigation.history_entry->document_state->id, navigation.history_entry->document_state);- auto history_entry_or_error = CanonicalSessionHistoryEntry::create_from_descriptor(Web::HTML::create_session_history_entry_descriptor(parameters.history_entry, 0), document_states, CanonicalSessionHistoryEntry::UpdateDocumentState::Yes);- if (history_entry_or_error.is_error()) {+ // NB: historyEntry is the navigation's. A javascript: URL navigation whose document was refused has none.+ if (!navigable->navigation_being_finalized()->history_entry) {finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});return;}- auto history_entry = history_entry_or_error.release_value();-- // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-to-a-javascript:-url- // NB: The process running a javascript: URL navigation evaluates it, creates newDocument in navigable's active- // browsing context, and reports historyEntry with these steps. The UI process runs the steps that decide- // newDocument's origin, and populates the document it holds for historyEntry's document state.- if (navigation.state.has<CanonicalNavigation::EvaluatingJavaScriptURL>()) {- auto const& initiator_origin = history_entry->document_state->initiator_origin;-- // 4. If initiatorOrigin is not same origin-domain with targetNavigable's active document's origin, then return.- if (!initiator_origin.has_value() || !initiator_origin->is_same_origin_domain(navigable->active_document().origin())) {- finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});- return;- }-- if (!parameters.document_id.has_value()) {- finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});- return;- }-- // 8. Assert: initiatorOrigin is newDocument's origin.- // NB: The UI process creates its newDocument with initiatorOrigin, and with the opener policy evaluating a- // javascript: URL gives it: targetNavigable's active document's opener policy.- auto const& coop = navigable->active_document().opener_policy();- NavigationLoader::ResponseDocument response_document {- .is_inline_content = false,- .coop_enforcement_result = { .url = history_entry->url, .origin = *initiator_origin, .opener_policy = coop },- .response_url = history_entry->url,- .request_current_url = {},- .origin = *initiator_origin,- .opener_policy = coop,- .final_sandboxing_flag_set = navigable->active_document().active_sandboxing_flag_set(),- .environment_id = parameters.environment_id,- .document_id = *parameters.document_id,- };-- // 11. Let documentState be a new document state with- // [...]- // initiator origin: initiatorOrigin- // origin: initiatorOrigin- history_entry->document_state->origin = initiator_origin;-- // 12. Let historyEntry be a new session history entry, with- // URL: entryToReplace's URL- // document state: documentState- navigation.history_entry = history_entry;- if (!navigable->populate_document_for_javascript_url(history_entry->document_state, navigable->create_and_initialize_a_document(response_document))) {- finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});- return;- }- }+ NonnullRefPtr history_entry = *navigable->navigation_being_finalized()->history_entry;// 1. Assert: this is running on navigable's traversable navigable's session history traversal queue.VERIFY(operation.queue_promise);@@ -2597,8 +2613,8 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperationVERIFY(&navigable->top_level_traversable() == this);// 2. Set navigable's is delaying load events to false.- // NB: The process hosting navigable performed this step when the operation reached its queue position, before- // answering that it started.+ // 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.// 3. If historyEntry's document is null, then return.// NB: The document populated for historyEntry waits in its document state until historyEntry is activated.@@ -2701,25 +2717,13 @@ void CanonicalTraversable::did_receive_history_operation_ready(WebContentPage& sauto* operation = find_history_operation(operation_id);if (!operation || operation->algorithm || !history_operation_needs_preparation(operation->parameters) || !operation->was_initiated_by(source_page))return;- if (!navigation_transaction_matches(*operation, source_page)) {- finish_history_operation(operation_id, Web::HTML::HistoryStepResult::Applied, {});- return;- }if (auto const* step_result = result.get_pointer<Web::HTML::HistoryStepResult>()) {finish_history_operation(operation_id, *step_result, {});return;}- operation->parameters.visit(- [&](Web::FinalizeCrossDocumentNavigationHistoryOperationParameters const&) {- finalize_a_cross_document_navigation(*operation);- },- [&](Web::ResumeTraverseHistoryOperationParameters const& parameters) {- resume_applying_the_traverse_history_step(*operation, parameters.target_step, parameters.user_involvement);- },- [](auto const&) {- VERIFY_NOT_REACHED();- });+ auto const& parameters = operation->parameters.get<Web::ResumeTraverseHistoryOperationParameters>();+ resume_applying_the_traverse_history_step(*operation, parameters.target_step, parameters.user_involvement);}void CanonicalTraversable::finish_history_operation(Web::HTML::CrossProcessId operation_id, Web::HTML::HistoryStepResult result, Optional<i32> committed_step)diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.hindex e8d01f620cf..18ba0797957 100644--- a/Libraries/LibWebView/CanonicalTraversable.h+++ b/Libraries/LibWebView/CanonicalTraversable.h@@ -219,6 +219,7 @@ private:void enqueue_browser_history_traversal(Web::TraverseToStepHistoryOperationParameters, bool check_for_cancelation, OnHistoryOperationComplete = nullptr);void run_browser_history_traversal_at_queue_position(Web::TraverseToStepHistoryOperationParameters, bool check_for_cancelation, u64 sequence_number, Function<void()> on_ready, OnHistoryOperationComplete, NonnullRefPtr<Core::Promise<Empty>>);void start_history_operation(HistoryOperation&, NonnullRefPtr<Core::Promise<Empty>>);+ void did_attempt_to_populate_the_history_entry_document(WebContentPage& source_page, Web::FinalizeCrossDocumentNavigationHistoryOperationParameters const&);void finalize_a_cross_document_navigation(HistoryOperation&);void apply_history_step(HistoryOperation&, i32 step, bool check_for_cancelation, Optional<Web::HTML::CrossProcessId> initiator_to_check, Web::HTML::UserNavigationInvolvement, Optional<Web::Bindings::NavigationType>, Optional<Web::InitiatorSourceSnapshot> initiator_source_snapshot = {});void apply_the_push_or_replace_history_step(HistoryOperation&, i32 step, Web::HTML::HistoryHandlingBehavior, Web::HTML::UserNavigationInvolvement);