Truly independent web browser
LibWeb: Keep navigables and BiDi session state per agent
Andreas Kling756afb317825
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex b7a8bc281ef..16509efaa1d 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -569,7 +569,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam// 1. Let request be fetchParams’s request.auto request = fetch_params.request();- if (WebDriver::BiDi::g_network_events_enabled)+ if (WebDriver::BiDi::network_events_enabled())WebDriver::BiDi::initialize_network_request(*request);// 2. Let response be null.@@ -985,7 +985,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const// 1. Let timingInfo be fetchParams’s timing info.auto timing_info = fetch_params.timing_info();- if (WebDriver::BiDi::g_network_events_enabled) {+ if (WebDriver::BiDi::network_events_enabled()) {if (response.is_network_error())WebDriver::BiDi::network_fetch_error(fetch_params, response.network_error_message().value_or("Network error"_string));else {@@ -1117,7 +1117,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams constauto process_response_end_of_body = [fetch_params = NonnullRefPtr { fetch_params }, controller, response_view, setup_report_timing_steps, report_timing_if_appropriate] {// 1-2. (See setup_report_timing_steps above)setup_report_timing_steps();- if (WebDriver::BiDi::g_network_events_enabled && !response_view->is_network_error())+ if (WebDriver::BiDi::network_events_enabled() && !response_view->is_network_error())WebDriver::BiDi::network_response_completed(fetch_params, *response_view->record()->unsafe_response());// 3. Let processResponseEndOfBodyTask be the following steps:@@ -1152,11 +1152,11 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const// AD-HOC: Report a body's timing once the network has delivered it. Fetch reports it from the flush of the// transform the body is piped through, which backpressure on an unread body can hold back.- if (internal_body.has_value() && (fetch_params.request()->initiator_type().has_value() || (WebDriver::BiDi::g_network_events_enabled && !fetch_params.request()->is_navigation_request()))) {+ if (internal_body.has_value() && (fetch_params.request()->initiator_type().has_value() || (WebDriver::BiDi::network_events_enabled() && !fetch_params.request()->is_navigation_request()))) {auto network_completion_steps = GC::create_function(GC::Heap::the(), [fetch_params = NonnullRefPtr { fetch_params }, response_view, setup_report_timing_steps, report_timing_if_appropriate] {setup_report_timing_steps();report_timing_if_appropriate();- if (WebDriver::BiDi::g_network_events_enabled)+ if (WebDriver::BiDi::network_events_enabled())WebDriver::BiDi::network_response_completed(*fetch_params, *response_view->record()->unsafe_response());});internal_body->bytes().visit(@@ -1661,7 +1661,7 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParamif (request->service_workers_mode() == Infrastructure::Request::ServiceWorkersMode::All) {// 1. Let requestForServiceWorker be a clone of request.auto request_for_service_worker = request->clone();- if (WebDriver::BiDi::g_network_events_enabled || WebDriver::BiDi::g_network_configuration_enabled)+ if (WebDriver::BiDi::network_events_enabled() || WebDriver::BiDi::network_configuration_enabled())request_for_service_worker->set_webdriver_bidi_network_request(request->webdriver_bidi_network_request());// 2. If requestForServiceWorker’s body is non-null, then:@@ -1821,7 +1821,7 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParam// 6. If internalResponse’s status is a redirect status:if (Infrastructure::is_redirect_status(internal_response->status())) {- if (WebDriver::BiDi::g_network_events_enabled)+ if (WebDriver::BiDi::network_events_enabled())WebDriver::BiDi::network_response_completed(fetch_params, *internal_response);// 1. If request is a navigation request, then append to request's navigation timing allow values list// given request and internalResponse.@@ -2099,7 +2099,7 @@ static void process_http_network_or_cache_response(JS::Object& global_object, Inreturn;}- if (WebDriver::BiDi::g_network_events_enabled && authentication_handled == AuthenticationHandled::No) {+ if (WebDriver::BiDi::network_events_enabled() && authentication_handled == AuthenticationHandled::No) {auto intercepts = WebDriver::BiDi::get_network_intercepts(fetch_params, "authRequired"sv);if (!intercepts.is_empty()) {WebDriver::BiDi::block_network_request(fetch_params, response, "authRequired"_string, move(intercepts), WebDriver::BiDi::NetworkContinuation::create(realm.heap(), [&realm, fetch_params = NonnullRefPtr { fetch_params }, http_request, returned_pending_response, is_authentication_fetch, is_new_connection_fetch, include_credentials](WebDriver::BiDi::NetworkContinuationAction action, RefPtr<Infrastructure::Response> continued_response) {@@ -2264,7 +2264,7 @@ static GC::Ref<PendingResponse> continue_http_network_or_cache_fetch(JS::Object&// 11. Set response’s URL list to a clone of httpRequest’s URL list.response->set_url_list(http_request->url_list());- if (WebDriver::BiDi::g_network_events_enabled) {+ if (WebDriver::BiDi::network_events_enabled()) {// NB: Instrumentation may have been enabled after httpRequest was cloned. The controller's original// request must share the identity reported by the prepared request, including on cancellation.WebDriver::BiDi::synchronize_network_request(*fetch_params->request(), *http_request);@@ -2294,10 +2294,10 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru// 1. Let request be fetchParams’s request.auto request = fetch_params.request();- if (WebDriver::BiDi::g_network_events_enabled)+ if (WebDriver::BiDi::network_events_enabled())WebDriver::BiDi::initialize_network_request(*request);- if (WebDriver::BiDi::g_network_configuration_enabled)+ if (WebDriver::BiDi::network_configuration_enabled())WebDriver::BiDi::apply_network_cache_behavior(*request);// 2. Let httpFetchParams be null.@@ -2552,7 +2552,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru// NOTE: This intentionally does not depend on httpRequest’s credentials mode.WebDriver::BiDi::apply_network_request_header_overrides(*http_request);- if (WebDriver::BiDi::g_network_configuration_enabled)+ if (WebDriver::BiDi::network_configuration_enabled())WebDriver::BiDi::apply_network_extra_headers(*http_request);// 23. Set httpCache to the result of determining the HTTP cache partition, given httpRequest.@@ -2596,7 +2596,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastruif (aborted)return PendingResponse::create(fetch_params, Infrastructure::Response::appropriate_network_error(fetch_params));- if (WebDriver::BiDi::g_network_events_enabled) {+ if (WebDriver::BiDi::network_events_enabled()) {auto intercepts = WebDriver::BiDi::get_network_intercepts(*http_fetch_params, "beforeRequestSent"sv);if (!intercepts.is_empty()) {auto returned_pending_response = PendingResponse::create(fetch_params);@@ -3143,7 +3143,7 @@ GC::Ref<PendingResponse> cors_preflight_fetch(JS::Realm& realm, Infrastructure::preflight_response->when_loaded([request = fetch_params.request(), returned_pending_response, preflight_controller = GC::Ref { *preflight_controller }, preflight_fetch_params](NonnullRefPtr<Infrastructure::Response> response) {dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' preflight_response load callback");- if (WebDriver::BiDi::g_network_events_enabled) {+ if (WebDriver::BiDi::network_events_enabled()) {if (response->is_network_error())WebDriver::BiDi::network_fetch_error(*preflight_fetch_params, response->network_error_message().value_or("Preflight failed"_string));elsediff --git a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cppindex b404f4f2bae..4edb3693326 100644--- a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp@@ -125,7 +125,7 @@ void NetworkBodyPump::handle_network_complete(Optional<u64> encoded_body_size)void NetworkBodyPump::handle_network_error(StringView error_message){- if (WebDriver::BiDi::g_network_events_enabled && m_fetch_params)+ if (WebDriver::BiDi::network_events_enabled() && m_fetch_params)WebDriver::BiDi::network_fetch_error(*m_fetch_params, error_message);abandon_cache_entry();m_channel->error({ .message = MUST(ByteBuffer::copy(error_message.bytes())) });diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cppindex fa8fb1737b8..80d52671597 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp@@ -77,7 +77,7 @@ void FetchController::set_original_fetch_params(Badge<FetchParams>, FetchParamsvoid FetchController::report_webdriver_bidi_fetch_error(StringView error_text) const{- if (!WebDriver::BiDi::g_network_events_enabled)+ if (!WebDriver::BiDi::network_events_enabled())return;if (auto fetch_params = m_original_fetch_params.strong_ref())WebDriver::BiDi::network_fetch_error(*fetch_params, error_text);diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex c86ccc625d2..7f94afaa7ae 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -559,7 +559,7 @@ static bool handle_navigation_response_as_download(GC::Ref<NavigationParams> navreturn true;JsonValue network_response = navigation_params->webdriver_bidi_network_response;- if (WebDriver::BiDi::g_network_events_enabled && response->network_transport().has_value() && !network_response.is_object() && navigation_params->request) {+ if (WebDriver::BiDi::network_events_enabled() && response->network_transport().has_value() && !network_response.is_object() && navigation_params->request) {RefPtr<Fetch::Infrastructure::FetchTimingInfo> timing_info = navigation_params->fetch_timing_info;if (navigation_params->fetch_controller)timing_info = navigation_params->fetch_controller->full_timing_info();@@ -704,7 +704,8 @@ void PopulateSessionHistoryEntryDocumentOutput::visit_edges(Cell::Visitor& visitHashTable<GC::RawRef<LocalNavigable>>& all_local_navigables(){- static NeverDestroyed<HashTable<GC::RawRef<LocalNavigable>>> set;+ // NB: Worker agents have no navigables, and these are cells of the window agent's heap.+ static thread_local NeverDestroyed<HashTable<GC::RawRef<LocalNavigable>>> set;return *set;}@@ -2596,7 +2597,7 @@ static void create_navigation_params_by_fetching(request->set_redirect_mode(Fetch::Infrastructure::Request::RedirectMode::Manual);request->set_replaces_client_id(active_document.relevant_settings_object().id);request->set_mode(Fetch::Infrastructure::Request::Mode::Navigate);- if (WebDriver::BiDi::g_network_events_enabled || WebDriver::BiDi::g_network_configuration_enabled)+ if (WebDriver::BiDi::network_events_enabled() || WebDriver::BiDi::network_configuration_enabled())WebDriver::BiDi::initialize_network_request(*request, navigable, navigation_id);request->set_referrer(document_state.request_referrer);request->set_referrer_policy(document_state.request_referrer_policy);diff --git a/Libraries/LibWeb/HTML/RemoteNavigable.cpp b/Libraries/LibWeb/HTML/RemoteNavigable.cppindex 846c8589c89..3f5ca2c7efb 100644--- a/Libraries/LibWeb/HTML/RemoteNavigable.cpp+++ b/Libraries/LibWeb/HTML/RemoteNavigable.cpp@@ -25,7 +25,8 @@ GC_DEFINE_ALLOCATOR(RemoteNavigable);HashTable<GC::RawRef<RemoteNavigable>>& all_remote_navigables(){- static NeverDestroyed<HashTable<GC::RawRef<RemoteNavigable>>> set;+ // NB: Worker agents have no navigables, and these are cells of the window agent's heap.+ static thread_local NeverDestroyed<HashTable<GC::RawRef<RemoteNavigable>>> set;return *set;}diff --git a/Libraries/LibWeb/WebDriver/BiDi/BiDi.cpp b/Libraries/LibWeb/WebDriver/BiDi/BiDi.cppindex 7cd6febf2ba..84664e4524d 100644--- a/Libraries/LibWeb/WebDriver/BiDi/BiDi.cpp+++ b/Libraries/LibWeb/WebDriver/BiDi/BiDi.cpp@@ -26,7 +26,12 @@namespace Web::WebDriver::BiDi {-bool g_is_active { false };+static thread_local bool g_is_active { false };++bool is_active()+{+ return g_is_active;+}static BiDiSessionState& state(){diff --git a/Libraries/LibWeb/WebDriver/BiDi/BiDi.h b/Libraries/LibWeb/WebDriver/BiDi/BiDi.hindex 283a3c92606..d5597860e36 100644--- a/Libraries/LibWeb/WebDriver/BiDi/BiDi.h+++ b/Libraries/LibWeb/WebDriver/BiDi/BiDi.h@@ -24,10 +24,9 @@// this work while the browser process says a BiDi session is active, which only WebDriver can start.namespace Web::WebDriver::BiDi {-WEB_API extern bool g_is_active;-// Whether a BiDi session is active.-inline bool is_active() { return g_is_active; }+// NB: A session reaches only the window agent, so this is false on the thread of a worker.+WEB_API bool is_active();WEB_API void set_session_state(BiDiSessionState);WEB_API BiDiSessionState const& session_state();diff --git a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp b/Libraries/LibWeb/WebDriver/BiDi/Network.cppindex e23c646186b..93cd160051a 100644--- a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp+++ b/Libraries/LibWeb/WebDriver/BiDi/Network.cpp@@ -7,6 +7,7 @@#include <AK/Base64.h>#include <AK/GenericLexer.h>#include <AK/JsonArray.h>+#include <AK/NeverDestroyed.h>#include <AK/Random.h>#include <AK/StringBuilder.h>#include <AK/Time.h>@@ -59,8 +60,18 @@ void NetworkContinuation::invoke(NetworkContinuationAction action, RefPtr<Responm_callback = nullptr;}-bool g_network_events_enabled { false };-bool g_network_configuration_enabled { false };+static thread_local bool g_network_events_enabled { false };+static thread_local bool g_network_configuration_enabled { false };++bool network_events_enabled()+{+ return g_network_events_enabled;+}++bool network_configuration_enabled()+{+ return g_network_configuration_enabled;+}void NetworkContinuation::clear_blocked_state(){@@ -75,8 +86,8 @@ void NetworkContinuation::clear_blocked_state()static HashMap<String, GC::Root<NetworkContinuation>>& blocked_requests(){- static auto& requests = *new HashMap<String, GC::Root<NetworkContinuation>>;- return requests;+ static thread_local NeverDestroyed<HashMap<String, GC::Root<NetworkContinuation>>> requests;+ return *requests;}static GC::Ptr<NetworkContinuation> blocked_request(Request const& request)diff --git a/Libraries/LibWeb/WebDriver/BiDi/Network.h b/Libraries/LibWeb/WebDriver/BiDi/Network.hindex c595fd73192..06320de9b65 100644--- a/Libraries/LibWeb/WebDriver/BiDi/Network.h+++ b/Libraries/LibWeb/WebDriver/BiDi/Network.h@@ -89,8 +89,9 @@ private:};// NB: Fetch checks this cached flag before doing any network instrumentation.-WEB_API extern bool g_network_events_enabled;-WEB_API extern bool g_network_configuration_enabled;+// NB: A session reaches only the window agent, so these are false on the thread of a worker.+WEB_API bool network_events_enabled();+WEB_API bool network_configuration_enabled();void network_session_state_changed();void cancel_network_interception(Fetch::Infrastructure::FetchParams const&);