Skip to main content

servo/
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//! Servo, the mighty web browser engine from the future.
6//!
7//! This crate wires all of Servo's components together as type [`Servo`], along with
8//! a Webview implementation, [`WebView`], to create a working web engine.
9//!
10//! To embed Servo in your application, you need to:
11//! 1) Create an instance of a type that implements the [`EventLoopWaker`] trait.
12//!    This trait allows Servo to integrate with your application's event loop. Your
13//!    [`EventLoopWaker::wake`] implementation needs to ensure that
14//!    [`Servo::spin_event_loop`] is eventually called to let Servo process user input,
15//!    network events etc.
16//! 2) Create a [`Servo`] instance using the [`ServoBuilder`] type. You can optionally
17//!    register a [`ServoDelegate`] implementation to subscribe to notifications about
18//!    events and customize certain behaviours.
19//! 3) Create an instance of a type that implements the [`RenderingContext`] trait.
20//!    Note: You can either use one of the existing types in this crate (such as
21//!    [`WindowRenderingContext`], [`OffscreenRenderingContext`] or [`SoftwareRenderingContext`])
22//!    or provide a custom implementation.
23//! 4) For each [`WebView`] in your application, create a [`WebViewBuilder`] by passing
24//!    it the [`RenderingContext`] and [`Servo`] instance, and then configure the
25//!    builder and use it to create a [`WebView`] instance. The builder must be provided
26//!    with a [`WebViewDelegate`] implementation which, at a minimum, should handle
27//!    [`WebViewDelegate::notify_new_frame_ready`] by calling [`WebView::paint`]
28//!    and presenting the rendered page using [`RenderingContext::present`]. Refer to the
29//!    documentation of the [`WebView`] type to learn more about the relation
30//!    between a [`WebView`] and its [`RenderingContext`].
31//! 5) Run the application's event loop. Your application's event handlers need to
32//!    forward input events for a particular [`WebView`] using one of the `notify_*` methods
33//!    on that [`WebView`] instance. For example, to forward a mouse event to a [`WebView`],
34//!    call the [`WebView::notify_input_event`]. You can also invoke methods on a [`WebView`]
35//!    to request certain actions. For instance, the [`WebView::load`] method requests that
36//!    Servo navigate to a new page. In both cases, the calls to the [`WebView`] methods
37//!    must be followed by calls to [`Servo::spin_event_loop`] to allow Servo to process
38//!    those requests.
39//!
40//! For a minimal working example, refer to the [`winit_minimal`] code.
41//!
42//! [`winit_minimal`]: https://github.com/servo/servo/blob/main/components/servo/examples/winit_minimal.rs
43
44mod clipboard_delegate;
45#[cfg(feature = "gamepad")]
46mod gamepad_delegate;
47#[cfg(feature = "media-gstreamer")]
48mod gstreamer_plugins;
49mod javascript_evaluator;
50mod network_manager;
51mod proxies;
52mod responders;
53mod servo;
54mod servo_delegate;
55mod site_data_manager;
56mod user_content_manager;
57mod webview;
58mod webview_delegate;
59
60// These are Servo's public exports. Everything (apart from a couple exceptions below)
61// should be exported at the root. See <https://github.com/servo/servo/issues/18475>.
62pub use accesskit;
63pub use embedder_traits::user_contents::UserScript;
64pub use embedder_traits::{submit_resource_reader, *};
65pub use image::RgbaImage;
66pub use keyboard_types::{
67    Code, CompositionEvent, CompositionState, Key, KeyState, Location, Modifiers, NamedKey,
68};
69pub use media::{
70    GlApi as MediaGlApi, GlContext as MediaGlContext, NativeDisplay as MediaNativeDisplay,
71};
72pub use net::image_cache::should_panic_hook_suppress_termination;
73pub use net_traits::CookieSource;
74// This API should probably not be exposed in this way. Instead there should be a fully
75// fleshed out public domains API if we want to expose it.
76pub use net_traits::pub_domains::is_reg_domain;
77pub use paint::WebRenderDebugOption;
78pub use paint_api::rendering_context::{
79    OffscreenRenderingContext, RenderingContext, SoftwareRenderingContext, WindowRenderingContext,
80};
81// Expose our profile traits for servoshell, so we can instrument code there, but don't
82// add it as an official API.
83#[doc(hidden)]
84pub use profile_traits;
85// This should be replaced with an API on ServoBuilder.
86// See <https://github.com/servo/servo/issues/40950>.
87pub use resources;
88pub use servo_base::generic_channel::GenericSender;
89pub use servo_base::id::WebViewId;
90pub use servo_config::opts::{DiagnosticsLogging, DiagnosticsLoggingOption, Opts, OutputOptions};
91pub use servo_config::prefs::{PrefValue, Preferences, UserAgentPlatform};
92pub use servo_config::{opts, pref, prefs};
93pub use servo_geometry::{
94    DeviceIndependentIntRect, DeviceIndependentPixel, convert_rect_to_css_pixel,
95};
96#[doc(hidden)]
97pub use servo_tracing;
98pub use servo_url::ServoUrl;
99pub use style::Zero;
100pub use style_traits::CSSPixel;
101pub use webrender_api::units::{
102    DeviceIntPoint, DeviceIntRect, DeviceIntSize, DevicePixel, DevicePoint, DeviceVector2D,
103};
104
105pub use crate::clipboard_delegate::{ClipboardDelegate, StringRequest};
106#[cfg(feature = "gamepad")]
107pub use crate::gamepad_delegate::{
108    GamepadDelegate, GamepadHapticEffectRequest, GamepadHapticEffectRequestType,
109};
110pub use crate::network_manager::{CacheEntry, NetworkManager};
111pub use crate::servo::{Servo, ServoBuilder, run_content_process};
112pub use crate::servo_delegate::{ServoDelegate, ServoError};
113pub use crate::site_data_manager::{SiteData, SiteDataManager, StorageType};
114pub use crate::user_content_manager::UserContentManager;
115pub use crate::webview::{WebView, WebViewBuilder};
116pub use crate::webview_delegate::{
117    AlertDialog, AllowOrDenyRequest, AuthenticationRequest, BluetoothDeviceSelectionRequest,
118    ColorPicker, ConfirmDialog, ContextMenu, CreateNewWebViewRequest, EmbedderControl, FilePicker,
119    InputMethodControl, NavigationRequest, PermissionRequest, PromptDialog, SelectElement,
120    SimpleDialog, WebResourceLoad, WebViewDelegate,
121};
122
123#[cfg(feature = "webxr")]
124pub mod webxr {
125    #[cfg(not(any(target_os = "android", target_env = "ohos")))]
126    pub use webxr::glwindow::{GlWindow, GlWindowDiscovery, GlWindowMode, GlWindowRenderTarget};
127    #[cfg(not(any(target_os = "android", target_env = "ohos")))]
128    pub use webxr::headless::HeadlessMockDiscovery;
129    #[cfg(target_os = "windows")]
130    pub use webxr::openxr::{AppInfo as OpenXrAppInfo, OpenXrDiscovery};
131    pub use webxr::{Discovery, MainThreadRegistry, WebXrRegistry};
132}
133
134// TODO: The protocol handler interface needs to be cleaned and simplified.
135pub mod protocol_handler {
136    pub use net::fetch::methods::{DoneChannel, FetchContext};
137    pub use net::filemanager_thread::FILE_CHUNK_SIZE;
138    pub use net::protocols::{ProtocolHandler, ProtocolRegistry};
139    pub use net_traits::filemanager_thread::RelativePos;
140    pub use net_traits::http_status::HttpStatus;
141    pub use net_traits::request::Request;
142    pub use net_traits::response::{Response, ResponseBody};
143    pub use net_traits::{NetworkError, ResourceFetchTiming};
144
145    pub use crate::webview_delegate::ProtocolHandlerRegistration;
146}
147
148// We need to reference this crate, in order for the linker not to remove it.
149#[cfg(all(feature = "baked-in-resources", not(target_env = "ohos")))]
150use servo_default_resources as _;