Truly independent web browser
LibWeb: Pass a container's loading cache mode to cross-process children
Andreas Klingee022d78d260
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 90c551c4019..4c669421578 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -634,13 +634,10 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type typedocument->m_browsing_context_group_has_remote_contexts = navigation_params.request->browsing_context_group_has_multiple_contexts();if (auto cache_mode = navigation_params.request->cache_mode(); cache_mode == HTTP::CacheMode::Reload || cache_mode == HTTP::CacheMode::ForceCache)document->m_navigation_cache_mode = cache_mode;- }-- // AD-HOC: A document that is not fetched, such as the one of a srcdoc iframe, loads the way its container document- // does. Its requests use the same cache mode while it loads.- if (!navigation_params.request) {- if (auto container_document = navigation_params.navigable->container_document())- document->m_navigation_cache_mode = container_document->cache_mode_while_loading();+ } else if (navigation_params.navigable->parent()) {+ // AD-HOC: A document that is not fetched, such as the one of a srcdoc iframe, loads the way its container+ // document does. Its requests use the same cache mode while it loads.+ document->m_navigation_cache_mode = navigation_params.navigable->container_state().document_cache_mode_while_loading;}// NOTE: Step 16 is treated as the else branch of step 15. Otherwise, redirectCount is undefined when the fetch@@ -5106,9 +5103,15 @@ void Document::completely_finish_loading()// loading before they are activated. The timestamp must still be recorded so// that activate_history_entry can detect this and re-trigger the container// notification steps.- if (!m_completely_loaded_time.has_value())+ if (!m_completely_loaded_time.has_value()) {m_completely_loaded_time = AK::UnixDateTime::now();+ // NB: The requests of this document no longer use the cache mode of its navigation, and neither do the+ // navigations of its child navigables, which a process that hosts one reads from its container's state.+ if (m_navigation_cache_mode != HTTP::CacheMode::Default)+ report_container_state_of_child_navigables();+ }+GC::RootVector time_containers_to_begin { exchange(m_smil_time_containers_to_begin, {}) };for (auto& time_container : time_containers_to_begin)time_container->begin();@@ -6588,6 +6591,16 @@ void Document::destroy_a_document_and_its_descendants(GC::Ptr<GC::Function<void(// NB: This is handled by destruction_state.}+void Document::report_container_state_of_child_navigables()+{+ // NB: Navigable containers in shadow trees have child navigables too.+ for_each_shadow_including_descendant([&](Node& node) {+ if (auto* container = as_if<HTML::NavigableContainer>(node); container && container->content_navigable())+ page().client().page_did_change_navigable_container_state(container->content_navigable()->id(), container->replicated_container_state());+ return TraversalDecision::Continue;+ });+}+// https://html.spec.whatwg.org/multipage/document-lifecycle.html#abort-a-documentvoid Document::abort(){@@ -6603,7 +6616,8 @@ void Document::abort()// NB: An aborted document may never become completely loaded. Its load is over, so the requests it makes from now// on must not use the cache mode of its navigation anymore.- m_navigation_cache_mode = HTTP::CacheMode::Default;+ if (exchange(m_navigation_cache_mode, HTTP::CacheMode::Default) != HTTP::CacheMode::Default && !is_completely_loaded())+ report_container_state_of_child_navigables();auto cancel_fetch = [&](GC::Ptr<Fetch::Infrastructure::FetchController> controller) {if (!controller || controller->state() != Fetch::Infrastructure::FetchController::State::Ongoing)diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 11cb8803c7a..8aedfb2c7a8 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -963,6 +963,10 @@ public:// own use this cache mode too. Otherwise, this is "default".HTTP::CacheMode cache_mode_while_loading() const { return is_completely_loaded() ? HTTP::CacheMode::Default : m_navigation_cache_mode; }+ // Reports the state of the containers of this document's child navigables to the UI process, which forwards it to+ // the processes that host child navigables whose documents are in another process.+ void report_container_state_of_child_navigables();+GC::Ptr<Element> current_script() const { return m_current_script; }Variant<GC::Ref<HTML::HTMLScriptElement>, GC::Ref<SVG::SVGScriptElement>, Empty> current_script_for_bindings() const;void set_current_script(Badge<HTML::HTMLOrSVGScriptElement>, GC::Ptr<Element> script) { m_current_script = move(script); }diff --git a/Libraries/LibWeb/HTML/HTMLMetaElement.cpp b/Libraries/LibWeb/HTML/HTMLMetaElement.cppindex bb06bb573af..b7c753336c4 100644--- a/Libraries/LibWeb/HTML/HTMLMetaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMetaElement.cpp@@ -19,8 +19,6 @@#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/HTMLHeadElement.h>#include <LibWeb/HTML/HTMLMetaElement.h>-#include <LibWeb/HTML/Navigable.h>-#include <LibWeb/HTML/NavigableContainer.h>#include <LibWeb/HTML/PolicyContainers.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/Page/Page.h>@@ -37,16 +35,6 @@ HTMLMetaElement::HTMLMetaElement(DOM::Document& document, DOM::QualifiedName quaHTMLMetaElement::~HTMLMetaElement() = default;-static void report_container_state_of_child_navigables(DOM::Document& document)-{- // NB: Navigable containers in shadow trees have child navigables too.- document.for_each_shadow_including_descendant([&](DOM::Node& node) {- if (auto* container = as_if<NavigableContainer>(node); container && container->content_navigable())- document.page().client().page_did_change_navigable_container_state(container->content_navigable()->id(), container->replicated_container_state());- return TraversalDecision::Continue;- });-}-Optional<HTMLMetaElement::HttpEquivAttributeState> HTMLMetaElement::http_equiv_state() const{auto value = get_attribute_ns({}, HTML::AttributeNames::http_equiv).value_or({});@@ -239,7 +227,7 @@ void HTMLMetaElement::inserted()// NB: A process hosting a child navigable reads its container's node document's CSP list from the// container's state: Its frame-src governs where the child navigable can be navigated to, and a// document created in the child navigable inherits its insecure requests policy.- report_container_state_of_child_navigables(document());+ document().report_container_state_of_child_navigables();break;}default:diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 2008ecb2ed5..113f637b3fc 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -2738,8 +2738,9 @@ static void create_navigation_params_by_fetching(// AD-HOC: While a document loads bypassing the HTTP cache, or using it without revalidation, so do the documents// of its child navigables. Bypassing the cache wins over a history traversal of the child navigable too.- if (auto container_document = navigable->container_document()) {- auto container_cache_mode = container_document->cache_mode_while_loading();+ // The container state carries the mode when the container document is in another process.+ if (navigable->parent()) {+ auto container_cache_mode = navigable->container_state().document_cache_mode_while_loading;if (request->cache_mode() == HTTP::CacheMode::Default|| (request->cache_mode() == HTTP::CacheMode::ForceCache && container_cache_mode == HTTP::CacheMode::Reload))request->set_cache_mode(container_cache_mode);diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cppindex cf0b15a1179..7b7766d3b62 100644--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp@@ -638,6 +638,7 @@ ReplicatedContainerState NavigableContainer::replicated_container_state()state.document_upgrade_insecure_navigations_set = document_policy_container->upgrade_insecure_navigations_set();state.document_csp_list = document_policy_container->csp_list.serialize();state.used_color_scheme = m_used_color_scheme;+ state.document_cache_mode_while_loading = document().cache_mode_while_loading();return state;}diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cppindex e38460f7aef..89a1aa148c0 100644--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp@@ -27,6 +27,7 @@ ErrorOr<void> encode(Encoder& encoder, Web::HTML::ReplicatedContainerState constTRY(encoder.encode(state.document_upgrade_insecure_navigations_set));TRY(encoder.encode(state.document_csp_list));TRY(encoder.encode(to_underlying(state.used_color_scheme)));+ TRY(encoder.encode(to_underlying(state.document_cache_mode_while_loading)));return {};}@@ -39,6 +40,16 @@ static ErrorOr<Web::CSS::PreferredColorScheme> decode_used_color_scheme(Decoder&return value;}+// A document loads with the default cache mode, or after a navigation that bypassed the HTTP cache or used it without+// revalidation.+static ErrorOr<HTTP::CacheMode> decode_document_cache_mode_while_loading(Decoder& decoder)+{+ auto value = static_cast<HTTP::CacheMode>(TRY(decoder.decode<UnderlyingType<HTTP::CacheMode>>()));+ if (value != HTTP::CacheMode::Default && value != HTTP::CacheMode::Reload && value != HTTP::CacheMode::ForceCache)+ return Error::from_string_literal("Invalid document cache mode");+ return value;+}+template<>ErrorOr<Web::HTML::ReplicatedContainerState> decode(Decoder& decoder){@@ -56,6 +67,7 @@ ErrorOr<Web::HTML::ReplicatedContainerState> decode(Decoder& decoder).document_upgrade_insecure_navigations_set = TRY(decoder.decode<Web::UpgradeInsecureRequests::UpgradeInsecureNavigationsSet>()),.document_csp_list = TRY(decoder.decode<Vector<Web::ContentSecurityPolicy::SerializedPolicy>>()),.used_color_scheme = TRY(decode_used_color_scheme(decoder)),+ .document_cache_mode_while_loading = TRY(decode_document_cache_mode_while_loading(decoder)),};}diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.hindex c44be160345..edc297c6496 100644--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h@@ -9,6 +9,7 @@#include <AK/Optional.h>#include <AK/Utf16FlyString.h>#include <AK/Utf16String.h>+#include <LibHTTP/Cache/CacheMode.h>#include <LibIPC/Forward.h>#include <LibURL/Origin.h>#include <LibURL/URL.h>@@ -74,6 +75,10 @@ struct ReplicatedContainerState {// https://drafts.csswg.org/css-color-adjust-1/#color-scheme-effect// The container's used color scheme, which decides whether the canvas of the navigable's document is opaque.CSS::PreferredColorScheme used_color_scheme { CSS::PreferredColorScheme::Light };++ // The cache mode that the container's node document loads with, which the navigations of the navigable use while+ // that document loads.+ HTTP::CacheMode document_cache_mode_while_loading { HTTP::CacheMode::Default };};// What the process hosting a navigable's active document reports of it to the UI process, which composes thediff --git a/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt b/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txtindex a53d2931568..adf2c737415 100644--- a/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt+++ b/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt@@ -3,7 +3,7 @@ reload bypassing the cache: script: pragma: no-cache, cache-control: no-cachereload bypassing the cache: iframe: pragma: no-cache, cache-control: no-cachereload bypassing the cache: cross-site iframe: pragma: no-cache, cache-control: no-cachereload bypassing the cache: script in the cross-site iframe: pragma: no-cache, cache-control: no-cache-reload bypassing the cache: navigation the cross-site iframe starts: no cache headers+reload bypassing the cache: navigation the cross-site iframe starts: pragma: no-cache, cache-control: no-cachereload bypassing the cache: cross-site iframe hosted in another process: truereload: document: cache-control: max-age=0, if-modified-sincereload: script: no cache headers@@ -16,6 +16,6 @@ reload bypassing the cache, with the document cached: script: pragma: no-cache,reload bypassing the cache, with the document cached: iframe: pragma: no-cache, cache-control: no-cachereload bypassing the cache, with the document cached: cross-site iframe: pragma: no-cache, cache-control: no-cachereload bypassing the cache, with the document cached: script in the cross-site iframe: pragma: no-cache, cache-control: no-cache-reload bypassing the cache, with the document cached: navigation the cross-site iframe starts: no cache headers+reload bypassing the cache, with the document cached: navigation the cross-site iframe starts: pragma: no-cache, cache-control: no-cachereload bypassing the cache, with the document cached: fetch() with cache "force-cache": no cache headersreload bypassing the cache, with the document cached: fetch() after the load event: no cache headers