script: Have FetchResponseListener::process_response_eof consume the listener (#40556)

The goal of this change is to prevent having to copy so much data out of
listeners when a fetch completes, which will be particularly important
for off-the-main thread parsing of CSS (see #22478). This change has
pros and cons:

Pros:
- This makes the design of the `FetchResponseListener` a great deal
simpler.
They no longer individually store a dummy `ResourceFetchTiming` that is
   only replaced right before `process_response_eof`.
 - The creation of the `Arc<Mutex<FetchResponseListener>>` in the
   `NetworkListener` is abstracted away from clients and now they just
   pass the `FetchResponseListener` to the fetch methods in the global.

Cons:
 - Now each `FetchResponseListener` must explicitly call `submit_timing`
   instead of having the `NetworkListener` do it. This is arguably a bit
   easier to follow in the code.
 - Since the internal data of the `NetworkListener` is now an
   `Arc<Mutex<Option<FetchResponseListener>>>`, when the fetching code
   needs to share state with the `NetworkListener` it either needs to
   share an `Option` or some sort of internal state. In one case I've
   stored the `Option` and in another case, I've stored a new inner
   shared value.

Testing: This should not change observable behavior and is thus covered
by existing tests.
Fixes: #22550

---------

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson
2025-11-11 23:30:40 +01:00
committed by GitHub
parent 900243f00d
commit bfde51c0db
29 changed files with 301 additions and 560 deletions

View File

@@ -10,7 +10,6 @@ use mime::{self, Mime};
use net_traits::request::{CorsSettings, Destination, RequestId};
use net_traits::{
FetchMetadata, FilteredMetadata, Metadata, NetworkError, ReferrerPolicy, ResourceFetchTiming,
ResourceTimingType,
};
use servo_arc::Arc;
use servo_url::ServoUrl;
@@ -90,7 +89,6 @@ pub(crate) struct StylesheetContext {
/// A token which must match the generation id of the `HTMLLinkElement` for it to load the stylesheet.
/// This is ignored for `HTMLStyleElement` and imports.
request_generation_id: Option<RequestGenerationId>,
resource_timing: ResourceFetchTiming,
}
impl StylesheetContext {
@@ -149,7 +147,7 @@ impl FetchResponseListener for StylesheetContext {
}
fn process_response_eof(
&mut self,
mut self,
_: RequestId,
status: Result<ResourceFetchTiming, NetworkError>,
) {
@@ -157,10 +155,13 @@ impl FetchResponseListener for StylesheetContext {
let document = self.document.root();
let mut successful = false;
if status.is_ok() {
if let Ok(response) = &status {
let metadata = match self.metadata.take() {
Some(meta) => meta,
None => return,
None => {
network_listener::submit_timing(&self, response, CanGc::note());
return;
},
};
let mut is_css = metadata.content_type.is_some_and(|ct| {
@@ -290,18 +291,10 @@ impl FetchResponseListener for StylesheetContext {
.upcast::<EventTarget>()
.fire_event(event, CanGc::note());
}
}
fn resource_timing_mut(&mut self) -> &mut ResourceFetchTiming {
&mut self.resource_timing
}
fn resource_timing(&self) -> &ResourceFetchTiming {
&self.resource_timing
}
fn submit_resource_timing(&mut self) {
network_listener::submit_timing(self, CanGc::note())
if let Ok(response) = status {
network_listener::submit_timing(&self, &response, CanGc::note());
}
}
fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
@@ -364,7 +357,6 @@ impl ElementStylesheetLoader<'_> {
shadow_root,
origin_clean: true,
request_generation_id: generation,
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
};
let owner = self