devtools/actors/
worker.rs1use std::cell::RefCell;
6use std::collections::HashMap;
7use std::net::TcpStream;
8
9use base::id::TEST_PIPELINE_ID;
10use devtools_traits::DevtoolScriptControlMsg::WantsLiveNotifications;
11use devtools_traits::{DevtoolScriptControlMsg, WorkerId};
12use ipc_channel::ipc::IpcSender;
13use serde::Serialize;
14use serde_json::{Map, Value};
15use servo_url::ServoUrl;
16
17use crate::StreamId;
18use crate::actor::{Actor, ActorError, ActorRegistry};
19use crate::protocol::{ClientRequest, JsonPacketStream};
20use crate::resource::ResourceAvailable;
21
22#[derive(Clone, Copy)]
23#[allow(dead_code)]
24pub enum WorkerType {
25 Dedicated = 0,
26 Shared = 1,
27 Service = 2,
28}
29
30pub(crate) struct WorkerActor {
31 pub name: String,
32 pub console: String,
33 pub thread: String,
34 pub worker_id: WorkerId,
35 pub url: ServoUrl,
36 pub type_: WorkerType,
37 pub script_chan: IpcSender<DevtoolScriptControlMsg>,
38 pub streams: RefCell<HashMap<StreamId, TcpStream>>,
39}
40
41impl WorkerActor {
42 pub(crate) fn encodable(&self) -> WorkerMsg {
43 WorkerMsg {
44 actor: self.name.clone(),
45 console_actor: self.console.clone(),
46 thread_actor: self.thread.clone(),
47 id: self.worker_id.0.to_string(),
48 url: self.url.to_string(),
49 traits: WorkerTraits {
50 is_parent_intercept_enabled: false,
51 supports_top_level_target_flag: false,
52 },
53 type_: self.type_ as u32,
54 target_type: "worker".to_string(),
55 }
56 }
57}
58
59impl ResourceAvailable for WorkerActor {
60 fn actor_name(&self) -> String {
61 self.name.clone()
62 }
63}
64
65impl Actor for WorkerActor {
66 fn name(&self) -> String {
67 self.name.clone()
68 }
69 fn handle_message(
70 &self,
71 mut request: ClientRequest,
72 _registry: &ActorRegistry,
73 msg_type: &str,
74 _msg: &Map<String, Value>,
75 stream_id: StreamId,
76 ) -> Result<(), ActorError> {
77 match msg_type {
78 "attach" => {
79 let msg = AttachedReply {
80 from: self.name(),
81 type_: "attached".to_owned(),
82 url: self.url.as_str().to_owned(),
83 };
84 request.write_json_packet(&msg)?;
86 self.streams
87 .borrow_mut()
88 .insert(stream_id, request.try_clone_stream().unwrap());
89 self.script_chan
91 .send(WantsLiveNotifications(TEST_PIPELINE_ID, true))
92 .unwrap();
93 },
94
95 "connect" => {
96 let msg = ConnectReply {
97 from: self.name(),
98 type_: "connected".to_owned(),
99 thread_actor: self.thread.clone(),
100 console_actor: self.console.clone(),
101 };
102 request.write_json_packet(&msg)?;
104 },
105
106 "detach" => {
107 let msg = DetachedReply {
108 from: self.name(),
109 type_: "detached".to_string(),
110 };
111 self.cleanup(stream_id);
112 request.write_json_packet(&msg)?;
114 },
115
116 _ => return Err(ActorError::UnrecognizedPacketType),
117 };
118 Ok(())
119 }
120
121 fn cleanup(&self, stream_id: StreamId) {
122 self.streams.borrow_mut().remove(&stream_id);
123 if self.streams.borrow().is_empty() {
124 self.script_chan
125 .send(WantsLiveNotifications(TEST_PIPELINE_ID, false))
126 .unwrap();
127 }
128 }
129}
130
131#[derive(Serialize)]
132struct DetachedReply {
133 from: String,
134 #[serde(rename = "type")]
135 type_: String,
136}
137
138#[derive(Serialize)]
139struct AttachedReply {
140 from: String,
141 #[serde(rename = "type")]
142 type_: String,
143 url: String,
144}
145
146#[derive(Serialize)]
147#[serde(rename_all = "camelCase")]
148struct ConnectReply {
149 from: String,
150 #[serde(rename = "type")]
151 type_: String,
152 thread_actor: String,
153 console_actor: String,
154}
155
156#[derive(Serialize)]
157#[serde(rename_all = "camelCase")]
158struct WorkerTraits {
159 is_parent_intercept_enabled: bool,
160 supports_top_level_target_flag: bool,
161}
162
163#[derive(Serialize)]
164#[serde(rename_all = "camelCase")]
165pub(crate) struct WorkerMsg {
166 actor: String,
167 console_actor: String,
168 thread_actor: String,
169 id: String,
170 url: String,
171 traits: WorkerTraits,
172 #[serde(rename = "type")]
173 type_: u32,
174 #[serde(rename = "targetType")]
175 target_type: String,
176}