1use std::sync::Arc;
13
14use devtools_traits::DevtoolScriptControlMsg;
15use malloc_size_of_derive::MallocSizeOf;
16use serde::Serialize;
17use serde_json::{Map, Value};
18use servo_base::generic_channel;
19use servo_url::ServoUrl;
20
21use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry, new_actor_name};
22use crate::actors::browsing_context::{BrowsingContextActor, BrowsingContextActorMsg};
23use crate::actors::root::{DescriptorTraits, RootActor};
24use crate::actors::watcher::{WatcherActor, WatcherActorMsg};
25use crate::protocol::ClientRequest;
26use crate::{EmptyReplyMsg, StreamId};
27
28#[derive(Serialize)]
29#[serde(rename_all = "camelCase")]
30pub(crate) struct TabDescriptorActorMsg {
31 actor: String,
32 #[serde(rename = "browserId")]
34 browser_id: u32,
35 #[serde(rename = "browsingContextID")]
36 browsing_context_id: u32,
37 is_zombie_tab: bool,
38 #[serde(rename = "outerWindowID")]
39 outer_window_id: u32,
40 pub selected: bool,
41 title: String,
42 traits: DescriptorTraits,
43 url: String,
44}
45
46#[derive(Serialize)]
47struct GetTargetReply {
48 from: String,
49 frame: BrowsingContextActorMsg,
50}
51
52#[derive(Serialize)]
53struct GetFaviconReply {
54 from: String,
55 favicon: Option<Vec<u8>>,
56}
57
58#[derive(Serialize)]
59struct GetWatcherReply {
60 from: String,
61 #[serde(flatten)]
62 watcher: WatcherActorMsg,
63}
64
65#[derive(MallocSizeOf)]
66pub(crate) struct TabDescriptorActor {
67 name: String,
68 pub browsing_context_name: String,
69}
70
71impl Actor for TabDescriptorActor {
72 fn name(&self) -> &str {
73 &self.name
74 }
75
76 fn handle_message(
87 &self,
88 request: ClientRequest,
89 registry: &ActorRegistry,
90 msg_type: &str,
91 msg: &Map<String, Value>,
92 _id: StreamId,
93 ) -> Result<(), ActorError> {
94 let browsing_context_actor =
95 registry.find::<BrowsingContextActor>(&self.browsing_context_name);
96 let pipeline = browsing_context_actor.pipeline_id();
97
98 match msg_type {
99 "getTarget" => request.reply_final(&GetTargetReply {
100 from: self.name().into(),
101 frame: browsing_context_actor.encode(registry),
102 })?,
103 "getFavicon" => {
104 let (tx, rx) = generic_channel::channel().ok_or(ActorError::Internal)?;
105 browsing_context_actor
106 .script_chan()
107 .send(DevtoolScriptControlMsg::GetFavicon(pipeline, tx))
108 .map_err(|_| ActorError::Internal)?;
109 let favicon = rx.recv().map_err(|_| ActorError::Internal)?;
110 request.reply_final(&GetFaviconReply {
111 from: self.name().into(),
112 favicon,
113 })?
114 },
115 "getWatcher" => request.reply_final(&GetWatcherReply {
116 from: self.name().into(),
117 watcher: registry.encode::<WatcherActor, _>(&browsing_context_actor.watcher_name),
118 })?,
119 "goBack" => {
120 browsing_context_actor
121 .script_chan()
122 .send(DevtoolScriptControlMsg::GoBack(pipeline))
123 .map_err(|_| ActorError::Internal)?;
124 request.reply_final(&EmptyReplyMsg {
125 from: self.name().into(),
126 })?
127 },
128 "goForward" => {
129 browsing_context_actor
130 .script_chan()
131 .send(DevtoolScriptControlMsg::GoForward(pipeline))
132 .map_err(|_| ActorError::Internal)?;
133 request.reply_final(&EmptyReplyMsg {
134 from: self.name().into(),
135 })?
136 },
137 "navigateTo" => {
138 if msg.get("waitForLoad").unwrap_or(&Value::Bool(false)) != &Value::Bool(false) {
139 log::warn!("waitForLoad option for devtools navigation is not supported.");
140 }
141 let url = msg
142 .get("url")
143 .and_then(|value| value.as_str())
144 .map(ServoUrl::parse)
145 .ok_or(ActorError::Internal)?
146 .map_err(|_| ActorError::Internal)?;
147
148 browsing_context_actor
149 .script_chan()
150 .send(DevtoolScriptControlMsg::NavigateTo(pipeline, url))
151 .map_err(|_| ActorError::Internal)?;
152
153 request.reply_final(&EmptyReplyMsg {
154 from: self.name().into(),
155 })?
156 },
157 "reloadDescriptor" => {
158 browsing_context_actor
160 .script_chan()
161 .send(DevtoolScriptControlMsg::Reload(pipeline))
162 .map_err(|_| ActorError::Internal)?;
163
164 request.reply_final(&EmptyReplyMsg {
165 from: self.name().into(),
166 })?
167 },
168 _ => return Err(ActorError::UnrecognizedPacketType),
169 };
170 Ok(())
171 }
172}
173
174impl TabDescriptorActor {
175 pub(crate) fn register(registry: &ActorRegistry, browsing_context_name: String) -> Arc<Self> {
176 let name = new_actor_name::<Self>();
177 let root_actor = registry.find::<RootActor>("root");
178 root_actor.tabs.borrow_mut().push(name.clone());
179 let actor = Self {
180 name,
181 browsing_context_name,
182 };
183 registry.register::<Self>(actor)
184 }
185
186 pub(crate) fn is_top_level_global(&self, registry: &ActorRegistry) -> bool {
187 registry
188 .find::<BrowsingContextActor>(&self.browsing_context_name)
189 .page_info
190 .borrow()
191 .is_top_level_global
192 }
193}
194
195impl ActorEncode<TabDescriptorActorMsg> for TabDescriptorActor {
196 fn encode(&self, registry: &ActorRegistry) -> TabDescriptorActorMsg {
197 let browsing_context_actor =
198 registry.find::<BrowsingContextActor>(&self.browsing_context_name);
199 let root_actor = registry.find::<RootActor>("root");
200 TabDescriptorActorMsg {
201 actor: self.name().into(),
202 browser_id: browsing_context_actor.browser_id.value(),
203 browsing_context_id: browsing_context_actor.browsing_context_id.value(),
204 is_zombie_tab: false,
205 outer_window_id: browsing_context_actor.outer_window_id().value(),
206 selected: root_actor.active_tab().as_deref() == Some(self.name()),
207 title: browsing_context_actor.title(),
208 traits: DescriptorTraits {
209 watcher: true,
210 supports_navigation: true,
211 supports_reload_descriptor: true,
212 },
213 url: browsing_context_actor.url(),
214 }
215 }
216}