1use core::fmt;
6#[cfg(feature = "webgpu")]
7use std::cell::RefCell;
8use std::option::Option;
9use std::result::Result;
10
11use crossbeam_channel::{Receiver, SendError, Sender, select};
12use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
13use embedder_traits::{EmbedderControlId, EmbedderControlResponse, ScriptToEmbedderChan};
14use net_traits::FetchResponseMsg;
15use net_traits::image_cache::ImageCacheResponseMessage;
16use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
17use profile_traits::time::{self as profile_time};
18use rustc_hash::FxHashSet;
19use script_traits::{Painter, ScriptThreadMessage};
20use servo_base::generic_channel::{GenericCallback, GenericSender, RoutedReceiver};
21use servo_base::id::{PipelineId, WebViewId};
22#[cfg(feature = "bluetooth")]
23use servo_bluetooth_traits::BluetoothRequest;
24use servo_constellation_traits::ScriptToConstellationMessage;
25use stylo_atoms::Atom;
26use timers::TimerScheduler;
27#[cfg(feature = "webgpu")]
28use webgpu_traits::WebGPUMsg;
29
30use crate::dom::abstractworker::WorkerScriptMsg;
31use crate::dom::bindings::trace::CustomTraceable;
32use crate::dom::csp::Violation;
33use crate::dom::dedicatedworkerglobalscope::DedicatedWorkerScriptMsg;
34use crate::dom::serviceworkerglobalscope::ServiceWorkerScriptMsg;
35use crate::dom::sharedworkerglobalscope::SharedWorkerScriptMsg;
36use crate::dom::worker::TrustedWorkerAddress;
37use crate::script_runtime::ScriptThreadEventCategory;
38use crate::task::TaskBox;
39use crate::task_queue::{QueuedTask, QueuedTaskConversion, TaskQueue};
40use crate::task_source::TaskSourceName;
41
42#[expect(clippy::large_enum_variant)]
43#[derive(Debug)]
44pub(crate) enum MixedMessage {
45 FromConstellation(ScriptThreadMessage),
46 FromScript(MainThreadScriptMsg),
47 FromDevtools(DevtoolScriptControlMsg),
48 FromImageCache(ImageCacheResponseMessage),
49 #[cfg(feature = "webgpu")]
50 FromWebGPUServer(WebGPUMsg),
51 TimerFired,
52}
53
54impl MixedMessage {
55 pub(crate) fn pipeline_id(&self) -> Option<PipelineId> {
56 match self {
57 MixedMessage::FromConstellation(inner_msg) => match inner_msg {
58 ScriptThreadMessage::StopDelayingLoadEventsMode(id) => Some(*id),
59 ScriptThreadMessage::SpawnPipeline(new_pipeline_info) => new_pipeline_info
60 .parent_info
61 .or(Some(new_pipeline_info.new_pipeline_id)),
62 ScriptThreadMessage::Resize(id, ..) => Some(*id),
63 ScriptThreadMessage::ThemeChange(id, ..) => Some(*id),
64 ScriptThreadMessage::ResizeInactive(id, ..) => Some(*id),
65 ScriptThreadMessage::UnloadDocument(id) => Some(*id),
66 ScriptThreadMessage::ExitPipeline(_webview_id, id, ..) => Some(*id),
67 ScriptThreadMessage::ExitScriptThread => None,
68 ScriptThreadMessage::SendInputEvent(_, id, _) => Some(*id),
69 ScriptThreadMessage::RefreshCursor(id, ..) => Some(*id),
70 ScriptThreadMessage::GetTitle(id) => Some(*id),
71 ScriptThreadMessage::GetDocumentOrigin(id, _) => Some(*id),
72 ScriptThreadMessage::SetDocumentActivity(id, ..) => Some(*id),
73 ScriptThreadMessage::SetThrottled(_, id, ..) => Some(*id),
74 ScriptThreadMessage::SetThrottledInContainingIframe(_, id, ..) => Some(*id),
75 ScriptThreadMessage::NavigateIframe(id, ..) => Some(*id),
76 ScriptThreadMessage::PostMessage { target: id, .. } => Some(*id),
77 ScriptThreadMessage::UpdatePipelineId(_, _, _, id, _) => Some(*id),
78 ScriptThreadMessage::UpdateHistoryState(id, ..) => Some(*id),
79 ScriptThreadMessage::RemoveHistoryStates(id, ..) => Some(*id),
80
81 ScriptThreadMessage::FocusDocumentAsPartOfFocusingSteps(id, ..) => Some(*id),
82 ScriptThreadMessage::UnfocusDocumentAsPartOfFocusingSteps(id, ..) => Some(*id),
83 ScriptThreadMessage::FocusDocument(id, ..) => Some(*id),
84 ScriptThreadMessage::WebDriverScriptCommand(id, ..) => Some(*id),
85 ScriptThreadMessage::TickAllAnimations(..) => None,
86 ScriptThreadMessage::WebFontLoaded(id) => Some(*id),
87 ScriptThreadMessage::DispatchIFrameLoadEvent {
88 target: _,
89 parent: id,
90 child: _,
91 } => Some(*id),
92 ScriptThreadMessage::DispatchStorageEvent(id, ..) => Some(*id),
93 ScriptThreadMessage::ReportCSSError(id, ..) => Some(*id),
94 ScriptThreadMessage::Reload(id, ..) => Some(*id),
95 ScriptThreadMessage::PaintMetric(id, ..) => Some(*id),
96 ScriptThreadMessage::ExitFullScreen(id, ..) => Some(*id),
97 ScriptThreadMessage::MediaSessionAction(..) => None,
98 #[cfg(feature = "webgpu")]
99 ScriptThreadMessage::SetWebGPUPort(..) => None,
100 ScriptThreadMessage::SetScrollStates(id, ..) => Some(*id),
101 ScriptThreadMessage::EvaluateJavaScript(_, id, _, _) => Some(*id),
102 ScriptThreadMessage::SendImageKeysBatch(..) => None,
103 ScriptThreadMessage::PreferencesUpdated(..) => None,
104 ScriptThreadMessage::NoLongerWaitingOnAsychronousImageUpdates(_) => None,
105 ScriptThreadMessage::ForwardKeyboardScroll(id, _) => Some(*id),
106 ScriptThreadMessage::RequestScreenshotReadiness(_, id) => Some(*id),
107 ScriptThreadMessage::EmbedderControlResponse(id, _) => Some(id.pipeline_id),
108 ScriptThreadMessage::SetUserContents(..) => None,
109 ScriptThreadMessage::DestroyUserContentManager(..) => None,
110 ScriptThreadMessage::UpdatePinchZoomInfos(id, _) => Some(*id),
111 ScriptThreadMessage::SetAccessibilityActive(..) => None,
112 ScriptThreadMessage::TriggerGarbageCollection => None,
113 },
114 MixedMessage::FromScript(inner_msg) => match inner_msg {
115 MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
116 *pipeline_id
117 },
118 MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
119 MainThreadScriptMsg::Common(CommonScriptMsg::ReportCspViolations(
120 pipeline_id,
121 _,
122 )) => Some(*pipeline_id),
123 MainThreadScriptMsg::NavigationResponse { pipeline_id, .. } => Some(*pipeline_id),
124 MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(*pipeline_id),
125 MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(*pipeline_id),
126 MainThreadScriptMsg::Inactive => None,
127 MainThreadScriptMsg::WakeUp => None,
128 MainThreadScriptMsg::ForwardEmbedderControlResponseFromFileManager(
129 control_id,
130 ..,
131 ) => Some(control_id.pipeline_id),
132 },
133 MixedMessage::FromImageCache(response) => match response {
134 ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
135 Some(response.pipeline_id)
136 },
137 ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
138 Some(response.pipeline_id)
139 },
140 },
141 MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
142 #[cfg(feature = "webgpu")]
143 MixedMessage::FromWebGPUServer(..) => None,
144 }
145 }
146}
147
148#[derive(Debug)]
150pub(crate) enum MainThreadScriptMsg {
151 Common(CommonScriptMsg),
153 WorkletLoaded(PipelineId),
156 NavigationResponse {
157 pipeline_id: PipelineId,
158 message: Box<FetchResponseMsg>,
159 },
160 RegisterPaintWorklet {
162 pipeline_id: PipelineId,
163 name: Atom,
164 properties: Vec<Atom>,
165 painter: Box<dyn Painter>,
166 },
167 Inactive,
169 WakeUp,
171 ForwardEmbedderControlResponseFromFileManager(EmbedderControlId, EmbedderControlResponse),
174}
175
176pub(crate) enum CommonScriptMsg {
178 CollectReports(ReportsChan),
181 Task(
183 ScriptThreadEventCategory,
184 Box<dyn TaskBox>,
185 Option<PipelineId>,
186 TaskSourceName,
187 ),
188 ReportCspViolations(PipelineId, Vec<Violation>),
190}
191
192impl fmt::Debug for CommonScriptMsg {
193 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
194 match *self {
195 CommonScriptMsg::CollectReports(_) => write!(f, "CollectReports(...)"),
196 CommonScriptMsg::Task(ref category, ref task, _, _) => {
197 f.debug_tuple("Task").field(category).field(task).finish()
198 },
199 CommonScriptMsg::ReportCspViolations(..) => write!(f, "ReportCspViolations(...)"),
200 }
201 }
202}
203
204#[derive(Clone, JSTraceable, MallocSizeOf)]
208pub(crate) enum ScriptEventLoopSender {
209 MainThread(Sender<MainThreadScriptMsg>),
211 SharedWorker(Sender<SharedWorkerScriptMsg>),
213 ServiceWorker(Sender<ServiceWorkerScriptMsg>),
215 DedicatedWorker {
219 sender: Sender<DedicatedWorkerScriptMsg>,
220 main_thread_worker: TrustedWorkerAddress,
221 },
222}
223
224impl ScriptEventLoopSender {
225 pub(crate) fn send(&self, message: CommonScriptMsg) -> Result<(), SendError<()>> {
227 match self {
228 Self::MainThread(sender) => sender
229 .send(MainThreadScriptMsg::Common(message))
230 .map_err(|_| SendError(())),
231 Self::SharedWorker(sender) => sender
232 .send(SharedWorkerScriptMsg::CommonWorker(
233 WorkerScriptMsg::Common(message),
234 ))
235 .map_err(|_| SendError(())),
236 Self::ServiceWorker(sender) => sender
237 .send(ServiceWorkerScriptMsg::CommonWorker(
238 WorkerScriptMsg::Common(message),
239 ))
240 .map_err(|_| SendError(())),
241 Self::DedicatedWorker {
242 sender,
243 main_thread_worker,
244 } => {
245 let common_message = WorkerScriptMsg::Common(message);
246 sender
247 .send(DedicatedWorkerScriptMsg::CommonWorker(
248 main_thread_worker.clone(),
249 common_message,
250 ))
251 .map_err(|_| SendError(()))
252 },
253 }
254 }
255}
256
257pub(crate) enum ScriptEventLoopReceiver {
261 MainThread(Receiver<MainThreadScriptMsg>),
263 SharedWorker(Receiver<SharedWorkerScriptMsg>),
265 DedicatedWorker(Receiver<DedicatedWorkerScriptMsg>),
267}
268
269impl ScriptEventLoopReceiver {
270 pub(crate) fn recv(&self) -> Result<CommonScriptMsg, ()> {
271 match self {
272 Self::MainThread(receiver) => match receiver.recv() {
273 Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
274 Ok(_) => panic!("unexpected main thread event message!"),
275 Err(_) => Err(()),
276 },
277 Self::SharedWorker(receiver) => match receiver.recv() {
278 Ok(SharedWorkerScriptMsg::CommonWorker(WorkerScriptMsg::Common(message))) => {
279 Ok(message)
280 },
281 Ok(_) => panic!("unexpected shared worker event message!"),
282 Err(_) => Err(()),
283 },
284 Self::DedicatedWorker(receiver) => match receiver.recv() {
285 Ok(DedicatedWorkerScriptMsg::CommonWorker(_, WorkerScriptMsg::Common(message))) => {
286 Ok(message)
287 },
288 Ok(_) => panic!("unexpected worker event message!"),
289 Err(_) => Err(()),
290 },
291 }
292 }
293}
294
295impl QueuedTaskConversion for MainThreadScriptMsg {
296 fn task_source_name(&self) -> Option<&TaskSourceName> {
297 let script_msg = match self {
298 MainThreadScriptMsg::Common(script_msg) => script_msg,
299 _ => return None,
300 };
301 match script_msg {
302 CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
303 Some(task_source)
304 },
305 _ => None,
306 }
307 }
308
309 fn pipeline_id(&self) -> Option<PipelineId> {
310 let script_msg = match self {
311 MainThreadScriptMsg::Common(script_msg) => script_msg,
312 _ => return None,
313 };
314 match script_msg {
315 CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
316 _ => None,
317 }
318 }
319
320 fn into_queued_task(self) -> Option<QueuedTask> {
321 let script_msg = match self {
322 MainThreadScriptMsg::Common(script_msg) => script_msg,
323 _ => return None,
324 };
325 let (event_category, task, pipeline_id, task_source) = match script_msg {
326 CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
327 (category, boxed, pipeline_id, task_source)
328 },
329 _ => return None,
330 };
331 Some(QueuedTask {
332 worker: None,
333 event_category,
334 task,
335 pipeline_id,
336 task_source,
337 })
338 }
339
340 fn from_queued_task(queued_task: QueuedTask) -> Self {
341 let script_msg = CommonScriptMsg::Task(
342 queued_task.event_category,
343 queued_task.task,
344 queued_task.pipeline_id,
345 queued_task.task_source,
346 );
347 MainThreadScriptMsg::Common(script_msg)
348 }
349
350 fn inactive_msg() -> Self {
351 MainThreadScriptMsg::Inactive
352 }
353
354 fn wake_up_msg() -> Self {
355 MainThreadScriptMsg::WakeUp
356 }
357
358 fn is_wake_up(&self) -> bool {
359 matches!(self, MainThreadScriptMsg::WakeUp)
360 }
361}
362
363impl OpaqueSender<CommonScriptMsg> for ScriptEventLoopSender {
364 fn send(&self, message: CommonScriptMsg) {
365 self.send(message).unwrap()
366 }
367}
368
369#[derive(Clone, JSTraceable)]
370pub(crate) struct ScriptThreadSenders {
371 pub(crate) self_sender: Sender<MainThreadScriptMsg>,
374
375 #[no_trace]
377 #[cfg(feature = "bluetooth")]
378 pub(crate) bluetooth_sender: GenericSender<BluetoothRequest>,
379
380 #[no_trace]
382 pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
383
384 #[no_trace]
387 pub(crate) pipeline_to_constellation_sender:
388 GenericSender<(WebViewId, PipelineId, ScriptToConstellationMessage)>,
389
390 #[no_trace]
392 pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
393
394 #[no_trace]
397 pub(crate) image_cache_sender: Sender<ImageCacheResponseMessage>,
398
399 #[no_trace]
401 pub(crate) time_profiler_sender: profile_time::ProfilerChan,
402
403 #[no_trace]
405 pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
406
407 #[no_trace]
409 pub(crate) devtools_server_sender: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
410
411 #[no_trace]
412 pub(crate) devtools_client_to_script_thread_sender: GenericSender<DevtoolScriptControlMsg>,
413}
414
415#[derive(JSTraceable)]
416pub(crate) struct ScriptThreadReceivers {
417 #[no_trace]
419 pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
420
421 #[no_trace]
423 pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
424
425 #[no_trace]
428 pub(crate) devtools_server_receiver: RoutedReceiver<DevtoolScriptControlMsg>,
429
430 #[no_trace]
433 #[cfg(feature = "webgpu")]
434 pub(crate) webgpu_receiver: RefCell<RoutedReceiver<WebGPUMsg>>,
435}
436
437impl ScriptThreadReceivers {
438 pub(crate) fn recv(
441 &self,
442 task_queue: &TaskQueue<MainThreadScriptMsg>,
443 timer_scheduler: &TimerScheduler,
444 fully_active: &FxHashSet<PipelineId>,
445 ) -> MixedMessage {
446 select! {
447 recv(task_queue.select()) -> msg => {
448 task_queue.take_tasks(msg.unwrap(), fully_active);
449 let event = task_queue
450 .recv()
451 .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
452 MixedMessage::FromScript(event)
453 },
454 recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap().unwrap()),
455 recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap().unwrap()),
456 recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
457 recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
458 recv({
459 #[cfg(feature = "webgpu")]
460 {
461 self.webgpu_receiver.borrow()
462 }
463 #[cfg(not(feature = "webgpu"))]
464 {
465 crossbeam_channel::never::<()>()
466 }
467 }) -> msg => {
468 #[cfg(feature = "webgpu")]
469 {
470 MixedMessage::FromWebGPUServer(msg.unwrap().unwrap())
471 }
472 #[cfg(not(feature = "webgpu"))]
473 {
474 unreachable!("This should never be hit when webgpu is disabled ({msg:?})");
475 }
476 }
477 }
478 }
479
480 pub(crate) fn try_recv(
483 &self,
484 task_queue: &TaskQueue<MainThreadScriptMsg>,
485 fully_active: &FxHashSet<PipelineId>,
486 ) -> Option<MixedMessage> {
487 if let Ok(message) = self.constellation_receiver.try_recv() {
488 let message = message
489 .inspect_err(|e| {
490 log::warn!(
491 "ScriptThreadReceivers IPC error on constellation_receiver: {:?}",
492 e
493 );
494 })
495 .ok()?;
496 return MixedMessage::FromConstellation(message).into();
497 }
498 if let Ok(message) = task_queue.take_tasks_and_recv(fully_active) {
499 return MixedMessage::FromScript(message).into();
500 }
501 if let Ok(message) = self.devtools_server_receiver.try_recv() {
502 return MixedMessage::FromDevtools(message.unwrap()).into();
503 }
504 if let Ok(message) = self.image_cache_receiver.try_recv() {
505 return MixedMessage::FromImageCache(message).into();
506 }
507 #[cfg(feature = "webgpu")]
508 if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
509 return MixedMessage::FromWebGPUServer(message.unwrap()).into();
510 }
511 None
512 }
513}