Skip to main content

servo/
webview.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 std::cell::{Ref, RefCell, RefMut};
6use std::hash::Hash;
7use std::rc::{Rc, Weak};
8use std::time::Duration;
9
10use accesskit::{
11    Node as AccesskitNode, NodeId, Role, Tree, TreeId, TreeUpdate, Uuid as AccesskitUuid,
12};
13use dpi::PhysicalSize;
14use embedder_traits::{
15    ContextMenuAction, ContextMenuItem, Cursor, EmbedderControlId, EmbedderControlRequest, Image,
16    InputEvent, InputEventAndId, InputEventId, JSValue, JavaScriptEvaluationError, LoadStatus,
17    MediaSessionActionType, NewWebViewDetails, ScreenGeometry, ScreenshotCaptureError, Scroll,
18    Theme, TraversalId, UrlRequest, ViewportDetails, WebViewPoint, WebViewRect,
19};
20use euclid::{Scale, Size2D};
21use image::RgbaImage;
22use log::debug;
23use paint_api::WebViewTrait;
24use paint_api::rendering_context::RenderingContext;
25use servo_base::Epoch;
26use servo_base::generic_channel::GenericSender;
27use servo_base::id::WebViewId;
28use servo_config::pref;
29use servo_constellation_traits::{EmbedderToConstellationMessage, TraversalDirection};
30use servo_geometry::DeviceIndependentPixel;
31use servo_url::ServoUrl;
32use style_traits::CSSPixel;
33use url::Url;
34use webrender_api::units::{DeviceIntRect, DevicePixel, DevicePoint, DeviceSize};
35
36use crate::clipboard_delegate::{ClipboardDelegate, DefaultClipboardDelegate};
37#[cfg(feature = "gamepad")]
38use crate::gamepad_delegate::{DefaultGamepadDelegate, GamepadDelegate};
39use crate::responders::IpcResponder;
40use crate::servo::PendingHandledInputEvent;
41use crate::webview_delegate::{CreateNewWebViewRequest, DefaultWebViewDelegate, WebViewDelegate};
42use crate::{
43    ColorPicker, ContextMenu, EmbedderControl, InputMethodControl, SelectElement, Servo,
44    UserContentManager, WebRenderDebugOption,
45};
46
47pub(crate) const MINIMUM_WEBVIEW_SIZE: Size2D<i32, DevicePixel> = Size2D::new(1, 1);
48
49/// A handle to a Servo webview. If you clone this handle, it does not create a new webview,
50/// but instead creates a new handle to the webview. Once the last handle is dropped, Servo
51/// considers that the webview has closed and will clean up all associated resources related
52/// to this webview.
53///
54/// ## Creating a WebView
55///
56/// To create a [`WebView`], use [`WebViewBuilder`].
57///
58/// ## Rendering Model
59///
60/// Every [`WebView`] has a [`RenderingContext`]. The embedder manages when
61/// the contents of the [`WebView`] paint to the [`RenderingContext`]. When
62/// a [`WebView`] needs to be painted, for instance, because its contents have changed, Servo will
63/// call [`WebViewDelegate::notify_new_frame_ready`] in order to signal that it is time to repaint
64/// the [`WebView`] using [`WebView::paint`].
65///
66/// An example of how this flow might work is:
67///
68/// 1. [`WebViewDelegate::notify_new_frame_ready`] is called. The applications triggers a request
69///    to repaint the window that contains this [`WebView`].
70/// 2. During window repainting, the application calls [`WebView::paint`] and the contents of the
71///    [`RenderingContext`] are updated.
72/// 3. If the [`RenderingContext`] is double-buffered, the
73///    application then calls [`crate::RenderingContext::present()`] in order to swap the back buffer
74///    to the front, finally displaying the updated [`WebView`] contents.
75///
76/// In cases where the [`WebView`] contents have not been updated, but a repaint is necessary, for
77/// instance when repainting a window due to damage, an application may simply perform the final two
78/// steps and Servo will repaint even without first calling the
79/// [`WebViewDelegate::notify_new_frame_ready`] method.
80#[derive(Clone)]
81pub struct WebView(Rc<RefCell<WebViewInner>>);
82
83impl PartialEq for WebView {
84    fn eq(&self, other: &Self) -> bool {
85        self.inner().id == other.inner().id
86    }
87}
88
89impl Hash for WebView {
90    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
91        self.inner().id.hash(state);
92    }
93}
94
95pub(crate) struct WebViewInner {
96    pub(crate) id: WebViewId,
97    pub(crate) servo: Servo,
98    pub(crate) delegate: Rc<dyn WebViewDelegate>,
99    pub(crate) clipboard_delegate: Rc<dyn ClipboardDelegate>,
100    #[cfg(feature = "gamepad")]
101    pub(crate) gamepad_delegate: Rc<dyn GamepadDelegate>,
102
103    /// AccessKit subtree id for this [`WebView`], if accessibility is active.
104    ///
105    /// Set by [`WebView::set_accessibility_active()`], and forwarded to the constellation via
106    /// [`EmbedderToConstellationMessage::SetAccessibilityActive`].
107    pub(crate) accesskit_tree_id: Option<TreeId>,
108    /// [`TreeId`] of the web contents of this [`WebView`]’s active top-level pipeline,
109    /// which is grafted into the tree for this [`WebView`].
110    pub(crate) grafted_accesskit_tree_id: Option<TreeId>,
111    /// A counter for changes to the grafted accesskit tree for this webview.
112    /// See [`Self::grafted_accesskit_tree_id`].
113    grafted_accesskit_tree_epoch: Option<Epoch>,
114
115    rendering_context: Rc<dyn RenderingContext>,
116    user_content_manager: Option<Rc<UserContentManager>>,
117    hidpi_scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
118    load_status: LoadStatus,
119    status_text: Option<String>,
120    page_title: Option<String>,
121    favicon: Option<Image>,
122    focused: bool,
123    animating: bool,
124    cursor: Cursor,
125
126    /// The back / forward list of this WebView.
127    back_forward_list: Vec<Url>,
128
129    /// The current index in the back / forward list.
130    back_forward_list_index: usize,
131}
132
133impl Drop for WebViewInner {
134    fn drop(&mut self) {
135        self.servo
136            .constellation_proxy()
137            .send(EmbedderToConstellationMessage::CloseWebView(self.id));
138        self.servo.paint_mut().remove_webview(self.id);
139    }
140}
141
142impl WebView {
143    pub(crate) fn new(mut builder: WebViewBuilder) -> Self {
144        let servo = builder.servo;
145        let painter_id = servo
146            .paint_mut()
147            .register_rendering_context(builder.rendering_context.clone());
148
149        let id = WebViewId::new(painter_id);
150        let webview = Self(Rc::new(RefCell::new(WebViewInner {
151            id,
152            servo: servo.clone(),
153            rendering_context: builder.rendering_context,
154            delegate: builder.delegate,
155            clipboard_delegate: builder
156                .clipboard_delegate
157                .unwrap_or_else(|| Rc::new(DefaultClipboardDelegate)),
158            #[cfg(feature = "gamepad")]
159            gamepad_delegate: builder
160                .gamepad_delegate
161                .unwrap_or_else(|| Rc::new(DefaultGamepadDelegate)),
162            accesskit_tree_id: None,
163            grafted_accesskit_tree_id: None,
164            grafted_accesskit_tree_epoch: None,
165            hidpi_scale_factor: builder.hidpi_scale_factor,
166            load_status: LoadStatus::Started,
167            status_text: None,
168            page_title: None,
169            favicon: None,
170            focused: false,
171            animating: false,
172            cursor: Cursor::Pointer,
173            back_forward_list: Default::default(),
174            back_forward_list_index: 0,
175            user_content_manager: builder.user_content_manager.clone(),
176        })));
177
178        let viewport_details = webview.viewport_details();
179        servo.paint().add_webview(
180            Box::new(ServoRendererWebView {
181                weak_handle: webview.weak_handle(),
182                id,
183            }),
184            viewport_details,
185        );
186
187        servo
188            .webviews_mut()
189            .insert(webview.id(), webview.weak_handle());
190
191        let user_content_manager_id = builder
192            .user_content_manager
193            .as_ref()
194            .map(|user_content_manager| user_content_manager.id());
195
196        let new_webview_details = NewWebViewDetails {
197            webview_id: webview.id(),
198            viewport_details,
199            user_content_manager_id,
200        };
201
202        // There are two possibilities here. Either the WebView is a new toplevel
203        // WebView in which case `Self::create_new_webview_responder` is `None` or this
204        // is the response to a `WebViewDelegate::request_create_new` method in which
205        // case script expects that we just return the information directly back to
206        // the `ScriptThread`.
207        match builder.create_new_webview_responder.as_mut() {
208            Some(responder) => {
209                let _ = responder.send(Some(new_webview_details));
210            },
211            None => {
212                let url = builder.url.unwrap_or(
213                    Url::parse("about:blank")
214                        .expect("Should always be able to parse 'about:blank'."),
215                );
216
217                servo
218                    .constellation_proxy()
219                    .send(EmbedderToConstellationMessage::NewWebView(
220                        url.into(),
221                        new_webview_details,
222                    ));
223            },
224        }
225
226        webview
227    }
228
229    fn inner(&self) -> Ref<'_, WebViewInner> {
230        self.0.borrow()
231    }
232
233    fn inner_mut(&self) -> RefMut<'_, WebViewInner> {
234        self.0.borrow_mut()
235    }
236
237    pub(crate) fn request_create_new(
238        &self,
239        response_sender: GenericSender<Option<NewWebViewDetails>>,
240    ) {
241        let request = CreateNewWebViewRequest {
242            servo: self.inner().servo.clone(),
243            responder: IpcResponder::new(response_sender, None),
244        };
245        self.delegate().request_create_new(self.clone(), request);
246    }
247
248    pub(crate) fn viewport_details(&self) -> ViewportDetails {
249        // The division by 1 represents the page's default zoom of 100%,
250        // and gives us the appropriate CSSPixel type for the viewport.
251        let inner = self.inner();
252        let scaled_viewport_size =
253            inner.rendering_context.size2d().to_f32() / inner.hidpi_scale_factor;
254        ViewportDetails {
255            size: scaled_viewport_size / Scale::new(1.0),
256            hidpi_scale_factor: Scale::new(inner.hidpi_scale_factor.0),
257        }
258    }
259
260    pub(crate) fn from_weak_handle(inner: &Weak<RefCell<WebViewInner>>) -> Option<Self> {
261        inner.upgrade().map(WebView)
262    }
263
264    pub(crate) fn weak_handle(&self) -> Weak<RefCell<WebViewInner>> {
265        Rc::downgrade(&self.0)
266    }
267
268    /// Get the [`WebViewDelegate`] associated with this [`WebView`].
269    pub fn delegate(&self) -> Rc<dyn WebViewDelegate> {
270        self.inner().delegate.clone()
271    }
272
273    /// Get the [`ClipboardDelegate`] associated with this [`WebView`].
274    pub fn clipboard_delegate(&self) -> Rc<dyn ClipboardDelegate> {
275        self.inner().clipboard_delegate.clone()
276    }
277
278    /// Get the [`GamepadDelegate`] associated with this [`WebView`].
279    #[cfg(feature = "gamepad")]
280    pub fn gamepad_delegate(&self) -> Rc<dyn GamepadDelegate> {
281        self.inner().gamepad_delegate.clone()
282    }
283
284    /// Get the unique identifier for this [`WebView`].
285    pub fn id(&self) -> WebViewId {
286        self.inner().id
287    }
288
289    /// Get the load status for the page that is currently loading or loaded in this [`WebView`].
290    ///
291    /// The embedder can use [`WebViewDelegate::notify_load_status_changed`] to subscribe
292    /// to changes in the load status.
293    pub fn load_status(&self) -> LoadStatus {
294        self.inner().load_status
295    }
296
297    pub(crate) fn set_load_status(self, new_value: LoadStatus) {
298        if self.inner().load_status == new_value {
299            return;
300        }
301        self.inner_mut().load_status = new_value;
302        self.delegate().notify_load_status_changed(self, new_value);
303    }
304
305    /// Get the URL of the currently active page in this [`WebView`]'s navigation history.
306    /// Returns `None` if no page is currently loaded.
307    pub fn url(&self) -> Option<Url> {
308        let inner = self.inner();
309        inner
310            .back_forward_list
311            .get(inner.back_forward_list_index)
312            .cloned()
313    }
314
315    /// Get the current status text for this [`WebView`]. Returns `None` if there is no status text.
316    ///
317    /// The status text changes as the user interacts with the page, for example, by hovering over
318    /// a link. The embedder can use [`WebViewDelegate::notify_status_text_changed`] to subscribe
319    /// to changes in the status text.
320    pub fn status_text(&self) -> Option<String> {
321        self.inner().status_text.clone()
322    }
323
324    pub(crate) fn set_status_text(self, new_value: Option<String>) {
325        if self.inner().status_text == new_value {
326            return;
327        }
328        self.inner_mut().status_text = new_value.clone();
329        self.delegate().notify_status_text_changed(self, new_value);
330    }
331
332    /// Get the title of the currently active page in this [`WebView`]. Returns `None` if the
333    /// page has no title.
334    ///
335    /// The embedder can use [`WebViewDelegate::notify_page_title_changed`] to subscribe
336    /// to changes in the [`WebView`]'s page title.
337    pub fn page_title(&self) -> Option<String> {
338        self.inner().page_title.clone()
339    }
340
341    pub(crate) fn set_page_title(self, new_value: Option<String>) {
342        if self.inner().page_title == new_value {
343            return;
344        }
345        self.inner_mut().page_title = new_value.clone();
346        self.delegate().notify_page_title_changed(self, new_value);
347    }
348
349    /// Get a read-only reference to the image data for the favicon of the currently
350    /// active page in this [`WebView`]. Returns `None` if no favicon is available
351    /// for the currently active page.
352    ///
353    /// The embedder can use [`WebViewDelegate::notify_favicon_changed`] to subscribe
354    /// to changes in the [`WebView`]'s favicon.
355    pub fn favicon(&self) -> Option<Ref<'_, Image>> {
356        Ref::filter_map(self.inner(), |inner| inner.favicon.as_ref()).ok()
357    }
358
359    pub(crate) fn set_favicon(self, new_value: Image) {
360        self.inner_mut().favicon = Some(new_value);
361        self.delegate().notify_favicon_changed(self);
362    }
363
364    /// Whether or not this [`WebView`] currently has the keyboard focus.
365    ///
366    /// The embedder can use [`WebViewDelegate::notify_focus_changed`] to subscribe
367    /// to changes in the  [`WebView`]'s focus state.
368    pub fn focused(&self) -> bool {
369        self.inner().focused
370    }
371
372    pub(crate) fn set_focused(self, new_value: bool) {
373        if self.inner().focused == new_value {
374            return;
375        }
376        self.inner_mut().focused = new_value;
377        self.delegate().notify_focus_changed(self, new_value);
378    }
379
380    /// Get the current [`Cursor`] for this [`WebView`].
381    ///
382    /// The cursor can change as the user interacts with page content. The embedder
383    /// can use [`WebViewDelegate::notify_cursor_changed`] to subscribe to changes in
384    /// the  [`WebView`]'s cursor.
385    pub fn cursor(&self) -> Cursor {
386        self.inner().cursor
387    }
388
389    pub(crate) fn set_cursor(self, new_value: Cursor) {
390        if self.inner().cursor == new_value {
391            return;
392        }
393        self.inner_mut().cursor = new_value;
394        self.delegate().notify_cursor_changed(self, new_value);
395    }
396
397    /// Notify Servo that this [`WebView`] has gained keyboard focus.
398    pub fn focus(&self) {
399        self.inner()
400            .servo
401            .constellation_proxy()
402            .send(EmbedderToConstellationMessage::FocusWebView(self.id()));
403    }
404
405    /// Notify Servo that this [`WebView`] has lost keyboard focus.
406    pub fn blur(&self) {
407        self.inner()
408            .servo
409            .constellation_proxy()
410            .send(EmbedderToConstellationMessage::BlurWebView);
411    }
412
413    /// Whether or not this [`WebView`] has animating content, such as a CSS animation or
414    /// transition or is running `requestAnimationFrame` callbacks. This indicates that the
415    /// embedding application should be spinning the Servo event loop on regular intervals
416    /// in order to trigger animation updates.
417    pub fn animating(&self) -> bool {
418        self.inner().animating
419    }
420
421    pub(crate) fn set_animating(self, new_value: bool) {
422        if self.inner().animating == new_value {
423            return;
424        }
425        self.inner_mut().animating = new_value;
426        self.delegate().notify_animating_changed(self, new_value);
427    }
428
429    /// The size of this [`WebView`]'s [`RenderingContext`].
430    pub fn size(&self) -> DeviceSize {
431        self.inner().rendering_context.size2d().to_f32()
432    }
433
434    /// Request that the given [`WebView`]'s [`RenderingContext`] be resized. Note that the
435    /// minimum size for a WebView is 1 pixel by 1 pixel so any requested size will be
436    /// clamped by that value.
437    ///
438    /// This will also resize any other [`WebView`] using the same [`RenderingContext`]. A
439    /// [`WebView`] is always as big as its [`RenderingContext`].
440    pub fn resize(&self, new_size: PhysicalSize<u32>) {
441        let new_size = PhysicalSize {
442            width: new_size.width.max(MINIMUM_WEBVIEW_SIZE.width as u32),
443            height: new_size.height.max(MINIMUM_WEBVIEW_SIZE.height as u32),
444        };
445
446        self.inner()
447            .servo
448            .paint()
449            .resize_rendering_context(self.id(), new_size);
450    }
451
452    /// Get the HiDPI scale factor for this [`WebView`].
453    pub fn hidpi_scale_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
454        self.inner().hidpi_scale_factor
455    }
456
457    /// Set the HiDPI scale factor for this [`WebView`].
458    ///
459    /// This scale factor determines how device-independent pixels map to physical device pixels
460    /// and therefore depends on which device this [`WebView`] is being displayed.
461    pub fn set_hidpi_scale_factor(
462        &self,
463        new_scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
464    ) {
465        if self.inner().hidpi_scale_factor == new_scale_factor {
466            return;
467        }
468
469        self.inner_mut().hidpi_scale_factor = new_scale_factor;
470        self.inner()
471            .servo
472            .paint()
473            .set_hidpi_scale_factor(self.id(), new_scale_factor);
474    }
475
476    /// Make this [`WebView`] visible within its [`RenderingContext`].
477    pub fn show(&self) {
478        self.inner()
479            .servo
480            .paint()
481            .show_webview(self.id())
482            .expect("BUG: invalid WebView instance");
483    }
484
485    /// Hide this [`WebView`] within its [`RenderingContext`].
486    pub fn hide(&self) {
487        self.inner()
488            .servo
489            .paint()
490            .hide_webview(self.id())
491            .expect("BUG: invalid WebView instance");
492    }
493
494    /// Notify this [`WebView`] of a change to the system theme (e.g. light or dark mode).
495    pub fn notify_theme_change(&self, theme: Theme) {
496        self.inner()
497            .servo
498            .constellation_proxy()
499            .send(EmbedderToConstellationMessage::ThemeChange(
500                self.id(),
501                theme,
502            ))
503    }
504
505    /// Load the given URL into this [`WebView`] using the default request headers.
506    ///
507    /// This pushes a new entry onto the navigation history, so the user can navigate
508    /// back to the previous page.
509    pub fn load(&self, url: Url) {
510        self.inner()
511            .servo
512            .constellation_proxy()
513            .send(EmbedderToConstellationMessage::LoadUrl(
514                self.id(),
515                UrlRequest::new(url),
516            ))
517    }
518
519    /// Load a [`UrlRequest`] with custom headers into this [`WebView`].
520    ///
521    /// This pushes a new entry onto the navigation history, so the user can navigate
522    /// back to the previous page.
523    pub fn load_request(&self, url_request: UrlRequest) {
524        self.inner()
525            .servo
526            .constellation_proxy()
527            .send(EmbedderToConstellationMessage::LoadUrl(
528                self.id(),
529                url_request,
530            ))
531    }
532
533    /// Reload the currently loaded page in this [`WebView`].
534    pub fn reload(&self) {
535        self.inner_mut().load_status = LoadStatus::Started;
536        self.inner()
537            .servo
538            .constellation_proxy()
539            .send(EmbedderToConstellationMessage::Reload(self.id()))
540    }
541
542    /// Whether or not this [`WebView`] can go backward in its navigation history.
543    ///
544    /// This is `false` if the currently active page is the oldest entry in the
545    /// [`WebView`]'s navigation history.
546    pub fn can_go_back(&self) -> bool {
547        self.inner().back_forward_list_index != 0
548    }
549
550    /// Go backward in this [`WebView`]'s navigation history by the given number of steps.
551    ///
552    /// Returns a [`TraversalId`] that can be used with the
553    /// [`WebViewDelegate::notify_traversal_complete`] callback to determine when the
554    /// traversal is complete.
555    pub fn go_back(&self, amount: usize) -> TraversalId {
556        let traversal_id = TraversalId::new();
557        self.inner().servo.constellation_proxy().send(
558            EmbedderToConstellationMessage::TraverseHistory(
559                self.id(),
560                TraversalDirection::Back(amount),
561                traversal_id.clone(),
562            ),
563        );
564        traversal_id
565    }
566
567    /// Whether or not this [`WebView`] can go forward in its navigation history.
568    ///
569    /// This is `false` if the currently active page is the most recent entry in
570    /// the [`WebView`]'s navigation history.
571    pub fn can_go_forward(&self) -> bool {
572        let inner = self.inner();
573        inner.back_forward_list.len() > inner.back_forward_list_index + 1
574    }
575
576    /// Go forward in this [`WebView`]'s navigation history by the given number of steps.
577    ///
578    /// Returns a [`TraversalId`] that can be used with the
579    /// [`WebViewDelegate::notify_traversal_complete`] callback to determine when the
580    /// traversal is complete.
581    pub fn go_forward(&self, amount: usize) -> TraversalId {
582        let traversal_id = TraversalId::new();
583        self.inner().servo.constellation_proxy().send(
584            EmbedderToConstellationMessage::TraverseHistory(
585                self.id(),
586                TraversalDirection::Forward(amount),
587                traversal_id.clone(),
588            ),
589        );
590        traversal_id
591    }
592
593    /// Ask the [`WebView`] to scroll the scrollable area under `point` to the
594    /// given `scroll` destination.
595    pub fn notify_scroll_event(&self, scroll: Scroll, point: WebViewPoint) {
596        self.inner()
597            .servo
598            .paint()
599            .notify_scroll_event(self.id(), scroll, point);
600    }
601
602    /// Notify this [`WebView`] about an [`InputEvent`] such as a mouse click, touch
603    /// event, or key press.
604    ///
605    /// Returns an [`InputEventId`] that can be used with the
606    /// [`WebViewDelegate::notify_input_event_handled`] callback to determine the result of
607    /// processing of the event by the page content.
608    pub fn notify_input_event(&self, event: InputEvent) -> InputEventId {
609        let event: InputEventAndId = event.into();
610        let event_id = event.id;
611        let webview_id = self.id();
612        let servo = &self.inner().servo;
613        // Events with a `point` first go to `Paint` for hit testing.
614        if event.event.point().is_some() {
615            if !servo.paint().notify_input_event(self.id(), event) {
616                servo.add_pending_handled_input_event(PendingHandledInputEvent {
617                    event_id,
618                    webview_id,
619                });
620                servo.event_loop_waker().wake();
621            }
622        } else {
623            servo
624                .constellation_proxy()
625                .send(EmbedderToConstellationMessage::ForwardInputEvent(
626                    webview_id, event, None, /* hit_test */
627                ));
628        }
629
630        event_id
631    }
632
633    /// Notify this [`WebView`] about a media session event (e.g. play, pause, next track).
634    pub fn notify_media_session_action_event(&self, event: MediaSessionActionType) {
635        self.inner()
636            .servo
637            .constellation_proxy()
638            .send(EmbedderToConstellationMessage::MediaSessionAction(event));
639    }
640
641    /// Set the page zoom of the [`WebView`]. This sets the final page zoom value of the
642    /// [`WebView`]. Unlike [`WebView::pinch_zoom`] *it is not* multiplied by the current
643    /// page zoom value, but overrides it.
644    ///
645    /// [`WebView`]s have two types of zoom, pinch zoom and page zoom. This adjusts page
646    /// zoom, which will adjust the `devicePixelRatio` of the page and cause it to modify
647    /// its layout.
648    ///
649    /// These values will be clamped internally to the inclusive range [0.1, 10.0]).
650    pub fn set_page_zoom(&self, new_zoom: f32) {
651        self.inner()
652            .servo
653            .paint()
654            .set_page_zoom(self.id(), new_zoom);
655    }
656
657    /// Get the page zoom of the [`WebView`].
658    pub fn page_zoom(&self) -> f32 {
659        self.inner().servo.paint().page_zoom(self.id())
660    }
661
662    /// Adjust the pinch zoom on this [`WebView`] multiplying the current pinch zoom
663    /// level with the provided `pinch_zoom_delta`.
664    ///
665    /// [`WebView`]s have two types of zoom, pinch zoom and page zoom. This adjusts pinch
666    /// zoom, which is a type of zoom which does not modify layout, and instead simply
667    /// magnifies the view in the viewport.
668    ///
669    /// The final pinch zoom values will be clamped to defaults (the inclusive range [1.0, 10.0]).
670    /// The values used for clamping can be adjusted by page content when `<meta viewport>`
671    /// parsing is enabled via `Prefs::viewport_meta_enabled`, exclusively on mobile devices.
672    pub fn adjust_pinch_zoom(&self, pinch_zoom_delta: f32, center: DevicePoint) {
673        self.inner()
674            .servo
675            .paint()
676            .adjust_pinch_zoom(self.id(), pinch_zoom_delta, center);
677    }
678
679    /// Get the pinch zoom of the [`WebView`].
680    pub fn pinch_zoom(&self) -> f32 {
681        self.inner().servo.paint().pinch_zoom(self.id())
682    }
683
684    /// Get the ratio of physical device pixels to CSS pixels for this [`WebView`].
685    ///
686    /// The returned scale factor takes into account page zoom, pinch zoom and the
687    /// HiDPI scaling factor.
688    pub fn device_pixels_per_css_pixel(&self) -> Scale<f32, CSSPixel, DevicePixel> {
689        self.inner()
690            .servo
691            .paint()
692            .device_pixels_per_page_pixel(self.id())
693    }
694
695    /// Tell the currently active page in this [`WebView`] to exit fullscreen mode.
696    pub fn exit_fullscreen(&self) {
697        self.inner()
698            .servo
699            .constellation_proxy()
700            .send(EmbedderToConstellationMessage::ExitFullScreen(self.id()));
701    }
702
703    /// Set whether resource usage of this [`WebView`] should be throttled or not.
704    ///
705    /// A throttled [`WebView`] attempts to use less system resources by stopping
706    /// animations and running timers at a heavily limited rate.
707    pub fn set_throttled(&self, throttled: bool) {
708        self.inner().servo.constellation_proxy().send(
709            EmbedderToConstellationMessage::SetWebViewThrottled(self.id(), throttled),
710        );
711    }
712
713    /// Toggle the given [`WebRenderDebugOption`] from its current state.
714    ///
715    /// Note that this method toggles the debugging options globally i.e., it affects
716    /// all [`WebView`]s managed by Servo and not just the [`WebView`] on which
717    /// this method is invoked.
718    pub fn toggle_webrender_debugging(&self, debugging: WebRenderDebugOption) {
719        self.inner().servo.paint().toggle_webrender_debug(debugging);
720    }
721
722    /// Capture the current WebRender state for this [`WebView`] for debugging.
723    ///
724    /// Note that the captured state includes information about all [`WebView`]s
725    /// that share this [`WebView`]'s [`RenderingContext`].
726    pub fn capture_webrender(&self) {
727        self.inner().servo.paint().capture_webrender(self.id());
728    }
729
730    /// Enable the sampling profiler for debugging performance issues.
731    ///
732    /// The `rate` determines how often samples are taken and `max_duration` is
733    /// the maximum period for which sampling is enabled.
734    ///
735    /// Note that the profiler is enabled globally i.e., for all [`WebView`]s managed
736    /// by Servo rather than just the [`WebView`] on which this method is invoked.
737    pub fn toggle_sampling_profiler(&self, rate: Duration, max_duration: Duration) {
738        self.inner().servo.constellation_proxy().send(
739            EmbedderToConstellationMessage::ToggleProfiler(rate, max_duration),
740        );
741    }
742
743    /// Paint the contents of this [`WebView`] into its [`RenderingContext`].
744    pub fn paint(&self) {
745        self.inner().servo.paint().render(self.id());
746    }
747
748    /// Get the [`UserContentManager`] associated with this [`WebView`].
749    pub fn user_content_manager(&self) -> Option<Rc<UserContentManager>> {
750        self.inner().user_content_manager.clone()
751    }
752
753    /// Evaluate the specified string of JavaScript code. Once execution is complete or an error
754    /// occurs, Servo will call `callback`.
755    pub fn evaluate_javascript<T: ToString>(
756        &self,
757        script: T,
758        callback: impl FnOnce(Result<JSValue, JavaScriptEvaluationError>) + 'static,
759    ) {
760        self.inner().servo.javascript_evaluator_mut().evaluate(
761            self.id(),
762            script.to_string(),
763            Box::new(callback),
764        );
765    }
766
767    /// Asynchronously take a screenshot of the [`WebView`] contents, given a `rect` or the whole
768    /// viewport, if no `rect` is given.
769    ///
770    /// This method will wait until the [`WebView`] is ready before the screenshot is taken.
771    /// This includes waiting for:
772    ///
773    ///  - all frames to fire their `load` event.
774    ///  - all render blocking elements, such as stylesheets included via the `<link>`
775    ///    element, to stop blocking the rendering.
776    ///  - all images to be loaded and displayed.
777    ///  - all web fonts are loaded.
778    ///  - the `reftest-wait` and `test-wait` classes have been removed from the root element.
779    ///  - the rendering is up-to-date
780    ///
781    /// Once all these conditions are met and the rendering does not have any pending frames
782    /// to render, the provided `callback` will be called with the results of the screenshot
783    /// operation.
784    pub fn take_screenshot(
785        &self,
786        rect: Option<WebViewRect>,
787        callback: impl FnOnce(Result<RgbaImage, ScreenshotCaptureError>) + 'static,
788    ) {
789        self.inner()
790            .servo
791            .paint()
792            .request_screenshot(self.id(), rect, Box::new(callback));
793    }
794
795    pub(crate) fn set_history(self, new_back_forward_list: Vec<ServoUrl>, new_index: usize) {
796        {
797            let mut inner_mut = self.inner_mut();
798            inner_mut.back_forward_list_index = new_index;
799            inner_mut.back_forward_list = new_back_forward_list
800                .into_iter()
801                .map(ServoUrl::into_url)
802                .collect();
803        }
804
805        let back_forward_list = self.inner().back_forward_list.clone();
806        let back_forward_list_index = self.inner().back_forward_list_index;
807        self.delegate().notify_url_changed(
808            self.clone(),
809            back_forward_list[back_forward_list_index].clone(),
810        );
811        self.delegate().notify_history_changed(
812            self.clone(),
813            back_forward_list,
814            back_forward_list_index,
815        );
816    }
817
818    pub(crate) fn show_embedder_control(
819        self,
820        control_id: EmbedderControlId,
821        position: DeviceIntRect,
822        embedder_control_request: EmbedderControlRequest,
823    ) {
824        let constellation_proxy = self.inner().servo.constellation_proxy().clone();
825        let embedder_control = match embedder_control_request {
826            EmbedderControlRequest::SelectElement(request) => {
827                EmbedderControl::SelectElement(SelectElement {
828                    id: control_id,
829                    select_element_request: request,
830                    position,
831                    constellation_proxy,
832                    response_sent: false,
833                })
834            },
835            EmbedderControlRequest::ColorPicker(current_color) => {
836                EmbedderControl::ColorPicker(ColorPicker {
837                    id: control_id,
838                    current_color: Some(current_color),
839                    position,
840                    constellation_proxy,
841                    response_sent: false,
842                })
843            },
844            EmbedderControlRequest::InputMethod(input_method_request) => {
845                EmbedderControl::InputMethod(InputMethodControl {
846                    id: control_id,
847                    input_method_type: input_method_request.input_method_type,
848                    text: input_method_request.text,
849                    insertion_point: input_method_request.insertion_point,
850                    position,
851                    multiline: input_method_request.multiline,
852                    allow_virtual_keyboard: input_method_request.allow_virtual_keyboard,
853                })
854            },
855            EmbedderControlRequest::ContextMenu(mut context_menu_request) => {
856                for item in context_menu_request.items.iter_mut() {
857                    match item {
858                        ContextMenuItem::Item {
859                            action: ContextMenuAction::GoBack,
860                            enabled,
861                            ..
862                        } => *enabled = self.can_go_back(),
863                        ContextMenuItem::Item {
864                            action: ContextMenuAction::GoForward,
865                            enabled,
866                            ..
867                        } => *enabled = self.can_go_forward(),
868                        _ => {},
869                    }
870                }
871                EmbedderControl::ContextMenu(ContextMenu {
872                    id: control_id,
873                    position,
874                    items: context_menu_request.items,
875                    element_info: context_menu_request.element_info,
876                    constellation_proxy,
877                    response_sent: false,
878                })
879            },
880            EmbedderControlRequest::FilePicker { .. } => {
881                unreachable!("This message should be routed through the FileManagerThread")
882            },
883        };
884
885        self.delegate()
886            .show_embedder_control(self.clone(), embedder_control);
887    }
888
889    /// AccessKit subtree id for this [`WebView`], if accessibility is active.
890    pub fn accesskit_tree_id(&self) -> Option<TreeId> {
891        self.inner().accesskit_tree_id
892    }
893
894    /// Activate or deactivate accessibility features for this [`WebView`], returning the
895    /// AccessKit subtree id if accessibility is now active.
896    ///
897    /// After accessibility is activated, you must [graft] (with [`set_tree_id()`]) the returned
898    /// [`TreeId`] into your application’s main AccessKit tree as soon as possible, *before*
899    /// sending any tree updates from the webview to your AccessKit adapter. Otherwise you may
900    /// violate AccessKit’s subtree invariants and **panic**.
901    ///
902    /// If your impl for [`WebViewDelegate::notify_accessibility_tree_update()`] can’t create the
903    /// graft node (and send *that* update to AccessKit) before sending any updates from this
904    /// webview to AccessKit, then it must queue those updates until it can guarantee that.
905    ///
906    /// [graft]: https://docs.rs/accesskit/0.24.0/accesskit/struct.Node.html#method.tree_id
907    /// [`set_tree_id()`]: https://docs.rs/accesskit/0.24.0/accesskit/struct.Node.html#method.set_tree_id
908    pub fn set_accessibility_active(&self, active: bool) -> Option<TreeId> {
909        if !pref!(accessibility_enabled) {
910            return None;
911        }
912
913        if active == self.inner().accesskit_tree_id.is_some() {
914            return self.accesskit_tree_id();
915        }
916
917        if active {
918            let accesskit_tree_id = TreeId(AccesskitUuid::new_v4());
919            self.inner_mut().accesskit_tree_id = Some(accesskit_tree_id);
920        } else {
921            self.inner_mut().accesskit_tree_id = None;
922            self.inner_mut().grafted_accesskit_tree_id = None;
923            self.inner_mut().grafted_accesskit_tree_epoch = None;
924        }
925
926        self.inner().servo.constellation_proxy().send(
927            EmbedderToConstellationMessage::SetAccessibilityActive(self.id(), active),
928        );
929
930        self.accesskit_tree_id()
931    }
932
933    pub(crate) fn notify_document_accessibility_tree_id(&self, grafted_tree_id: TreeId) {
934        let Some(webview_accesskit_tree_id) = self.inner().accesskit_tree_id else {
935            return;
936        };
937        let old_grafted_tree_id = self
938            .inner_mut()
939            .grafted_accesskit_tree_id
940            .replace(grafted_tree_id);
941        // TODO(#4344): try to avoid duplicate notifications in the first place?
942        // (see ConstellationWebView::new for more details)
943        if old_grafted_tree_id == Some(grafted_tree_id) {
944            return;
945        }
946        let root_node_id = NodeId(0);
947        let mut root_node = AccesskitNode::new(Role::ScrollView);
948        let graft_node_id = NodeId(1);
949        let mut graft_node = AccesskitNode::new(Role::GenericContainer);
950        graft_node.set_tree_id(grafted_tree_id);
951        root_node.set_children(vec![graft_node_id]);
952        self.delegate().notify_accessibility_tree_update(
953            self.clone(),
954            TreeUpdate {
955                nodes: vec![(root_node_id, root_node), (graft_node_id, graft_node)],
956                tree: Some(Tree {
957                    root: root_node_id,
958                    toolkit_name: None,
959                    toolkit_version: None,
960                }),
961                tree_id: webview_accesskit_tree_id,
962                focus: root_node_id,
963            },
964        );
965    }
966
967    pub(crate) fn process_accessibility_tree_update(&self, tree_update: TreeUpdate, epoch: Epoch) {
968        if self
969            .inner()
970            .grafted_accesskit_tree_epoch
971            .is_some_and(|current| epoch < current)
972        {
973            // We expect this to happen occasionally when the constellation navigates, because
974            // deactivating accessibility happens asynchronously, so the script thread of the
975            // previously active document may continue sending updates for a short period of time.
976            debug!("Ignoring stale tree update for {:?}", tree_update.tree_id);
977            return;
978        }
979        if self
980            .inner()
981            .grafted_accesskit_tree_epoch
982            .is_none_or(|current| epoch > current)
983        {
984            self.notify_document_accessibility_tree_id(tree_update.tree_id);
985            self.inner_mut().grafted_accesskit_tree_epoch = Some(epoch);
986        }
987        self.delegate()
988            .notify_accessibility_tree_update(self.clone(), tree_update);
989    }
990}
991
992/// A structure used to expose a view of the [`WebView`] to the Servo
993/// renderer, without having the Servo renderer depend on the embedding layer.
994struct ServoRendererWebView {
995    id: WebViewId,
996    weak_handle: Weak<RefCell<WebViewInner>>,
997}
998
999impl WebViewTrait for ServoRendererWebView {
1000    fn id(&self) -> WebViewId {
1001        self.id
1002    }
1003
1004    fn screen_geometry(&self) -> Option<ScreenGeometry> {
1005        let webview = WebView::from_weak_handle(&self.weak_handle)?;
1006        webview.delegate().screen_geometry(webview)
1007    }
1008
1009    fn set_animating(&self, new_value: bool) {
1010        if let Some(webview) = WebView::from_weak_handle(&self.weak_handle) {
1011            webview.set_animating(new_value);
1012        }
1013    }
1014}
1015
1016/// Builder for creating a [`WebView`].
1017pub struct WebViewBuilder {
1018    servo: Servo,
1019    rendering_context: Rc<dyn RenderingContext>,
1020    delegate: Rc<dyn WebViewDelegate>,
1021    url: Option<Url>,
1022    hidpi_scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
1023    create_new_webview_responder: Option<IpcResponder<Option<NewWebViewDetails>>>,
1024    user_content_manager: Option<Rc<UserContentManager>>,
1025    clipboard_delegate: Option<Rc<dyn ClipboardDelegate>>,
1026    #[cfg(feature = "gamepad")]
1027    gamepad_delegate: Option<Rc<dyn GamepadDelegate>>,
1028}
1029
1030impl WebViewBuilder {
1031    /// Create a [`WebViewBuilder`] that can be used to configure and create a [`WebView`].
1032    ///
1033    /// The new [`WebView`] will be managed by the given `servo` instance and will
1034    /// use `rendering_context` to paint its contents.
1035    pub fn new(servo: &Servo, rendering_context: Rc<dyn RenderingContext>) -> Self {
1036        Self {
1037            servo: servo.clone(),
1038            rendering_context,
1039            url: None,
1040            hidpi_scale_factor: Scale::new(1.0),
1041            delegate: Rc::new(DefaultWebViewDelegate),
1042            create_new_webview_responder: None,
1043            user_content_manager: None,
1044            clipboard_delegate: None,
1045            #[cfg(feature = "gamepad")]
1046            gamepad_delegate: None,
1047        }
1048    }
1049
1050    pub(crate) fn new_for_create_request(
1051        servo: &Servo,
1052        rendering_context: Rc<dyn RenderingContext>,
1053        responder: IpcResponder<Option<NewWebViewDetails>>,
1054    ) -> Self {
1055        let mut builder = Self::new(servo, rendering_context);
1056        builder.create_new_webview_responder = Some(responder);
1057        builder
1058    }
1059
1060    /// Set the [`WebViewDelegate`] that will receive notifications about the events
1061    /// in the [`WebView`] being created.
1062    pub fn delegate(mut self, delegate: Rc<dyn WebViewDelegate>) -> Self {
1063        self.delegate = delegate;
1064        self
1065    }
1066
1067    /// Set the initial URL to load in the [`WebView`] being created.
1068    pub fn url(mut self, url: Url) -> Self {
1069        self.url = Some(url);
1070        self
1071    }
1072
1073    /// Set the initial HiDPI scale factor for the [`WebView`] being created.
1074    pub fn hidpi_scale_factor(
1075        mut self,
1076        hidpi_scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
1077    ) -> Self {
1078        self.hidpi_scale_factor = hidpi_scale_factor;
1079        self
1080    }
1081
1082    /// Set the [`UserContentManager`] for the `WebView` being created. The same
1083    /// `UserContentManager` can be shared among multiple `WebView`s. Any updates
1084    /// to the `UserContentManager` will take effect only after the document is reloaded.
1085    pub fn user_content_manager(mut self, user_content_manager: Rc<UserContentManager>) -> Self {
1086        self.user_content_manager = Some(user_content_manager);
1087        self
1088    }
1089
1090    /// Set the [`ClipboardDelegate`] for the `WebView` being created. The same
1091    /// [`ClipboardDelegate`] can be shared among multiple `WebView`s.
1092    pub fn clipboard_delegate(mut self, clipboard_delegate: Rc<dyn ClipboardDelegate>) -> Self {
1093        self.clipboard_delegate = Some(clipboard_delegate);
1094        self
1095    }
1096
1097    /// Set the [`GamepadDelegate`] for the `WebView` being created. The same
1098    /// [`GamepadDelegate`] can be shared among multiple `WebView`s.
1099    #[cfg(feature = "gamepad")]
1100    pub fn gamepad_delegate(mut self, gamepad_delegate: Rc<dyn GamepadDelegate>) -> Self {
1101        self.gamepad_delegate = Some(gamepad_delegate);
1102        self
1103    }
1104
1105    /// Create the [`WebView`] using the configuration specified in this [`WebViewBuilder`].
1106    pub fn build(self) -> WebView {
1107        WebView::new(self)
1108    }
1109}