1use core::fmt;
6#[cfg(feature = "webgpu")]
7use std::cell::RefCell;
8use std::option::Option;
9use std::result::Result;
10
11use base::generic_channel::{GenericSender, RoutedReceiver};
12use base::id::PipelineId;
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::ScriptToEmbedderChan;
19use ipc_channel::ipc::IpcSender;
20use net_traits::FetchResponseMsg;
21use net_traits::image_cache::ImageCacheResponseMessage;
22use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
23use profile_traits::time::{self as profile_time};
24use rustc_hash::FxHashSet;
25use script_traits::{Painter, ScriptThreadMessage};
26use stylo_atoms::Atom;
27use timers::TimerScheduler;
28#[cfg(feature = "webgpu")]
29use webgpu_traits::WebGPUMsg;
30
31use crate::dom::abstractworker::WorkerScriptMsg;
32use crate::dom::bindings::trace::CustomTraceable;
33use crate::dom::csp::Violation;
34use crate::dom::dedicatedworkerglobalscope::DedicatedWorkerScriptMsg;
35use crate::dom::serviceworkerglobalscope::ServiceWorkerScriptMsg;
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#[allow(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::AttachLayout(new_layout_info) => new_layout_info
60 .parent_info
61 .or(Some(new_layout_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::Viewport(id, ..) => Some(*id),
71 ScriptThreadMessage::GetTitle(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 ScriptThreadMessage::FocusIFrame(id, ..) => Some(*id),
81 ScriptThreadMessage::FocusDocument(id, ..) => Some(*id),
82 ScriptThreadMessage::Unfocus(id, ..) => Some(*id),
83 ScriptThreadMessage::WebDriverScriptCommand(id, ..) => Some(*id),
84 ScriptThreadMessage::TickAllAnimations(..) => None,
85 ScriptThreadMessage::WebFontLoaded(id, ..) => Some(*id),
86 ScriptThreadMessage::DispatchIFrameLoadEvent {
87 target: _,
88 parent: id,
89 child: _,
90 } => Some(*id),
91 ScriptThreadMessage::DispatchStorageEvent(id, ..) => Some(*id),
92 ScriptThreadMessage::ReportCSSError(id, ..) => Some(*id),
93 ScriptThreadMessage::Reload(id, ..) => Some(*id),
94 ScriptThreadMessage::PaintMetric(id, ..) => Some(*id),
95 ScriptThreadMessage::ExitFullScreen(id, ..) => Some(*id),
96 ScriptThreadMessage::MediaSessionAction(..) => None,
97 #[cfg(feature = "webgpu")]
98 ScriptThreadMessage::SetWebGPUPort(..) => None,
99 ScriptThreadMessage::SetScrollStates(id, ..) => Some(*id),
100 ScriptThreadMessage::EvaluateJavaScript(id, _, _) => Some(*id),
101 ScriptThreadMessage::SendImageKeysBatch(..) => None,
102 ScriptThreadMessage::PreferencesUpdated(..) => None,
103 ScriptThreadMessage::NoLongerWaitingOnAsychronousImageUpdates(_) => None,
104 },
105 MixedMessage::FromScript(inner_msg) => match inner_msg {
106 MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
107 *pipeline_id
108 },
109 MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
110 MainThreadScriptMsg::Common(CommonScriptMsg::ReportCspViolations(
111 pipeline_id,
112 _,
113 )) => Some(*pipeline_id),
114 MainThreadScriptMsg::NavigationResponse { pipeline_id, .. } => Some(*pipeline_id),
115 MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(*pipeline_id),
116 MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(*pipeline_id),
117 MainThreadScriptMsg::Inactive => None,
118 MainThreadScriptMsg::WakeUp => None,
119 },
120 MixedMessage::FromImageCache(response) => match response {
121 ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
122 Some(response.pipeline_id)
123 },
124 ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
125 Some(response.pipeline_id)
126 },
127 },
128 MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
129 #[cfg(feature = "webgpu")]
130 MixedMessage::FromWebGPUServer(..) => None,
131 }
132 }
133}
134
135#[derive(Debug)]
137pub(crate) enum MainThreadScriptMsg {
138 Common(CommonScriptMsg),
140 WorkletLoaded(PipelineId),
143 NavigationResponse {
144 pipeline_id: PipelineId,
145 message: Box<FetchResponseMsg>,
146 },
147 RegisterPaintWorklet {
149 pipeline_id: PipelineId,
150 name: Atom,
151 properties: Vec<Atom>,
152 painter: Box<dyn Painter>,
153 },
154 Inactive,
156 WakeUp,
158}
159
160pub(crate) enum CommonScriptMsg {
162 CollectReports(ReportsChan),
165 Task(
167 ScriptThreadEventCategory,
168 Box<dyn TaskBox>,
169 Option<PipelineId>,
170 TaskSourceName,
171 ),
172 ReportCspViolations(PipelineId, Vec<Violation>),
174}
175
176impl fmt::Debug for CommonScriptMsg {
177 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
178 match *self {
179 CommonScriptMsg::CollectReports(_) => write!(f, "CollectReports(...)"),
180 CommonScriptMsg::Task(ref category, ref task, _, _) => {
181 f.debug_tuple("Task").field(category).field(task).finish()
182 },
183 CommonScriptMsg::ReportCspViolations(..) => write!(f, "ReportCspViolations(...)"),
184 }
185 }
186}
187
188#[derive(Clone, JSTraceable, MallocSizeOf)]
192pub(crate) enum ScriptEventLoopSender {
193 MainThread(Sender<MainThreadScriptMsg>),
195 ServiceWorker(Sender<ServiceWorkerScriptMsg>),
197 DedicatedWorker {
201 sender: Sender<DedicatedWorkerScriptMsg>,
202 main_thread_worker: TrustedWorkerAddress,
203 },
204}
205
206impl ScriptEventLoopSender {
207 pub(crate) fn send(&self, message: CommonScriptMsg) -> Result<(), SendError<()>> {
209 match self {
210 Self::MainThread(sender) => sender
211 .send(MainThreadScriptMsg::Common(message))
212 .map_err(|_| SendError(())),
213 Self::ServiceWorker(sender) => sender
214 .send(ServiceWorkerScriptMsg::CommonWorker(
215 WorkerScriptMsg::Common(message),
216 ))
217 .map_err(|_| SendError(())),
218 Self::DedicatedWorker {
219 sender,
220 main_thread_worker,
221 } => {
222 let common_message = WorkerScriptMsg::Common(message);
223 sender
224 .send(DedicatedWorkerScriptMsg::CommonWorker(
225 main_thread_worker.clone(),
226 common_message,
227 ))
228 .map_err(|_| SendError(()))
229 },
230 }
231 }
232}
233
234pub(crate) enum ScriptEventLoopReceiver {
238 MainThread(Receiver<MainThreadScriptMsg>),
240 DedicatedWorker(Receiver<DedicatedWorkerScriptMsg>),
242}
243
244impl ScriptEventLoopReceiver {
245 pub(crate) fn recv(&self) -> Result<CommonScriptMsg, ()> {
246 match self {
247 Self::MainThread(receiver) => match receiver.recv() {
248 Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
249 Ok(_) => panic!("unexpected main thread event message!"),
250 Err(_) => Err(()),
251 },
252 Self::DedicatedWorker(receiver) => match receiver.recv() {
253 Ok(DedicatedWorkerScriptMsg::CommonWorker(_, WorkerScriptMsg::Common(message))) => {
254 Ok(message)
255 },
256 Ok(_) => panic!("unexpected worker event message!"),
257 Err(_) => Err(()),
258 },
259 }
260 }
261}
262
263impl QueuedTaskConversion for MainThreadScriptMsg {
264 fn task_source_name(&self) -> Option<&TaskSourceName> {
265 let script_msg = match self {
266 MainThreadScriptMsg::Common(script_msg) => script_msg,
267 _ => return None,
268 };
269 match script_msg {
270 CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
271 Some(task_source)
272 },
273 _ => None,
274 }
275 }
276
277 fn pipeline_id(&self) -> Option<PipelineId> {
278 let script_msg = match self {
279 MainThreadScriptMsg::Common(script_msg) => script_msg,
280 _ => return None,
281 };
282 match script_msg {
283 CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
284 _ => None,
285 }
286 }
287
288 fn into_queued_task(self) -> Option<QueuedTask> {
289 let script_msg = match self {
290 MainThreadScriptMsg::Common(script_msg) => script_msg,
291 _ => return None,
292 };
293 let (category, boxed, pipeline_id, task_source) = match script_msg {
294 CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
295 (category, boxed, pipeline_id, task_source)
296 },
297 _ => return None,
298 };
299 Some((None, category, boxed, pipeline_id, task_source))
300 }
301
302 fn from_queued_task(queued_task: QueuedTask) -> Self {
303 let (_worker, category, boxed, pipeline_id, task_source) = queued_task;
304 let script_msg = CommonScriptMsg::Task(category, boxed, pipeline_id, task_source);
305 MainThreadScriptMsg::Common(script_msg)
306 }
307
308 fn inactive_msg() -> Self {
309 MainThreadScriptMsg::Inactive
310 }
311
312 fn wake_up_msg() -> Self {
313 MainThreadScriptMsg::WakeUp
314 }
315
316 fn is_wake_up(&self) -> bool {
317 matches!(self, MainThreadScriptMsg::WakeUp)
318 }
319}
320
321impl OpaqueSender<CommonScriptMsg> for ScriptEventLoopSender {
322 fn send(&self, message: CommonScriptMsg) {
323 self.send(message).unwrap()
324 }
325}
326
327#[derive(Clone, JSTraceable)]
328pub(crate) struct ScriptThreadSenders {
329 pub(crate) self_sender: Sender<MainThreadScriptMsg>,
332
333 #[no_trace]
335 #[cfg(feature = "bluetooth")]
336 pub(crate) bluetooth_sender: IpcSender<BluetoothRequest>,
337
338 #[no_trace]
340 pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
341
342 #[no_trace]
345 pub(crate) pipeline_to_constellation_sender:
346 GenericSender<(PipelineId, ScriptToConstellationMessage)>,
347
348 #[no_trace]
350 pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
351
352 #[no_trace]
356 pub(crate) image_cache_sender: IpcSender<ImageCacheResponseMessage>,
357
358 #[no_trace]
360 pub(crate) time_profiler_sender: profile_time::ProfilerChan,
361
362 #[no_trace]
364 pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
365
366 #[no_trace]
368 pub(crate) devtools_server_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
369
370 #[no_trace]
371 pub(crate) devtools_client_to_script_thread_sender: IpcSender<DevtoolScriptControlMsg>,
372
373 #[no_trace]
374 pub(crate) content_process_shutdown_sender: Sender<()>,
375}
376
377#[derive(JSTraceable)]
378pub(crate) struct ScriptThreadReceivers {
379 #[no_trace]
381 pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
382
383 #[no_trace]
385 pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
386
387 #[no_trace]
390 pub(crate) devtools_server_receiver: Receiver<DevtoolScriptControlMsg>,
391
392 #[no_trace]
395 #[cfg(feature = "webgpu")]
396 pub(crate) webgpu_receiver: RefCell<Receiver<WebGPUMsg>>,
397}
398
399impl ScriptThreadReceivers {
400 pub(crate) fn recv(
403 &self,
404 task_queue: &TaskQueue<MainThreadScriptMsg>,
405 timer_scheduler: &TimerScheduler,
406 fully_active: &FxHashSet<PipelineId>,
407 ) -> MixedMessage {
408 select! {
409 recv(task_queue.select()) -> msg => {
410 task_queue.take_tasks(msg.unwrap(), fully_active);
411 let event = task_queue
412 .recv()
413 .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
414 MixedMessage::FromScript(event)
415 },
416 recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap().unwrap()),
417 recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap()),
418 recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
419 recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
420 recv({
421 #[cfg(feature = "webgpu")]
422 {
423 self.webgpu_receiver.borrow()
424 }
425 #[cfg(not(feature = "webgpu"))]
426 {
427 crossbeam_channel::never::<()>()
428 }
429 }) -> msg => {
430 #[cfg(feature = "webgpu")]
431 {
432 MixedMessage::FromWebGPUServer(msg.unwrap())
433 }
434 #[cfg(not(feature = "webgpu"))]
435 {
436 unreachable!("This should never be hit when webgpu is disabled ({msg:?})");
437 }
438 }
439 }
440 }
441
442 pub(crate) fn try_recv(
445 &self,
446 task_queue: &TaskQueue<MainThreadScriptMsg>,
447 fully_active: &FxHashSet<PipelineId>,
448 ) -> Option<MixedMessage> {
449 if let Ok(message) = self.constellation_receiver.try_recv() {
450 let message = message
451 .inspect_err(|e| {
452 log::warn!(
453 "ScriptThreadReceivers IPC error on constellation_receiver: {:?}",
454 e
455 );
456 })
457 .ok()?;
458 return MixedMessage::FromConstellation(message).into();
459 }
460 if let Ok(message) = task_queue.take_tasks_and_recv(fully_active) {
461 return MixedMessage::FromScript(message).into();
462 }
463 if let Ok(message) = self.devtools_server_receiver.try_recv() {
464 return MixedMessage::FromDevtools(message).into();
465 }
466 if let Ok(message) = self.image_cache_receiver.try_recv() {
467 return MixedMessage::FromImageCache(message).into();
468 }
469 #[cfg(feature = "webgpu")]
470 if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
471 return MixedMessage::FromWebGPUServer(message).into();
472 }
473 None
474 }
475}