Skip to main content

servo_constellation/
event_loop.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
5//! This module contains the `EventLoop` type, which is the constellation's
6//! view of a script thread. When an `EventLoop` is dropped, an `ExitScriptThread`
7//! message is sent to the script thread, asking it to shut down.
8
9use std::hash::Hash;
10use std::marker::PhantomData;
11use std::rc::Rc;
12
13use background_hang_monitor_api::{BackgroundHangMonitorControlMsg, HangMonitorAlert};
14use embedder_traits::ScriptToEmbedderChan;
15use ipc_channel::IpcError;
16use layout_api::ScriptThreadFactory;
17use log::error;
18use media::WindowGLContext;
19use script_traits::{InitialScriptState, ScriptThreadMessage};
20use serde::{Deserialize, Serialize};
21use servo_base::generic_channel::{self, GenericReceiver, GenericSender, SendError};
22use servo_base::id::ScriptEventLoopId;
23use servo_config::opts::{self, Opts};
24use servo_config::prefs::{self, Preferences};
25use servo_constellation_traits::ServiceWorkerManagerFactory;
26
27use crate::sandboxing::spawn_multiprocess;
28use crate::{Constellation, UnprivilegedContent};
29
30/// <https://html.spec.whatwg.org/multipage/#event-loop>
31pub struct EventLoop {
32    script_chan: GenericSender<ScriptThreadMessage>,
33    id: ScriptEventLoopId,
34    /// When running in another process, this is an `IpcSender` to the BackgroundHangMonitor
35    /// on the other side of the process boundary. When running in the same process, the
36    /// BackgroundHangMonitor is shared among all [`EventLoop`]s so this will be `None`.
37    background_hang_monitor_sender: Option<GenericSender<BackgroundHangMonitorControlMsg>>,
38    dont_send_or_sync: PhantomData<Rc<()>>,
39}
40
41impl PartialEq for EventLoop {
42    fn eq(&self, other: &Self) -> bool {
43        self.id == other.id
44    }
45}
46
47impl Eq for EventLoop {}
48
49impl Hash for EventLoop {
50    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
51        self.id.hash(state);
52    }
53}
54
55impl Drop for EventLoop {
56    fn drop(&mut self) {
57        self.send_message_to_background_hang_monitor(&BackgroundHangMonitorControlMsg::Exit);
58
59        if let Err(error) = self.script_chan.send(ScriptThreadMessage::ExitScriptThread) {
60            error!("Did not successfully request EventLoop exit: {error}");
61        }
62    }
63}
64
65impl EventLoop {
66    pub(crate) fn spawn<STF: ScriptThreadFactory, SWF: ServiceWorkerManagerFactory>(
67        constellation: &mut Constellation<STF, SWF>,
68        is_private: bool,
69    ) -> Result<Rc<Self>, IpcError> {
70        let (script_chan, script_port) =
71            servo_base::generic_channel::channel().expect("Pipeline script chan");
72
73        let embedder_chan = constellation.embedder_proxy.sender.clone();
74        let eventloop_waker = constellation.embedder_proxy.event_loop_waker.clone();
75        let script_to_embedder_sender = ScriptToEmbedderChan::new(embedder_chan, eventloop_waker);
76
77        let resource_threads = if is_private {
78            constellation.private_resource_threads.clone()
79        } else {
80            constellation.public_resource_threads.clone()
81        };
82        let storage_threads = if is_private {
83            constellation.private_storage_threads.clone()
84        } else {
85            constellation.public_storage_threads.clone()
86        };
87
88        let event_loop_id = ScriptEventLoopId::new();
89        let initial_script_state = InitialScriptState {
90            id: event_loop_id,
91            script_to_constellation_sender: constellation.script_sender.clone(),
92            script_to_embedder_sender,
93            namespace_request_sender: constellation.namespace_ipc_sender.clone(),
94            devtools_server_sender: constellation.script_to_devtools_callback(),
95            #[cfg(feature = "bluetooth")]
96            bluetooth_sender: constellation.bluetooth_ipc_sender.clone(),
97            system_font_service: constellation.system_font_service.to_sender(),
98            resource_threads,
99            storage_threads,
100            time_profiler_sender: constellation.time_profiler_chan.clone(),
101            memory_profiler_sender: constellation.mem_profiler_chan.clone(),
102            constellation_to_script_sender: script_chan,
103            constellation_to_script_receiver: script_port,
104            pipeline_namespace_id: constellation.next_pipeline_namespace_id(),
105            cross_process_paint_api: constellation.paint_proxy.cross_process_paint_api.clone(),
106            webgl_chan: constellation
107                .webgl_threads
108                .as_ref()
109                .map(|threads| threads.pipeline()),
110            webxr_registry: constellation.webxr_registry.clone(),
111            player_context: WindowGLContext::get(),
112            privileged_urls: constellation.privileged_urls.clone(),
113            user_contents_for_manager_id: constellation.user_contents_for_manager_id.clone(),
114        };
115
116        let event_loop = if opts::get().multiprocess {
117            Self::spawn_in_process(constellation, initial_script_state)?
118        } else {
119            Self::spawn_in_thread(constellation, initial_script_state)
120        };
121
122        let event_loop = Rc::new(event_loop);
123        constellation.add_event_loop(&event_loop);
124        Ok(event_loop)
125    }
126
127    fn spawn_in_thread<STF: ScriptThreadFactory, SWF: ServiceWorkerManagerFactory>(
128        constellation: &mut Constellation<STF, SWF>,
129        initial_script_state: InitialScriptState,
130    ) -> Self {
131        let script_chan = initial_script_state.constellation_to_script_sender.clone();
132        let id = initial_script_state.id;
133        let background_hang_monitor_register = constellation
134            .background_monitor_register
135            .clone()
136            .expect("Couldn't start content, no background monitor has been initiated");
137        let join_handle = STF::create(
138            initial_script_state,
139            constellation.layout_factory.clone(),
140            constellation.image_cache_factory.clone(),
141            background_hang_monitor_register,
142        );
143        constellation.add_event_loop_join_handle(join_handle);
144
145        Self {
146            script_chan,
147            id,
148            background_hang_monitor_sender: None,
149            dont_send_or_sync: PhantomData,
150        }
151    }
152
153    fn spawn_in_process<STF: ScriptThreadFactory, SWF: ServiceWorkerManagerFactory>(
154        constellation: &mut Constellation<STF, SWF>,
155        initial_script_state: InitialScriptState,
156    ) -> Result<Self, IpcError> {
157        let script_chan = initial_script_state.constellation_to_script_sender.clone();
158        let id = initial_script_state.id;
159
160        let (background_hand_monitor_sender, backgrond_hand_monitor_receiver) =
161            generic_channel::channel().expect("Sampler chan");
162        let (lifeline_sender, lifeline_receiver) =
163            generic_channel::channel().expect("Failed to create lifeline channel");
164
165        let process = spawn_multiprocess(UnprivilegedContent::ScriptEventLoop(
166            NewScriptEventLoopProcessInfo {
167                initial_script_state,
168                constellation_to_bhm_receiver: backgrond_hand_monitor_receiver,
169                bhm_to_constellation_sender: constellation.background_hang_monitor_sender.clone(),
170                lifeline_sender,
171                opts: (*opts::get()).clone(),
172                prefs: Box::new(prefs::get().clone()),
173                broken_image_icon_data: constellation.broken_image_icon_data.clone(),
174            },
175        ))?;
176
177        let crossbeam_receiver = lifeline_receiver.route_preserving_errors();
178        constellation
179            .process_manager
180            .add(crossbeam_receiver, process);
181
182        Ok(Self {
183            script_chan,
184            id,
185            background_hang_monitor_sender: Some(background_hand_monitor_sender),
186            dont_send_or_sync: PhantomData,
187        })
188    }
189
190    pub(crate) fn id(&self) -> ScriptEventLoopId {
191        self.id
192    }
193
194    /// Send a message to the event loop.
195    pub fn send(&self, msg: ScriptThreadMessage) -> Result<(), SendError> {
196        self.script_chan.send(msg)
197    }
198
199    /// If this is [`EventLoop`] is in another process, send a message to its `BackgroundHangMonitor`,
200    /// otherwise do nothing.
201    pub(crate) fn send_message_to_background_hang_monitor(
202        &self,
203        message: &BackgroundHangMonitorControlMsg,
204    ) {
205        if let Some(background_hang_monitor_sender) = &self.background_hang_monitor_sender &&
206            let Err(error) = background_hang_monitor_sender.send(message.clone())
207        {
208            error!("Could not send message ({message:?}) to BHM: {error}");
209        }
210    }
211}
212
213/// All of the information necessary to create a new script [`EventLoop`] in a new process.
214#[derive(Deserialize, Serialize)]
215pub struct NewScriptEventLoopProcessInfo {
216    pub initial_script_state: InitialScriptState,
217    pub constellation_to_bhm_receiver: GenericReceiver<BackgroundHangMonitorControlMsg>,
218    pub bhm_to_constellation_sender: GenericSender<HangMonitorAlert>,
219    pub lifeline_sender: GenericSender<()>,
220    pub opts: Opts,
221    pub prefs: Box<Preferences>,
222    /// The broken image icon data that is used to create an image to show in place of broken images.
223    pub broken_image_icon_data: Vec<u8>,
224}