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