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