Skip to content

Truly independent web browser

LibWeb+LibWebView+WebContent: Support bypass-the-cache reloads

sideshowbarker572ae02a753f
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 8001e3432e3..1d39aa05e70 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -557,6 +557,7 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type type
|| navigation_params.request->text_directive_user_activation();
navigation_params.request->set_text_directive_user_activation(false);
document->m_browsing_context_group_has_remote_contexts = navigation_params.request->browsing_context_group_has_multiple_contexts();
+ document->m_navigation_bypassed_cache = navigation_params.request->cache_mode() == HTTP::CacheMode::Reload;
}
// NOTE: Step 16 is treated as the else branch of step 15. Otherwise, redirectCount is undefined when the fetch
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 7abfac44063..35776e8ac77 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -845,6 +845,11 @@ public:
bool browsing_context_group_has_remote_contexts() const { return m_browsing_context_group_has_remote_contexts; }
void set_browsing_context_group_has_remote_contexts(bool value) { m_browsing_context_group_has_remote_contexts = value; }
+ // AD-HOC: Whether this document is still loading after a navigation that bypassed the HTTP cache: a reload that
+ // bypasses it, or a child navigable's navigation while its container document loaded that way. Until it
+ // has completely loaded, the requests it makes without a cache mode of their own bypass the cache too.
+ bool is_loading_bypassing_cache() const { return m_navigation_bypassed_cache && !is_completely_loaded(); }
+
GC::Ptr<HTML::HTMLScriptElement> current_script() const { return m_current_script.ptr(); }
void set_current_script(Badge<HTML::HTMLScriptElement>, GC::Ptr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
static constexpr size_t current_script_offset() { return offsetof(Document, m_current_script); }
@@ -1954,6 +1959,7 @@ private:
// Each Document has a text directive user activation, which is a boolean, initially false.
bool m_text_directive_user_activation { false };
bool m_browsing_context_group_has_remote_contexts { false };
+ bool m_navigation_bypassed_cache { false };
GC::Ptr<HTML::HTMLScriptElement> m_current_script;
u32 m_ignore_destructive_writes_counter { 0 };
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 39fdd569a1a..da9747e3930 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -1919,6 +1919,13 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
http_request->set_cache_mode(HTTP::CacheMode::NoStore);
}
+ // AD-HOC: While a document loads bypassing the HTTP cache, so do the requests it makes without a cache mode of
+ // their own, as do its child navigables' navigations (see create navigation params by fetching).
+ if (http_request->cache_mode() == HTTP::CacheMode::Default && http_request->mode() != Infrastructure::Request::Mode::Navigate && http_request->client()) {
+ if (auto* window = HTML::window_from_global_object(http_request->client()->global_object()); window && window->associated_document().is_loading_bypassing_cache())
+ http_request->set_cache_mode(HTTP::CacheMode::Reload);
+ }
+
// 17. If httpRequest’s cache mode is "no-cache", httpRequest’s prevent no-cache cache-control header
// modification flag is unset, and httpRequest’s header list does not contain `Cache-Control`, then append
// (`Cache-Control`, `max-age=0`) to httpRequest’s header list.
diff --git a/Libraries/LibWeb/HTML/DocumentState.h b/Libraries/LibWeb/HTML/DocumentState.h
index 0b2955bcaea..fda08af9c8d 100644
--- a/Libraries/LibWeb/HTML/DocumentState.h
+++ b/Libraries/LibWeb/HTML/DocumentState.h
@@ -66,6 +66,9 @@ public:
[[nodiscard]] bool reload_pending() const { return m_reload_pending; }
void set_reload_pending(bool reload_pending) { m_reload_pending = reload_pending; }
+ [[nodiscard]] bool reload_bypasses_cache() const { return m_reload_bypasses_cache; }
+ void set_reload_bypasses_cache(bool reload_bypasses_cache) { m_reload_bypasses_cache = reload_bypasses_cache; }
+
[[nodiscard]] bool ever_populated() const { return m_ever_populated; }
void set_ever_populated(bool ever_populated) { m_ever_populated = ever_populated; }
@@ -110,6 +113,12 @@ private:
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-reload-pending
bool m_reload_pending { false };
+ // AD-HOC: Whether the pending reload bypasses the HTTP cache, as the user can ask one to. HTML leaves that to the
+ // user agent: "When a user requests a reload of a navigable, user agents may provide a mechanism for
+ // ignoring any caches when reloading."
+ // https://html.spec.whatwg.org/multipage/speculative-loading.html#nav-traversal-ui
+ bool m_reload_bypasses_cache { false };
+
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-ever-populated
bool m_ever_populated { false };
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
index a112c251aee..948c85fc6a2 100644
--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp
+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
@@ -525,7 +525,9 @@ void HistoryExecutor::run_ui_changing_navigable_history_job(CrossProcessId opera
// AD-HOC: Preserve reload pending for steps 6 and 7 before step 5 clears it.
// See https://github.com/whatwg/html/issues/12760.
auto input_reload_pending = target_entry->document_state()->reload_pending();
+ auto input_reload_bypasses_cache = target_entry->document_state()->reload_bypasses_cache();
target_entry->document_state()->set_reload_pending(false);
+ target_entry->document_state()->set_reload_bypasses_cache(false);
m_page->client().page_did_set_session_history_entry_document_state_reload_pending(
navigable->id(), target_entry->navigation_api_key(), false);
@@ -547,6 +549,7 @@ void HistoryExecutor::run_ui_changing_navigable_history_job(CrossProcessId opera
.navigation_id = {},
};
request.history_entry.document_state.reload_pending = input_reload_pending;
+ request.history_entry.document_state.reload_bypasses_cache = input_reload_bypasses_cache;
// 7. In parallel, attempt to populate the history entry's document for targetEntry, given navigable, potentiallyTargetSpecificSourceSnapshotParams,
// targetSnapshotParams, userInvolvement, with allowPOST set to allowPOST and completionSteps set to
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 5ae7c928860..1f2e0d64f93 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -2573,12 +2573,24 @@ static void create_navigation_params_by_fetching(
// Chromium (FetchCacheMode::kValidateCache) and Firefox (nsIRequest::VALIDATE_ALWAYS); WebKit goes
// further, and bypasses the cache entirely (ReloadIgnoringCacheData).
request->set_cache_mode(HTTP::CacheMode::NoCache);
+
+ // AD-HOC: A reload that bypasses the HTTP cache uses the "reload" cache mode instead, with which "Fetch behaves
+ // as if there is no HTTP cache on the way to the network. Ergo, it creates a normal request and updates
+ // the HTTP cache with the response."
+ if (document_state.reload_bypasses_cache)
+ request->set_cache_mode(HTTP::CacheMode::Reload);
}
// 8. Otherwise, if entry's document state's ever populated is true, then set request's history-navigation flag.
else if (document_state.ever_populated)
request->set_history_navigation(true);
+ // AD-HOC: While a document loads bypassing the HTTP cache, so do the documents of its child navigables.
+ if (request->cache_mode() == HTTP::CacheMode::Default) {
+ if (auto container_document = navigable->container_document(); container_document && container_document->is_loading_bypassing_cache())
+ request->set_cache_mode(HTTP::CacheMode::Reload);
+ }
+
// 9. If sourceSnapshotParams's has transient activation is true, then set request's user-activation to true.
if (source_snapshot_params->has_transient_activation)
request->set_user_activation(true);
diff --git a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
index c1ebf860081..3e4fa896512 100644
--- a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
+++ b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
@@ -53,6 +53,7 @@ static NavigationRequestDescriptor create_navigation_request_descriptor(Fetch::I
.redirect_count = request.redirect_count(),
.text_directive_user_activation = request.text_directive_user_activation(),
.browsing_context_group_has_multiple_contexts = request.browsing_context_group_has_multiple_contexts(),
+ .cache_mode = request.cache_mode(),
};
}
@@ -243,6 +244,7 @@ static GC::Ptr<Fetch::Infrastructure::Request> create_navigation_request_from_de
request->set_redirect_count(descriptor->redirect_count);
request->set_text_directive_user_activation(descriptor->text_directive_user_activation);
request->set_browsing_context_group_has_multiple_contexts(descriptor->browsing_context_group_has_multiple_contexts);
+ request->set_cache_mode(descriptor->cache_mode);
return request;
}
diff --git a/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp b/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp
index 8a41323ca3b..0a1deb8b765 100644
--- a/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp
+++ b/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp
@@ -93,6 +93,7 @@ SessionHistoryDocumentStateDescriptor create_session_history_document_state_desc
.about_base_url = document_state.about_base_url(),
.resource = document_state.resource(),
.reload_pending = document_state.reload_pending(),
+ .reload_bypasses_cache = document_state.reload_bypasses_cache(),
.ever_populated = document_state.ever_populated(),
.user_agent_initiated = document_state.user_agent_initiated(),
.navigable_target_name = document_state.navigable_target_name(),
@@ -211,6 +212,7 @@ void apply_session_history_document_state_descriptor_from_ui_process(DocumentSta
document_state.set_about_base_url(document_state_descriptor.about_base_url);
document_state.set_resource(document_state_descriptor.resource);
document_state.set_reload_pending(document_state_descriptor.reload_pending);
+ document_state.set_reload_bypasses_cache(document_state_descriptor.reload_bypasses_cache);
document_state.set_ever_populated(document_state_descriptor.ever_populated);
document_state.set_user_agent_initiated(document_state_descriptor.user_agent_initiated);
document_state.set_navigable_target_name(document_state_descriptor.navigable_target_name);
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 2d86194e9cd..c05f97925f5 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -1861,7 +1861,12 @@ void Internals::stop_loading_through_ui_process()
void Internals::reload_through_ui_process()
{
- window().associated_document().page().client().reload_through_ui_process_for_testing();
+ window().associated_document().page().client().reload_through_ui_process_for_testing(false);
+}
+
+void Internals::reload_bypassing_cache_through_ui_process()
+{
+ window().associated_document().page().client().reload_through_ui_process_for_testing(true);
}
void Internals::traverse_history_through_ui_process(i32 delta)
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index da7389c9e8a..feea0f5abac 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -222,6 +222,7 @@ public:
void crash_remote_frame_processes();
void stop_loading_through_ui_process();
void reload_through_ui_process();
+ void reload_bypassing_cache_through_ui_process();
void traverse_history_through_ui_process(i32 delta);
GC::Ref<WebIDL::Promise> flush_session_history_traversal_queue();
bool html_parser_body_is_exhausted(DOM::Document&);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 175767cf3e2..1c741c6b8bb 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -222,6 +222,8 @@ interface Internals {
// Stops loading and reloads as the browser's Stop and Reload buttons do.
undefined stopLoadingThroughUIProcess();
undefined reloadThroughUIProcess();
+ // Reloads bypassing the HTTP cache, as the browser's Reload Without Cache action does.
+ undefined reloadBypassingCacheThroughUIProcess();
// Traverses the history by a delta as the browser's Back and Forward buttons do, with no source document.
undefined traverseHistoryThroughUIProcess(long delta);
Promise<undefined> flushSessionHistoryTraversalQueue();
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index f989bb82952..506f2d1a6e4 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -674,7 +674,7 @@ public:
virtual void crash_remote_frame_processes_for_testing() { }
virtual void page_did_spoof_document_origin_for_testing(HTML::EnvironmentSettingsObject const&, URL::Origin const&) { }
virtual void stop_loading_through_ui_process_for_testing() { }
- virtual void reload_through_ui_process_for_testing() { }
+ virtual void reload_through_ui_process_for_testing([[maybe_unused]] bool bypass_cache) { }
virtual void traverse_history_by_delta_through_ui_process_for_testing(i32) { }
virtual void send_bad_ipc_message_for_testing([[maybe_unused]] StringView kind, [[maybe_unused]] URL::URL const& active_document_url) { }
virtual Gfx::Palette palette() const = 0;
diff --git a/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.cpp b/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.cpp
index ef93a28aa41..eb941bb322d 100644
--- a/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.cpp
+++ b/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.cpp
@@ -68,6 +68,7 @@ ErrorOr<void> encode(Encoder& encoder, Web::HTML::NavigationRequestDescriptor co
TRY(encoder.encode(request.redirect_count));
TRY(encoder.encode(request.text_directive_user_activation));
TRY(encoder.encode(request.browsing_context_group_has_multiple_contexts));
+ TRY(encoder.encode(request.cache_mode));
return {};
}
@@ -84,6 +85,7 @@ ErrorOr<Web::HTML::NavigationRequestDescriptor> decode(Decoder& decoder)
.redirect_count = TRY(decoder.decode<u8>()),
.text_directive_user_activation = TRY(decoder.decode<bool>()),
.browsing_context_group_has_multiple_contexts = TRY(decoder.decode<bool>()),
+ .cache_mode = TRY(decoder.decode<HTTP::CacheMode>()),
};
}
diff --git a/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.h b/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.h
index 31cdbccbf38..bcc86bf272e 100644
--- a/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.h
+++ b/Libraries/LibWebCommon/HTML/NavigationParamsDescriptor.h
@@ -14,6 +14,7 @@
#include <AK/Utf16String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
+#include <LibHTTP/Cache/CacheMode.h>
#include <LibHTTP/Header.h>
#include <LibIPC/Forward.h>
#include <LibURL/Origin.h>
@@ -54,6 +55,9 @@ struct NavigationRequestDescriptor {
u8 redirect_count { 0 };
bool text_directive_user_activation { false };
bool browsing_context_group_has_multiple_contexts { false };
+
+ // Whether the navigation bypassed the HTTP cache, which the new Document's own fetches then do while it loads.
+ HTTP::CacheMode cache_mode { HTTP::CacheMode::Default };
};
struct NavigationResponseBodyHandle {
diff --git a/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.cpp b/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.cpp
index fa8edabadca..26296ed4bbe 100644
--- a/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.cpp
+++ b/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.cpp
@@ -75,6 +75,7 @@ NavigationPopulationRequest create_navigation_population_request(NavigationStart
.about_base_url = {},
.resource = move(start_request.document_resource),
.reload_pending = false,
+ .reload_bypasses_cache = false,
.ever_populated = false,
// A navigation with no source document is one the user agent supplied the URL for — and only that one is
// "browser UI" (steps 5 and 6 of navigate).
diff --git a/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.cpp b/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.cpp
index f8e2a4f23c6..ef493244ec6 100644
--- a/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.cpp
+++ b/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.cpp
@@ -33,6 +33,7 @@ SessionHistoryEntryDescriptor create_initial_session_history_entry_descriptor(Cr
.about_base_url = move(about_base_url),
.resource = Empty {},
.reload_pending = false,
+ .reload_bypasses_cache = false,
.ever_populated = false,
.user_agent_initiated = UserAgentInitiated::No,
.navigable_target_name = move(navigable_target_name),
@@ -217,6 +218,7 @@ ErrorOr<void> IPC::encode(Encoder& encoder, Web::HTML::SessionHistoryDocumentSta
TRY(encoder.encode(document_state.about_base_url));
TRY(encoder.encode(document_state.resource));
TRY(encoder.encode(document_state.reload_pending));
+ TRY(encoder.encode(document_state.reload_bypasses_cache));
TRY(encoder.encode(document_state.ever_populated));
TRY(encoder.encode(document_state.user_agent_initiated));
TRY(encoder.encode(document_state.navigable_target_name));
@@ -235,6 +237,7 @@ ErrorOr<Web::HTML::SessionHistoryDocumentStateDescriptor> IPC::decode(Decoder& d
auto about_base_url = TRY(decoder.decode<Optional<URL::URL>>());
auto resource = TRY(decoder.decode<Web::HTML::DocumentResource>());
auto reload_pending = TRY(decoder.decode<bool>());
+ auto reload_bypasses_cache = TRY(decoder.decode<bool>());
auto ever_populated = TRY(decoder.decode<bool>());
auto user_agent_initiated = TRY(decoder.decode<Web::HTML::UserAgentInitiated>());
auto navigable_target_name = TRY(decoder.decode<Utf16String>());
@@ -249,6 +252,7 @@ ErrorOr<Web::HTML::SessionHistoryDocumentStateDescriptor> IPC::decode(Decoder& d
.about_base_url = move(about_base_url),
.resource = move(resource),
.reload_pending = reload_pending,
+ .reload_bypasses_cache = reload_bypasses_cache,
.ever_populated = ever_populated,
.user_agent_initiated = user_agent_initiated,
.navigable_target_name = move(navigable_target_name),
diff --git a/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.h b/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.h
index 1a12ce3e3ba..418a2eff872 100644
--- a/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.h
+++ b/Libraries/LibWebCommon/HTML/SessionHistoryEntryDescriptor.h
@@ -56,6 +56,8 @@ struct SessionHistoryDocumentStateDescriptor {
Optional<URL::URL> about_base_url;
DocumentResource resource;
bool reload_pending { false };
+ // AD-HOC: Whether the pending reload bypasses the HTTP cache.
+ bool reload_bypasses_cache { false };
bool ever_populated { false };
UserAgentInitiated user_agent_initiated { UserAgentInitiated::No };
Utf16String navigable_target_name;
diff --git a/Libraries/LibWebView/ApplyHistoryStep.cpp b/Libraries/LibWebView/ApplyHistoryStep.cpp
index b22cdf1c41e..82530d04212 100644
--- a/Libraries/LibWebView/ApplyHistoryStep.cpp
+++ b/Libraries/LibWebView/ApplyHistoryStep.cpp
@@ -170,6 +170,7 @@ void ApplyHistoryStep::run_changing_navigable_jobs()
// navigable's active window to run the steps:
for (auto& changing_navigable : m_changing_navigables) {
changing_navigable->target_entry_reload_pending = changing_navigable->target_entry->document_state->reload_pending;
+ changing_navigable->target_entry_reload_bypasses_cache = changing_navigable->target_entry->document_state->reload_bypasses_cache;
changing_navigable->displayed_document = changing_navigable->navigable().active_document();
if (!m_traversable.run_changing_navigable_history_step_job(*this, *changing_navigable))
end_changing_navigable_job(*changing_navigable);
@@ -446,6 +447,7 @@ Web::HTML::SessionHistoryEntryDescriptor ChangingNavigable::target_entry_descrip
{
auto descriptor = target_entry->descriptor();
descriptor.document_state.reload_pending = target_entry_reload_pending;
+ descriptor.document_state.reload_bypasses_cache = target_entry_reload_bypasses_cache;
return descriptor;
}
diff --git a/Libraries/LibWebView/ApplyHistoryStep.h b/Libraries/LibWebView/ApplyHistoryStep.h
index 066232d0541..60f432b8464 100644
--- a/Libraries/LibWebView/ApplyHistoryStep.h
+++ b/Libraries/LibWebView/ApplyHistoryStep.h
@@ -56,9 +56,10 @@ struct WEBVIEW_API ChangingNavigable {
// history projection. The run looks it up again before it is activated, and follows a sync navigation that changed
// it (whatwg/html#12961).
NonnullRefPtr<CanonicalSessionHistoryEntry> target_entry;
- // Whether targetEntry's document state's reload pending was true when the job claimed it. Populating the document
- // sets it to false.
+ // Whether targetEntry's document state's reload pending was true when the job claimed it, and whether that reload
+ // bypasses the HTTP cache. Populating the document sets both to false.
bool target_entry_reload_pending { false };
+ bool target_entry_reload_bypasses_cache { false };
// The target entry as the job claimed it, for the processes the job runs in.
Web::HTML::SessionHistoryEntryDescriptor target_entry_descriptor() const;
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
index d4d7b8d19a7..8d10c9673d5 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
@@ -66,6 +66,7 @@ Web::HTML::SessionHistoryDocumentStateDescriptor CanonicalDocumentState::descrip
.about_base_url = about_base_url,
.resource = resource,
.reload_pending = reload_pending,
+ .reload_bypasses_cache = reload_bypasses_cache,
.ever_populated = ever_populated,
.user_agent_initiated = user_agent_initiated,
.navigable_target_name = navigable_target_name,
@@ -94,6 +95,7 @@ static NonnullRefPtr<CanonicalDocumentState> document_state_from_descriptor(Web:
document_state->about_base_url = descriptor.about_base_url;
document_state->resource = descriptor.resource;
document_state->reload_pending = descriptor.reload_pending;
+ document_state->reload_bypasses_cache = descriptor.reload_bypasses_cache;
document_state->ever_populated = descriptor.ever_populated;
document_state->user_agent_initiated = descriptor.user_agent_initiated;
document_state->navigable_target_name = descriptor.navigable_target_name;
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
index 399ad93857e..c4bbda48966 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
@@ -67,6 +67,7 @@ public:
Optional<URL::URL> about_base_url;
Web::HTML::DocumentResource resource;
bool reload_pending { false };
+ bool reload_bypasses_cache { false };
bool ever_populated { false };
Web::HTML::UserAgentInitiated user_agent_initiated { Web::HTML::UserAgentInitiated::No };
Utf16String navigable_target_name;
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index a5fa2412ca4..0ae78fa94e3 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -662,7 +662,7 @@ ByteString CanonicalTraversable::queued_same_document_session_history_entries_fo
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#reload
// NB: This is a reload from the browser's UI. One by script fires the navigate event in the process hosting the active
// document, which then asks for the reload history step.
-void CanonicalTraversable::reload()
+void CanonicalTraversable::reload(BypassCache bypass_cache)
{
auto user_involvement = Web::HTML::UserNavigationInvolvement::BrowserUI;
@@ -674,6 +674,7 @@ void CanonicalTraversable::reload()
// 2. Set navigable's active session history entry's document state's reload pending to true.
active_session_history_entry()->document_state->reload_pending = true;
+ active_session_history_entry()->document_state->reload_bypasses_cache = bypass_cache == BypassCache::Yes;
session_history_changed();
// 3. Let traversable be navigable's traversable navigable.
@@ -872,6 +873,8 @@ bool CanonicalTraversable::set_session_history_entry_document_state_reload_pendi
if (!entry)
return false;
entry->document_state->reload_pending = reload_pending;
+ // A page's own reload never bypasses the HTTP cache, and population clearing reload pending ends one that does.
+ entry->document_state->reload_bypasses_cache = false;
session_history_changed();
return true;
}
@@ -1194,10 +1197,12 @@ void CanonicalTraversable::recover_from_web_content_process_crash()
target_step = traversal->browser_traversal().target_step;
abandon_history_operations();
- // The tab's document is reloaded, the crashed process having destroyed the one its entry holds.
+ // The tab's document is reloaded, the crashed process having destroyed the one its entry holds. That reload never
+ // bypasses the HTTP cache, even when what the crash cut short was a reload that did.
auto* reloading_entry = m_session_history.get_the_target_history_entry(*this, target_step);
VERIFY(reloading_entry);
reloading_entry->document_state->reload_pending = true;
+ reloading_entry->document_state->reload_bypasses_cache = false;
session_history_changed();
enqueue_browser_history_traversal(target_step, false);
}
@@ -2503,8 +2508,10 @@ void CanonicalTraversable::did_not_apply_reload(Web::HTML::CrossProcessId naviga
// AD-HOC: The spec leaves reload pending set when the reload does not apply, so a later history step would reload
// the document anyway.
if (auto navigable = find(navigable_id); navigable.has_value()) {
- if (auto const& entry = navigable->active_session_history_entry())
+ if (auto const& entry = navigable->active_session_history_entry()) {
entry->document_state->reload_pending = false;
+ entry->document_state->reload_bypasses_cache = false;
+ }
}
if (navigable_id != id())
diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.h
index 96c77df8f99..37f17911253 100644
--- a/Libraries/LibWebView/CanonicalTraversable.h
+++ b/Libraries/LibWebView/CanonicalTraversable.h
@@ -43,6 +43,11 @@ enum class WebContentProcessLost : bool {
Yes,
};
+enum class BypassCache : bool {
+ No,
+ Yes,
+};
+
class WEBVIEW_API CanonicalTraversable final : public CanonicalNavigable {
friend class ApplyHistoryStep;
@@ -158,7 +163,7 @@ public:
Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> queued_same_document_session_history_entries(CanonicalNavigable const&) const;
ByteString queued_same_document_session_history_entries_for_debug() const;
- void reload();
+ void reload(BypassCache);
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&);
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index b3fb7f55692..a206c7d1cef 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -486,7 +486,7 @@ void ViewImplementation::load_navigation_error_page(StringView text)
load_html(builder.string_view());
}
-void ViewImplementation::reload()
+void ViewImplementation::reload(BypassCache bypass_cache)
{
m_history_visit_transition_for_next_load = HistoryVisitTransition::Reload;
@@ -511,7 +511,7 @@ void ViewImplementation::reload()
return;
}
- traversable().reload();
+ traversable().reload(bypass_cache);
update_navigation_action_state();
dump_session_history("reload-mark-current-entry-reload-pending"sv);
}
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index d589ce5792d..82d36fbfedf 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -161,7 +161,7 @@ public:
void load_html(StringView);
void load_navigation_error_page(StringView);
- void reload();
+ void reload(BypassCache = BypassCache::No);
void stop_loading();
bool is_loading() const { return m_is_loading; }
// Whether the most recent load ran to completion, rather than being stopped or canceled.
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 91189150184..0c98737d458 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2855,10 +2855,10 @@ void WebContentPage::did_request_stop_loading_for_testing()
view().stop_loading();
}
-void WebContentPage::did_request_reload_for_testing()
+void WebContentPage::did_request_reload_for_testing(bool bypass_cache)
{
if (displays_tab())
- view().reload();
+ view().reload(bypass_cache ? BypassCache::Yes : BypassCache::No);
}
// The tab's Back and Forward buttons, which traverse the history with no source document.
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index af2e4722a69..689b3fba4a3 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -363,7 +363,7 @@ private:
Messages::WebContentTestClient::DidRequestSiteIsolationProcessTreeForTestingResponse did_request_site_isolation_process_tree_for_testing();
virtual void did_request_crash_of_remote_frame_processes_for_testing() override;
virtual void did_request_stop_loading_for_testing() override;
- virtual void did_request_reload_for_testing() override;
+ virtual void did_request_reload_for_testing(bool bypass_cache) override;
virtual void did_request_traverse_history_by_delta_for_testing(i32 delta) override;
virtual void did_reset_session_history_for_testing(Web::HTML::SessionHistoryEntryDescriptor) override;
Messages::WebContentTestClient::DidRequestHasPopulatedDocumentForTestingResponse did_request_has_populated_document_for_testing(Web::HTML::CrossProcessId navigable_id) const;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 98be8b5b2de..de11fd8a7b0 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1837,10 +1837,10 @@ void PageClient::stop_loading_through_ui_process_for_testing()
test_connection->async_did_request_stop_loading_for_testing(m_id);
}
-void PageClient::reload_through_ui_process_for_testing()
+void PageClient::reload_through_ui_process_for_testing(bool bypass_cache)
{
if (auto* test_connection = client().test_connection())
- test_connection->async_did_request_reload_for_testing(m_id);
+ test_connection->async_did_request_reload_for_testing(m_id, bypass_cache);
}
void PageClient::traverse_history_by_delta_through_ui_process_for_testing(i32 delta)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 64c5aea443a..5aad954ba97 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -211,7 +211,7 @@ private:
virtual void crash_remote_frame_processes_for_testing() override;
virtual void page_did_spoof_document_origin_for_testing(Web::HTML::EnvironmentSettingsObject const&, URL::Origin const&) override;
virtual void stop_loading_through_ui_process_for_testing() override;
- virtual void reload_through_ui_process_for_testing() override;
+ virtual void reload_through_ui_process_for_testing(bool bypass_cache) override;
virtual void traverse_history_by_delta_through_ui_process_for_testing(i32 delta) override;
virtual void send_bad_ipc_message_for_testing(StringView kind, URL::URL const& active_document_url) override;
virtual Gfx::Palette palette() const override;
diff --git a/Services/WebContent/WebContentTestClient.ipc b/Services/WebContent/WebContentTestClient.ipc
index 59282bb158e..fb80f516702 100644
--- a/Services/WebContent/WebContentTestClient.ipc
+++ b/Services/WebContent/WebContentTestClient.ipc
@@ -22,7 +22,7 @@ endpoint WebContentTestClient routed by Web::PageId page_id
[NotRouted] did_request_has_populated_document_for_testing(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) => (bool has_populated_document)
did_request_crash_of_remote_frame_processes_for_testing(Web::PageId page_id) =|
did_request_stop_loading_for_testing(Web::PageId page_id) =|
- did_request_reload_for_testing(Web::PageId page_id) =|
+ did_request_reload_for_testing(Web::PageId page_id, bool bypass_cache) =|
did_request_traverse_history_by_delta_for_testing(Web::PageId page_id, i32 delta) =|
did_reset_session_history_for_testing(Web::PageId page_id, Web::HTML::SessionHistoryEntryDescriptor active_entry) =|
diff --git a/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt b/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt
new file mode 100644
index 00000000000..3cada343b21
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Cache/reload-bypassing-cache.txt
@@ -0,0 +1,18 @@
+reload bypassing the cache: document: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache: script: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache: iframe: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache: cross-site iframe: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache: script in the cross-site iframe: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache: cross-site iframe hosted in another process: true
+reload: document: cache-control: max-age=0, if-modified-since
+reload: script: no cache headers
+reload: iframe: no cache headers
+reload: cross-site iframe: no cache headers
+reload: script in the cross-site iframe: no cache headers
+reload bypassing the cache, with the document cached: document: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache, with the document cached: script: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache, with the document cached: iframe: pragma: no-cache, cache-control: no-cache
+reload bypassing the cache, with the document cached: cross-site iframe: pragma: no-cache, cache-control: no-cache
+reload 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: fetch() with cache "force-cache": no cache headers
+reload bypassing the cache, with the document cached: fetch() after the load event: no cache headers
diff --git a/Tests/LibWeb/Text/input/Cache/reload-bypassing-cache.html b/Tests/LibWeb/Text/input/Cache/reload-bypassing-cache.html
new file mode 100644
index 00000000000..0d601f46803
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Cache/reload-bypassing-cache.html
@@ -0,0 +1,111 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A reload that bypasses the cache fetches the document from the network without consulting the HTTP cache, and
+ // stores what it gets, so a plain reload after it revalidates the stored document. While the document loads, its
+ // subresources and its iframes are fetched the same way — a cross-site iframe that another process hosts, and that
+ // iframe's own subresources, included — unless a fetch asks for a cache mode of its own. Once the document has
+ // loaded, its fetches use the cache again.
+ function pageScript(config) {
+ const LABELS = ["initial load", "reload bypassing the cache", "reload", "reload bypassing the cache, with the document cached"];
+ const STEP_KEY = `step-${config.id}`;
+ const RESULTS_KEY = `results-${config.id}`;
+ const step = Number(sessionStorage.getItem(STEP_KEY) ?? 0);
+ const results = JSON.parse(sessionStorage.getItem(RESULTS_KEY) ?? "[]");
+
+ const explicitFetch = fetch(config.explicitURL, { cache: "force-cache" });
+
+ async function describeRequest(label, url) {
+ const response = await fetch(`/recorded-request-headers${new URL(url).pathname}`, { cache: "no-store" });
+ const headers = Object.fromEntries(Object.entries(await response.json()).map(([name, values]) => [name.toLowerCase(), values]));
+ const cacheHeaders = [];
+ for (const name of ["pragma", "cache-control"]) {
+ if (headers[name])
+ cacheHeaders.push(`${name}: ${headers[name][0]}`);
+ }
+ if (headers["if-modified-since"])
+ cacheHeaders.push("if-modified-since");
+ results.push(`${LABELS[step]}: ${label}: ${cacheHeaders.length ? cacheHeaders.join(", ") : "no cache headers"}`);
+ }
+
+ onload = () => setTimeout(async () => {
+ // In test mode, the disk cache only stores responses to requests carrying the cache-enablement header, which
+ // a page can't attach to a navigation request. So have every navigation request carry it from here on.
+ if (step === 0) {
+ internals.setDiskCacheEnabledForNavigations(true);
+ } else {
+ await describeRequest("document", location.href);
+ await describeRequest("script", config.scriptURL);
+ await describeRequest("iframe", config.frameURL);
+ await describeRequest("cross-site iframe", config.crossSiteFrameURL);
+ await describeRequest("script in the cross-site iframe", config.crossSiteScriptURL);
+ if (step === 1)
+ results.push(`${LABELS[step]}: cross-site iframe hosted in another process: ${internals.dumpSiteIsolationProcessTree().includes("remote")}`);
+ }
+
+ // The process serving this page can't navigate back to the test file, so this page reports the results.
+ if (step === 3) {
+ await explicitFetch;
+ await describeRequest('fetch() with cache "force-cache"', config.explicitURL);
+ await fetch(config.lateURL);
+ await describeRequest("fetch() after the load event", config.lateURL);
+ internals.setDiskCacheEnabledForNavigations(false);
+ internals.signalTestIsDone(results.map(line => `${line}\n`).join(""));
+ return;
+ }
+
+ sessionStorage.setItem(STEP_KEY, String(step + 1));
+ sessionStorage.setItem(RESULTS_KEY, JSON.stringify(results));
+ if (step === 1)
+ internals.reloadThroughUIProcess();
+ else
+ internals.reloadBypassingCacheThroughUIProcess();
+ });
+ }
+
+ asyncTest(async () => {
+ const id = crypto.randomUUID();
+ const server = httpTestServer().withEchoScope(id);
+ async function createEcho(name, contentType, body = "") {
+ return server.createEcho("GET", `/reload-bypassing-cache-${name}`, {
+ status: 200,
+ headers: { "Cache-Control": "no-store", "Content-Type": contentType },
+ body,
+ });
+ }
+
+ const endScript = "<" + "/script>";
+ const crossSiteHostname = uniqueLocalhostHostname("reload-bypassing-cache");
+ function onCrossSiteHost(url) {
+ const crossSiteURL = new URL(url);
+ crossSiteURL.hostname = crossSiteHostname;
+ return crossSiteURL.href;
+ }
+ const crossSiteScriptURL = onCrossSiteHost(await createEcho("cross-site-script", "text/javascript"));
+
+ const config = {
+ id,
+ scriptURL: await createEcho("script", "text/javascript"),
+ frameURL: await createEcho("frame", "text/html"),
+ explicitURL: await createEcho("explicit", "text/plain"),
+ lateURL: await createEcho("late", "text/plain"),
+ crossSiteFrameURL: onCrossSiteHost(await createEcho("cross-site-frame", "text/html", `<script src="${crossSiteScriptURL}">${endScript}`)),
+ crossSiteScriptURL,
+ };
+
+ const pageURL = await server.createEcho("GET", "/reload-bypassing-cache-page", {
+ status: 200,
+ headers: {
+ "Cache-Control": "max-age=999",
+ "Content-Type": "text/html",
+ "Last-Modified": "Mon, 01 Jan 2024 00:00:00 GMT",
+ },
+ body: `<!DOCTYPE html>
+<script src="${config.scriptURL}">${endScript}
+<iframe src="${config.frameURL}"></iframe>
+<iframe src="${config.crossSiteFrameURL}"></iframe>
+<script>(${pageScript})(${JSON.stringify(config)});${endScript}`,
+ });
+ internals.loadURL(pageURL);
+ });
+</script>