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, 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 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::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::SetDocumentActivity(id, ..) => Some(*id),
72 ScriptThreadMessage::SetThrottled(_, id, ..) => Some(*id),
73 ScriptThreadMessage::SetThrottledInContainingIframe(_, id, ..) => Some(*id),
74 ScriptThreadMessage::NavigateIframe(id, ..) => Some(*id),
75 ScriptThreadMessage::PostMessage { target: id, .. } => Some(*id),
76 ScriptThreadMessage::UpdatePipelineId(_, _, _, id, _) => Some(*id),
77 ScriptThreadMessage::UpdateHistoryState(id, ..) => Some(*id),
78 ScriptThreadMessage::RemoveHistoryStates(id, ..) => Some(*id),
79 ScriptThreadMessage::FocusIFrame(id, ..) => Some(*id),
80 ScriptThreadMessage::FocusDocument(id, ..) => Some(*id),
81 ScriptThreadMessage::Unfocus(id, ..) => Some(*id),
82 ScriptThreadMessage::WebDriverScriptCommand(id, ..) => Some(*id),
83 ScriptThreadMessage::TickAllAnimations(..) => None,
84 ScriptThreadMessage::WebFontLoaded(id, ..) => Some(*id),
85 ScriptThreadMessage::DispatchIFrameLoadEvent {
86 target: _,
87 parent: id,
88 child: _,
89 } => Some(*id),
90 ScriptThreadMessage::DispatchStorageEvent(id, ..) => Some(*id),
91 ScriptThreadMessage::ReportCSSError(id, ..) => Some(*id),
92 ScriptThreadMessage::Reload(id, ..) => Some(*id),
93 ScriptThreadMessage::PaintMetric(id, ..) => Some(*id),
94 ScriptThreadMessage::ExitFullScreen(id, ..) => Some(*id),
95 ScriptThreadMessage::MediaSessionAction(..) => None,
96 #[cfg(feature = "webgpu")]
97 ScriptThreadMessage::SetWebGPUPort(..) => None,
98 ScriptThreadMessage::SetScrollStates(id, ..) => Some(*id),
99 ScriptThreadMessage::EvaluateJavaScript(_, id, _, _) => Some(*id),
100 ScriptThreadMessage::SendImageKeysBatch(..) => None,
101 ScriptThreadMessage::PreferencesUpdated(..) => None,
102 ScriptThreadMessage::NoLongerWaitingOnAsychronousImageUpdates(_) => None,
103 ScriptThreadMessage::ForwardKeyboardScroll(id, _) => Some(*id),
104 ScriptThreadMessage::RequestScreenshotReadiness(_, id) => Some(*id),
105 ScriptThreadMessage::EmbedderControlResponse(id, _) => Some(id.pipeline_id),
106 },
107 MixedMessage::FromScript(inner_msg) => match inner_msg {
108 MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
109 *pipeline_id
110 },
111 MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
112 MainThreadScriptMsg::Common(CommonScriptMsg::ReportCspViolations(
113 pipeline_id,
114 _,
115 )) => Some(*pipeline_id),
116 MainThreadScriptMsg::NavigationResponse { pipeline_id, .. } => Some(*pipeline_id),
117 MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(*pipeline_id),
118 MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(*pipeline_id),
119 MainThreadScriptMsg::Inactive => None,
120 MainThreadScriptMsg::WakeUp => None,
121 MainThreadScriptMsg::ForwardEmbedderControlResponseFromFileManager(
122 control_id,
123 ..,
124 ) => Some(control_id.pipeline_id),
125 },
126 MixedMessage::FromImageCache(response) => match response {
127 ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
128 Some(response.pipeline_id)
129 },
130 ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
131 Some(response.pipeline_id)
132 },
133 },
134 MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
135 #[cfg(feature = "webgpu")]
136 MixedMessage::FromWebGPUServer(..) => None,
137 }
138 }
139}
140
141#[derive(Debug)]
143pub(crate) enum MainThreadScriptMsg {
144 Common(CommonScriptMsg),
146 WorkletLoaded(PipelineId),
149 NavigationResponse {
150 pipeline_id: PipelineId,
151 message: Box<FetchResponseMsg>,
152 },
153 RegisterPaintWorklet {
155 pipeline_id: PipelineId,
156 name: Atom,
157 properties: Vec<Atom>,
158 painter: Box<dyn Painter>,
159 },
160 Inactive,
162 WakeUp,
164 ForwardEmbedderControlResponseFromFileManager(EmbedderControlId, EmbedderControlResponse),
167}
168
169pub(crate) enum CommonScriptMsg {
171 CollectReports(ReportsChan),
174 Task(
176 ScriptThreadEventCategory,
177 Box<dyn TaskBox>,
178 Option<PipelineId>,
179 TaskSourceName,
180 ),
181 ReportCspViolations(PipelineId, Vec<Violation>),
183}
184
185impl fmt::Debug for CommonScriptMsg {
186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187 match *self {
188 CommonScriptMsg::CollectReports(_) => write!(f, "CollectReports(...)"),
189 CommonScriptMsg::Task(ref category, ref task, _, _) => {
190 f.debug_tuple("Task").field(category).field(task).finish()
191 },
192 CommonScriptMsg::ReportCspViolations(..) => write!(f, "ReportCspViolations(...)"),
193 }
194 }
195}
196
197#[derive(Clone, JSTraceable, MallocSizeOf)]
201pub(crate) enum ScriptEventLoopSender {
202 MainThread(Sender<MainThreadScriptMsg>),
204 ServiceWorker(Sender<ServiceWorkerScriptMsg>),
206 DedicatedWorker {
210 sender: Sender<DedicatedWorkerScriptMsg>,
211 main_thread_worker: TrustedWorkerAddress,
212 },
213}
214
215impl ScriptEventLoopSender {
216 pub(crate) fn send(&self, message: CommonScriptMsg) -> Result<(), SendError<()>> {
218 match self {
219 Self::MainThread(sender) => sender
220 .send(MainThreadScriptMsg::Common(message))
221 .map_err(|_| SendError(())),
222 Self::ServiceWorker(sender) => sender
223 .send(ServiceWorkerScriptMsg::CommonWorker(
224 WorkerScriptMsg::Common(message),
225 ))
226 .map_err(|_| SendError(())),
227 Self::DedicatedWorker {
228 sender,
229 main_thread_worker,
230 } => {
231 let common_message = WorkerScriptMsg::Common(message);
232 sender
233 .send(DedicatedWorkerScriptMsg::CommonWorker(
234 main_thread_worker.clone(),
235 common_message,
236 ))
237 .map_err(|_| SendError(()))
238 },
239 }
240 }
241}
242
243pub(crate) enum ScriptEventLoopReceiver {
247 MainThread(Receiver<MainThreadScriptMsg>),
249 DedicatedWorker(Receiver<DedicatedWorkerScriptMsg>),
251}
252
253impl ScriptEventLoopReceiver {
254 pub(crate) fn recv(&self) -> Result<CommonScriptMsg, ()> {
255 match self {
256 Self::MainThread(receiver) => match receiver.recv() {
257 Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
258 Ok(_) => panic!("unexpected main thread event message!"),
259 Err(_) => Err(()),
260 },
261 Self::DedicatedWorker(receiver) => match receiver.recv() {
262 Ok(DedicatedWorkerScriptMsg::CommonWorker(_, WorkerScriptMsg::Common(message))) => {
263 Ok(message)
264 },
265 Ok(_) => panic!("unexpected worker event message!"),
266 Err(_) => Err(()),
267 },
268 }
269 }
270}
271
272impl QueuedTaskConversion for MainThreadScriptMsg {
273 fn task_source_name(&self) -> Option<&TaskSourceName> {
274 let script_msg = match self {
275 MainThreadScriptMsg::Common(script_msg) => script_msg,
276 _ => return None,
277 };
278 match script_msg {
279 CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
280 Some(task_source)
281 },
282 _ => None,
283 }
284 }
285
286 fn pipeline_id(&self) -> Option<PipelineId> {
287 let script_msg = match self {
288 MainThreadScriptMsg::Common(script_msg) => script_msg,
289 _ => return None,
290 };
291 match script_msg {
292 CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
293 _ => None,
294 }
295 }
296
297 fn into_queued_task(self) -> Option<QueuedTask> {
298 let script_msg = match self {
299 MainThreadScriptMsg::Common(script_msg) => script_msg,
300 _ => return None,
301 };
302 let (category, boxed, pipeline_id, task_source) = match script_msg {
303 CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
304 (category, boxed, pipeline_id, task_source)
305 },
306 _ => return None,
307 };
308 Some((None, category, boxed, pipeline_id, task_source))
309 }
310
311 fn from_queued_task(queued_task: QueuedTask) -> Self {
312 let (_worker, category, boxed, pipeline_id, task_source) = queued_task;
313 let script_msg = CommonScriptMsg::Task(category, boxed, pipeline_id, task_source);
314 MainThreadScriptMsg::Common(script_msg)
315 }
316
317 fn inactive_msg() -> Self {
318 MainThreadScriptMsg::Inactive
319 }
320
321 fn wake_up_msg() -> Self {
322 MainThreadScriptMsg::WakeUp
323 }
324
325 fn is_wake_up(&self) -> bool {
326 matches!(self, MainThreadScriptMsg::WakeUp)
327 }
328}
329
330impl OpaqueSender<CommonScriptMsg> for ScriptEventLoopSender {
331 fn send(&self, message: CommonScriptMsg) {
332 self.send(message).unwrap()
333 }
334}
335
336#[derive(Clone, JSTraceable)]
337pub(crate) struct ScriptThreadSenders {
338 pub(crate) self_sender: Sender<MainThreadScriptMsg>,
341
342 #[no_trace]
344 #[cfg(feature = "bluetooth")]
345 pub(crate) bluetooth_sender: IpcSender<BluetoothRequest>,
346
347 #[no_trace]
349 pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
350
351 #[no_trace]
354 pub(crate) pipeline_to_constellation_sender:
355 GenericSender<(WebViewId, PipelineId, ScriptToConstellationMessage)>,
356
357 #[no_trace]
359 pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
360
361 #[no_trace]
364 pub(crate) image_cache_sender: Sender<ImageCacheResponseMessage>,
365
366 #[no_trace]
368 pub(crate) time_profiler_sender: profile_time::ProfilerChan,
369
370 #[no_trace]
372 pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
373
374 #[no_trace]
376 pub(crate) devtools_server_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
377
378 #[no_trace]
379 pub(crate) devtools_client_to_script_thread_sender: IpcSender<DevtoolScriptControlMsg>,
380}
381
382#[derive(JSTraceable)]
383pub(crate) struct ScriptThreadReceivers {
384 #[no_trace]
386 pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
387
388 #[no_trace]
390 pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
391
392 #[no_trace]
395 pub(crate) devtools_server_receiver: Receiver<DevtoolScriptControlMsg>,
396
397 #[no_trace]
400 #[cfg(feature = "webgpu")]
401 pub(crate) webgpu_receiver: RefCell<Receiver<WebGPUMsg>>,
402}
403
404impl ScriptThreadReceivers {
405 pub(crate) fn recv(
408 &self,
409 task_queue: &TaskQueue<MainThreadScriptMsg>,
410 timer_scheduler: &TimerScheduler,
411 fully_active: &FxHashSet<PipelineId>,
412 ) -> MixedMessage {
413 select! {
414 recv(task_queue.select()) -> msg => {
415 task_queue.take_tasks(msg.unwrap(), fully_active);
416 let event = task_queue
417 .recv()
418 .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
419 MixedMessage::FromScript(event)
420 },
421 recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap().unwrap()),
422 recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap()),
423 recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
424 recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
425 recv({
426 #[cfg(feature = "webgpu")]
427 {
428 self.webgpu_receiver.borrow()
429 }
430 #[cfg(not(feature = "webgpu"))]
431 {
432 crossbeam_channel::never::<()>()
433 }
434 }) -> msg => {
435 #[cfg(feature = "webgpu")]
436 {
437 MixedMessage::FromWebGPUServer(msg.unwrap())
438 }
439 #[cfg(not(feature = "webgpu"))]
440 {
441 unreachable!("This should never be hit when webgpu is disabled ({msg:?})");
442 }
443 }
444 }
445 }
446
447 pub(crate) fn try_recv(
450 &self,
451 task_queue: &TaskQueue<MainThreadScriptMsg>,
452 fully_active: &FxHashSet<PipelineId>,
453 ) -> Option<MixedMessage> {
454 if let Ok(message) = self.constellation_receiver.try_recv() {
455 let message = message
456 .inspect_err(|e| {
457 log::warn!(
458 "ScriptThreadReceivers IPC error on constellation_receiver: {:?}",
459 e
460 );
461 })
462 .ok()?;
463 return MixedMessage::FromConstellation(message).into();
464 }
465 if let Ok(message) = task_queue.take_tasks_and_recv(fully_active) {
466 return MixedMessage::FromScript(message).into();
467 }
468 if let Ok(message) = self.devtools_server_receiver.try_recv() {
469 return MixedMessage::FromDevtools(message).into();
470 }
471 if let Ok(message) = self.image_cache_receiver.try_recv() {
472 return MixedMessage::FromImageCache(message).into();
473 }
474 #[cfg(feature = "webgpu")]
475 if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
476 return MixedMessage::FromWebGPUServer(message).into();
477 }
478 None
479 }
480}