servo/components/devtools/network_handler.rs
Usman Yahaya Baba 7d2c9ec19c
Fix network event update Message (#37543)
- Add `ResourceArrayType` with `Available` and `Updated` variants
- Rename `resources-available` and `resource-available` to
`resources-array` ,`resource-array`
- Add `ResourceArrayType` as an argument to decide the type of resources
- Add `Option<ResponseContentMsg>`,`Option<ResponseStartMsg>`,
`Option<ResponseCookiesMsg>`,
`Option<ResponseHeadersMsg>`,`Option<RequestCookiesMsg>`,
`Option<RequestHeadersMsg>`, `total_time`, `security_state` to
`NetworkEventActor` struct , and serialize the data in each to
`resource_updates` , flattening the nested arrays into a single JSON
- Refactor the following methods `request_headers`,`response_start` ,
`response_content`,`response_cookies`,`response_headers`,
`request_cookies`,`total_time` to associated functions passing
`HttpRequest` and `HttpResponse` as parameters .

Testing: Ran servo with devtools flag to see the logs corresponding to
the changes
Fixes: https://github.com/servo/servo/issues/37479

This PR Builds on https://github.com/servo/servo/pull/37517 and was
opened due to merge conflicts and branch issues

---------

Signed-off-by: Uthman Yahaya Baba <uthmanyahayababa@gmail.com>
2025-06-19 16:00:37 +00:00

73 lines
2.5 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::ActorRegistry;
use crate::actors::browsing_context::BrowsingContextActor;
use crate::actors::network_event::NetworkEventActor;
use crate::resource::{ResourceArrayType, ResourceAvailable};
#[derive(Clone, Serialize)]
pub struct Cause {
#[serde(rename = "type")]
pub type_: String,
#[serde(rename = "loadingDocumentUri")]
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,
browsing_context_actor_name: String,
) {
let mut actors = actors.lock().unwrap();
match network_event {
NetworkEvent::HttpRequest(httprequest) => {
// Scope mutable borrow
let event_actor = {
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);
actor.add_request(httprequest);
actor.event_actor()
};
let browsing_context_actor =
actors.find::<BrowsingContextActor>(&browsing_context_actor_name);
for stream in &mut connections {
browsing_context_actor.resource_array(
event_actor.clone(),
"network-event".to_string(),
ResourceArrayType::Available,
stream,
);
}
},
NetworkEvent::HttpResponse(httpresponse) => {
// Scope mutable borrow
let resource = {
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);
// Store the response information in the actor
actor.add_response(httpresponse);
actor.resource_updates()
};
let browsing_context_actor =
actors.find::<BrowsingContextActor>(&browsing_context_actor_name);
for stream in &mut connections {
browsing_context_actor.resource_array(
resource.clone(),
"network-event".to_string(),
ResourceArrayType::Updated,
stream,
);
}
},
}
}