microsoft-webui-desktop 3.0.3

Configurable native desktop SDK for WebUI applications
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use block2::RcBlock;
#[cfg(feature = "local-server")]
use objc2::rc::Retained;
use objc2::rc::Weak;
use objc2::runtime::AnyObject;
use objc2::MainThreadOnly;
#[cfg(feature = "local-server")]
use objc2_foundation::NSTimer;
use objc2_foundation::{NSError, NSString};
use objc2_web_kit::{WKContentWorld, WKWebView};

use crate::ipc::{
    CommittedMainDocument, DocumentActivation, IpcBridge, IpcErrorCode, NativeControl, SessionInfo,
};
use crate::native_ipc::NativeIpcTasks;

use super::ipc_control::{bounded_string, nonce};
use super::ipc_wake::MainWake;
pub(super) use super::navigation::trusted_app_url as trusted_url;

pub(super) struct MacIpc {
    pub(super) bridge: IpcBridge,
    epoch: Cell<crate::document::DocumentEpoch>,
    pub(super) tasks: RefCell<Rc<NativeIpcTasks>>,
    pub(super) wake: std::sync::Arc<MainWake>,
    pub(super) proof: RefCell<Option<DocumentActivation>>,
    pub(super) hello_started: Cell<bool>,
    pub(super) session: RefCell<Option<SessionInfo>>,
    pending_controls: RefCell<Vec<NativeControl>>,
    webview: RefCell<Weak<WKWebView>>,
    #[cfg(feature = "local-server")]
    pub(super) local: Option<(crate::LoopbackOrigin, crate::HostLifetime)>,
    #[cfg(feature = "local-server")]
    pub(super) data: Rc<RefCell<crate::ipc::native_data::NativeData>>,
    #[cfg(feature = "local-server")]
    data_timer: RefCell<Option<Retained<NSTimer>>>,
}

impl MacIpc {
    pub(super) fn new(bridge: IpcBridge) -> Rc<Self> {
        Self::new_with_local(bridge, None)
    }

    #[cfg(feature = "local-server")]
    pub(super) fn new_local(
        bridge: IpcBridge,
        origin: crate::LoopbackOrigin,
        lifetime: crate::HostLifetime,
    ) -> Rc<Self> {
        Self::new_with_local(bridge, Some((origin, lifetime)))
    }

    fn new_with_local(
        bridge: IpcBridge,
        #[cfg(feature = "local-server")] local: Option<(
            crate::LoopbackOrigin,
            crate::HostLifetime,
        )>,
        #[cfg(not(feature = "local-server"))] _local: Option<()>,
    ) -> Rc<Self> {
        let wake = MainWake::new();
        let state = Rc::new(Self {
            bridge,
            epoch: Cell::default(),
            tasks: RefCell::new(Rc::new(NativeIpcTasks::new(wake.clone(), 128))),
            wake,
            proof: RefCell::new(None),
            hello_started: Cell::new(false),
            session: RefCell::new(None),
            pending_controls: RefCell::new(Vec::new()),
            webview: RefCell::new(Weak::default()),
            #[cfg(feature = "local-server")]
            local,
            #[cfg(feature = "local-server")]
            data: Rc::new(RefCell::new(Default::default())),
            #[cfg(feature = "local-server")]
            data_timer: RefCell::new(None),
        });
        state.wake.attach(&state);
        if state.bridge.attach_waker(state.wake.clone()).is_err() {
            state.close();
        }
        state
    }

    pub(super) fn attach(&self, webview: &WKWebView) {
        *self.webview.borrow_mut() = Weak::new(webview);
    }

    pub(super) fn is_current(&self, navigation: u64) -> bool {
        self.epoch.get().current(navigation)
    }

    pub(super) fn navigation(&self) -> u64 {
        self.epoch.get().navigation
    }

    pub(super) fn accepts_proof(&self, proof: &DocumentActivation) -> bool {
        self.epoch
            .get()
            .accepts(self.proof.borrow().as_ref(), proof)
    }

    #[cfg(test)]
    pub(super) fn commit_for_test(&self) {
        self.navigate();
        let mut epoch = self.epoch.get();
        epoch.commit();
        self.epoch.set(epoch);
    }

    pub(super) fn navigate(&self) {
        let mut epoch = self.epoch.get();
        if epoch.closed {
            return;
        }
        let Some(next) = epoch.advance() else {
            self.close();
            return;
        };
        self.epoch.set(epoch);
        self.proof.borrow_mut().take();
        self.hello_started.set(false);
        self.session.borrow_mut().take();
        self.pending_controls.borrow_mut().clear();
        #[cfg(feature = "local-server")]
        self.clear_data();
        self.bridge.navigate(next);
        let old = self
            .tasks
            .replace(Rc::new(NativeIpcTasks::new(self.wake.clone(), 128)));
        old.close();
    }

    pub(super) fn navigation_started(&self) {
        self.navigation_started_with(|proof, generation| {
            let control = NativeControl::Closed {
                generation,
                code: IpcErrorCode::Navigated,
            };
            let script = match crate::native_ipc::control_script(&proof, control) {
                Ok(script) => script,
                Err(error) => {
                    eprintln!("WebUI: could not encode document retirement control: {error}");
                    return;
                }
            };
            let webview = self.webview.borrow().load();
            if let Some(webview) = webview {
                evaluate(&webview, &script, None);
            }
        });
    }

    pub(super) fn retire_before_history_navigation(
        self: &Rc<Self>,
        webview: &WKWebView,
        decision: &block2::DynBlock<dyn Fn(objc2_web_kit::WKNavigationActionPolicy)>,
    ) -> bool {
        let outgoing = self.proof.borrow().clone().zip(
            self.session
                .borrow()
                .as_ref()
                .map(|session| session.generation),
        );
        let Some((proof, generation)) =
            outgoing.filter(|(proof, _)| self.is_current(proof.navigation))
        else {
            return false;
        };
        let script = match crate::native_ipc::control_script(
            &proof,
            NativeControl::Closed {
                generation,
                code: IpcErrorCode::Navigated,
            },
        ) {
            Ok(script) => script,
            Err(error) => {
                eprintln!("WebUI: could not encode history retirement control: {error}");
                return false;
            }
        };
        // History traversal can abort Fetch before didStartProvisionalNavigation
        // or pagehide. Queue the outgoing realm's terminal control and revoke
        // native ownership before yielding to its asynchronous completion.
        // WebKit may traverse only after that control has been evaluated.
        // Same-document traversals do not enter this native history policy path.
        let decision = decision.copy();
        let weak = Rc::downgrade(self);
        let retired_epoch = proof.navigation.checked_add(1);
        let callback = RcBlock::new(move |_: *mut AnyObject, error: *mut NSError| {
            let current = weak.upgrade().is_some_and(|state| {
                let epoch = state.epoch.get();
                !epoch.closed && Some(epoch.navigation) == retired_epoch
            });
            let policy = if error.is_null() && current {
                objc2_web_kit::WKNavigationActionPolicy::Allow
            } else {
                eprintln!("WebUI: could not deliver history retirement; navigation cancelled");
                objc2_web_kit::WKNavigationActionPolicy::Cancel
            };
            decision.call((policy,));
        });
        evaluate(webview, &script, Some(&callback));
        self.navigate();
        true
    }

    fn navigation_started_with(&self, publish: impl FnOnce(DocumentActivation, u64)) {
        let outgoing = self.proof.borrow().clone().zip(
            self.session
                .borrow()
                .as_ref()
                .map(|session| session.generation),
        );
        if let Some((proof, generation)) =
            outgoing.filter(|(proof, _)| self.is_current(proof.navigation))
        {
            // Queue the outgoing document's nonce-bound terminal control before
            // clearing its identity or cancelling native response completions.
            // A late evaluation cannot target the replacement's fresh nonce.
            publish(proof, generation);
        }
        self.navigate();
    }

    pub(super) fn committed(self: &Rc<Self>, webview: &WKWebView) {
        let mut epoch = self.epoch.get();
        let Some(navigation) = epoch.commit() else {
            return;
        };
        self.epoch.set(epoch);
        if !self.trusted_webview(webview) {
            self.fail_document();
            return;
        }
        let weak = Rc::downgrade(self);
        let callback = RcBlock::new(move |result: *mut AnyObject, error: *mut NSError| {
            let Some(state) = weak.upgrade().filter(|state| state.is_current(navigation)) else {
                return;
            };
            if !error.is_null() {
                state.fail_document();
                return;
            }
            // SAFETY: WebKit supplies a live result for the duration of this callback.
            let value = unsafe { result.as_ref() };
            let nonce = value
                .and_then(|value| value.downcast_ref::<NSString>())
                .and_then(|text| bounded_string(text, 32))
                .and_then(|value| nonce(&value));
            let Some(nonce) = nonce else {
                state.fail_document();
                return;
            };
            state.activate(navigation, nonce);
        });
        evaluate(
            webview,
            "window.__webuiDesktopIpcV2?.documentNonce",
            Some(&callback),
        );
    }

    fn activate(self: &Rc<Self>, navigation: u64, nonce: [u8; 16]) {
        let Some(webview) = self.webview.borrow().load() else {
            return;
        };
        if !self.is_current(navigation) || !self.trusted_webview(&webview) {
            return;
        }
        let identity = CommittedMainDocument {
            navigation,
            origin: self.origin().into(),
        };
        let Ok(proof) = self.bridge.begin_document(identity, nonce) else {
            self.fail_document();
            return;
        };
        let Ok(script) = (if self.is_local() {
            crate::native_ipc::activation_script_local(&proof)
        } else {
            crate::native_ipc::activation_script(&proof)
        }) else {
            self.fail_document();
            return;
        };
        *self.proof.borrow_mut() = Some(proof);
        let weak = Rc::downgrade(self);
        let callback = RcBlock::new(move |result: *mut AnyObject, error: *mut NSError| {
            let Some(state) = weak.upgrade().filter(|state| state.is_current(navigation)) else {
                return;
            };
            // SAFETY: WebKit owns the result throughout this completion callback.
            let accepted = unsafe { result.as_ref() }
                .and_then(|value| value.downcast_ref::<objc2_foundation::NSNumber>())
                .is_some_and(|value| value.boolValue());
            if !error.is_null() || !accepted {
                state.fail_document();
            }
        });
        evaluate(&webview, &script, Some(&callback));
    }

    pub(super) fn fail_document(&self) {
        // A failed/cancelled activation cannot be retried in the surviving
        // document. Revocation also cancels completions before native delivery.
        self.navigate();
    }

    pub(super) fn disconnected(&self, generation: u64) {
        if !self
            .session
            .borrow()
            .as_ref()
            .is_some_and(|session| session.generation == generation)
        {
            return;
        }
        self.session.borrow_mut().take();
        self.pending_controls.borrow_mut().clear();
        #[cfg(feature = "local-server")]
        self.clear_data();
        let previous = self
            .tasks
            .replace(Rc::new(NativeIpcTasks::new(self.wake.clone(), 128)));
        previous.close();
    }

    pub(super) fn drain(&self) {
        let tasks = Rc::clone(&self.tasks.borrow());
        tasks.poll_ready();
        while let Ok(Some(control)) = self.bridge.take_control() {
            if self.session.borrow().is_none() {
                let mut pending = self.pending_controls.borrow_mut();
                if pending.len() == 2 {
                    pending.remove(0);
                }
                pending.push(control);
            } else {
                self.push_control(control);
            }
        }
        if self.session.borrow().is_some() {
            let controls = std::mem::take(&mut *self.pending_controls.borrow_mut());
            for control in controls {
                self.push_control(control);
            }
        }
    }

    fn push_control(&self, control: NativeControl) {
        let closed = matches!(control, NativeControl::Closed { .. });
        let generation = match &control {
            NativeControl::Ready { generation } | NativeControl::Closed { generation, .. } => {
                *generation
            }
        };
        if !self
            .session
            .borrow()
            .as_ref()
            .is_some_and(|session| session.generation == generation)
        {
            return;
        }
        let proof = self.proof.borrow().clone();
        let Some(proof) = proof.filter(|proof| self.is_current(proof.navigation)) else {
            return;
        };
        let Some(webview) = self.webview.borrow().load() else {
            return;
        };
        let Ok(script) = crate::native_ipc::control_script(&proof, control) else {
            return;
        };
        evaluate(&webview, &script, None);
        if closed {
            self.disconnected(generation);
        }
    }

    pub(super) fn close(&self) {
        let mut epoch = self.epoch.get();
        if epoch.closed {
            return;
        }
        epoch.closed = true;
        self.epoch.set(epoch);
        self.wake.close();
        self.bridge.close();
        let tasks = Rc::clone(&self.tasks.borrow());
        tasks.close();
        self.proof.borrow_mut().take();
        self.session.borrow_mut().take();
        self.pending_controls.borrow_mut().clear();
        #[cfg(feature = "local-server")]
        self.clear_data();
    }

    pub(super) fn is_local(&self) -> bool {
        #[cfg(feature = "local-server")]
        {
            self.local.is_some()
        }
        #[cfg(not(feature = "local-server"))]
        {
            false
        }
    }

    pub(super) fn origin(&self) -> &str {
        #[cfg(feature = "local-server")]
        if let Some((origin, _)) = &self.local {
            return origin.as_str();
        }
        super::APP_ORIGIN
    }

    pub(super) fn trusted_webview(&self, webview: &WKWebView) -> bool {
        // SAFETY: Current URL is read on the owning WebKit main thread.
        let Some(url) = (unsafe { webview.URL() }) else {
            return false;
        };
        #[cfg(feature = "local-server")]
        if let Some((origin, lifetime)) = &self.local {
            return lifetime.is_active()
                && url
                    .absoluteString()
                    .is_some_and(|value| origin.allows(&value.to_string()))
                && url.user().is_none()
                && url.password().is_none();
        }

        trusted_url(&url)
    }

    pub(super) fn trusted_current_webview(&self) -> bool {
        self.webview
            .borrow()
            .load()
            .is_some_and(|view| self.trusted_webview(&view))
    }

    pub(super) fn trusted_frame(
        &self,
        scheme: Option<&str>,
        host: Option<&str>,
        port: isize,
    ) -> bool {
        #[cfg(feature = "local-server")]
        if let Some((origin, lifetime)) = &self.local {
            return lifetime.is_active()
                && scheme.zip(host).is_some_and(|(scheme, host)| {
                    origin.matches_security_origin(scheme, host, port)
                });
        }
        scheme == Some("webui") && host == Some("app") && port == 0
    }

    #[cfg(feature = "local-server")]
    fn clear_data(&self) {
        if let Some(timer) = self.data_timer.borrow_mut().take() {
            timer.invalidate();
        }
        self.data.borrow_mut().reset();
    }

    /// At most one native run-loop timer per local document, rearmed only when
    /// a cursor is retained. Invalidation on navigation prevents stale timers
    /// from touching a replacement document.
    #[cfg(feature = "local-server")]
    pub(super) fn arm_data_deadline(self: &Rc<Self>) {
        if let Some(timer) = self.data_timer.borrow_mut().take() {
            timer.invalidate();
        }
        let Some(until) = self.data.borrow().next_deadline() else {
            return;
        };
        let interval = until
            .saturating_duration_since(std::time::Instant::now())
            .as_secs_f64()
            .max(0.001);
        let navigation = self.navigation();
        let weak = Rc::downgrade(self);
        let block = RcBlock::new(move |_: std::ptr::NonNull<NSTimer>| {
            let Some(state) = weak.upgrade().filter(|state| state.is_current(navigation)) else {
                return;
            };
            state.data.borrow_mut().expire(std::time::Instant::now());
            state.arm_data_deadline();
        });
        // SAFETY: This callback and its timer are created/invalidated only on
        // WebKit's main run loop; it captures a weak document owner.
        let timer = unsafe {
            NSTimer::scheduledTimerWithTimeInterval_repeats_block(interval, false, &block)
        };
        *self.data_timer.borrow_mut() = Some(timer);
    }
}

impl Drop for MacIpc {
    fn drop(&mut self) {
        self.close();
    }
}

fn evaluate(
    webview: &WKWebView,
    script: &str,
    completion: Option<&block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSError)>>,
) {
    // SAFETY: nil frame explicitly targets the current main frame; pageWorld
    // contains the document-start bootstrap. Completion blocks are copied by WebKit.
    unsafe {
        webview.evaluateJavaScript_inFrame_inContentWorld_completionHandler(
            &NSString::from_str(script),
            None,
            &WKContentWorld::pageWorld(webview.mtm()),
            completion,
        );
    }
}

#[cfg(test)]
#[allow(clippy::disallowed_methods)]
#[path = "ipc_tests.rs"]
mod tests;