vs-engine-webkit 0.2.15

Browser engine bindings for vibesurfer.
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
//! `VsNavDelegate` — a custom main-thread-only Obj-C class that
//! implements `WKNavigationDelegate`. Drops the navigation result into
//! a shared slot when the page finishes loading or fails.

use std::cell::{Cell, RefCell};
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Duration;

use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::{define_class, msg_send, DefinedClass, MainThreadMarker, MainThreadOnly, Message};
use objc2_foundation::{
    NSData, NSError, NSHTTPURLResponse, NSObject, NSObjectProtocol, NSRect, NSString, NSURLRequest,
    NSURLResponse, NSURL,
};
use objc2_web_kit::{
    WKDownload, WKDownloadDelegate, WKNavigation, WKNavigationAction, WKNavigationDelegate,
    WKNavigationResponse, WKNavigationResponsePolicy, WKUIDelegate, WKWebView,
    WKWebViewConfiguration, WKWindowFeatures,
};

use crate::backend::native_download::{
    is_attachment, name_from_disposition, temp_path, SharedDownloads, MAX_BYTES,
};

/// Slot shared between the delegate and the Rust caller. Both ends
/// live on the main thread, so `Rc<RefCell<...>>` is fine.
///
/// `started` / `committed` are recorded separately from `done` because
/// the web-process launch and the page load are different waits. A
/// cold `WKWebView` spends most of its first navigation spawning the
/// web-content process; folding that into the 15s load budget makes
/// `open` return `TIMEOUT` for a page that has not even started.
#[derive(Debug, Default)]
pub(super) struct NavState {
    pub started: bool,
    pub committed: bool,
    pub done: Option<Result<(), String>>,
}

impl NavState {
    pub(super) fn reset(&mut self) {
        *self = Self::default();
    }
}

pub(super) type NavSlot = Rc<RefCell<NavState>>;

pub(super) struct NavDelegateIvars {
    pub(super) slot: NavSlot,
    /// A new-window navigation WebKit asked for and we have not
    /// loaded yet. See `create_web_view`.
    pub(super) new_window: RefCell<Option<Retained<NSURLRequest>>>,
    /// Downloads the engine received, drained by `vs_download`.
    pub(super) downloads: SharedDownloads,
    /// In-flight `WKDownload`s: object address, buffer id, temp file.
    pub(super) in_flight: RefCell<Vec<(usize, u64, PathBuf)>>,
    /// The main-frame response just became a download. WebKit then
    /// fails the provisional navigation, which is not a failed load.
    pub(super) main_became_download: Cell<bool>,
}

fn ns_str(s: Option<Retained<NSString>>) -> String {
    s.map(|s| s.to_string()).unwrap_or_default()
}

fn response_url(r: &NSURLResponse) -> String {
    r.URL()
        .and_then(|u| u.absoluteString())
        .map(|s| s.to_string())
        .unwrap_or_default()
}

fn disposition(r: &NSURLResponse) -> String {
    let r: Retained<NSURLResponse> = r.retain();
    r.downcast::<NSHTTPURLResponse>()
        .ok()
        .and_then(|h| h.valueForHTTPHeaderField(&NSString::from_str("Content-Disposition")))
        .map(|s| s.to_string())
        .unwrap_or_default()
}

/// Whether a navigation response should be saved instead of shown:
/// the server marked it an attachment, or WebKit cannot render it.
/// `blob:` and `data:` saves come from page script and the shim
/// already has them.
fn wants_download(resp: &WKNavigationResponse) -> bool {
    let r = unsafe { resp.response() };
    let url = response_url(&r);
    if !(url.starts_with("http:") || url.starts_with("https:")) {
        return false;
    }
    is_attachment(&disposition(&r)) || !unsafe { resp.canShowMIMEType() }
}

define_class!(
    /// `WKNavigationDelegate` that signals page-load completion.
    #[unsafe(super = NSObject)]
    #[thread_kind = MainThreadOnly]
    #[name = "VsNavDelegate"]
    #[ivars = NavDelegateIvars]
    pub(super) struct NavDelegate;

    impl NavDelegate {
        #[unsafe(method(webView:didStartProvisionalNavigation:))]
        fn did_start(&self, _web_view: &WKWebView, _nav: Option<&WKNavigation>) {
            self.ivars().slot.borrow_mut().started = true;
        }

        #[unsafe(method(webView:didCommitNavigation:))]
        fn did_commit(&self, _web_view: &WKWebView, _nav: Option<&WKNavigation>) {
            let mut slot = self.ivars().slot.borrow_mut();
            slot.started = true;
            slot.committed = true;
        }

        #[unsafe(method(webView:didFinishNavigation:))]
        fn did_finish(&self, _web_view: &WKWebView, _nav: Option<&WKNavigation>) {
            let mut slot = self.ivars().slot.borrow_mut();
            slot.started = true;
            slot.committed = true;
            slot.done = Some(Ok(()));
        }

        #[unsafe(method(webView:didFailNavigation:withError:))]
        fn did_fail(
            &self,
            _web_view: &WKWebView,
            _nav: Option<&WKNavigation>,
            error: &NSError,
        ) {
            let msg = error.localizedDescription().to_string();
            self.ivars().slot.borrow_mut().done = Some(Err(msg));
        }

        #[unsafe(method(webView:didFailProvisionalNavigation:withError:))]
        fn did_fail_provisional(
            &self,
            _web_view: &WKWebView,
            _nav: Option<&WKNavigation>,
            error: &NSError,
        ) {
            // A main-frame response that became a download ends the
            // provisional load with "frame load interrupted". The page
            // is unchanged and the file is on its way: not a failure.
            if self.ivars().main_became_download.replace(false) {
                self.ivars().slot.borrow_mut().done = Some(Ok(()));
                return;
            }
            let msg = error.localizedDescription().to_string();
            self.ivars().slot.borrow_mut().done = Some(Err(msg));
        }

        /// Save attachments instead of dropping them.
        ///
        /// Without this method WebKit cancels any response it cannot
        /// render, and a `Content-Disposition: attachment` response is
        /// lost with no event: an export button, a form POST that
        /// answers with a file, a hidden-iframe download.
        #[unsafe(method(webView:decidePolicyForNavigationResponse:decisionHandler:))]
        fn decide_response(
            &self,
            _web_view: &WKWebView,
            resp: &WKNavigationResponse,
            handler: &block2::DynBlock<dyn Fn(WKNavigationResponsePolicy)>,
        ) {
            let policy = if wants_download(resp) {
                if unsafe { resp.isForMainFrame() } {
                    self.ivars().main_became_download.set(true);
                }
                WKNavigationResponsePolicy::Download
            } else {
                WKNavigationResponsePolicy::Allow
            };
            handler.call((policy,));
        }

        #[unsafe(method(webView:navigationResponse:didBecomeDownload:))]
        fn response_became_download(
            &self,
            _web_view: &WKWebView,
            resp: &WKNavigationResponse,
            download: &WKDownload,
        ) {
            let r = unsafe { resp.response() };
            let name = name_from_disposition(&disposition(&r))
                .unwrap_or_else(|| ns_str(r.suggestedFilename()));
            let id = match self.ivars().downloads.lock() {
                Ok(mut d) => d.begin(&response_url(&r), &name, &ns_str(r.MIMEType())),
                Err(_) => return,
            };
            let key = std::ptr::from_ref(download) as usize;
            self.ivars()
                .in_flight
                .borrow_mut()
                .push((key, id, temp_path(id)));
            unsafe { download.setDelegate(Some(ProtocolObject::from_ref(self))) };
        }

        /// Answer WebKit's request for the host window's frame.
        ///
        /// `window.outerWidth` / `outerHeight` / `screenX` / `screenY`
        /// are sourced from the UI client, not from the NSWindow. With
        /// no UI delegate installed WebKit had nothing to ask and the
        /// page saw a zero-sized outer window — which no real browser
        /// reports, and which gives nonsense to any responsive code
        /// deriving browser-chrome height from
        /// `outerHeight - innerHeight`.
        #[unsafe(method(_webView:getWindowFrameWithCompletionHandler:))]
        fn get_window_frame(
            &self,
            web_view: &WKWebView,
            handler: &block2::DynBlock<dyn Fn(NSRect)>,
        ) {
            let frame = web_view
                .window()
                .map_or_else(|| web_view.frame(), |w| w.frame());
            handler.call((frame,));
        }

        /// A `target="_blank"` link or form, or `window.open()`.
        ///
        /// A headless page has nowhere to put a second window, and
        /// without this method WebKit drops the navigation: the click
        /// does nothing and the agent sees an unchanged tree. Park the
        /// request instead; the input primitive that caused it loads it
        /// in this page and waits for it (see `follow_new_window`). A
        /// load started from inside this callback is dropped, because
        /// WebKit is still deciding the originating navigation. The
        /// request carries method and body, so a POSTed form (SAML and
        /// institution-login handoffs) arrives intact.
        #[unsafe(method(webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:))]
        fn create_web_view(
            &self,
            _web_view: &WKWebView,
            _config: &WKWebViewConfiguration,
            action: &WKNavigationAction,
            _features: &WKWindowFeatures,
        ) -> *mut WKWebView {
            let request = unsafe { action.request() };
            *self.ivars().new_window.borrow_mut() = Some(request);
            std::ptr::null_mut()
        }
    }

    unsafe impl NSObjectProtocol for NavDelegate {}
    unsafe impl WKNavigationDelegate for NavDelegate {}
    unsafe impl WKUIDelegate for NavDelegate {}

    unsafe impl WKDownloadDelegate for NavDelegate {
        #[unsafe(method(download:decideDestinationUsingResponse:suggestedFilename:completionHandler:))]
        fn decide_destination(
            &self,
            download: &WKDownload,
            response: &NSURLResponse,
            suggested: &NSString,
            handler: &block2::DynBlock<dyn Fn(*mut NSURL)>,
        ) {
            let Some((id, path)) = self.in_flight_entry(download) else {
                handler.call((std::ptr::null_mut(),));
                return;
            };
            if let Ok(mut d) = self.ivars().downloads.lock() {
                let header = name_from_disposition(&disposition(response));
                if header.is_none() {
                    d.set_name(id, &suggested.to_string());
                }
            }
            let expected = response.expectedContentLength();
            if u64::try_from(expected).is_ok_and(|n| n > MAX_BYTES) {
                self.settle(download, Err(format!(
                    "download exceeds {MAX_BYTES} bytes ({expected})"
                )));
                handler.call((std::ptr::null_mut(),));
                return;
            }
            let _ = std::fs::remove_file(&path);
            let url = NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()));
            handler.call((Retained::as_ptr(&url).cast_mut(),));
        }

        #[unsafe(method(downloadDidFinish:))]
        fn download_did_finish(&self, download: &WKDownload) {
            let Some((_, path)) = self.in_flight_entry(download) else {
                return;
            };
            let bytes = std::fs::read(&path).map_err(|e| format!("read {}: {e}", path.display()));
            let _ = std::fs::remove_file(&path);
            self.settle(download, bytes);
        }

        #[unsafe(method(download:didFailWithError:resumeData:))]
        fn download_did_fail(
            &self,
            download: &WKDownload,
            error: &NSError,
            _resume: Option<&NSData>,
        ) {
            if let Some((_, path)) = self.in_flight_entry(download) {
                let _ = std::fs::remove_file(&path);
            }
            self.settle(download, Err(error.localizedDescription().to_string()));
        }
    }
);

impl NavDelegate {
    pub(super) fn new(
        mtm: MainThreadMarker,
        slot: NavSlot,
        downloads: SharedDownloads,
    ) -> Retained<Self> {
        let this = Self::alloc(mtm).set_ivars(NavDelegateIvars {
            slot,
            new_window: RefCell::new(None),
            downloads,
            in_flight: RefCell::new(Vec::new()),
            main_became_download: Cell::new(false),
        });
        unsafe { msg_send![super(this), init] }
    }

    /// Shared navigation-result slot. Reused by `navigate` to await an
    /// in-place navigation on the same delegate.
    pub(super) fn slot(&self) -> NavSlot {
        self.ivars().slot.clone()
    }

    /// Downloads this page's responses turned into.
    pub(super) fn downloads(&self) -> SharedDownloads {
        self.ivars().downloads.clone()
    }

    /// Whether the page has asked for a new window we have not loaded.
    pub(super) fn has_new_window(&self) -> bool {
        self.ivars().new_window.borrow().is_some()
    }

    fn in_flight_entry(&self, download: &WKDownload) -> Option<(u64, PathBuf)> {
        let key = std::ptr::from_ref(download) as usize;
        self.ivars()
            .in_flight
            .borrow()
            .iter()
            .find(|(k, _, _)| *k == key)
            .map(|(_, id, p)| (*id, p.clone()))
    }

    /// Record the outcome and forget the `WKDownload`.
    fn settle(&self, download: &WKDownload, result: Result<Vec<u8>, String>) {
        let key = std::ptr::from_ref(download) as usize;
        let mut in_flight = self.ivars().in_flight.borrow_mut();
        let Some(pos) = in_flight.iter().position(|(k, _, _)| *k == key) else {
            return;
        };
        let (_, id, _) = in_flight.remove(pos);
        if let Ok(mut d) = self.ivars().downloads.lock() {
            d.finish(id, result);
        }
    }

    /// Take the parked new-window request, if the page asked for one.
    pub(super) fn take_new_window(&self) -> Option<Retained<NSURLRequest>> {
        self.ivars().new_window.borrow_mut().take()
    }
}

/// How long a web-content process may take to accept the first load.
///
/// This is not the page-load budget. A cold `WKWebView` (first open
/// after the daemon starts, or the first open after the process pool
/// has been starved) spends that time in XPC before `didStart` fires.
/// Charging it against the 15s load budget made `open` return
/// `! TIMEOUT 15000ms open` for a navigation that had not begun. 45s
/// is the allowance the integration harness already had to grant the
/// whole open for the same reason.
const LAUNCH_BUDGET: Duration = Duration::from_secs(45);

/// Wait until `slot` finishes, fails, or the load budget expires.
///
/// The load budget starts at provisional navigation, not at
/// `loadRequest`. If the document has committed and subresources then
/// hang, the page is usable and this returns `Ok` — a tracker that
/// never completes is not a failed open.
pub(super) fn wait_for_navigation(
    slot: &NavSlot,
    load_budget: Duration,
    primitive: &'static str,
) -> crate::engine::EngineResult<()> {
    use crate::engine::EngineError;

    use super::eval::run_loop_until;

    let launched = run_loop_until(
        || {
            let state = slot.borrow();
            state.started || state.committed || state.done.is_some()
        },
        LAUNCH_BUDGET,
    );
    if !launched {
        return Err(EngineError::Timeout {
            budget: LAUNCH_BUDGET,
            primitive,
            detail: "web process did not start",
        });
    }
    if let Some(result) = slot.borrow().done.clone() {
        return result.map_err(|msg| EngineError::Other(format!("navigation failed: {msg}")));
    }

    let _finished = run_loop_until(|| slot.borrow().done.is_some(), load_budget);
    let state = slot.borrow();
    match &state.done {
        Some(Ok(())) => Ok(()),
        Some(Err(msg)) => Err(EngineError::Other(format!("navigation failed: {msg}"))),
        // The main document is in. A subresource that never completes
        // (a tracker, an open websocket counted as a load) must not
        // fail the open — the page is readable.
        None if state.committed => Ok(()),
        None => Err(EngineError::Timeout {
            budget: load_budget,
            primitive,
            detail: "navigation did not finish",
        }),
    }
}