Truly independent web browser
LibWebView: Hold navigations and their loaders by reference count
Shannon Booth7f4fe349876c
diff --git a/Libraries/LibWebView/ApplyHistoryStep.cpp b/Libraries/LibWebView/ApplyHistoryStep.cppindex 677c857f163..fcac115df99 100644--- a/Libraries/LibWebView/ApplyHistoryStep.cpp+++ b/Libraries/LibWebView/ApplyHistoryStep.cpp@@ -539,7 +539,7 @@ void ApplyHistoryStep::set_ongoing_navigation_to_traversal(ChangingNavigable& ch// See https://github.com/whatwg/html/issues/12581.auto is_cross_document_traversal = m_navigation_type == Web::Bindings::NavigationType::Traverse && traversal_crosses_documents;auto ongoing_navigation_id = navigable.ongoing_navigation_id();- auto const* ongoing_navigation = ongoing_navigation_id.has_value() ? navigable.navigation_with_id(*ongoing_navigation_id) : nullptr;+ auto ongoing_navigation = ongoing_navigation_id.has_value() ? navigable.navigation_with_id(*ongoing_navigation_id) : nullptr;if (!is_cross_document_traversal&& ongoing_navigation&& ongoing_navigation->sequence_number > m_operation_sequence_number) {diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cppindex fe684bbec39..e7098de3a95 100644--- a/Libraries/LibWebView/CanonicalNavigable.cpp+++ b/Libraries/LibWebView/CanonicalNavigable.cpp@@ -56,29 +56,24 @@ static Utf16String generate_a_random_uuid()return Utf16String::from_ascii_without_validation(uuid.bytes());}-// The page with the source document, running the navigation's steps before the page to host its document takes over.-WebContentPage* CanonicalNavigation::worker() const+RefPtr<WebContentPage> CanonicalNavigation::worker() const{- if (state.has<CheckingIfUnloadingIsCanceled>() || state.has<CreatingNavigationParams>() || state.has<AcquiringResponseBody>())- return page.ptr();- return nullptr;+ return state.visit([&](auto const& state) { return state.runs_in == RunsIn::Worker ? page : nullptr; });}-WebContentPage* CanonicalNavigation::host() const+RefPtr<WebContentPage> CanonicalNavigation::host() const{- if (state.has<EvaluatingJavaScriptURL>() || state.has<Populating>() || state.has<Finalizing>())- return page.ptr();- return nullptr;+ return state.visit([&](auto const& state) { return state.runs_in == RunsIn::Host ? page : nullptr; });}-NavigationLoader* CanonicalNavigation::loader() const+RefPtr<NavigationLoader> CanonicalNavigation::loader() const{- return state.visit(- [](CreatingNavigationParams const& state) -> NavigationLoader* { return state.loader.ptr(); },- [](AcquiringResponseBody const& state) -> NavigationLoader* { return state.loader.ptr(); },- [](Populating const& state) -> NavigationLoader* { return state.loader.ptr(); },- [](Finalizing const& state) -> NavigationLoader* { return state.loader.ptr(); },- [](auto const&) -> NavigationLoader* { return nullptr; });+ return state.visit([](auto const& state) -> RefPtr<NavigationLoader> {+ if constexpr (requires { state.loader; })+ return state.loader;+ else+ return nullptr;+ });}// A navigation made again from the same source — one the user stopped, or a crash cut short — as a GET: A retry@@ -262,15 +257,7 @@ void CanonicalNavigable::begin_navigation(Web::HTML::PreparedNavigationDescripto// 20. If url's scheme is "javascript", then:if (is_javascript_url) {- set_ongoing_navigation({- .url = url,- .navigation_id = navigation_id,- .sequence_number = traversable.next_sequence_number(),- .state = CanonicalNavigation::EvaluatingJavaScriptURL {},- .page = host,- });- if (is_top_level_traversable() && host->displays_tab())- host->begin_top_level_load(url);+ begin_evaluating_javascript_url(url, navigation_id, *host);// 1. Let request be a new request whose URL is url and whose policy container is sourceSnapshotParams's source// policy container.@@ -1166,7 +1153,7 @@ void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& ent{// A navigation admitted after the operation, such as one a same-document traversal yields to, is newer than it.auto ongoing_navigation_id = this->ongoing_navigation_id();- auto const* ongoing_navigation = ongoing_navigation_id.has_value() ? navigation_with_id(*ongoing_navigation_id) : nullptr;+ auto ongoing_navigation = ongoing_navigation_id.has_value() ? navigation_with_id(*ongoing_navigation_id) : nullptr;auto commits_ongoing_navigation = !ongoing_navigation|| (navigation_id.has_value()? *navigation_id == ongoing_navigation->navigation_id@@ -1260,57 +1247,57 @@ Optional<Utf16String const&> CanonicalNavigable::ongoing_navigation_id() constreturn {};}-CanonicalNavigation* CanonicalNavigable::navigation_with_id(Utf16String const& navigation_id)+RefPtr<CanonicalNavigation> CanonicalNavigable::navigation_with_id(Utf16String const& navigation_id) const{- for (auto& navigation : m_navigations) {+ for (auto const& navigation : m_navigations) {if (navigation->navigation_id == navigation_id)- return navigation.ptr();+ return navigation;}return nullptr;}-CanonicalNavigation const* CanonicalNavigable::navigation_with_id(Utf16String const& navigation_id) const-{- return const_cast<CanonicalNavigable&>(*this).navigation_with_id(navigation_id);-}--CanonicalNavigation* CanonicalNavigable::uncommitted_navigation()+RefPtr<CanonicalNavigation> CanonicalNavigable::uncommitted_navigation() const{auto navigation_id = ongoing_navigation_id();if (!navigation_id.has_value())return nullptr;- auto* navigation = navigation_with_id(*navigation_id);+ auto navigation = navigation_with_id(*navigation_id);return navigation && !navigation->is_finalizing() ? navigation : nullptr;}-CanonicalNavigation const* CanonicalNavigable::uncommitted_navigation() const-{- return const_cast<CanonicalNavigable&>(*this).uncommitted_navigation();-}--CanonicalNavigation const* CanonicalNavigable::navigation_being_finalized() const+RefPtr<CanonicalNavigation> CanonicalNavigable::navigation_being_finalized() const{for (size_t i = m_navigations.size(); i > 0; --i) {if (m_navigations[i - 1]->is_finalizing())- return m_navigations[i - 1].ptr();+ return m_navigations[i - 1];}return nullptr;}// https://html.spec.whatwg.org/multipage/browsing-the-web.html#set-the-ongoing-navigation-// NB: The steps of a navigation the ongoing navigation no longer names end, unless they appended its finalization. The-// page evaluating a javascript: URL goes on with it, and reports how it ends (whatwg/html#12120).+// NB: The steps of a navigation the ongoing navigation no longer names end.void CanonicalNavigable::set_the_ongoing_navigation(OngoingNavigation value){+ // NB: Whatever the ongoing navigation becomes, the navigation start the pending blob URL handle covers has been+ // announced, and its navigation holds a handle of its own, or is not coming.+ m_pending_navigation_blob_url = {};++ // 1. If navigable's ongoing navigation is equal to newValue, then return.+ if (m_ongoing_navigation == value)+ return;++ // 2. Inform the navigation API about aborting navigation given navigable.+ // NB: The page holding navigable does this as it sets its own ongoing navigation.++ // 3. Set navigable's ongoing navigation to newValue.m_ongoing_navigation = move(value);auto ongoing_navigation_id = this->ongoing_navigation_id();- Vector<NonnullOwnPtr<CanonicalNavigation>> ended_navigations;+ Vector<NonnullRefPtr<CanonicalNavigation>> ended_navigations;for (size_t i = 0; i < m_navigations.size();) {auto const& navigation = *m_navigations[i];- auto ends = !navigation.is_finalizing()- && (!ongoing_navigation_id.has_value() || navigation.navigation_id != *ongoing_navigation_id)- && !navigation.state.has<CanonicalNavigation::EvaluatingJavaScriptURL>();+ auto ends = navigation.ends_when_superseded()+ && (!ongoing_navigation_id.has_value() || navigation.navigation_id != *ongoing_navigation_id);if (ends)ended_navigations.append(m_navigations.take(i));else@@ -1324,22 +1311,35 @@ void CanonicalNavigable::set_the_ongoing_navigation(OngoingNavigation value)}}-void CanonicalNavigable::set_ongoing_navigation(CanonicalNavigation navigation)+void CanonicalNavigable::set_ongoing_navigation(CanonicalNavigation::Start navigation){// NB: Taken before the handle covering this navigation's start is dropped below, so that a revoked entry is not// let go of in between.auto blob_url = BlobURLHandle::for_url(blob_url_store(), navigation.url);auto navigation_id = navigation.navigation_id;- m_navigations.append(make<CanonicalNavigation>(move(navigation)));+ auto ongoing_navigation = CanonicalNavigation::create(move(navigation));+ ongoing_navigation->blob_url = move(blob_url);+ m_navigations.append(move(ongoing_navigation));set_the_ongoing_navigation(move(navigation_id));- m_pending_navigation_blob_url = {};- m_navigation_blob_url = move(blob_url);+}++// Navigate, step 20, in the page hosting navigable's active document.+void CanonicalNavigable::begin_evaluating_javascript_url(URL::URL const& url, Utf16String const& navigation_id, WebContentPage& host)+{+ set_ongoing_navigation({+ .url = url,+ .navigation_id = navigation_id,+ .sequence_number = top_level_traversable().next_sequence_number(),+ .state = CanonicalNavigation::EvaluatingJavaScriptURL {},+ .page = host,+ });+ if (is_top_level_traversable() && host.displays_tab())+ host.begin_top_level_load(url);}void CanonicalNavigable::set_ongoing_navigation_to_traversal(Web::HTML::CrossProcessId operation_id){- VERIFY(!ongoing_navigation_id().has_value());set_the_ongoing_navigation(Traversal { operation_id });}@@ -1352,10 +1352,6 @@ void CanonicalNavigable::clear_ongoing_navigation_traversal(Web::HTML::CrossProcvoid CanonicalNavigable::clear_ongoing_navigation(){set_the_ongoing_navigation(Empty {});-- // NB: The navigation this covered has either been announced, and is held below, or is not coming.- m_pending_navigation_blob_url = {};- m_navigation_blob_url = {};}void CanonicalNavigable::end_navigation(CanonicalNavigation& navigation)@@ -1383,15 +1379,15 @@ void CanonicalNavigable::retain_blob_url_token(URL::BlobURLEntry::Token token)void CanonicalNavigable::cancel_navigation_for_client(WebContentClient& client){- auto is_page_of_client = [&](WebContentPage const* page) {+ auto is_page_of_client = [&](RefPtr<WebContentPage> const& page) {return page && &page->client() == &client;};- Vector<CanonicalNavigation*> canceled_navigations;- for (auto& navigation : m_navigations) {+ Vector<NonnullRefPtr<CanonicalNavigation>> canceled_navigations;+ for (auto const& navigation : m_navigations) {if (!navigation->is_finalizing() && (is_page_of_client(navigation->worker()) || is_page_of_client(navigation->host())))- canceled_navigations.append(navigation.ptr());+ canceled_navigations.append(navigation);}- for (auto* navigation : canceled_navigations)+ for (auto const& navigation : canceled_navigations)end_navigation(*navigation);}@@ -1399,7 +1395,7 @@ void CanonicalNavigable::cancel_navigation_for_client(WebContentClient& client)// going to be.void CanonicalNavigable::did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::HistoryStepResult result){- auto* navigation = navigation_with_id(navigation_id);+ auto navigation = navigation_with_id(navigation_id);if (!navigation || !navigation->is_finalizing())return;end_navigation(*navigation);@@ -1409,7 +1405,7 @@ void CanonicalNavigable::did_finish_finalizing_navigation(Utf16String const& navvoid CanonicalNavigable::did_cancel_navigation(Utf16String navigation_id){- auto* navigation = navigation_with_id(navigation_id);+ auto navigation = navigation_with_id(navigation_id);auto canceled_uncommitted_navigation = navigation && !navigation->is_finalizing();auto is_tracked_load = tracked_load_navigation_id() == navigation_id;if (canceled_uncommitted_navigation)@@ -1427,7 +1423,7 @@ Optional<Utf16String> CanonicalNavigable::tracked_load_navigation_id() const{if (auto navigation_id = ongoing_navigation_id(); navigation_id.has_value())return *navigation_id;- if (auto const* navigation = navigation_being_finalized())+ if (auto navigation = navigation_being_finalized())return navigation->navigation_id;return m_active_document_load.navigation_id;}diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.hindex f8919ad55e2..c665a3586bf 100644--- a/Libraries/LibWebView/CanonicalNavigable.h+++ b/Libraries/LibWebView/CanonicalNavigable.h@@ -210,24 +210,25 @@ public:// NB: "traversal" names the history operation that set it, so that another operation leaves it alone.struct Traversal {Web::HTML::CrossProcessId operation_id;++ bool operator==(Traversal const&) const = default;};using OngoingNavigation = Variant<Empty, Traversal, Utf16String>;OngoingNavigation const& ongoing_navigation() const { return m_ongoing_navigation; }Optional<Utf16String const&> ongoing_navigation_id() const;- CanonicalNavigation* navigation_with_id(Utf16String const&);- CanonicalNavigation const* navigation_with_id(Utf16String const&) const;+ RefPtr<CanonicalNavigation> navigation_with_id(Utf16String const&) const;// The navigation the ongoing navigation names, until its finalization is appended.- CanonicalNavigation* uncommitted_navigation();- CanonicalNavigation const* uncommitted_navigation() const;+ RefPtr<CanonicalNavigation> uncommitted_navigation() const;bool has_uncommitted_navigation() const { return uncommitted_navigation(); }- CanonicalNavigation const* navigation_being_finalized() const;+ RefPtr<CanonicalNavigation> navigation_being_finalized() const;// Held so that revoking a blob URL cannot take the entry away from a navigation on its way to this navigable, or// from the document it loaded. Session history holds none, so a revoked blob URL cannot be traversed back to.void retain_blob_url_token(URL::BlobURLEntry::Token);- void set_ongoing_navigation(CanonicalNavigation);+ void set_ongoing_navigation(CanonicalNavigation::Start);+ void begin_evaluating_javascript_url(URL::URL const&, Utf16String const& navigation_id, WebContentPage& host);void set_ongoing_navigation_to_traversal(Web::HTML::CrossProcessId operation_id);void clear_ongoing_navigation_traversal(Web::HTML::CrossProcessId operation_id);void clear_ongoing_navigation();@@ -255,14 +256,13 @@ private:void set_the_ongoing_navigation(OngoingNavigation);void end_navigation(CanonicalNavigation&);OngoingNavigation m_ongoing_navigation;- Vector<NonnullOwnPtr<CanonicalNavigation>> m_navigations;+ Vector<NonnullRefPtr<CanonicalNavigation>> m_navigations;RefPtr<WebContentClient> process_to_host_for_site_isolation(CanonicalDocument const&, Optional<URL::Origin> const& initiator_origin) const;ErrorOr<NonnullRefPtr<WebContentPage>> obtain_page_to_host_in(RefPtr<WebContentClient> process);BlobURLStore* blob_url_store() const;BlobURLHandle m_pending_navigation_blob_url;- BlobURLHandle m_navigation_blob_url;BlobURLHandle m_document_blob_url;ActiveDocumentLoad m_active_document_load;Optional<Web::DevicePixelRect> m_viewport_rect;diff --git a/Libraries/LibWebView/CanonicalNavigation.h b/Libraries/LibWebView/CanonicalNavigation.hindex d521f4c61c6..1ba02a5c4ac 100644--- a/Libraries/LibWebView/CanonicalNavigation.h+++ b/Libraries/LibWebView/CanonicalNavigation.h@@ -10,12 +10,14 @@#include <AK/NonnullRefPtr.h>#include <AK/Optional.h>#include <AK/OwnPtr.h>+#include <AK/RefCounted.h>#include <AK/RefPtr.h>#include <AK/Types.h>#include <AK/Utf16String.h>#include <AK/Variant.h>#include <LibURL/URL.h>#include <LibWebCommon/HTML/NavigationPopulationRequest.h>+#include <LibWebView/BlobURLStore.h>#include <LibWebView/CanonicalDocument.h>#include <LibWebView/CanonicalSessionHistoryEntry.h>#include <LibWebView/NavigationLoader.h>@@ -24,38 +26,62 @@namespace WebView {// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-// The steps navigate runs for one navigation, from its admission until its document is activated or it ends without-// one. They go on after the navigable's ongoing navigation stops being their navigation ID only once their-// finalization is appended, or while a javascript: URL is evaluated.-class CanonicalNavigation {+// One navigation, from its admission until its document is activated or it ends without one.+class CanonicalNavigation final : public RefCounted<CanonicalNavigation> {AK_ALLOC_WITH_KMALLOC;public:- // Navigating to a javascript: URL, in the page with the document it is evaluated against.- struct EvaluatingJavaScriptURL { };- // Navigate, step 23.1: checking if unloading is canceled, in the page with the source document.+ // The worker is the page with the source document; the host is the page to host the new one.+ enum class RunsIn : u8 {+ Worker,+ Host,+ };++ // Step 20.+ struct EvaluatingJavaScriptURL {+ static constexpr auto runs_in = RunsIn::Host;+ };+ // Step 23.1.struct CheckingIfUnloadingIsCanceled {+ static constexpr auto runs_in = RunsIn::Worker;Web::HTML::NavigationStartRequest start_request;};- // Attempting to populate the history entry's document, steps 1 to 4, in the page with the source document.+ // Attempt to populate the history entry's document, steps 1 to 4.struct CreatingNavigationParams {- NonnullOwnPtr<NavigationLoader> loader;+ static constexpr auto runs_in = RunsIn::Worker;+ NonnullRefPtr<NavigationLoader> loader;};- // NB: The page with the source document hands the response body over to the page to host the document.+ // NB: The worker hands the response body over to the host.struct AcquiringResponseBody {- NonnullOwnPtr<NavigationLoader> loader;+ static constexpr auto runs_in = RunsIn::Worker;+ NonnullRefPtr<NavigationLoader> loader;};- // Attempting to populate the history entry's document, step 5 on, in the page to host it.+ // Attempt to populate the history entry's document, step 5 on.struct Populating {- NonnullOwnPtr<NavigationLoader> loader;+ static constexpr auto runs_in = RunsIn::Host;+ NonnullRefPtr<NavigationLoader> loader;};- // Attempting to populate the history entry's document is over, and its completion steps appended finalizing the- // navigation to the traversal queue.+ // The completion steps appended finalizing the navigation to the traversal queue.struct Finalizing {- OwnPtr<NavigationLoader> loader;+ static constexpr auto runs_in = RunsIn::Host;+ RefPtr<NavigationLoader> loader;};using State = Variant<EvaluatingJavaScriptURL, CheckingIfUnloadingIsCanceled, CreatingNavigationParams, AcquiringResponseBody, Populating, Finalizing>;+ struct Start {+ URL::URL url;+ Utf16String navigation_id;+ Optional<Web::HTML::PreparedNavigationDescriptor> retry {};+ u64 sequence_number { 0 };+ State state;+ RefPtr<WebContentPage> page {};+ RefPtr<CanonicalSessionHistoryEntry> history_entry {};+ };+ static NonnullRefPtr<CanonicalNavigation> create(Start start)+ {+ return adopt_ref(*new CanonicalNavigation(move(start)));+ }+URL::URL url;Utf16String navigation_id;// The navigation to make again if the user stops this one and reloads, or a crash cuts it short. Kept from the start@@ -67,11 +93,27 @@ public:RefPtr<WebContentPage> page {};// Navigate's historyEntry, whose document state holds the document populated for it.RefPtr<CanonicalSessionHistoryEntry> history_entry {};+ // Held so that revoking the blob URL the navigation is to cannot take the entry away from it.+ BlobURLHandle blob_url;- WebContentPage* worker() const;- WebContentPage* host() const;- NavigationLoader* loader() const;+ RefPtr<WebContentPage> worker() const;+ RefPtr<WebContentPage> host() const;+ RefPtr<NavigationLoader> loader() const;bool is_finalizing() const { return state.has<Finalizing>(); }+ // AD-HOC: A later navigation doesn't end a javascript: URL's evaluation, only stopping does (whatwg/html#12120).+ bool ends_when_superseded() const { return !is_finalizing() && !state.has<EvaluatingJavaScriptURL>(); }++private:+ explicit CanonicalNavigation(Start start)+ : url(move(start.url))+ , navigation_id(move(start.navigation_id))+ , retry(move(start.retry))+ , sequence_number(start.sequence_number)+ , state(move(start.state))+ , page(move(start.page))+ , history_entry(move(start.history_entry))+ {+ }};WEBVIEW_API Web::HTML::PreparedNavigationDescriptor prepare_navigation_to_retry(Web::HTML::PreparedNavigationDescriptor);diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cppindex c9361622d09..aa718fcedd2 100644--- a/Libraries/LibWebView/CanonicalTraversable.cpp+++ b/Libraries/LibWebView/CanonicalTraversable.cpp@@ -1032,7 +1032,7 @@ struct CanonicalTraversable::HistoryOperation {Web::HTML::UnloadDisplayedDocument unload_displayed_document { Web::HTML::UnloadDisplayedDocument::No };- OwnPtr<NavigationLoader> population_loader;+ RefPtr<NavigationLoader> population_loader;RefPtr<CanonicalDocument> document;};HashMap<Web::HTML::CrossProcessId, NonnullOwnPtr<PendingChangingJob>> pending_changing_jobs;@@ -1219,7 +1219,7 @@ Optional<CanonicalTraversable::BrowserHistoryTraversalDiagnostic> CanonicalTrave}// The navigation a cross-document navigation's finalization finalizes.-CanonicalNavigation const* CanonicalTraversable::finalized_navigation(HistoryOperation const& operation) const+RefPtr<CanonicalNavigation> CanonicalTraversable::finalized_navigation(HistoryOperation const& operation) const{auto const* parameters = operation.parameters.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>();if (!parameters)@@ -1227,7 +1227,7 @@ CanonicalNavigation const* CanonicalTraversable::finalized_navigation(HistoryOpeauto navigable = find(parameters->navigable_id);if (!navigable.has_value())return nullptr;- auto const* navigation = navigable->navigation_with_id(parameters->navigation_id);+ auto navigation = navigable->navigation_with_id(parameters->navigation_id);return navigation && navigation->is_finalizing() ? navigation : nullptr;}@@ -1347,7 +1347,7 @@ bool CanonicalTraversable::navigation_transaction_matches(HistoryOperation constauto const& parameters = operation.parameters.get<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>();if (reply_navigable_id.has_value() && *reply_navigable_id != parameters.navigable_id)return true;- auto const* navigation = finalized_navigation(operation);+ auto navigation = finalized_navigation(operation);return navigation && navigation->host() == &page;}@@ -1664,7 +1664,7 @@ void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operatiohost = source_page;Optional<Utf16String const&> navigation_id;- if (auto const* navigation = finalized_navigation(operation))+ if (auto navigation = finalized_navigation(operation))navigation_id = navigation->navigation_id;navigable->did_commit_navigation(target_entry, move(activated_navigable_state), operation.sequence_number, navigation_id, move(host));@@ -2117,12 +2117,12 @@ void CanonicalTraversable::did_attempt_to_populate_the_history_entry_document(Weauto navigable = find(parameters.navigable_id);if (!navigable.has_value())return;- auto* navigation = navigable->navigation_with_id(parameters.navigation_id);+ auto navigation = navigable->navigation_with_id(parameters.navigation_id);if (!navigation || navigation->is_finalizing() || navigation->host() != &source_page)return;auto is_javascript_url_navigation = navigation->state.has<CanonicalNavigation::EvaluatingJavaScriptURL>();- OwnPtr<NavigationLoader> loader;+ RefPtr<NavigationLoader> loader;if (auto* populating = navigation->state.get_pointer<CanonicalNavigation::Populating>())loader = move(populating->loader);navigation->state = CanonicalNavigation::Finalizing { move(loader) };@@ -2587,7 +2587,7 @@ void CanonicalTraversable::run_direct_history_operation(HistoryOperation& operat},[&](Web::FinalizeCrossDocumentNavigationHistoryOperationParameters const&) {// A navigation that ended before its completion steps appended this has nothing to finalize.- auto const* navigation = finalized_navigation(operation);+ auto navigation = finalized_navigation(operation);if (!navigation || navigation->host() != operation.initiating_page.ptr()) {finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::Applied, {});return;@@ -2642,7 +2642,7 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation}// NB: historyEntry is the navigation's. A javascript: URL navigation whose document was refused has none.- auto const* navigation = finalized_navigation(operation);+ auto navigation = finalized_navigation(operation);if (!navigation || !navigation->history_entry) {finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});return;diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.hindex 929bde5c2bc..153bf60012c 100644--- a/Libraries/LibWebView/CanonicalTraversable.h+++ b/Libraries/LibWebView/CanonicalTraversable.h@@ -187,7 +187,7 @@ private:void session_history_changed();HistoryOperation* find_history_operation(Web::HTML::CrossProcessId operation_id);bool navigation_transaction_matches(HistoryOperation const&, WebContentPage&, Optional<Web::HTML::CrossProcessId> reply_navigable_id = {}) const;- CanonicalNavigation const* finalized_navigation(HistoryOperation const&) const;+ RefPtr<CanonicalNavigation> finalized_navigation(HistoryOperation const&) const;bool update_session_history_entry_persisted_state(CanonicalNavigable&, Web::HTML::SessionHistoryEntryPersistedState const&);void add_history_operation_completion_endpoint(HistoryOperation&, NonnullRefPtr<WebContentPage>);bool select_changing_navigable_history_step_job_endpoint(HistoryOperation&, ApplyHistoryStepJobs::ChangingNavigableHistoryStepJob&);diff --git a/Libraries/LibWebView/NavigationLoader.h b/Libraries/LibWebView/NavigationLoader.hindex df80d3f830e..aa52b21a507 100644--- a/Libraries/LibWebView/NavigationLoader.h+++ b/Libraries/LibWebView/NavigationLoader.h@@ -9,6 +9,7 @@#include <AK/Function.h>#include <AK/Optional.h>#include <AK/OwnPtr.h>+#include <AK/RefCounted.h>#include <AK/RefPtr.h>#include <AK/Weakable.h>#include <LibRequests/Forward.h>@@ -24,13 +25,15 @@ namespace WebView {// UI-process owner of navigation population state. It keeps the pending entry// and response body alive while the document host is being selected.-class WEBVIEW_API NavigationLoader final : public Weakable<NavigationLoader> {+class WEBVIEW_API NavigationLoader final+ : public RefCounted<NavigationLoader>+ , public Weakable<NavigationLoader> {public:AK_ALLOC_WITH_KMALLOC;- static NonnullOwnPtr<NavigationLoader> create(IsPrivate is_private, Web::HTML::NavigationPopulationRequest request)+ static NonnullRefPtr<NavigationLoader> create(IsPrivate is_private, Web::HTML::NavigationPopulationRequest request){- return adopt_own(*new NavigationLoader(is_private, move(request)));+ return adopt_ref(*new NavigationLoader(is_private, move(request)));}~NavigationLoader();diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 518f6f98a10..36e887a093e 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -3248,7 +3248,7 @@ void ViewImplementation::dump_session_history(StringView reason, SessionHistoryDauto traversal = traversable().browser_history_traversal_for_testing();Optional<URL::URL> loading_url;- if (auto const* navigation = traversable().uncommitted_navigation())+ if (auto navigation = traversable().uncommitted_navigation())loading_url = navigation->url;// The page displaying the tab can have lost its process, as when a crash recovery ends.@@ -3375,7 +3375,7 @@ void ViewImplementation::respawn_web_content_process_after_crash()Optional<ViewImplementation::NavigationToRetry> ViewImplementation::navigation_to_retry_for_ongoing_navigation() const{- auto const* navigation = traversable().uncommitted_navigation();+ auto navigation = traversable().uncommitted_navigation();if (!navigation)navigation = traversable().navigation_being_finalized();if (!navigation)diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 1597494e32a..52afd763bec 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -301,7 +301,7 @@ bool WebContentPage::continue_navigation_population_in_selected_process(Web::HTMif (!navigable.has_value())return false;- auto* navigation = navigable->navigation_with_id(navigation_id);+ auto navigation = navigable->navigation_with_id(navigation_id);if (!navigation || !navigation->state.has<CanonicalNavigation::AcquiringResponseBody>())return false;@@ -330,8 +330,8 @@ bool WebContentPage::continue_navigation_population_in_selected_process(Web::HTMauto navigable = host->traversable().top_level_traversable().find(navigable_id);if (!navigable.has_value())return;- auto* navigation = navigable->navigation_with_id(navigation_id);- if (!navigation || !navigation->state.has<CanonicalNavigation::Populating>() || navigation->loader() != weak_loader.ptr())+ auto navigation = navigable->navigation_with_id(navigation_id);+ if (!navigation || !navigation->state.has<CanonicalNavigation::Populating>() || navigation->loader().ptr() != weak_loader.ptr())return;if (!succeeded) {navigable->did_cancel_navigation(navigation_id);@@ -1928,36 +1928,21 @@ void WebContentPage::did_request_navigation_start(Web::HTML::CrossProcessId naviif (start_request.has_value())start_request->initiator_origin = initiator_origin.release_value();- auto sequence_number = target_navigable->top_level_traversable().next_sequence_number();-- // A javascript: navigation runs synchronously in the requesting process and never populates an entry.- // Record its admission without population state, owned by the evaluating process, so its failure or- // produced document is validated against that process.if (!start_request.has_value()) {if (url.scheme() != "javascript"sv) {client().did_misbehave("did_request_navigation_start"sv, "navigation without a start request is not to a javascript: URL"sv);return;}- target_navigable->set_ongoing_navigation(CanonicalNavigation {- .url = url,- .navigation_id = navigation_id,- .sequence_number = sequence_number,- .state = CanonicalNavigation::EvaluatingJavaScriptURL {},- .page = this,- });- if (target_navigable->is_top_level_traversable()) {- if (displays_tab())- begin_top_level_load(url);- }+ target_navigable->begin_evaluating_javascript_url(url, navigation_id, *this);return;}auto retry = prepare_navigation_to_retry(*start_request);- target_navigable->set_ongoing_navigation(CanonicalNavigation {+ target_navigable->set_ongoing_navigation(CanonicalNavigation::Start {.url = move(url),.navigation_id = navigation_id,.retry = move(retry),- .sequence_number = sequence_number,+ .sequence_number = target_navigable->top_level_traversable().next_sequence_number(),.state = CanonicalNavigation::CheckingIfUnloadingIsCanceled { start_request.release_value() },.page = this,});@@ -1993,7 +1978,7 @@ void WebContentPage::did_complete_navigation_unload_check(Web::HTML::CrossProcesif (!navigable.has_value())return;- auto* ongoing_navigation = navigable->navigation_with_id(navigation_id);+ auto ongoing_navigation = navigable->navigation_with_id(navigation_id);auto* checking = ongoing_navigation ? ongoing_navigation->state.get_pointer<CanonicalNavigation::CheckingIfUnloadingIsCanceled>() : nullptr;if (!checking || ongoing_navigation->worker() != this)return;@@ -2023,7 +2008,7 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProcereturn;}- auto* ongoing_navigation = navigable->navigation_with_id(navigation_id);+ auto ongoing_navigation = navigable->navigation_with_id(navigation_id);auto* creating = ongoing_navigation ? ongoing_navigation->state.get_pointer<CanonicalNavigation::CreatingNavigationParams>() : nullptr;if (!creating || ongoing_navigation->worker() != this) {if (result.has_value())@@ -2040,10 +2025,10 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProce// sync with redirects before choosing the process that will run step 5.auto loader = move(creating->loader);ongoing_navigation->state = CanonicalNavigation::AcquiringResponseBody { move(loader) };- auto& acquiring_loader = *ongoing_navigation->loader();- acquiring_loader.did_finish_navigation_params_creation(result.release_value());- if (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();+ NonnullRefPtr acquiring_loader = *ongoing_navigation->loader();+ acquiring_loader->did_finish_navigation_params_creation(result.release_value());+ if (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);navigable->did_cancel_navigation(navigation_id);client().did_misbehave("did_finish_navigation_params_creation"sv, "replacement document state forms a cycle"sv);@@ -2051,7 +2036,7 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProce}ongoing_navigation->url = ongoing_navigation->history_entry->url;- acquiring_loader.acquire_response_body([page = NonnullRefPtr<WebContentPage>(*this), navigable_id, navigation_id = move(navigation_id)](bool succeeded) {+ acquiring_loader->acquire_response_body([page = NonnullRefPtr<WebContentPage>(*this), navigable_id, navigation_id = move(navigation_id)](bool succeeded) {// The page may have closed while the body was fetched; its navigation then has nothing left to finish.if (!page->is_open())return;@@ -2076,7 +2061,7 @@ void WebContentPage::did_fail_navigation_population(Web::HTML::CrossProcessId na// The failure must name the admitted transaction, and must come from a process that owns part of its// outcome: the population worker (unload check, javascript: evaluation) or the population host.- auto* ongoing_navigation = navigable->navigation_with_id(navigation_id);+ auto ongoing_navigation = navigable->navigation_with_id(navigation_id);if (!ongoing_navigation|| ongoing_navigation->is_finalizing()|| (ongoing_navigation->worker() != this && ongoing_navigation->host() != this)) {@@ -2185,7 +2170,7 @@ Messages::WebContentClient::DidStartDownloadResponse WebContentPage::did_start_d// received that response may claim it, and only while its population is in flight.bool matches_in_flight_navigation = false;if (auto navigable = population_worker_navigable(navigable_id); navigable.has_value()) {- auto const* ongoing_navigation = navigation_id.has_value() ? navigable->navigation_with_id(*navigation_id) : nullptr;+ auto ongoing_navigation = navigation_id.has_value() ? navigable->navigation_with_id(*navigation_id) : nullptr;if (ongoing_navigation&& (ongoing_navigation->state.has<CanonicalNavigation::CreatingNavigationParams>() || ongoing_navigation->state.has<CanonicalNavigation::Populating>())&& ongoing_navigation->loader()->response_body_matches(request_server_client_id, request_server_request_id)) {diff --git a/Tests/LibWebView/TestApplyHistoryStep.cpp b/Tests/LibWebView/TestApplyHistoryStep.cppindex 21848f2b0ca..34e65afbb01 100644--- a/Tests/LibWebView/TestApplyHistoryStep.cpp+++ b/Tests/LibWebView/TestApplyHistoryStep.cpp@@ -30,7 +30,7 @@ static URL::URL parse_url(StringView url)// Each test entry has a document state of its own, unless a test gives it one.// A navigation whose steps are checking if unloading is canceled, in no page.-static WebView::CanonicalNavigation navigation_for_testing(Utf16String navigation_id, u64 sequence_number = 0, RefPtr<WebView::CanonicalSessionHistoryEntry> history_entry = {})+static WebView::CanonicalNavigation::Start navigation_for_testing(Utf16String navigation_id, u64 sequence_number = 0, RefPtr<WebView::CanonicalSessionHistoryEntry> history_entry = {}){return {.url = URL::about_blank(),diff --git a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cppindex 5e6fcbd3316..1e0e16ca38f 100644--- a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp+++ b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp@@ -17,7 +17,7 @@#include <LibWebView/CanonicalWindow.h>// A navigation whose steps are checking if unloading is canceled, in no page.-static WebView::CanonicalNavigation navigation_for_testing(Utf16String navigation_id, u64 sequence_number = 0, RefPtr<WebView::CanonicalSessionHistoryEntry> history_entry = {})+static WebView::CanonicalNavigation::Start navigation_for_testing(Utf16String navigation_id, u64 sequence_number = 0, RefPtr<WebView::CanonicalSessionHistoryEntry> history_entry = {}){return {.url = URL::about_blank(),