script/
messaging.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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/// Messages used to control the script event loop.
147#[derive(Debug)]
148pub(crate) enum MainThreadScriptMsg {
149    /// Common variants associated with the script messages
150    Common(CommonScriptMsg),
151    /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
152    /// reflowed.
153    WorkletLoaded(PipelineId),
154    NavigationResponse {
155        pipeline_id: PipelineId,
156        message: Box<FetchResponseMsg>,
157    },
158    /// Notifies the script thread that a new paint worklet has been registered.
159    RegisterPaintWorklet {
160        pipeline_id: PipelineId,
161        name: Atom,
162        properties: Vec<Atom>,
163        painter: Box<dyn Painter>,
164    },
165    /// A task related to a not fully-active document has been throttled.
166    Inactive,
167    /// Wake-up call from the task queue.
168    WakeUp,
169    /// The `FileManagerThread` has finished selecting files is forwarding the response to
170    /// the main thread of this `ScriptThread`.
171    ForwardEmbedderControlResponseFromFileManager(EmbedderControlId, EmbedderControlResponse),
172}
173
174/// Common messages used to control the event loops in both the script and the worker
175pub(crate) enum CommonScriptMsg {
176    /// Requests that the script thread measure its memory usage. The results are sent back via the
177    /// supplied channel.
178    CollectReports(ReportsChan),
179    /// Generic message that encapsulates event handling.
180    Task(
181        ScriptThreadEventCategory,
182        Box<dyn TaskBox>,
183        Option<PipelineId>,
184        TaskSourceName,
185    ),
186    /// Report CSP violations in the script
187    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/// A wrapper around various types of `Sender`s that send messages back to the event loop
203/// of a script context event loop. This will either target the main `ScriptThread` event
204/// loop or that of a worker.
205#[derive(Clone, JSTraceable, MallocSizeOf)]
206pub(crate) enum ScriptEventLoopSender {
207    /// A sender that sends to the main `ScriptThread` event loop.
208    MainThread(Sender<MainThreadScriptMsg>),
209    /// A sender that sends to a `ServiceWorker` event loop.
210    ServiceWorker(Sender<ServiceWorkerScriptMsg>),
211    /// A sender that sends to a dedicated worker (such as a generic Web Worker) event loop.
212    /// Note that this sender keeps the main thread Worker DOM object alive as long as it or
213    /// or any message it sends is not dropped.
214    DedicatedWorker {
215        sender: Sender<DedicatedWorkerScriptMsg>,
216        main_thread_worker: TrustedWorkerAddress,
217    },
218}
219
220impl ScriptEventLoopSender {
221    /// Send a message to the event loop, which might be a main thread event loop or a worker event loop.
222    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
248/// A wrapper around various types of `Receiver`s that receive event loop messages. Used for
249/// synchronous DOM APIs that need to abstract over multiple kinds of event loops (worker/main
250/// thread) with different Receiver interfaces.
251pub(crate) enum ScriptEventLoopReceiver {
252    /// A receiver that receives messages to the main `ScriptThread` event loop.
253    MainThread(Receiver<MainThreadScriptMsg>),
254    /// A receiver that receives messages to dedicated workers (such as a generic Web Worker) event loop.
255    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    /// A channel to hand out to script thread-based entities that need to be able to enqueue
354    /// events in the event queue.
355    pub(crate) self_sender: Sender<MainThreadScriptMsg>,
356
357    /// A handle to the bluetooth thread.
358    #[no_trace]
359    #[cfg(feature = "bluetooth")]
360    pub(crate) bluetooth_sender: GenericSender<BluetoothRequest>,
361
362    /// A [`Sender`] that sends messages to the `ScriptThread`.
363    #[no_trace]
364    pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
365
366    /// A [`Sender`] that sends messages to the `Constellation` associated with
367    /// particular pipelines.
368    #[no_trace]
369    pub(crate) pipeline_to_constellation_sender:
370        GenericSender<(WebViewId, PipelineId, ScriptToConstellationMessage)>,
371
372    /// A channel to send messages to the Embedder.
373    #[no_trace]
374    pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
375
376    /// The shared [`Sender`] which is sent to the `ImageCache` when requesting an image.
377    /// Messages on this channel are sent to [`ScriptThreadReceivers::image_cache_receiver`].
378    #[no_trace]
379    pub(crate) image_cache_sender: Sender<ImageCacheResponseMessage>,
380
381    /// For providing contact with the time profiler.
382    #[no_trace]
383    pub(crate) time_profiler_sender: profile_time::ProfilerChan,
384
385    /// For providing contact with the memory profiler.
386    #[no_trace]
387    pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
388
389    /// For providing instructions to an optional devtools server.
390    #[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    /// A [`Receiver`] that receives messages from the constellation.
400    #[no_trace]
401    pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
402
403    /// The [`Receiver`] which receives incoming messages from the `ImageCache`.
404    #[no_trace]
405    pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
406
407    /// For receiving commands from an optional devtools server. Will be ignored if no such server
408    /// exists. When devtools are not active this will be [`crossbeam_channel::never()`].
409    #[no_trace]
410    pub(crate) devtools_server_receiver: RoutedReceiver<DevtoolScriptControlMsg>,
411
412    /// Receiver to receive commands from optional WebGPU server. When there is no active
413    /// WebGPU context, this will be [`crossbeam_channel::never()`].
414    #[no_trace]
415    #[cfg(feature = "webgpu")]
416    pub(crate) webgpu_receiver: RefCell<RoutedReceiver<WebGPUMsg>>,
417}
418
419impl ScriptThreadReceivers {
420    /// Block until a message is received by any of the receivers of this [`ScriptThreadReceivers`]
421    /// or the given [`TaskQueue`] or [`TimerScheduler`]. Return the first message received.
422    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    /// Try to receive a from any of the receivers of this [`ScriptThreadReceivers`] or the given
463    /// [`TaskQueue`]. Return `None` if no messages are ready to be received.
464    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}