script_traits/
lib.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 traits in script used generically in the rest of Servo.
6//! The traits are here instead of in script so that these modules won't have
7//! to depend on script.
8
9#![deny(missing_docs)]
10#![deny(unsafe_code)]
11
12use std::fmt;
13
14use base::cross_process_instant::CrossProcessInstant;
15use base::generic_channel::{GenericCallback, GenericReceiver, GenericSender};
16use base::id::{
17    BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespaceId, PipelineNamespaceRequest,
18    ScriptEventLoopId, WebViewId,
19};
20#[cfg(feature = "bluetooth")]
21use bluetooth_traits::BluetoothRequest;
22use canvas_traits::webgl::WebGLPipeline;
23use constellation_traits::{
24    KeyboardScroll, LoadData, NavigationHistoryBehavior, ScriptToConstellationSender,
25    ScrollStateUpdate, StructuredSerializedData, WindowSizeType,
26};
27use crossbeam_channel::RecvTimeoutError;
28use devtools_traits::ScriptToDevtoolsControlMsg;
29use embedder_traits::user_contents::{UserContentManagerId, UserContents};
30use embedder_traits::{
31    EmbedderControlId, EmbedderControlResponse, FocusSequenceNumber, InputEventAndId,
32    JavaScriptEvaluationId, MediaSessionActionType, PaintHitTestResult, ScriptToEmbedderChan,
33    Theme, ViewportDetails, WebDriverScriptCommand,
34};
35use euclid::{Scale, Size2D};
36use fonts_traits::SystemFontServiceProxySender;
37use keyboard_types::Modifiers;
38use malloc_size_of_derive::MallocSizeOf;
39use media::WindowGLContext;
40use net_traits::ResourceThreads;
41use paint_api::{CrossProcessPaintApi, PinchZoomInfos};
42use pixels::PixelFormat;
43use profile_traits::mem;
44use rustc_hash::FxHashMap;
45use serde::{Deserialize, Serialize};
46use servo_config::prefs::PrefValue;
47use servo_url::{ImmutableOrigin, ServoUrl};
48use storage_traits::StorageThreads;
49use storage_traits::webstorage_thread::WebStorageType;
50use strum::IntoStaticStr;
51use style_traits::{CSSPixel, SpeculativePainter};
52use stylo_atoms::Atom;
53#[cfg(feature = "webgpu")]
54use webgpu_traits::WebGPUMsg;
55use webrender_api::ImageKey;
56use webrender_api::units::DevicePixel;
57
58/// The initial data required to create a new `Pipeline` attached to an existing `ScriptThread`.
59#[derive(Clone, Debug, Deserialize, Serialize)]
60pub struct NewPipelineInfo {
61    /// The ID of the parent pipeline and frame type, if any.
62    /// If `None`, this is a root pipeline.
63    pub parent_info: Option<PipelineId>,
64    /// Id of the newly-created pipeline.
65    pub new_pipeline_id: PipelineId,
66    /// Id of the browsing context associated with this pipeline.
67    pub browsing_context_id: BrowsingContextId,
68    /// Id of the top-level browsing context associated with this pipeline.
69    pub webview_id: WebViewId,
70    /// Id of the opener, if any
71    pub opener: Option<BrowsingContextId>,
72    /// Network request data which will be initiated by the script thread.
73    pub load_data: LoadData,
74    /// Initial [`ViewportDetails`] for this layout.
75    pub viewport_details: ViewportDetails,
76    /// The ID of the `UserContentManager` associated with this new pipeline's `WebView`.
77    pub user_content_manager_id: Option<UserContentManagerId>,
78    /// The [`Theme`] of the new layout.
79    pub theme: Theme,
80}
81
82/// When a pipeline is closed, should its browsing context be discarded too?
83#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
84pub enum DiscardBrowsingContext {
85    /// Discard the browsing context
86    Yes,
87    /// Don't discard the browsing context
88    No,
89}
90
91/// Is a document fully active, active or inactive?
92/// A document is active if it is the current active document in its session history,
93/// it is fuly active if it is active and all of its ancestors are active,
94/// and it is inactive otherwise.
95///
96/// * <https://html.spec.whatwg.org/multipage/#active-document>
97/// * <https://html.spec.whatwg.org/multipage/#fully-active>
98#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
99pub enum DocumentActivity {
100    /// An inactive document
101    Inactive,
102    /// An active but not fully active document
103    Active,
104    /// A fully active document
105    FullyActive,
106}
107
108/// Type of recorded progressive web metric
109#[derive(Clone, Debug, Deserialize, Serialize)]
110pub enum ProgressiveWebMetricType {
111    /// Time to first Paint
112    FirstPaint,
113    /// Time to first contentful paint
114    FirstContentfulPaint,
115    /// Time for the largest contentful paint
116    LargestContentfulPaint {
117        /// The pixel area of the largest contentful element.
118        area: usize,
119        /// The URL of the largest contentful element, if any.
120        url: Option<ServoUrl>,
121    },
122    /// Time to interactive
123    TimeToInteractive,
124}
125
126impl ProgressiveWebMetricType {
127    /// Returns the area if the metric type is LargestContentfulPaint
128    pub fn area(&self) -> usize {
129        match self {
130            ProgressiveWebMetricType::LargestContentfulPaint { area, .. } => *area,
131            _ => 0,
132        }
133    }
134}
135
136/// The reason why the pipeline id of an iframe is being updated.
137#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
138pub enum UpdatePipelineIdReason {
139    /// The pipeline id is being updated due to a navigation.
140    Navigation,
141    /// The pipeline id is being updated due to a history traversal.
142    Traversal,
143}
144
145/// Messages sent to the `ScriptThread` event loop from the `Constellation`, `Paint`, and (for
146/// now) `Layout`.
147#[derive(Deserialize, IntoStaticStr, Serialize)]
148pub enum ScriptThreadMessage {
149    /// Span a new `Pipeline` in this `ScriptThread` and start fetching the contents
150    /// according to the provided `LoadData`. This will ultimately create a `Window`
151    /// and all associated data structures such as `Layout` in the `ScriptThread`.
152    SpawnPipeline(NewPipelineInfo),
153    /// Takes the associated window proxy out of "delaying-load-events-mode",
154    /// used if a scheduled navigated was refused by the embedder.
155    /// <https://html.spec.whatwg.org/multipage/#delaying-load-events-mode>
156    StopDelayingLoadEventsMode(PipelineId),
157    /// Window resized.  Sends a DOM event eventually, but first we combine events.
158    Resize(PipelineId, ViewportDetails, WindowSizeType),
159    /// Theme changed.
160    ThemeChange(PipelineId, Theme),
161    /// Notifies script that window has been resized but to not take immediate action.
162    ResizeInactive(PipelineId, ViewportDetails),
163    /// Window switched from fullscreen mode.
164    ExitFullScreen(PipelineId),
165    /// Notifies the script that the document associated with this pipeline should 'unload'.
166    UnloadDocument(PipelineId),
167    /// Notifies the script that a pipeline should be closed.
168    ExitPipeline(WebViewId, PipelineId, DiscardBrowsingContext),
169    /// Notifies the script that the whole thread should be closed.
170    ExitScriptThread,
171    /// Sends a DOM event.
172    SendInputEvent(WebViewId, PipelineId, ConstellationInputEvent),
173    /// Request that the given pipeline refresh the cursor by doing a hit test at the most
174    /// recently hovered cursor position and resetting the cursor. This happens after a
175    /// display list update is rendered.
176    RefreshCursor(PipelineId),
177    /// Requests that the script thread immediately send the constellation the title of a pipeline.
178    GetTitle(PipelineId),
179    /// Notifies script thread of a change to one of its document's activity
180    SetDocumentActivity(PipelineId, DocumentActivity),
181    /// Set whether to use less resources by running timers at a heavily limited rate.
182    SetThrottled(WebViewId, PipelineId, bool),
183    /// Notify the containing iframe (in PipelineId) that the nested browsing context (BrowsingContextId) is throttled.
184    SetThrottledInContainingIframe(WebViewId, PipelineId, BrowsingContextId, bool),
185    /// Notifies script thread that a url should be loaded in this iframe.
186    /// PipelineId is for the parent, BrowsingContextId is for the nested browsing context
187    NavigateIframe(
188        PipelineId,
189        BrowsingContextId,
190        LoadData,
191        NavigationHistoryBehavior,
192    ),
193    /// Post a message to a given window.
194    PostMessage {
195        /// The target of the message.
196        target: PipelineId,
197        /// The webview associated with the source pipeline.
198        source_webview: WebViewId,
199        /// The ancestry of browsing context associated with the source,
200        /// starting with the source itself.
201        source_with_ancestry: Vec<BrowsingContextId>,
202        /// The expected origin of the target.
203        target_origin: Option<ImmutableOrigin>,
204        /// The source origin of the message.
205        /// <https://html.spec.whatwg.org/multipage/#dom-messageevent-origin>
206        source_origin: ImmutableOrigin,
207        /// The data to be posted.
208        data: Box<StructuredSerializedData>,
209    },
210    /// Updates the current pipeline ID of a given iframe.
211    /// First PipelineId is for the parent, second is the new PipelineId for the frame.
212    UpdatePipelineId(
213        PipelineId,
214        BrowsingContextId,
215        WebViewId,
216        PipelineId,
217        UpdatePipelineIdReason,
218    ),
219    /// Updates the history state and url of a given pipeline.
220    UpdateHistoryState(PipelineId, Option<HistoryStateId>, ServoUrl),
221    /// Removes inaccesible history states.
222    RemoveHistoryStates(PipelineId, Vec<HistoryStateId>),
223    /// Set an iframe to be focused. Used when an element in an iframe gains focus.
224    /// PipelineId is for the parent, BrowsingContextId is for the nested browsing context
225    FocusIFrame(PipelineId, BrowsingContextId, FocusSequenceNumber),
226    /// Focus the document. Used when the container gains focus.
227    FocusDocument(PipelineId, FocusSequenceNumber),
228    /// Notifies that the document's container (e.g., an iframe) is not included
229    /// in the top-level browsing context's focus chain (not considering system
230    /// focus) anymore.
231    ///
232    /// Obviously, this message is invalid for a top-level document.
233    Unfocus(PipelineId, FocusSequenceNumber),
234    /// Passes a webdriver command to the script thread for execution
235    WebDriverScriptCommand(PipelineId, WebDriverScriptCommand),
236    /// Notifies script thread that all animations are done
237    TickAllAnimations(Vec<WebViewId>),
238    /// Notifies the script thread that a new Web font has been loaded, and thus the page should be
239    /// reflowed.
240    WebFontLoaded(PipelineId, bool /* success */),
241    /// Cause a `load` event to be dispatched at the appropriate iframe element.
242    DispatchIFrameLoadEvent {
243        /// The frame that has been marked as loaded.
244        target: BrowsingContextId,
245        /// The pipeline that contains a frame loading the target pipeline.
246        parent: PipelineId,
247        /// The pipeline that has completed loading.
248        child: PipelineId,
249    },
250    /// Cause a `storage` event to be dispatched at the appropriate window.
251    /// The strings are key, old value and new value.
252    DispatchStorageEvent(
253        PipelineId,
254        WebStorageType,
255        ServoUrl,
256        Option<String>,
257        Option<String>,
258        Option<String>,
259    ),
260    /// Report an error from a CSS parser for the given pipeline
261    ReportCSSError(PipelineId, String, u32, u32, String),
262    /// Reload the given page.
263    Reload(PipelineId),
264    /// Notifies the script thread about a new recorded paint metric.
265    PaintMetric(
266        PipelineId,
267        ProgressiveWebMetricType,
268        CrossProcessInstant,
269        bool, /* first_reflow */
270    ),
271    /// Notifies the media session about a user requested media session action.
272    MediaSessionAction(PipelineId, MediaSessionActionType),
273    /// Notifies script thread that WebGPU server has started
274    #[cfg(feature = "webgpu")]
275    SetWebGPUPort(GenericReceiver<WebGPUMsg>),
276    /// `Paint` scrolled and is updating the scroll states of the nodes in the given
277    /// pipeline via the Constellation.
278    SetScrollStates(PipelineId, ScrollStateUpdate),
279    /// Evaluate the given JavaScript and return a result via a corresponding message
280    /// to the Constellation.
281    EvaluateJavaScript(WebViewId, PipelineId, JavaScriptEvaluationId, String),
282    /// A new batch of keys for the image cache for the specific pipeline.
283    SendImageKeysBatch(PipelineId, Vec<ImageKey>),
284    /// Preferences were updated in the parent process.
285    PreferencesUpdated(Vec<(String, PrefValue)>),
286    /// Notify the `ScriptThread` that the Servo renderer is no longer waiting on
287    /// asynchronous image uploads for the given `Pipeline`. These are mainly used
288    /// by canvas to perform uploads while the display list is being built.
289    NoLongerWaitingOnAsychronousImageUpdates(PipelineId),
290    /// Forward a keyboard scroll operation from an `<iframe>` to a parent pipeline.
291    ForwardKeyboardScroll(PipelineId, KeyboardScroll),
292    /// Request readiness for a screenshot from the given pipeline. The pipeline will
293    /// respond when it is ready to take the screenshot or will not be able to take it
294    /// in the future.
295    RequestScreenshotReadiness(WebViewId, PipelineId),
296    /// A response to a request to show an embedder user interface control.
297    EmbedderControlResponse(EmbedderControlId, EmbedderControlResponse),
298    /// Set the `UserContents` for the given `UserContentManagerId`. A `ScriptThread` can host many
299    /// `WebView`s which share the same `UserContentManager`. Only documents loaded after
300    /// the processing of this message will observe the new `UserContents` of the specified
301    /// `UserContentManagerId`.
302    SetUserContents(UserContentManagerId, UserContents),
303    /// Release all data for the given `UserContentManagerId` from the `ScriptThread`'s
304    /// `user_contents_for_manager_id` map.
305    DestroyUserContentManager(UserContentManagerId),
306    /// Send the embedder an accessibility tree update.
307    AccessibilityTreeUpdate(WebViewId, accesskit::TreeUpdate),
308    /// Update the pinch zoom details of a pipeline. Each `Window` stores a `VisualViewport` DOM
309    /// instance that gets updated according to the changes from the `Compositor``.
310    UpdatePinchZoomInfos(PipelineId, PinchZoomInfos),
311    /// Activate or deactivate accessibility features.
312    SetAccessibilityActive(bool),
313    /// Force a garbage collection in this script thread.
314    TriggerGarbageCollection,
315}
316
317impl fmt::Debug for ScriptThreadMessage {
318    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
319        let variant_string: &'static str = self.into();
320        write!(formatter, "ConstellationControlMsg::{variant_string}")
321    }
322}
323
324/// Used to determine if a script has any pending asynchronous activity.
325#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
326pub enum DocumentState {
327    /// The document has been loaded and is idle.
328    Idle,
329    /// The document is either loading or waiting on an event.
330    Pending,
331}
332
333/// Input events from the embedder that are sent via the `Constellation`` to the `ScriptThread`.
334#[derive(Clone, Debug, Deserialize, Serialize)]
335pub struct ConstellationInputEvent {
336    /// The hit test result of this input event, if any.
337    pub hit_test_result: Option<PaintHitTestResult>,
338    /// The pressed mouse button state of the constellation when this input
339    /// event was triggered.
340    pub pressed_mouse_buttons: u16,
341    /// The currently active keyboard modifiers.
342    pub active_keyboard_modifiers: Modifiers,
343    /// The [`InputEventAndId`] itself.
344    pub event: InputEventAndId,
345}
346
347/// All of the information necessary to create a new [`ScriptThread`] for a new [`EventLoop`].
348///
349/// NB: *DO NOT* add any Senders or Receivers here! pcwalton will have to rewrite your code if you
350/// do! Use IPC senders and receivers instead.
351#[derive(Deserialize, Serialize)]
352pub struct InitialScriptState {
353    /// The id of the script event loop that this state will start. This is used to uniquely
354    /// identify an event loop.
355    pub id: ScriptEventLoopId,
356    /// The sender to use to install the `Pipeline` namespace into this process (if necessary).
357    pub namespace_request_sender: GenericSender<PipelineNamespaceRequest>,
358    /// A channel with which messages can be sent to us (the script thread).
359    pub constellation_to_script_sender: GenericSender<ScriptThreadMessage>,
360    /// A port on which messages sent by the constellation to script can be received.
361    pub constellation_to_script_receiver: GenericReceiver<ScriptThreadMessage>,
362    /// A channel on which messages can be sent to the constellation from script.
363    pub script_to_constellation_sender: ScriptToConstellationSender,
364    /// A channel which allows script to send messages directly to the Embedder
365    /// This will pump the embedder event loop.
366    pub script_to_embedder_sender: ScriptToEmbedderChan,
367    /// An IpcSender to the `SystemFontService` used to create a `SystemFontServiceProxy`.
368    pub system_font_service: SystemFontServiceProxySender,
369    /// A channel to the resource manager thread.
370    pub resource_threads: ResourceThreads,
371    /// A channel to the storage manager thread.
372    pub storage_threads: StorageThreads,
373    /// A channel to the bluetooth thread.
374    #[cfg(feature = "bluetooth")]
375    pub bluetooth_sender: GenericSender<BluetoothRequest>,
376    /// A channel to the time profiler thread.
377    pub time_profiler_sender: profile_traits::time::ProfilerChan,
378    /// A channel to the memory profiler thread.
379    pub memory_profiler_sender: mem::ProfilerChan,
380    /// A channel to the developer tools, if applicable.
381    pub devtools_server_sender: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
382    /// The ID of the pipeline namespace for this script thread.
383    pub pipeline_namespace_id: PipelineNamespaceId,
384    /// A channel to the WebGL thread used in this pipeline.
385    pub webgl_chan: Option<WebGLPipeline>,
386    /// The XR device registry
387    pub webxr_registry: Option<webxr_api::Registry>,
388    /// Access to `Paint` across a process boundary.
389    pub cross_process_paint_api: CrossProcessPaintApi,
390    /// Application window's GL Context for Media player
391    pub player_context: WindowGLContext,
392    /// A list of URLs that can access privileged internal APIs.
393    pub privileged_urls: Vec<ServoUrl>,
394    /// A copy of constellation's `UserContentManagerId` to `UserContents` map.
395    pub user_contents_for_manager_id: FxHashMap<UserContentManagerId, UserContents>,
396    /// Whether this script should be initialized with accessibility already active.
397    pub accessibility_active: bool,
398}
399
400/// Errors from executing a paint worklet
401#[derive(Clone, Debug, Deserialize, Serialize)]
402pub enum PaintWorkletError {
403    /// Execution timed out.
404    Timeout,
405    /// No such worklet.
406    WorkletNotFound,
407}
408
409impl From<RecvTimeoutError> for PaintWorkletError {
410    fn from(_: RecvTimeoutError) -> PaintWorkletError {
411        PaintWorkletError::Timeout
412    }
413}
414
415/// Execute paint code in the worklet thread pool.
416pub trait Painter: SpeculativePainter {
417    /// <https://drafts.css-houdini.org/css-paint-api/#draw-a-paint-image>
418    fn draw_a_paint_image(
419        &self,
420        size: Size2D<f32, CSSPixel>,
421        zoom: Scale<f32, CSSPixel, DevicePixel>,
422        properties: Vec<(Atom, String)>,
423        arguments: Vec<String>,
424    ) -> Result<DrawAPaintImageResult, PaintWorkletError>;
425}
426
427impl fmt::Debug for dyn Painter {
428    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
429        fmt.debug_tuple("Painter")
430            .field(&format_args!(".."))
431            .finish()
432    }
433}
434
435/// The result of executing paint code: the image together with any image URLs that need to be loaded.
436///
437/// TODO: this should return a WR display list. <https://github.com/servo/servo/issues/17497>
438#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
439pub struct DrawAPaintImageResult {
440    /// The image height
441    pub width: u32,
442    /// The image width
443    pub height: u32,
444    /// The image format
445    pub format: PixelFormat,
446    /// The image drawn, or None if an invalid paint image was drawn
447    pub image_key: Option<ImageKey>,
448    /// Drawing the image might have requested loading some image URLs.
449    pub missing_image_urls: Vec<ServoUrl>,
450}