Files
servo/components/devtools/network_handler.rs
eri f9a328a972 devtools: Improve serialization and remove mutability from NetworkEventActor (#41741)
Now `NetworkEventActor`'s methods take a non mutable reference to self
(`add_request`, `add_response` and `add_security_info`). This actor now
has three different `RefCells` inside (`request`, `response` and
`security`), which group pieces of data that are updated separately via
the public methods.

For the actor functionality itself, it now follows Firefox more closely.
In the `resource-available` message it only sends the `*_available`
fields (i.e. `response_headers_available`), deferring the actual data to
the `get*` messages (i.e `getResponseHeaders`). There were also a few
slight changes I noticed along the way that I tried to fix.

Simplify some repeated logic calculating header sizes and listing
cookies.

`resource_updates` is updated to use `Serialize` structs instead of
manually adding values to a map. This is more in line with the rest of
the DevTools code and probably less prone to future errors.

Fix "Resource of network-event is missing a browsingContextID or
innerWindowId attribute" error that shows multiple times each time the
Network tab is open. Updated all messages to use the correct
browsingContextID.

Testing: Manually compared all of the Network tab features to avoid
regressions.

---------

Signed-off-by: eri <eri@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2026-01-12 07:55:34 +00:00

99 lines
3.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::net::TcpStream;
use std::sync::{Arc, Mutex};
use devtools_traits::NetworkEvent;
use serde::Serialize;
use crate::actor::{ActorEncode, ActorRegistry};
use crate::actors::network_event::NetworkEventActor;
use crate::actors::watcher::WatcherActor;
use crate::resource::{ResourceArrayType, ResourceAvailable};
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Cause {
#[serde(rename = "type")]
pub type_: String,
pub loading_document_uri: Option<String>,
}
pub(crate) fn handle_network_event(
actors: Arc<Mutex<ActorRegistry>>,
netevent_actor_name: String,
mut connections: Vec<TcpStream>,
network_event: NetworkEvent,
) {
let actors = actors.lock().unwrap();
let actor = actors.find::<NetworkEventActor>(&netevent_actor_name);
let watcher = actors.find::<WatcherActor>(&actor.watcher);
match network_event {
NetworkEvent::HttpRequest(httprequest) => {
actor.add_request(httprequest);
let msg = actor.encode(&actors);
let resource = actor.resource_updates(&actors);
for stream in &mut connections {
watcher.resource_array(
msg.clone(),
"network-event".to_string(),
ResourceArrayType::Available,
stream,
);
// Also push initial resource update (request headers, cookies)
watcher.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
NetworkEvent::HttpRequestUpdate(httprequest) => {
actor.add_request(httprequest);
let resource = actor.resource_updates(&actors);
for stream in &mut connections {
watcher.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
NetworkEvent::HttpResponse(httpresponse) => {
actor.add_response(httpresponse);
let resource = actor.resource_updates(&actors);
for stream in &mut connections {
watcher.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
NetworkEvent::SecurityInfo(update) => {
actor.add_security_info(update.security_info);
let resource = actor.resource_updates(&actors);
for stream in &mut connections {
watcher.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
}
}