Skip to main content

windows_webview/
deferral.rs

1use super::*;
2
3/// Keeps an event request pending so it can be resolved after the handler
4/// returns. Obtain one from an event's `defer` method (for example
5/// [`PermissionRequestedArgs::defer`]) and call [`complete`](Self::complete)
6/// once the decision has been made. Dropping the deferral completes it
7/// automatically.
8#[must_use]
9pub struct Deferral(Option<ICoreWebView2Deferral>);
10
11impl Deferral {
12    pub(crate) fn new(deferral: ICoreWebView2Deferral) -> Self {
13        Self(Some(deferral))
14    }
15
16    /// Completes the deferred request, allowing WebView2 to proceed with the
17    /// state set on the originating event args.
18    pub fn complete(mut self) -> Result<()> {
19        match self.0.take() {
20            Some(deferral) => unsafe { deferral.Complete() }.ok(),
21            None => Ok(()),
22        }
23    }
24}
25
26impl Drop for Deferral {
27    fn drop(&mut self) {
28        if let Some(deferral) = self.0.take() {
29            let _ = unsafe { deferral.Complete() };
30        }
31    }
32}