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