mirror of
https://github.com/servo/servo
synced 2026-05-08 16:12:15 +02:00
Replace `RefCell` with `AtomicRefCell` for structs implementing Actor, making them `Sync`. Consolidate `register` and `register_later` into a single function, removing the need to wait for a loop before accessing newly created actors. Now `ActorRegsitry` has improved locking. Instead of locking the entire struct, each member can be locked separately. Additionally, since `find` now returns `Arc`, we can `find` and `register` multiple actors depending on each other, since the lock is only needed for the operation and we can keep the reference after that. Depends on: #41741, #41744 Testing: Manual testing --------- Signed-off-by: eri <eri@igalia.com> Co-authored-by: Martin Robinson <mrobinson@igalia.com>
98 lines
3.1 KiB
Rust
98 lines
3.1 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;
|
|
|
|
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<ActorRegistry>,
|
|
netevent_actor_name: String,
|
|
mut connections: Vec<TcpStream>,
|
|
network_event: NetworkEvent,
|
|
) {
|
|
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,
|
|
);
|
|
}
|
|
},
|
|
}
|
|
}
|