Truly independent web browser
LibWebView: Change a navigable's navigation state only from navigables
Shannon Booth845a4407bacc
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex 3be86c133b4..84551176734 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -276,7 +276,7 @@ void CanonicalNavigable::begin_navigation(Web::HTML::PreparedNavigationDescriptoif (is_javascript_url) {ongoing_navigation()->state = CanonicalNavigation::EvaluatingJavaScriptURL { *host };if (is_top_level_traversable() && host->displays_tab())- host->begin_top_level_load(navigation_id, url);+ host->begin_top_level_load(url);// 1. Let request be a new request whose URL is url and whose policy container is sourceSnapshotParams's source// policy container.@@ -1392,6 +1392,23 @@ void CanonicalNavigable::did_finish_finalizing_navigation(Utf16String const& navclear_active_document_load();}+void CanonicalNavigable::did_cancel_navigation(Utf16String navigation_id)+{+ if (tracked_load_navigation_id() != navigation_id)+ return;++ auto canceled_ongoing_navigation = has_uncommitted_navigation();+ if (canceled_ongoing_navigation)+ clear_ongoing_navigation();+ else+ clear_active_document_load();++ if (!is_top_level_traversable())+ return;+ if (auto view = top_level_traversable().view(); view.has_value())+ view->did_cancel_loading({}, canceled_ongoing_navigation);+}+Optional<Utf16String> CanonicalNavigable::tracked_load_navigation_id() const{if (auto const* navigation = ongoing_navigation())diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.hindex 47728144695..5144fa34d2e 100644--- a/Libraries/LibWebView/CanonicalNavigable.h+++ b/Libraries/LibWebView/CanonicalNavigable.h@@ -237,6 +237,7 @@ public: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(); }+ void did_cancel_navigation(Utf16String navigation_id);Optional<Utf16String> tracked_load_navigation_id() const;ActiveDocumentLoad const& active_document_load() const { return m_active_document_load; }diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cppindex a87026a9a42..a6d6353cdff 100644--- a/Libraries/LibWebView/CanonicalTraversable.cpp+++ b/Libraries/LibWebView/CanonicalTraversable.cpp@@ -659,6 +659,12 @@ void CanonicalTraversable::reload(OnHistoryOperationComplete on_complete){auto user_involvement = Web::HTML::UserNavigationInvolvement::BrowserUI;+ // NB: A reload from the browser's UI is the newest navigation. The navigation under way is abandoned, and the+ // active document's load is no longer the one tracked.+ if (has_uncommitted_navigation())+ clear_ongoing_navigation();+ clear_active_document_load();+// 2. Set navigable's active session history entry's document state's reload pending to true.NonnullRefPtr reloading_entry = *active_session_history_entry();reloading_entry->document_state->reload_pending = true;@@ -682,6 +688,27 @@ void CanonicalTraversable::reload(OnHistoryOperationComplete on_complete)});}+// https://html.spec.whatwg.org/multipage/document-lifecycle.html#nav-stop+void CanonicalTraversable::stop_loading()+{+ // 1. Let document be navigable's active document.+ // 2. If document's unload counter is 0, and navigable's ongoing navigation is a navigation ID, then set the ongoing+ // navigation for navigable to null.+ auto stopped_navigation = !is_unloading_document_of(id()) && has_uncommitted_navigation();+ if (stopped_navigation)+ clear_ongoing_navigation();+ else+ clear_active_document_load();++ // 3. Abort a document and its descendants given document.+ // NB: The page hosting document runs this step.+ if (auto page = display_page(); page && page->is_open())+ page->async_stop_loading();++ if (auto view = this->view(); view.has_value())+ view->did_stop_loading({}, stopped_navigation);+}+bool CanonicalTraversable::is_origin_held_by_a_document(URL::Origin const& origin){auto holds = [&](CanonicalDocument const& document) { return document.origin().is_same_origin(origin); };@@ -1132,7 +1159,7 @@ void CanonicalTraversable::run_browser_ui_traversal_at_queue_position(Function<OVERIFY(view.has_value());auto canceled_uncommitted_navigation = check_for_cancelation == CheckForCancelation::Yes && has_uncommitted_navigation();if (canceled_uncommitted_navigation)- view->cancel_uncommitted_top_level_navigation_for_browser_traversal();+ stop_loading();auto current_step = m_session_history.current_step();auto target_step = select_target_step();@@ -1164,10 +1191,8 @@ void CanonicalTraversable::supersede_browser_history_traversal(HistoryOperation&auto promise = operation.queue_promise.release_nonnull();auto operation_id = operation.operation_id;- auto view = this->view();- VERIFY(view.has_value());if (has_uncommitted_navigation())- view->cancel_uncommitted_top_level_navigation_for_browser_traversal();+ stop_loading();finish_history_operation(operation_id, Web::HTML::HistoryStepResult::CanceledByNavigate, {});run_browser_history_traversal_at_queue_position(Web::TraverseToStepHistoryOperationParameters {@@ -1220,6 +1245,10 @@ CanonicalNavigation const* CanonicalTraversable::finalized_navigation(HistoryOpevoid CanonicalTraversable::recover_from_web_content_process_crash(OnHistoryOperationComplete on_complete){+ if (has_uncommitted_navigation())+ clear_ongoing_navigation();+ clear_active_document_load();+// The step a traversal the crash interrupted was applying is applied again, otherwise the current step is.Optional<i32> target_step;if (auto* traversal = ongoing_browser_history_traversal())@@ -1362,8 +1391,11 @@ bool CanonicalTraversable::select_changing_navigable_history_step_job_endpoint(H// dispatched instead of having the job echo it back.if (navigable->is_top_level_traversable()&& operation.parameters.has<Web::ReloadHistoryOperationParameters>()) {- if (endpoint->is_open())- endpoint->begin_top_level_load({}, job.target_entry->url);+ if (endpoint->is_open()) {+ // A load a history operation starts, rather than a navigation, is tracked as the active document's.+ clear_active_document_load();+ endpoint->begin_top_level_load(job.target_entry->url);+ }}return true;}@@ -2612,10 +2644,7 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperationif (auto const& populated_document = history_entry->document_state->populated_document; populated_document.has_value())document = populated_document->document;if (!document) {- if (navigable->is_top_level_traversable()) {- if (auto view = this->view(); view.has_value())- view->did_cancel_loading(parameters.navigation_id);- }+ navigable->did_cancel_navigation(parameters.navigation_id);finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::Applied, {});return;}diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.hindex 18ba0797957..a7e21f9f2c9 100644--- a/Libraries/LibWebView/CanonicalTraversable.h+++ b/Libraries/LibWebView/CanonicalTraversable.h@@ -160,6 +160,7 @@ public:ByteString queued_same_document_session_history_entries_for_debug() const;void reload(OnHistoryOperationComplete = nullptr);+ void stop_loading();static CanonicalTraversable& create_a_new_top_level_traversable(Web::HTML::CrossProcessId id, Optional<CanonicalNavigable&> opener, Web::HTML::SessionHistoryEntryDescriptor initial_history_entry, Web::HTML::CrossProcessId document_id);static bool is_origin_held_by_a_document(URL::Origin const&);static CanonicalTraversable* traversable_containing(Web::HTML::CrossProcessId navigable_id);diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex c814ea33363..73379bbb588 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -424,10 +424,6 @@ void ViewImplementation::reload()on_before_browser_initiated_navigation();set_loading_state(true);- // A reload from the browser's UI is the newest navigation. The navigation under way is abandoned, and the active- // document's load is no longer the one the view tracks.- traversable().clear_ongoing_navigation();- traversable().clear_active_document_load();if (m_crash_state.has_value()) {prepare_for_navigation_after_crash();recover_current_session_history_entry_with_history_operation();@@ -456,12 +452,27 @@ void ViewImplementation::stop_loading()// active-document load reloads through the session history. A navigation a history operation is finalizing is// not stopped, and activates its document.m_last_stopped_navigation = traversable().has_uncommitted_navigation() ? navigation_to_retry_for_ongoing_navigation() : Optional<NavigationToRetry> {};- if (cancel_uncommitted_top_level_navigation("stop-loading"sv, true))- return;+ traversable().stop_loading();+}++void ViewImplementation::did_stop_loading(Badge<CanonicalTraversable>, bool stopped_navigation)+{set_loading_state(false);- traversable().clear_ongoing_navigation();- traversable().clear_active_document_load();- page().async_stop_loading();+ if (!stopped_navigation) {+ m_last_stopped_navigation.clear();+ return;+ }+ did_end_uncommitted_navigation("stop-loading"sv);+}++// The address shown is the current entry's again, and a navigation WebDriver waits on is over.+void ViewImplementation::did_end_uncommitted_navigation(StringView reason)+{+ if (auto const* current_entry = traversable().session_history().current_entry())+ set_url(current_entry->url);+ if (m_webdriver_navigation_observation.has_value())+ complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);+ dump_session_history(reason);}void ViewImplementation::traverse_the_history_by_delta(@@ -476,12 +487,6 @@ void ViewImplementation::traverse_the_history_by_delta(traversable().traverse_the_history_by_delta(delta, check_for_cancelation, move(on_ready));}-void ViewImplementation::cancel_uncommitted_top_level_navigation_for_browser_traversal()-{- auto canceled = cancel_uncommitted_top_level_navigation("traverse-canceled-pending-navigation"sv, true);- VERIFY(canceled);-}-void ViewImplementation::traverse_the_history_to_step(i32 step,CheckForCancelation check_for_cancelation,@@ -2477,41 +2482,19 @@ void ViewImplementation::cancel_all_native_geolocation_requests()Application::the().stop_watching_geolocation_position(watch.value);}-void ViewImplementation::did_start_navigation(Optional<Utf16String> const& navigation_id)+void ViewImplementation::did_start_navigation(){- // A load a history operation starts, rather than a navigation, is tracked as the active document's.- if (!navigation_id.has_value())- traversable().clear_active_document_load();-set_loading_state(true);dump_session_history("did-start-navigation"sv);}-bool ViewImplementation::did_cancel_navigation(Optional<Utf16String> const& navigation_id)+void ViewImplementation::did_cancel_loading(Badge<CanonicalNavigable>, bool canceled_ongoing_navigation){- if (navigation_id != traversable().tracked_load_navigation_id())- return false;-set_loading_state(false);- if (cancel_uncommitted_top_level_navigation("did-cancel-navigation"sv, false))- return true;-- traversable().clear_ongoing_navigation();- traversable().clear_active_document_load();- if (m_webdriver_navigation_observation.has_value()) {- auto webdriver_navigation_id = m_webdriver_navigation_observation->navigation_id;- complete_webdriver_navigation(webdriver_navigation_id);- return true;- }-- dump_session_history("did-cancel-navigation-ignored"sv);- return true;-}--void ViewImplementation::did_cancel_loading(Optional<Utf16String> const& navigation_id)-{- if (!did_cancel_navigation(navigation_id))- return;+ if (canceled_ongoing_navigation)+ did_end_uncommitted_navigation("did-cancel-navigation"sv);+ else if (m_webdriver_navigation_observation.has_value())+ complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);auto const& client_url = url();if (on_load_finish)@@ -2526,8 +2509,6 @@ void ViewImplementation::did_cancel_loading(Optional<Utf16String> const& navigatvoid ViewImplementation::did_finish_navigation(){set_loading_state(false);- traversable().clear_ongoing_navigation();- traversable().clear_active_document_load();if (!m_webdriver_navigation_observation.has_value())return;@@ -2556,32 +2537,6 @@ void ViewImplementation::set_loading_state(bool is_loading)on_loading_state_change(is_loading);}-bool ViewImplementation::cancel_uncommitted_top_level_navigation(StringView reason, bool stop_loading)-{- if (!traversable().has_uncommitted_navigation())- return false;-- // The document populated for the navigation is abandoned, with the page chosen to host it.- traversable().clear_ongoing_navigation();- set_loading_state(false);- if (stop_loading)- page().async_stop_loading();-- auto const* current_entry = traversable().session_history().current_entry();- if (!current_entry) {- if (m_webdriver_navigation_observation.has_value())- complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);- dump_session_history(reason);- return true;- }-- set_url(current_entry->url);- if (m_webdriver_navigation_observation.has_value())- complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);- dump_session_history(reason);- return true;-}-void ViewImplementation::run_webdriver_content_command(u64 command_id, Web::WebDriver::SessionBrowsingContext browsing_context, String const& name, JsonValue payload, Vector<String> arguments){Optional<Web::HTML::CrossProcessId> navigable_id;@@ -3252,8 +3207,11 @@ void ViewImplementation::request_history_operation(Badge<WebContentPage>, WebCon},[](auto const&) { return false; });auto requested_operation_completion = [this, reloads_top_level, finalizes_top_level_cross_document_navigation](Web::HTML::HistoryStepResult result, Optional<i32> committed_step) {- if (reloads_top_level && result != Web::HTML::HistoryStepResult::Applied)- did_cancel_navigation({});+ if (reloads_top_level && result != Web::HTML::HistoryStepResult::Applied && !traversable().tracked_load_navigation_id().has_value()) {+ set_loading_state(false);+ if (m_webdriver_navigation_observation.has_value())+ complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);+ }if (finalizes_top_level_cross_document_navigation && result == Web::HTML::HistoryStepResult::Applied) {if (auto const* current_entry = traversable().session_history().current_entry())set_url(current_entry->url);diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 3390135220d..dae7c567582 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -111,6 +111,8 @@ public:CanonicalTraversable& traversable() const;virtual void prepare_page_for_tab(WebContentPage&);void did_change_display_page(Badge<CanonicalNavigable>, RefPtr<WebContentPage> previous_page);+ void did_cancel_loading(Badge<CanonicalNavigable>, bool canceled_ongoing_navigation);+ void did_stop_loading(Badge<CanonicalTraversable>, bool stopped_navigation);void set_url(Badge<WebContentPage>, URL::URL url) { set_url(move(url)); }URL::URL const& url() const { return m_url; }@@ -146,7 +148,6 @@ public:void reload();void stop_loading();bool is_loading() const { return m_is_loading; }- void cancel_uncommitted_top_level_navigation_for_browser_traversal();bool crash_overlay_active() const { return m_crash_state.has_value(); }String crash_overlay_failed_url() const;@@ -566,10 +567,9 @@ protected:explicit ViewImplementation(IsPrivate = IsPrivate::No);void set_url(URL::URL);- void did_start_navigation(Optional<Utf16String> const& navigation_id);- void did_cancel_loading(Optional<Utf16String> const& navigation_id);- bool did_cancel_navigation(Optional<Utf16String> const& navigation_id);+ void did_start_navigation();void did_finish_navigation();+ void did_end_uncommitted_navigation(StringView reason);void set_loading_state(bool);void complete_webdriver_navigation_completion(u64 request_id, Web::WebDriver::Response);void set_webdriver_current_browsing_context(CanonicalNavigable const&);@@ -590,7 +590,6 @@ protected:void dump_session_history(StringView reason, SessionHistoryDumpMode = SessionHistoryDumpMode::IfDebuggingEnabled) const;void recover_current_session_history_entry_with_history_operation();void reconstruct_current_session_history_entry_with_history_operation(StringView reason);- bool cancel_uncommitted_top_level_navigation(StringView reason, bool stop_loading);NonnullRefPtr<Core::Promise<Empty>> reset_session_history_for_testing();virtual void update_zoom();diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex c14b659911b..09915d7bc4e 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -421,7 +421,7 @@ void WebContentPage::maybe_record_history_visit_for_current_load(URL::URL const&m_history_recorded_url_for_current_load = normalized_url.release_value();}-void WebContentPage::begin_top_level_load(Optional<Utf16String> navigation_id, URL::URL const& url)+void WebContentPage::begin_top_level_load(URL::URL const& url){if (auto process = WebView::Application::the().find_process(client().pid()); process.has_value())process->set_title(OptionalNone {});@@ -431,7 +431,7 @@ void WebContentPage::begin_top_level_load(Optional<Utf16String> navigation_id, Uauto& view = this->view();view.m_history_visit_transition_for_current_load = view.m_history_visit_transition_for_next_load;view.m_history_visit_transition_for_next_load = HistoryVisitTransition::Link;- view.did_start_navigation(navigation_id);+ view.did_start_navigation();view.set_url({}, url);view.set_title({}, Utf16String::from_utf8(url.serialize()));@@ -1962,7 +1962,7 @@ void WebContentPage::did_request_navigation_start(Web::HTML::CrossProcessId navi});if (target_navigable->is_top_level_traversable()) {if (displays_tab())- begin_top_level_load(move(navigation_id), url);+ begin_top_level_load(url);}return;}@@ -2024,7 +2024,7 @@ void WebContentPage::did_complete_navigation_unload_check(Web::HTML::CrossProces// load here.if (navigable->is_top_level_traversable()) {if (displays_tab())- begin_top_level_load(move(navigation_id), ongoing_navigation->loader()->request().history_entry.url);+ begin_top_level_load(ongoing_navigation->loader()->request().history_entry.url);}}@@ -2047,16 +2047,8 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProcereturn;}- auto end_recorded_load_for_canceled_navigation = [&] {- if (!navigable->is_top_level_traversable())- return;- if (displays_tab())- view().did_cancel_loading(navigation_id);- };-if (!result.has_value()) {- end_recorded_load_for_canceled_navigation();- navigable->clear_ongoing_navigation();+ navigable->did_cancel_navigation(navigation_id);return;}@@ -2069,8 +2061,7 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProceif (ongoing_navigation->history_entry->update_from_descriptor(Web::HTML::create_session_history_entry_descriptor(acquiring_loader.request().history_entry, 0)).is_error()) {auto rejected_result = acquiring_loader.take_result();NavigationLoader::discard(client().is_private(), rejected_result);- end_recorded_load_for_canceled_navigation();- navigable->clear_ongoing_navigation();+ navigable->did_cancel_navigation(navigation_id);client().did_misbehave("did_finish_navigation_params_creation"sv, "replacement document state forms a cycle"sv);return;}@@ -2080,24 +2071,10 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProce// The page may have closed while the body was fetched; its navigation then has nothing left to finish.if (!page->is_open())return;- auto* open_page = page.ptr();- auto cancel_navigation = [&](CanonicalNavigable& navigable) {- if (navigable.is_top_level_traversable() && open_page->displays_tab())- open_page->view().did_cancel_loading(navigation_id);- navigable.clear_ongoing_navigation();- };- if (!succeeded) {- if (auto navigable = open_page->population_worker_navigable(navigable_id); navigable.has_value())- cancel_navigation(*navigable);+ if (succeeded && page->continue_navigation_population_in_selected_process(navigable_id, navigation_id))return;- }- if (open_page->continue_navigation_population_in_selected_process(navigable_id, navigation_id))- return;- if (auto navigable = open_page->population_worker_navigable(navigable_id); navigable.has_value()) {- auto const* ongoing_navigation = navigable->ongoing_navigation();- if (ongoing_navigation && ongoing_navigation->navigation_id == navigation_id)- cancel_navigation(*navigable);- }+ if (auto navigable = page->population_worker_navigable(navigable_id); navigable.has_value())+ navigable->did_cancel_navigation(navigation_id);});}@@ -2127,11 +2104,7 @@ void WebContentPage::did_fail_navigation_population(Web::HTML::CrossProcessId napopulating->loader->reclaim_response_body_after_failed_handoff();m_history_recorded_url_for_current_load.clear();- if (navigable->is_top_level_traversable()) {- view().did_cancel_loading(navigation_id);- return;- }- navigable->clear_ongoing_navigation();+ navigable->did_cancel_navigation(move(navigation_id));}void WebContentPage::did_change_hosted_navigable_state(Web::HTML::CrossProcessId navigable_id, Web::HTML::HostedNavigableState state)@@ -2234,12 +2207,8 @@ Messages::WebContentClient::DidStartDownloadResponse WebContentPage::did_start_d&& !ongoing_navigation->state.has<CanonicalNavigation::AcquiringResponseBody>()&& ongoing_navigation->loader()&& ongoing_navigation->loader()->response_body_matches(request_server_client_id, request_server_request_id)) {- if (navigable->is_top_level_traversable()) {- if (displays_tab())- view().did_cancel_loading(ongoing_navigation->navigation_id);- }matches_in_flight_navigation = true;- navigable->clear_ongoing_navigation();+ navigable->did_cancel_navigation(*ongoing_navigation->navigation_id);}}if (!matches_in_flight_navigation)@@ -2297,10 +2266,9 @@ void WebContentPage::did_finish_loading(Web::HTML::CrossProcessId navigable_id,if (navigable->active_document().host() != this)return;- if (!navigable->is_top_level_traversable()) {- navigable->clear_active_document_load();+ navigable->clear_active_document_load();+ if (!navigable->is_top_level_traversable())return;- }if (displays_tab()) {auto const& committed_url = view().url();diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 0fdcb202534..40a171c1294 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -121,7 +121,7 @@ public:void set_needs_beforeunload_check(bool needs_beforeunload_check) { m_needs_beforeunload_check = needs_beforeunload_check; }void clear_history_recorded_url_for_current_load() { m_history_recorded_url_for_current_load.clear(); }- void begin_top_level_load(Optional<Utf16String> navigation_id, URL::URL const&);+ void begin_top_level_load(URL::URL const&);void begin_navigation_unload_check(CanonicalNavigable&, Utf16String const& navigation_id);void request_close();