iced_servo 0.1.0

Embed a Servo webview inside an Iced application via an offscreen rendering context
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Servo webview lifecycle, split in two:
//!
//! - [`ServoRuntime`] owns the process-global Servo engine and the one
//!   [`SoftwareRenderingContext`] that every webview renders into. Servo
//!   stores its startup options in a process-wide singleton, so there can
//!   only ever be **one** `Servo` in the process — a second
//!   `ServoBuilder::build()` panics with "Already initialized". Put the
//!   runtime at the top of your app and share it across every tab.
//! - [`ServoWebViewController`] is a per-webview handle: one page's
//!   navigation state, delegate bridge, frame slot, and `tick()` pump.
//!   Multiple controllers can live on one runtime — that's how the
//!   example implements browser tabs. Only the visible tab's controller
//!   needs `tick()` called each frame.
//!
//! `Servo` is `!Send + !Sync`; both the runtime and controllers live on
//! the iced main thread (which is where `App::new`, `update`, `view`,
//! and `Program::update` all run).

use std::{
    cell::{Cell, RefCell},
    rc::Rc,
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use dpi::PhysicalSize;
use euclid::{Box2D, Point2D, Scale};
use futures::{
    channel::mpsc::{UnboundedReceiver, UnboundedSender, unbounded},
    channel::oneshot,
    stream::{BoxStream, StreamExt},
};
use iced::{Subscription, Task};
use servo::{
    EventLoopWaker, JSValue, JavaScriptEvaluationError, RenderingContext, Servo, ServoBuilder,
    SoftwareRenderingContext, WebView, WebViewBuilder,
};
use tracing::error;
use url::Url;

use iced_frame::{Frame, FrameSource, SizeRequestSlot};

use crate::delegate::{DelegateState, ServoBridge, WebViewBridge};

/// How long a requested webview size must remain stable (no new
/// `request_size` call with a different value) before the controller's
/// `tick` actually applies it. Drag-resize fires resize requests every
/// frame; without this debounce, Servo gets a flood of resize events
/// and can never finish laying out a single frame at the target size.
const RESIZE_DEBOUNCE: Duration = Duration::from_millis(100);

/// What a webview should load on startup.
#[derive(Clone)]
pub enum Content {
    Url(String),
    Html(String),
}

impl Default for Content {
    fn default() -> Self {
        Self::Url("about:blank".into())
    }
}

/// Construction-time configuration for a Servo webview.
#[derive(Default)]
pub struct WebViewConfig {
    content: Content,
}

impl WebViewConfig {
    /// Load the given URL on startup.
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.content = Content::Url(url.into());
        self
    }

    /// Load the given HTML string on startup. Convenience wrapper around a
    /// `data:` URL.
    pub fn html(mut self, html: impl Into<String>) -> Self {
        self.content = Content::Html(html.into());
        self
    }
}

/// Shared Servo engine + rendering context. Build one of these at app
/// startup and pass it into each [`ServoWebViewController::new`] call.
/// Cheap to clone (`Rc` inside) — passing one to multiple tabs does not
/// duplicate the underlying engine.
///
/// Servo stores its global options in a process-wide singleton, so you
/// must only construct one runtime per process. Building a second one
/// will panic at `set_opts`.
#[derive(Clone)]
pub struct ServoRuntime {
    inner: Rc<ServoRuntimeInner>,
}

struct ServoRuntimeInner {
    servo: Servo,
    rendering_context: Rc<SoftwareRenderingContext>,
    rendering_context_dyn: Rc<dyn RenderingContext>,
    wake_rx: RefCell<Option<UnboundedReceiver<()>>>,
}

impl ServoRuntime {
    /// Build the shared Servo engine backed by a software rendering
    /// context of the given size. Subsequent webview resizes (driven by
    /// the shader widget's layout) will push the rendering context to
    /// the right dimensions through `webview.resize()` — the initial
    /// size is only what the first frame gets laid out at.
    pub fn new(initial_size: PhysicalSize<u32>) -> Result<Self, String> {
        let w = initial_size.width.max(1);
        let h = initial_size.height.max(1);
        let initial_size = PhysicalSize::new(w, h);

        let rendering_context = SoftwareRenderingContext::new(initial_size)
            .map_err(|e| format!("SoftwareRenderingContext::new failed: {e:?}"))?;
        let rendering_context = Rc::new(rendering_context);
        rendering_context
            .make_current()
            .map_err(|e| format!("SoftwareRenderingContext::make_current failed: {e:?}"))?;

        let (wake_tx, wake_rx) = unbounded::<()>();
        let waker: Box<dyn EventLoopWaker> = Box::new(ChannelWaker { tx: wake_tx });

        let servo = ServoBuilder::default().event_loop_waker(waker).build();
        // Intentionally skip `servo.setup_logging()` — embedders that
        // have already installed a global `log` crate logger (via
        // `tracing-subscriber` or similar) would otherwise hit
        // `SetLoggerError`. Servo's log output still flows through any
        // existing bridge.
        servo.set_delegate(Rc::new(ServoBridge));

        let rendering_context_dyn: Rc<dyn RenderingContext> = Rc::clone(&rendering_context) as _;

        Ok(Self {
            inner: Rc::new(ServoRuntimeInner {
                servo,
                rendering_context,
                rendering_context_dyn,
                wake_rx: RefCell::new(Some(wake_rx)),
            }),
        })
    }

    fn servo(&self) -> &Servo {
        &self.inner.servo
    }

    fn rendering_context(&self) -> &Rc<SoftwareRenderingContext> {
        &self.inner.rendering_context
    }

    fn rendering_context_dyn(&self) -> Rc<dyn RenderingContext> {
        Rc::clone(&self.inner.rendering_context_dyn)
    }

    /// Set a Servo preference at runtime.
    pub fn set_preference(&self, name: &str, value: servo::PrefValue) {
        self.inner.servo.set_preference(name, value);
    }

    /// The default user-agent string Servo uses for this platform.
    pub fn default_user_agent(&self) -> String {
        servo::UserAgentPlatform::default().to_user_agent_string()
    }

    /// Iced subscription that drains the Servo wake channel. Mount this
    /// once per app (not once per tab) — all webviews share the same
    /// event loop waker.
    pub fn subscription(&self) -> Subscription<()> {
        let wake_rx = self.inner.wake_rx.borrow_mut().take();
        let data = WakeSubData {
            wake_rx: Arc::new(Mutex::new(wake_rx)),
        };
        iced::Subscription::run_with(data, build_wake_drain)
    }
}

/// Per-webview state. Fields that mutate (resize debounce, scale,
/// handler slot) use `Cell` / `RefCell` directly — the outer controller
/// itself is just an `Rc<Inner>`, not `Rc<RefCell<Inner>>`, so most
/// accesses are a single deref.
struct Inner {
    runtime: ServoRuntime,
    webview: WebView,
    delegate_state: Rc<DelegateState>,

    /// Physical-pixel size the shader widget most recently requested,
    /// plus the [`Instant`] at which it was set. `tick()` only applies
    /// the resize once the value has been stable for [`RESIZE_DEBOUNCE`],
    /// so a window-drag that fires hundreds of resize requests per
    /// second collapses to a single Servo `webview.resize` after the
    /// user releases.
    pending_resize: Cell<Option<(PhysicalSize<u32>, Instant)>>,

    /// HiDPI scale factor — the ratio between iced's logical pixels and
    /// device pixels. Updated by the widget each `Primitive::prepare`
    /// via the `Viewport::scale_factor()`.
    scale_factor: Cell<f32>,

    /// Shared slot written to by the shader widget's
    /// `Primitive::prepare` with the current physical-pixel size and
    /// scale factor. `tick()` drains it and forwards the size to Servo.
    /// `Arc<Mutex<...>>` because `Primitive` must be `Send + Sync`.
    size_request: SizeRequestSlot,
}

/// Embedder handle for a single Servo webview. Cheap to clone — it's
/// just an `Rc` internally. Each tab/view in an app owns one controller;
/// they all share the same [`ServoRuntime`].
#[derive(Clone)]
pub struct ServoWebViewController {
    inner: Rc<Inner>,
}

impl ServoWebViewController {
    /// Build a new webview on the given runtime. The initial HiDPI
    /// scale factor is fed straight to Servo; the physical size is
    /// taken from the runtime's rendering context and the shader
    /// widget's first `Primitive::prepare` will push any updated size
    /// a frame or two later.
    pub fn new(
        runtime: &ServoRuntime,
        config: WebViewConfig,
        scale_factor: f32,
    ) -> Result<Self, String> {
        let delegate_state = Rc::new(DelegateState {
            webview: RefCell::new(None),
            rendering_context: RefCell::new(Some(runtime.rendering_context_dyn())),
            pending_popup_webview: RefCell::new(None),
            new_webview_handler: RefCell::new(None),
            needs_paint: Cell::new(false),
            current_cursor: Cell::new(servo::Cursor::Default),
            latest_frame: Arc::new(Mutex::new(None)),
            current_url: RefCell::new(None),
            current_title: RefCell::new(None),
            status_text: RefCell::new(None),
            load_status: Cell::new(servo::LoadStatus::Started),
        });

        let delegate = Rc::new(WebViewBridge {
            state: Rc::clone(&delegate_state),
        });

        let initial_url = match config.content {
            Content::Url(s) => Url::parse(&s).ok(),
            Content::Html(html) => Url::parse(&format!("data:text/html;charset=utf-8,{html}")).ok(),
        };

        let mut builder = WebViewBuilder::new(runtime.servo(), runtime.rendering_context_dyn())
            .delegate(delegate)
            .hidpi_scale_factor(Scale::new(scale_factor));
        if let Some(url) = initial_url {
            builder = builder.url(url);
        }

        let webview = builder.build();
        *delegate_state.webview.borrow_mut() = Some(webview.clone());

        Ok(Self {
            inner: Rc::new(Inner {
                runtime: runtime.clone(),
                webview,
                delegate_state,
                pending_resize: Cell::new(None),
                scale_factor: Cell::new(scale_factor),
                size_request: SizeRequestSlot::new(),
            }),
        })
    }

    /// Evaluate a JavaScript snippet in the webview and deliver the
    /// result through a callback. Servo runs the script asynchronously
    /// (on the next `spin_event_loop` tick that pumps the script
    /// thread) and invokes `callback` on the main thread when the
    /// result is ready or an error occurs.
    ///
    /// Prefer [`evaluate_javascript_task`](Self::evaluate_javascript_task)
    /// for iced applications — it hands you back a `Task<Message>` you
    /// can return from `update()`, which is the ergonomic fit for the
    /// Elm-style model. Use this lower-level callback form when you
    /// need to drive side effects without going through the Message
    /// enum.
    pub fn evaluate_javascript(
        &self,
        script: impl ToString,
        callback: impl FnOnce(Result<JSValue, JavaScriptEvaluationError>) + 'static,
    ) {
        self.inner.webview.evaluate_javascript(script, callback);
    }

    /// Evaluate a JavaScript snippet and wrap the asynchronous result
    /// in an iced [`Task`]. Typical usage in an app's `update`:
    ///
    /// ```ignore
    /// Message::RunScript => controller
    ///     .evaluate_javascript_task("document.title")
    ///     .map(Message::ScriptResult),
    /// ```
    ///
    /// The task resolves once Servo has finished executing the script.
    /// If the oneshot channel bridging Servo's callback to the task is
    /// dropped (e.g. the controller is destroyed mid-eval), the task
    /// resolves to `Err(JavaScriptEvaluationError::InternalError)` — we
    /// reuse that variant rather than inventing a new one because an
    /// embedder drop genuinely looks like "Servo couldn't complete the
    /// evaluation" from the caller's point of view.
    pub fn evaluate_javascript_task(
        &self,
        script: impl ToString,
    ) -> Task<Result<JSValue, JavaScriptEvaluationError>> {
        let (tx, rx) = oneshot::channel();
        self.inner
            .webview
            .evaluate_javascript(script, move |result| {
                let _ = tx.send(result);
            });
        Task::future(async move {
            rx.await
                .unwrap_or(Err(JavaScriptEvaluationError::InternalError))
        })
    }

    /// Navigate the webview to a new URL. Returns an error if the URL
    /// fails to parse.
    pub fn navigate(&self, url: &str) -> Result<(), url::ParseError> {
        let parsed = Url::parse(url)?;
        self.inner.webview.load(parsed);
        Ok(())
    }

    /// Go back one step in Servo's session history.
    pub fn go_back(&self) {
        let _ = self.inner.webview.go_back(1);
    }

    /// Go forward one step in Servo's session history.
    pub fn go_forward(&self) {
        let _ = self.inner.webview.go_forward(1);
    }

    /// Reload the current page.
    pub fn reload(&self) {
        self.inner.webview.reload();
    }

    /// Whether Servo's session history has any back entries.
    pub fn can_go_back(&self) -> bool {
        self.inner.webview.can_go_back()
    }

    /// Whether Servo's session history has any forward entries.
    pub fn can_go_forward(&self) -> bool {
        self.inner.webview.can_go_forward()
    }

    /// Mark this webview as the one that should paint into the shared
    /// rendering context. Call it on the active tab whenever the user
    /// switches tabs; it focuses + shows this webview, blurs/hides any
    /// other webview on the same runtime, and forces a repaint so the
    /// shader widget picks up a fresh frame on the very next tick.
    pub fn activate(&self) {
        self.inner.webview.show();
        self.inner.webview.focus();
        self.inner.delegate_state.needs_paint.set(true);
    }

    /// Hide + blur this webview so it stops painting into the shared
    /// rendering context while another tab is active.
    pub fn deactivate(&self) {
        self.inner.webview.blur();
        self.inner.webview.hide();
    }

    /// Register a handler to receive URLs from `window.open` /
    /// `target="_blank"` navigations. Servo fires a `request_create_new`
    /// for these; we capture the URL the popup would have loaded and
    /// hand it to the embedder here. A typical app opens a new tab with
    /// a fresh `ServoWebViewController` loading the given URL. Only one
    /// handler can be registered at a time — calling this replaces the
    /// previous handler.
    pub fn on_new_webview_requested(&self, handler: impl Fn(Url) + 'static) {
        *self.inner.delegate_state.new_webview_handler.borrow_mut() = Some(Rc::new(handler));
    }

    /// Current page title, as reported by the most recent delegate callback.
    pub fn title(&self) -> Option<String> {
        self.inner.delegate_state.current_title.borrow().clone()
    }

    /// Current page URL, as reported by the most recent delegate callback.
    pub fn url(&self) -> Option<String> {
        self.inner
            .delegate_state
            .current_url
            .borrow()
            .as_ref()
            .map(|u| u.to_string())
    }

    /// Status text (e.g. the URL of a hovered link).
    pub fn status_text(&self) -> Option<String> {
        self.inner.delegate_state.status_text.borrow().clone()
    }

    /// Current page load status.
    pub fn load_status(&self) -> servo::LoadStatus {
        self.inner.delegate_state.load_status.get()
    }

    /// Current cached HiDPI scale factor.
    pub(crate) fn scale_factor(&self) -> f32 {
        self.inner.scale_factor.get()
    }

    /// Returns the webview handle for input forwarding.
    pub(crate) fn webview(&self) -> &WebView {
        &self.inner.webview
    }

    /// Pump the Servo event loop and, if a new frame became ready
    /// during the spin, paint and read it out into the shared frame
    /// buffer. The runtime's `spin_event_loop` drives *every* webview
    /// on the runtime, so calling `tick()` on one controller moves all
    /// of them forward — but only the controller on which `tick()` is
    /// called writes pixels into its own frame slot.
    ///
    /// Call this on every `Tick` produced by the app's subscription.
    pub fn tick(&self) {
        let inner = &self.inner;

        // Drain the most recent size request from the shader widget
        // and feed it to the debounced resize path.
        if let Some(wb) = inner.size_request.bounds() {
            let size = wb.size();
            let scale = wb.scale_factor;
            if (inner.scale_factor.get() - scale).abs() > f32::EPSILON {
                inner.scale_factor.set(scale);
                inner.webview.set_hidpi_scale_factor(Scale::new(scale));
            }
            match inner.pending_resize.get() {
                Some((existing, _)) if existing == size => {}
                _ => inner.pending_resize.set(Some((size, Instant::now()))),
            }
        }

        let rendering_context = inner.runtime.rendering_context();

        // Apply a pending resize *only* once the requested size has
        // been stable for `RESIZE_DEBOUNCE`. Drag-resize storms us with
        // a new size every frame; forwarding each one means Servo
        // never finishes laying out a single frame at the target size.
        if let Some((new_size, requested_at)) = inner.pending_resize.get() {
            let current = rendering_context.size();
            if new_size != current && requested_at.elapsed() >= RESIZE_DEBOUNCE {
                inner.pending_resize.set(None);
                // Only call `webview.resize` — Servo's `WebView::resize`
                // internally sends `resize_rendering_context` to its
                // paint thread, which resizes surfman itself. Calling
                // `rendering_context.resize` ourselves from the main
                // thread races with that and leaves WebRender in a bad
                // state (`notify_new_frame_ready` stops firing).
                inner.webview.resize(new_size);
            } else if new_size == current {
                inner.pending_resize.set(None);
            }
        }

        inner.runtime.servo().spin_event_loop();

        if inner.delegate_state.needs_paint.replace(false) {
            if let Err(e) = rendering_context.make_current() {
                error!("SoftwareRenderingContext::make_current failed: {e:?}");
                return;
            }
            inner.webview.paint();

            // Read BEFORE present. Surfman's `present` swaps buffers
            // with `PreserveBuffer::No`, leaving the new back buffer's
            // contents undefined; reading after present returns
            // garbage.
            let size = rendering_context.size();
            let rect = Box2D::new(
                Point2D::new(0, 0),
                Point2D::new(size.width as i32, size.height as i32),
            );
            if let Some(rgba) = rendering_context.read_to_image(rect) {
                let frame = Frame::new(rgba.into_raw(), size.width, size.height);
                *inner.delegate_state.latest_frame.lock().unwrap() = Some(frame);
            }

            rendering_context.present();
        }
    }
}

impl FrameSource for ServoWebViewController {
    fn frame_slot(&self) -> Arc<Mutex<Option<Frame>>> {
        Arc::clone(&self.inner.delegate_state.latest_frame)
    }

    fn size_request_slot(&self) -> SizeRequestSlot {
        self.inner.size_request.clone()
    }

    fn cursor(&self) -> iced::mouse::Interaction {
        crate::input::cursor_to_interaction(self.inner.delegate_state.current_cursor.get())
    }

    fn handle_event(
        &self,
        event: &iced::Event,
        bounds: iced::Rectangle,
        cursor: iced::mouse::Cursor,
        focused: bool,
    ) -> bool {
        crate::input::translate_event(event, bounds, cursor, focused, self)
    }
}

struct WakeSubData {
    wake_rx: Arc<Mutex<Option<UnboundedReceiver<()>>>>,
}

impl std::hash::Hash for WakeSubData {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (Arc::as_ptr(&self.wake_rx) as usize).hash(state);
    }
}

fn build_wake_drain(data: &WakeSubData) -> BoxStream<'static, ()> {
    let rx = data.wake_rx.lock().unwrap().take();
    match rx {
        Some(rx) => Box::pin(rx.map(|()| ())),
        None => Box::pin(futures::stream::pending()),
    }
}

/// Send + Sync wake bridge handed to Servo. Servo calls `wake()` from any
/// thread; we forward to an unbounded futures channel that the runtime's
/// subscription drains.
struct ChannelWaker {
    tx: UnboundedSender<()>,
}

impl EventLoopWaker for ChannelWaker {
    fn clone_box(&self) -> Box<dyn EventLoopWaker> {
        Box::new(ChannelWaker {
            tx: self.tx.clone(),
        })
    }

    fn wake(&self) {
        let _ = self.tx.unbounded_send(());
    }
}