1use core::fmt;
6#[cfg(feature = "webgpu")]
7use std::cell::RefCell;
8use std::collections::HashSet;
9use std::option::Option;
10use std::result::Result;
11
12use base::generic_channel::{GenericSender, RoutedReceiver};
13use base::id::PipelineId;
14#[cfg(feature = "bluetooth")]
15use bluetooth_traits::BluetoothRequest;
16use constellation_traits::ScriptToConstellationMessage;
17use crossbeam_channel::{Receiver, SendError, Sender, select};
18use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
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 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#[allow(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::AttachLayout(new_layout_info) => new_layout_info
59 .parent_info
60 .or(Some(new_layout_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::Viewport(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 },
104 MixedMessage::FromScript(inner_msg) => match inner_msg {
105 MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
106 *pipeline_id
107 },
108 MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
109 MainThreadScriptMsg::Common(CommonScriptMsg::ReportCspViolations(
110 pipeline_id,
111 _,
112 )) => Some(*pipeline_id),
113 MainThreadScriptMsg::NavigationResponse { pipeline_id, .. } => Some(*pipeline_id),
114 MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(*pipeline_id),
115 MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(*pipeline_id),
116 MainThreadScriptMsg::Inactive => None,
117 MainThreadScriptMsg::WakeUp => None,
118 },
119 MixedMessage::FromImageCache(response) => match response {
120 ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
121 Some(response.pipeline_id)
122 },
123 ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
124 Some(response.pipeline_id)
125 },
126 },
127 MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
128 #[cfg(feature = "webgpu")]
129 MixedMessage::FromWebGPUServer(..) => None,
130 }
131 }
132}
133
134#[derive(Debug)]
136pub(crate) enum MainThreadScriptMsg {
137 Common(CommonScriptMsg),
139 WorkletLoaded(PipelineId),
142 NavigationResponse {
143 pipeline_id: PipelineId,
144 message: Box<FetchResponseMsg>,
145 },
146 RegisterPaintWorklet {
148 pipeline_id: PipelineId,
149 name: Atom,
150 properties: Vec<Atom>,
151 painter: Box<dyn Painter>,
152 },
153 Inactive,
155 WakeUp,
157}
158
159pub(crate) enum CommonScriptMsg {
161 CollectReports(ReportsChan),
164 Task(
166 ScriptThreadEventCategory,
167 Box<dyn TaskBox>,
168 Option<PipelineId>,
169 TaskSourceName,
170 ),
171 ReportCspViolations(PipelineId, Vec<Violation>),
173}
174
175impl fmt::Debug for CommonScriptMsg {
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177 match *self {
178 CommonScriptMsg::CollectReports(_) => write!(f, "CollectReports(...)"),
179 CommonScriptMsg::Task(ref category, ref task, _, _) => {
180 f.debug_tuple("Task").field(category).field(task).finish()
181 },
182 CommonScriptMsg::ReportCspViolations(..) => write!(f, "ReportCspViolations(...)"),
183 }
184 }
185}
186
187#[derive(Clone, JSTraceable, MallocSizeOf)]
191pub(crate) enum ScriptEventLoopSender {
192 MainThread(Sender<MainThreadScriptMsg>),
194 ServiceWorker(Sender<ServiceWorkerScriptMsg>),
196 DedicatedWorker {
200 sender: Sender<DedicatedWorkerScriptMsg>,
201 main_thread_worker: TrustedWorkerAddress,
202 },
203}
204
205impl ScriptEventLoopSender {
206 pub(crate) fn send(&self, message: CommonScriptMsg) -> Result<(), SendError<()>> {
208 match self {
209 Self::MainThread(sender) => sender
210 .send(MainThreadScriptMsg::Common(message))
211 .map_err(|_| SendError(())),
212 Self::ServiceWorker(sender) => sender
213 .send(ServiceWorkerScriptMsg::CommonWorker(
214 WorkerScriptMsg::Common(message),
215 ))
216 .map_err(|_| SendError(())),
217 Self::DedicatedWorker {
218 sender,
219 main_thread_worker,
220 } => {
221 let common_message = WorkerScriptMsg::Common(message);
222 sender
223 .send(DedicatedWorkerScriptMsg::CommonWorker(
224 main_thread_worker.clone(),
225 common_message,
226 ))
227 .map_err(|_| SendError(()))
228 },
229 }
230 }
231}
232
233pub(crate) enum ScriptEventLoopReceiver {
237 MainThread(Receiver<MainThreadScriptMsg>),
239 DedicatedWorker(Receiver<DedicatedWorkerScriptMsg>),
241}
242
243impl ScriptEventLoopReceiver {
244 pub(crate) fn recv(&self) -> Result<CommonScriptMsg, ()> {
245 match self {
246 Self::MainThread(receiver) => match receiver.recv() {
247 Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
248 Ok(_) => panic!("unexpected main thread event message!"),
249 Err(_) => Err(()),
250 },
251 Self::DedicatedWorker(receiver) => match receiver.recv() {
252 Ok(DedicatedWorkerScriptMsg::CommonWorker(_, WorkerScriptMsg::Common(message))) => {
253 Ok(message)
254 },
255 Ok(_) => panic!("unexpected worker event message!"),
256 Err(_) => Err(()),
257 },
258 }
259 }
260}
261
262impl QueuedTaskConversion for MainThreadScriptMsg {
263 fn task_source_name(&self) -> Option<&TaskSourceName> {
264 let script_msg = match self {
265 MainThreadScriptMsg::Common(script_msg) => script_msg,
266 _ => return None,
267 };
268 match script_msg {
269 CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
270 Some(task_source)
271 },
272 _ => None,
273 }
274 }
275
276 fn pipeline_id(&self) -> Option<PipelineId> {
277 let script_msg = match self {
278 MainThreadScriptMsg::Common(script_msg) => script_msg,
279 _ => return None,
280 };
281 match script_msg {
282 CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
283 _ => None,
284 }
285 }
286
287 fn into_queued_task(self) -> Option<QueuedTask> {
288 let script_msg = match self {
289 MainThreadScriptMsg::Common(script_msg) => script_msg,
290 _ => return None,
291 };
292 let (category, boxed, pipeline_id, task_source) = match script_msg {
293 CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
294 (category, boxed, pipeline_id, task_source)
295 },
296 _ => return None,
297 };
298 Some((None, category, boxed, pipeline_id, task_source))
299 }
300
301 fn from_queued_task(queued_task: QueuedTask) -> Self {
302 let (_worker, category, boxed, pipeline_id, task_source) = queued_task;
303 let script_msg = CommonScriptMsg::Task(category, boxed, pipeline_id, task_source);
304 MainThreadScriptMsg::Common(script_msg)
305 }
306
307 fn inactive_msg() -> Self {
308 MainThreadScriptMsg::Inactive
309 }
310
311 fn wake_up_msg() -> Self {
312 MainThreadScriptMsg::WakeUp
313 }
314
315 fn is_wake_up(&self) -> bool {
316 matches!(self, MainThreadScriptMsg::WakeUp)
317 }
318}
319
320impl OpaqueSender<CommonScriptMsg> for ScriptEventLoopSender {
321 fn send(&self, message: CommonScriptMsg) {
322 self.send(message).unwrap()
323 }
324}
325
326#[derive(Clone, JSTraceable)]
327pub(crate) struct ScriptThreadSenders {
328 pub(crate) self_sender: Sender<MainThreadScriptMsg>,
331
332 #[no_trace]
334 #[cfg(feature = "bluetooth")]
335 pub(crate) bluetooth_sender: IpcSender<BluetoothRequest>,
336
337 #[no_trace]
339 pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
340
341 #[no_trace]
344 pub(crate) pipeline_to_constellation_sender:
345 GenericSender<(PipelineId, ScriptToConstellationMessage)>,
346
347 #[no_trace]
351 pub(crate) image_cache_sender: IpcSender<ImageCacheResponseMessage>,
352
353 #[no_trace]
355 pub(crate) time_profiler_sender: profile_time::ProfilerChan,
356
357 #[no_trace]
359 pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
360
361 #[no_trace]
363 pub(crate) devtools_server_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
364
365 #[no_trace]
366 pub(crate) devtools_client_to_script_thread_sender: IpcSender<DevtoolScriptControlMsg>,
367
368 #[no_trace]
369 pub(crate) content_process_shutdown_sender: Sender<()>,
370}
371
372#[derive(JSTraceable)]
373pub(crate) struct ScriptThreadReceivers {
374 #[no_trace]
376 pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
377
378 #[no_trace]
380 pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
381
382 #[no_trace]
385 pub(crate) devtools_server_receiver: Receiver<DevtoolScriptControlMsg>,
386
387 #[no_trace]
390 #[cfg(feature = "webgpu")]
391 pub(crate) webgpu_receiver: RefCell<Receiver<WebGPUMsg>>,
392}
393
394impl ScriptThreadReceivers {
395 pub(crate) fn recv(
398 &self,
399 task_queue: &TaskQueue<MainThreadScriptMsg>,
400 timer_scheduler: &TimerScheduler,
401 fully_active: &HashSet<PipelineId>,
402 ) -> MixedMessage {
403 select! {
404 recv(task_queue.select()) -> msg => {
405 task_queue.take_tasks(msg.unwrap(), fully_active);
406 let event = task_queue
407 .recv()
408 .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
409 MixedMessage::FromScript(event)
410 },
411 recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap().unwrap()),
412 recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap()),
413 recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
414 recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
415 recv({
416 #[cfg(feature = "webgpu")]
417 {
418 self.webgpu_receiver.borrow()
419 }
420 #[cfg(not(feature = "webgpu"))]
421 {
422 crossbeam_channel::never::<()>()
423 }
424 }) -> msg => {
425 #[cfg(feature = "webgpu")]
426 {
427 MixedMessage::FromWebGPUServer(msg.unwrap())
428 }
429 #[cfg(not(feature = "webgpu"))]
430 {
431 unreachable!("This should never be hit when webgpu is disabled ({msg:?})");
432 }
433 }
434 }
435 }
436
437 pub(crate) fn try_recv(
440 &self,
441 task_queue: &TaskQueue<MainThreadScriptMsg>,
442 fully_active: &HashSet<PipelineId>,
443 ) -> Option<MixedMessage> {
444 if let Ok(message) = self.constellation_receiver.try_recv() {
445 let message = message
446 .inspect_err(|e| {
447 log::warn!(
448 "ScriptThreadReceivers IPC error on constellation_receiver: {:?}",
449 e
450 );
451 })
452 .ok()?;
453 return MixedMessage::FromConstellation(message).into();
454 }
455 if let Ok(message) = task_queue.take_tasks_and_recv(fully_active) {
456 return MixedMessage::FromScript(message).into();
457 }
458 if let Ok(message) = self.devtools_server_receiver.try_recv() {
459 return MixedMessage::FromDevtools(message).into();
460 }
461 if let Ok(message) = self.image_cache_receiver.try_recv() {
462 return MixedMessage::FromImageCache(message).into();
463 }
464 #[cfg(feature = "webgpu")]
465 if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
466 return MixedMessage::FromWebGPUServer(message).into();
467 }
468 None
469 }
470}