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 crossbeam_channel::{Receiver, SendError, Sender, select};
12use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
13use embedder_traits::{EmbedderControlId, EmbedderControlResponse, ScriptToEmbedderChan};
14use net_traits::FetchResponseMsg;
15use net_traits::image_cache::ImageCacheResponseMessage;
16use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
17use profile_traits::time::{self as profile_time};
18use rustc_hash::FxHashSet;
19use script_traits::{Painter, ScriptThreadMessage};
20use servo_base::generic_channel::{GenericCallback, GenericSender, RoutedReceiver};
21use servo_base::id::{PipelineId, WebViewId};
22#[cfg(feature = "bluetooth")]
23use servo_bluetooth_traits::BluetoothRequest;
24use servo_constellation_traits::ScriptToConstellationMessage;
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::sharedworkerglobalscope::SharedWorkerScriptMsg;
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#[expect(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::GetDocumentOrigin(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
81                ScriptThreadMessage::FocusDocumentAsPartOfFocusingSteps(id, ..) => Some(*id),
82                ScriptThreadMessage::UnfocusDocumentAsPartOfFocusingSteps(id, ..) => Some(*id),
83                ScriptThreadMessage::FocusDocument(id, ..) => Some(*id),
84                ScriptThreadMessage::WebDriverScriptCommand(id, ..) => Some(*id),
85                ScriptThreadMessage::TickAllAnimations(..) => None,
86                ScriptThreadMessage::WebFontLoaded(id) => Some(*id),
87                ScriptThreadMessage::DispatchIFrameLoadEvent {
88                    target: _,
89                    parent: id,
90                    child: _,
91                } => Some(*id),
92                ScriptThreadMessage::DispatchStorageEvent(id, ..) => Some(*id),
93                ScriptThreadMessage::ReportCSSError(id, ..) => Some(*id),
94                ScriptThreadMessage::Reload(id, ..) => Some(*id),
95                ScriptThreadMessage::PaintMetric(id, ..) => Some(*id),
96                ScriptThreadMessage::ExitFullScreen(id, ..) => Some(*id),
97                ScriptThreadMessage::MediaSessionAction(..) => None,
98                #[cfg(feature = "webgpu")]
99                ScriptThreadMessage::SetWebGPUPort(..) => None,
100                ScriptThreadMessage::SetScrollStates(id, ..) => Some(*id),
101                ScriptThreadMessage::EvaluateJavaScript(_, id, _, _) => Some(*id),
102                ScriptThreadMessage::SendImageKeysBatch(..) => None,
103                ScriptThreadMessage::PreferencesUpdated(..) => None,
104                ScriptThreadMessage::NoLongerWaitingOnAsychronousImageUpdates(_) => None,
105                ScriptThreadMessage::ForwardKeyboardScroll(id, _) => Some(*id),
106                ScriptThreadMessage::RequestScreenshotReadiness(_, id) => Some(*id),
107                ScriptThreadMessage::EmbedderControlResponse(id, _) => Some(id.pipeline_id),
108                ScriptThreadMessage::SetUserContents(..) => None,
109                ScriptThreadMessage::DestroyUserContentManager(..) => None,
110                ScriptThreadMessage::UpdatePinchZoomInfos(id, _) => Some(*id),
111                ScriptThreadMessage::SetAccessibilityActive(..) => None,
112                ScriptThreadMessage::TriggerGarbageCollection => None,
113            },
114            MixedMessage::FromScript(inner_msg) => match inner_msg {
115                MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
116                    *pipeline_id
117                },
118                MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
119                MainThreadScriptMsg::Common(CommonScriptMsg::ReportCspViolations(
120                    pipeline_id,
121                    _,
122                )) => Some(*pipeline_id),
123                MainThreadScriptMsg::NavigationResponse { pipeline_id, .. } => Some(*pipeline_id),
124                MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(*pipeline_id),
125                MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(*pipeline_id),
126                MainThreadScriptMsg::Inactive => None,
127                MainThreadScriptMsg::WakeUp => None,
128                MainThreadScriptMsg::ForwardEmbedderControlResponseFromFileManager(
129                    control_id,
130                    ..,
131                ) => Some(control_id.pipeline_id),
132            },
133            MixedMessage::FromImageCache(response) => match response {
134                ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
135                    Some(response.pipeline_id)
136                },
137                ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
138                    Some(response.pipeline_id)
139                },
140            },
141            MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
142            #[cfg(feature = "webgpu")]
143            MixedMessage::FromWebGPUServer(..) => None,
144        }
145    }
146}
147
148/// Messages used to control the script event loop.
149#[derive(Debug)]
150pub(crate) enum MainThreadScriptMsg {
151    /// Common variants associated with the script messages
152    Common(CommonScriptMsg),
153    /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
154    /// reflowed.
155    WorkletLoaded(PipelineId),
156    NavigationResponse {
157        pipeline_id: PipelineId,
158        message: Box<FetchResponseMsg>,
159    },
160    /// Notifies the script thread that a new paint worklet has been registered.
161    RegisterPaintWorklet {
162        pipeline_id: PipelineId,
163        name: Atom,
164        properties: Vec<Atom>,
165        painter: Box<dyn Painter>,
166    },
167    /// A task related to a not fully-active document has been throttled.
168    Inactive,
169    /// Wake-up call from the task queue.
170    WakeUp,
171    /// The `FileManagerThread` has finished selecting files is forwarding the response to
172    /// the main thread of this `ScriptThread`.
173    ForwardEmbedderControlResponseFromFileManager(EmbedderControlId, EmbedderControlResponse),
174}
175
176/// Common messages used to control the event loops in both the script and the worker
177pub(crate) enum CommonScriptMsg {
178    /// Requests that the script thread measure its memory usage. The results are sent back via the
179    /// supplied channel.
180    CollectReports(ReportsChan),
181    /// Generic message that encapsulates event handling.
182    Task(
183        ScriptThreadEventCategory,
184        Box<dyn TaskBox>,
185        Option<PipelineId>,
186        TaskSourceName,
187    ),
188    /// Report CSP violations in the script
189    ReportCspViolations(PipelineId, Vec<Violation>),
190}
191
192impl fmt::Debug for CommonScriptMsg {
193    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
194        match *self {
195            CommonScriptMsg::CollectReports(_) => write!(f, "CollectReports(...)"),
196            CommonScriptMsg::Task(ref category, ref task, _, _) => {
197                f.debug_tuple("Task").field(category).field(task).finish()
198            },
199            CommonScriptMsg::ReportCspViolations(..) => write!(f, "ReportCspViolations(...)"),
200        }
201    }
202}
203
204/// A wrapper around various types of `Sender`s that send messages back to the event loop
205/// of a script context event loop. This will either target the main `ScriptThread` event
206/// loop or that of a worker.
207#[derive(Clone, JSTraceable, MallocSizeOf)]
208pub(crate) enum ScriptEventLoopSender {
209    /// A sender that sends to the main `ScriptThread` event loop.
210    MainThread(Sender<MainThreadScriptMsg>),
211    /// A sender that sends to a `SharedWorker` event loop.
212    SharedWorker(Sender<SharedWorkerScriptMsg>),
213    /// A sender that sends to a `ServiceWorker` event loop.
214    ServiceWorker(Sender<ServiceWorkerScriptMsg>),
215    /// A sender that sends to a dedicated worker (such as a generic Web Worker) event loop.
216    /// Note that this sender keeps the main thread Worker DOM object alive as long as it or
217    /// or any message it sends is not dropped.
218    DedicatedWorker {
219        sender: Sender<DedicatedWorkerScriptMsg>,
220        main_thread_worker: TrustedWorkerAddress,
221    },
222}
223
224impl ScriptEventLoopSender {
225    /// Send a message to the event loop, which might be a main thread event loop or a worker event loop.
226    pub(crate) fn send(&self, message: CommonScriptMsg) -> Result<(), SendError<()>> {
227        match self {
228            Self::MainThread(sender) => sender
229                .send(MainThreadScriptMsg::Common(message))
230                .map_err(|_| SendError(())),
231            Self::SharedWorker(sender) => sender
232                .send(SharedWorkerScriptMsg::CommonWorker(
233                    WorkerScriptMsg::Common(message),
234                ))
235                .map_err(|_| SendError(())),
236            Self::ServiceWorker(sender) => sender
237                .send(ServiceWorkerScriptMsg::CommonWorker(
238                    WorkerScriptMsg::Common(message),
239                ))
240                .map_err(|_| SendError(())),
241            Self::DedicatedWorker {
242                sender,
243                main_thread_worker,
244            } => {
245                let common_message = WorkerScriptMsg::Common(message);
246                sender
247                    .send(DedicatedWorkerScriptMsg::CommonWorker(
248                        main_thread_worker.clone(),
249                        common_message,
250                    ))
251                    .map_err(|_| SendError(()))
252            },
253        }
254    }
255}
256
257/// A wrapper around various types of `Receiver`s that receive event loop messages. Used for
258/// synchronous DOM APIs that need to abstract over multiple kinds of event loops (worker/main
259/// thread) with different Receiver interfaces.
260pub(crate) enum ScriptEventLoopReceiver {
261    /// A receiver that receives messages to the main `ScriptThread` event loop.
262    MainThread(Receiver<MainThreadScriptMsg>),
263    /// A receiver that receives messages to shared worker event loops.
264    SharedWorker(Receiver<SharedWorkerScriptMsg>),
265    /// A receiver that receives messages to dedicated workers (such as a generic Web Worker) event loop.
266    DedicatedWorker(Receiver<DedicatedWorkerScriptMsg>),
267}
268
269impl ScriptEventLoopReceiver {
270    pub(crate) fn recv(&self) -> Result<CommonScriptMsg, ()> {
271        match self {
272            Self::MainThread(receiver) => match receiver.recv() {
273                Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
274                Ok(_) => panic!("unexpected main thread event message!"),
275                Err(_) => Err(()),
276            },
277            Self::SharedWorker(receiver) => match receiver.recv() {
278                Ok(SharedWorkerScriptMsg::CommonWorker(WorkerScriptMsg::Common(message))) => {
279                    Ok(message)
280                },
281                Ok(_) => panic!("unexpected shared worker event message!"),
282                Err(_) => Err(()),
283            },
284            Self::DedicatedWorker(receiver) => match receiver.recv() {
285                Ok(DedicatedWorkerScriptMsg::CommonWorker(_, WorkerScriptMsg::Common(message))) => {
286                    Ok(message)
287                },
288                Ok(_) => panic!("unexpected worker event message!"),
289                Err(_) => Err(()),
290            },
291        }
292    }
293}
294
295impl QueuedTaskConversion for MainThreadScriptMsg {
296    fn task_source_name(&self) -> Option<&TaskSourceName> {
297        let script_msg = match self {
298            MainThreadScriptMsg::Common(script_msg) => script_msg,
299            _ => return None,
300        };
301        match script_msg {
302            CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
303                Some(task_source)
304            },
305            _ => None,
306        }
307    }
308
309    fn pipeline_id(&self) -> Option<PipelineId> {
310        let script_msg = match self {
311            MainThreadScriptMsg::Common(script_msg) => script_msg,
312            _ => return None,
313        };
314        match script_msg {
315            CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
316            _ => None,
317        }
318    }
319
320    fn into_queued_task(self) -> Option<QueuedTask> {
321        let script_msg = match self {
322            MainThreadScriptMsg::Common(script_msg) => script_msg,
323            _ => return None,
324        };
325        let (event_category, task, pipeline_id, task_source) = match script_msg {
326            CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
327                (category, boxed, pipeline_id, task_source)
328            },
329            _ => return None,
330        };
331        Some(QueuedTask {
332            worker: None,
333            event_category,
334            task,
335            pipeline_id,
336            task_source,
337        })
338    }
339
340    fn from_queued_task(queued_task: QueuedTask) -> Self {
341        let script_msg = CommonScriptMsg::Task(
342            queued_task.event_category,
343            queued_task.task,
344            queued_task.pipeline_id,
345            queued_task.task_source,
346        );
347        MainThreadScriptMsg::Common(script_msg)
348    }
349
350    fn inactive_msg() -> Self {
351        MainThreadScriptMsg::Inactive
352    }
353
354    fn wake_up_msg() -> Self {
355        MainThreadScriptMsg::WakeUp
356    }
357
358    fn is_wake_up(&self) -> bool {
359        matches!(self, MainThreadScriptMsg::WakeUp)
360    }
361}
362
363impl OpaqueSender<CommonScriptMsg> for ScriptEventLoopSender {
364    fn send(&self, message: CommonScriptMsg) {
365        self.send(message).unwrap()
366    }
367}
368
369#[derive(Clone, JSTraceable)]
370pub(crate) struct ScriptThreadSenders {
371    /// A channel to hand out to script thread-based entities that need to be able to enqueue
372    /// events in the event queue.
373    pub(crate) self_sender: Sender<MainThreadScriptMsg>,
374
375    /// A handle to the bluetooth thread.
376    #[no_trace]
377    #[cfg(feature = "bluetooth")]
378    pub(crate) bluetooth_sender: GenericSender<BluetoothRequest>,
379
380    /// A [`Sender`] that sends messages to the `ScriptThread`.
381    #[no_trace]
382    pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
383
384    /// A [`Sender`] that sends messages to the `Constellation` associated with
385    /// particular pipelines.
386    #[no_trace]
387    pub(crate) pipeline_to_constellation_sender:
388        GenericSender<(WebViewId, PipelineId, ScriptToConstellationMessage)>,
389
390    /// A channel to send messages to the Embedder.
391    #[no_trace]
392    pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
393
394    /// The shared [`Sender`] which is sent to the `ImageCache` when requesting an image.
395    /// Messages on this channel are sent to [`ScriptThreadReceivers::image_cache_receiver`].
396    #[no_trace]
397    pub(crate) image_cache_sender: Sender<ImageCacheResponseMessage>,
398
399    /// For providing contact with the time profiler.
400    #[no_trace]
401    pub(crate) time_profiler_sender: profile_time::ProfilerChan,
402
403    /// For providing contact with the memory profiler.
404    #[no_trace]
405    pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
406
407    /// For providing instructions to an optional devtools server.
408    #[no_trace]
409    pub(crate) devtools_server_sender: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
410
411    #[no_trace]
412    pub(crate) devtools_client_to_script_thread_sender: GenericSender<DevtoolScriptControlMsg>,
413}
414
415#[derive(JSTraceable)]
416pub(crate) struct ScriptThreadReceivers {
417    /// A [`Receiver`] that receives messages from the constellation.
418    #[no_trace]
419    pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
420
421    /// The [`Receiver`] which receives incoming messages from the `ImageCache`.
422    #[no_trace]
423    pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
424
425    /// For receiving commands from an optional devtools server. Will be ignored if no such server
426    /// exists. When devtools are not active this will be [`crossbeam_channel::never()`].
427    #[no_trace]
428    pub(crate) devtools_server_receiver: RoutedReceiver<DevtoolScriptControlMsg>,
429
430    /// Receiver to receive commands from optional WebGPU server. When there is no active
431    /// WebGPU context, this will be [`crossbeam_channel::never()`].
432    #[no_trace]
433    #[cfg(feature = "webgpu")]
434    pub(crate) webgpu_receiver: RefCell<RoutedReceiver<WebGPUMsg>>,
435}
436
437impl ScriptThreadReceivers {
438    /// Block until a message is received by any of the receivers of this [`ScriptThreadReceivers`]
439    /// or the given [`TaskQueue`] or [`TimerScheduler`]. Return the first message received.
440    pub(crate) fn recv(
441        &self,
442        task_queue: &TaskQueue<MainThreadScriptMsg>,
443        timer_scheduler: &TimerScheduler,
444        fully_active: &FxHashSet<PipelineId>,
445    ) -> MixedMessage {
446        select! {
447            recv(task_queue.select()) -> msg => {
448                task_queue.take_tasks(msg.unwrap(), fully_active);
449                let event = task_queue
450                    .recv()
451                    .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
452                MixedMessage::FromScript(event)
453            },
454            recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap().unwrap()),
455            recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap().unwrap()),
456            recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
457            recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
458            recv({
459                #[cfg(feature = "webgpu")]
460                {
461                    self.webgpu_receiver.borrow()
462                }
463                #[cfg(not(feature = "webgpu"))]
464                {
465                    crossbeam_channel::never::<()>()
466                }
467            }) -> msg => {
468                #[cfg(feature = "webgpu")]
469                {
470                    MixedMessage::FromWebGPUServer(msg.unwrap().unwrap())
471                }
472                #[cfg(not(feature = "webgpu"))]
473                {
474                    unreachable!("This should never be hit when webgpu is disabled ({msg:?})");
475                }
476            }
477        }
478    }
479
480    /// Try to receive a from any of the receivers of this [`ScriptThreadReceivers`] or the given
481    /// [`TaskQueue`]. Return `None` if no messages are ready to be received.
482    pub(crate) fn try_recv(
483        &self,
484        task_queue: &TaskQueue<MainThreadScriptMsg>,
485        fully_active: &FxHashSet<PipelineId>,
486    ) -> Option<MixedMessage> {
487        if let Ok(message) = self.constellation_receiver.try_recv() {
488            let message = message
489                .inspect_err(|e| {
490                    log::warn!(
491                        "ScriptThreadReceivers IPC error on constellation_receiver: {:?}",
492                        e
493                    );
494                })
495                .ok()?;
496            return MixedMessage::FromConstellation(message).into();
497        }
498        if let Ok(message) = task_queue.take_tasks_and_recv(fully_active) {
499            return MixedMessage::FromScript(message).into();
500        }
501        if let Ok(message) = self.devtools_server_receiver.try_recv() {
502            return MixedMessage::FromDevtools(message.unwrap()).into();
503        }
504        if let Ok(message) = self.image_cache_receiver.try_recv() {
505            return MixedMessage::FromImageCache(message).into();
506        }
507        #[cfg(feature = "webgpu")]
508        if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
509            return MixedMessage::FromWebGPUServer(message.unwrap()).into();
510        }
511        None
512    }
513}