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::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/// Messages used to control the script event loop.
142#[derive(Debug)]
143pub(crate) enum MainThreadScriptMsg {
144    /// Common variants associated with the script messages
145    Common(CommonScriptMsg),
146    /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
147    /// reflowed.
148    WorkletLoaded(PipelineId),
149    NavigationResponse {
150        pipeline_id: PipelineId,
151        message: Box<FetchResponseMsg>,
152    },
153    /// Notifies the script thread that a new paint worklet has been registered.
154    RegisterPaintWorklet {
155        pipeline_id: PipelineId,
156        name: Atom,
157        properties: Vec<Atom>,
158        painter: Box<dyn Painter>,
159    },
160    /// A task related to a not fully-active document has been throttled.
161    Inactive,
162    /// Wake-up call from the task queue.
163    WakeUp,
164    /// The `FileManagerThread` has finished selecting files is forwarding the response to
165    /// the main thread of this `ScriptThread`.
166    ForwardEmbedderControlResponseFromFileManager(EmbedderControlId, EmbedderControlResponse),
167}
168
169/// Common messages used to control the event loops in both the script and the worker
170pub(crate) enum CommonScriptMsg {
171    /// Requests that the script thread measure its memory usage. The results are sent back via the
172    /// supplied channel.
173    CollectReports(ReportsChan),
174    /// Generic message that encapsulates event handling.
175    Task(
176        ScriptThreadEventCategory,
177        Box<dyn TaskBox>,
178        Option<PipelineId>,
179        TaskSourceName,
180    ),
181    /// Report CSP violations in the script
182    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/// A wrapper around various types of `Sender`s that send messages back to the event loop
198/// of a script context event loop. This will either target the main `ScriptThread` event
199/// loop or that of a worker.
200#[derive(Clone, JSTraceable, MallocSizeOf)]
201pub(crate) enum ScriptEventLoopSender {
202    /// A sender that sends to the main `ScriptThread` event loop.
203    MainThread(Sender<MainThreadScriptMsg>),
204    /// A sender that sends to a `ServiceWorker` event loop.
205    ServiceWorker(Sender<ServiceWorkerScriptMsg>),
206    /// A sender that sends to a dedicated worker (such as a generic Web Worker) event loop.
207    /// Note that this sender keeps the main thread Worker DOM object alive as long as it or
208    /// or any message it sends is not dropped.
209    DedicatedWorker {
210        sender: Sender<DedicatedWorkerScriptMsg>,
211        main_thread_worker: TrustedWorkerAddress,
212    },
213}
214
215impl ScriptEventLoopSender {
216    /// Send a message to the event loop, which might be a main thread event loop or a worker event loop.
217    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
243/// A wrapper around various types of `Receiver`s that receive event loop messages. Used for
244/// synchronous DOM APIs that need to abstract over multiple kinds of event loops (worker/main
245/// thread) with different Receiver interfaces.
246pub(crate) enum ScriptEventLoopReceiver {
247    /// A receiver that receives messages to the main `ScriptThread` event loop.
248    MainThread(Receiver<MainThreadScriptMsg>),
249    /// A receiver that receives messages to dedicated workers (such as a generic Web Worker) event loop.
250    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    /// A channel to hand out to script thread-based entities that need to be able to enqueue
339    /// events in the event queue.
340    pub(crate) self_sender: Sender<MainThreadScriptMsg>,
341
342    /// A handle to the bluetooth thread.
343    #[no_trace]
344    #[cfg(feature = "bluetooth")]
345    pub(crate) bluetooth_sender: IpcSender<BluetoothRequest>,
346
347    /// A [`Sender`] that sends messages to the `Constellation`.
348    #[no_trace]
349    pub(crate) constellation_sender: GenericSender<ScriptThreadMessage>,
350
351    /// A [`Sender`] that sends messages to the `Constellation` associated with
352    /// particular pipelines.
353    #[no_trace]
354    pub(crate) pipeline_to_constellation_sender:
355        GenericSender<(WebViewId, PipelineId, ScriptToConstellationMessage)>,
356
357    /// A channel to send messages to the Embedder.
358    #[no_trace]
359    pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
360
361    /// The shared [`Sender`] which is sent to the `ImageCache` when requesting an image.
362    /// Messages on this channel are sent to [`ScriptThreadReceivers::image_cache_receiver`].
363    #[no_trace]
364    pub(crate) image_cache_sender: Sender<ImageCacheResponseMessage>,
365
366    /// For providing contact with the time profiler.
367    #[no_trace]
368    pub(crate) time_profiler_sender: profile_time::ProfilerChan,
369
370    /// For providing contact with the memory profiler.
371    #[no_trace]
372    pub(crate) memory_profiler_sender: profile_mem::ProfilerChan,
373
374    /// For providing instructions to an optional devtools server.
375    #[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    /// A [`Receiver`] that receives messages from the constellation.
385    #[no_trace]
386    pub(crate) constellation_receiver: RoutedReceiver<ScriptThreadMessage>,
387
388    /// The [`Receiver`] which receives incoming messages from the `ImageCache`.
389    #[no_trace]
390    pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
391
392    /// For receiving commands from an optional devtools server. Will be ignored if no such server
393    /// exists. When devtools are not active this will be [`crossbeam_channel::never()`].
394    #[no_trace]
395    pub(crate) devtools_server_receiver: Receiver<DevtoolScriptControlMsg>,
396
397    /// Receiver to receive commands from optional WebGPU server. When there is no active
398    /// WebGPU context, this will be [`crossbeam_channel::never()`].
399    #[no_trace]
400    #[cfg(feature = "webgpu")]
401    pub(crate) webgpu_receiver: RefCell<Receiver<WebGPUMsg>>,
402}
403
404impl ScriptThreadReceivers {
405    /// Block until a message is received by any of the receivers of this [`ScriptThreadReceivers`]
406    /// or the given [`TaskQueue`] or [`TimerScheduler`]. Return the first message received.
407    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    /// Try to receive a from any of the receivers of this [`ScriptThreadReceivers`] or the given
448    /// [`TaskQueue`]. Return `None` if no messages are ready to be received.
449    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}