windows-webview 0.100.0

Windows WebView2 library
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
use super::*;

/// Details about a completed navigation, delivered to a
/// [`WebView::on_navigation_completed`] handler.
pub struct NavigationCompletedArgs(pub(crate) ICoreWebView2NavigationCompletedEventArgs);

impl NavigationCompletedArgs {
    /// Returns `true` if the navigation succeeded.
    pub fn is_success(&self) -> bool {
        unsafe { self.0.IsSuccess() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns the unique identifier for the navigation.
    pub fn navigation_id(&self) -> u64 {
        unsafe { self.0.NavigationId() }.unwrap_or(0)
    }
}

/// Details about a navigation that is about to start.
pub struct NavigationStartingArgs(pub(crate) ICoreWebView2NavigationStartingEventArgs);

impl NavigationStartingArgs {
    /// Returns the URI the navigation is targeting.
    pub fn uri(&self) -> String {
        unsafe { string::take_result(self.0.Uri()) }
    }

    /// Returns `true` if the navigation was initiated by the user (for example a
    /// link click) rather than by script.
    pub fn is_user_initiated(&self) -> bool {
        unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns `true` if the navigation is a redirect.
    pub fn is_redirected(&self) -> bool {
        unsafe { self.0.IsRedirected() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns the unique identifier for the navigation.
    pub fn navigation_id(&self) -> u64 {
        unsafe { self.0.NavigationId() }.unwrap_or(0)
    }

    /// Returns `true` if the navigation is currently marked to be canceled.
    pub fn is_cancelled(&self) -> bool {
        unsafe { self.0.Cancel() }.is_ok_and(|value| value.as_bool())
    }

    /// Sets whether the navigation is canceled.
    pub fn set_cancel(&self, cancel: bool) -> Result<()> {
        unsafe { self.0.SetCancel(cancel) }.ok()
    }
}

/// A message posted from the hosted page's JavaScript, delivered to a
/// [`WebView::on_web_message_received`] handler.
pub struct WebMessageReceivedArgs(pub(crate) ICoreWebView2WebMessageReceivedEventArgs);

impl WebMessageReceivedArgs {
    /// Returns the URI of the document that sent the message.
    pub fn source(&self) -> String {
        unsafe { string::take_result(self.0.Source()) }
    }

    /// Returns the message serialized as a JSON string. Messages sent with
    /// `window.chrome.webview.postMessage` arrive here regardless of type.
    pub fn web_message_as_json(&self) -> String {
        unsafe { string::take_result(self.0.WebMessageAsJson()) }
    }

    /// Returns the message as a string. Fails if the page posted a value that is
    /// not a JavaScript string.
    pub fn try_web_message_as_string(&self) -> Result<String> {
        let value = unsafe { self.0.TryGetWebMessageAsString()? };
        Ok(unsafe { string::take(value) })
    }
}

/// Details about content that is starting to load, delivered to a
/// [`WebView::on_content_loading`] handler.
pub struct ContentLoadingArgs(pub(crate) ICoreWebView2ContentLoadingEventArgs);

impl ContentLoadingArgs {
    /// Returns `true` if the loading content is the error page.
    pub fn is_error_page(&self) -> bool {
        unsafe { self.0.IsErrorPage() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns the unique identifier for the navigation.
    pub fn navigation_id(&self) -> u64 {
        unsafe { self.0.NavigationId() }.unwrap_or(0)
    }
}

/// Details about a page requesting to open a new window.
pub struct NewWindowRequestedArgs(pub(crate) ICoreWebView2NewWindowRequestedEventArgs);

impl NewWindowRequestedArgs {
    /// Returns the URI the new window would navigate to.
    pub fn uri(&self) -> String {
        unsafe { string::take_result(self.0.Uri()) }
    }

    /// Returns `true` if the request was initiated by the user rather than by
    /// script.
    pub fn is_user_initiated(&self) -> bool {
        unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns `true` if the request has been marked as handled.
    pub fn is_handled(&self) -> bool {
        unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
    }

    /// Marks the request as handled, suppressing the creation of a default new
    /// window. Set this (without providing a [new window](Self::set_new_window))
    /// to block the popup entirely.
    pub fn set_handled(&self, handled: bool) -> Result<()> {
        unsafe { self.0.SetHandled(handled) }.ok()
    }

    /// Provides an existing [`WebView`] to host the requested content instead of
    /// creating a new window.
    pub fn set_new_window(&self, webview: &WebView) -> Result<()> {
        unsafe { self.0.SetNewWindow(&webview.0) }.ok()
    }

    /// Takes a [`Deferral`] so the request can be resolved after the handler
    /// returns, for example once an asynchronously created window is ready.
    pub fn defer(&self) -> Result<Deferral> {
        Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
    }
}

/// The kind of capability a page is requesting in a
/// [`PermissionRequestedArgs`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PermissionKind {
    /// A permission kind not represented by the other variants.
    Unknown,
    /// Access to the microphone.
    Microphone,
    /// Access to the camera.
    Camera,
    /// Access to the device location.
    Geolocation,
    /// Permission to show notifications.
    Notifications,
    /// Access to device sensors other than location.
    OtherSensors,
    /// Permission to read the clipboard.
    ClipboardRead,
    /// Permission to start multiple downloads automatically.
    MultipleAutomaticDownloads,
    /// Access to files on the device.
    FileReadWrite,
    /// Permission to play media without user interaction.
    Autoplay,
    /// Access to locally installed fonts.
    LocalFonts,
    /// Access to system-exclusive MIDI messages.
    MidiSystemExclusiveMessages,
    /// Permission to inspect and place windows on screens.
    WindowManagement,
}

impl PermissionKind {
    fn from_raw(value: COREWEBVIEW2_PERMISSION_KIND) -> Self {
        match value {
            1 => Self::Microphone,
            2 => Self::Camera,
            3 => Self::Geolocation,
            4 => Self::Notifications,
            5 => Self::OtherSensors,
            6 => Self::ClipboardRead,
            7 => Self::MultipleAutomaticDownloads,
            8 => Self::FileReadWrite,
            9 => Self::Autoplay,
            10 => Self::LocalFonts,
            11 => Self::MidiSystemExclusiveMessages,
            12 => Self::WindowManagement,
            _ => Self::Unknown,
        }
    }
}

/// How a [`PermissionRequestedArgs`] should be resolved.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PermissionState {
    /// Defer to the browser default (typically prompting the user).
    Default,
    /// Grant the permission.
    Allow,
    /// Deny the permission.
    Deny,
}

impl PermissionState {
    fn from_raw(value: COREWEBVIEW2_PERMISSION_STATE) -> Self {
        match value {
            1 => Self::Allow,
            2 => Self::Deny,
            _ => Self::Default,
        }
    }

    fn to_raw(self) -> COREWEBVIEW2_PERMISSION_STATE {
        match self {
            Self::Default => 0,
            Self::Allow => 1,
            Self::Deny => 2,
        }
    }
}

/// Details about a permission a page is requesting (for example camera or
/// geolocation access), delivered to a [`WebView::on_permission_requested`]
/// handler. Decide the outcome with [`set_state`](Self::set_state).
pub struct PermissionRequestedArgs(pub(crate) ICoreWebView2PermissionRequestedEventArgs);

impl PermissionRequestedArgs {
    /// Returns the URI of the page requesting the permission.
    pub fn uri(&self) -> String {
        unsafe { string::take_result(self.0.Uri()) }
    }

    /// Returns the kind of permission being requested.
    pub fn kind(&self) -> PermissionKind {
        unsafe { self.0.PermissionKind() }.map_or(PermissionKind::Unknown, PermissionKind::from_raw)
    }

    /// Returns `true` if the request was initiated by the user rather than by
    /// script.
    pub fn is_user_initiated(&self) -> bool {
        unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
    }

    /// Returns the current resolution state of the request.
    pub fn state(&self) -> PermissionState {
        unsafe { self.0.State() }.map_or(PermissionState::Default, PermissionState::from_raw)
    }

    /// Sets how the request should be resolved.
    pub fn set_state(&self, state: PermissionState) -> Result<()> {
        unsafe { self.0.SetState(state.to_raw()) }.ok()
    }

    /// Takes a [`Deferral`] so the request can be resolved after the handler
    /// returns, for example once the user has answered a prompt.
    pub fn defer(&self) -> Result<Deferral> {
        Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
    }
}

/// The kind of process that failed, reported by [`ProcessFailedArgs::kind`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProcessFailedKind {
    /// A process failure kind not represented by the other variants.
    Unknown,
    /// The browser process ended unexpectedly; the `WebView` is no longer usable.
    BrowserProcessExited,
    /// A render process ended unexpectedly (a renderer crash); the page is gone
    /// but the `WebView` can be reloaded.
    RenderProcessExited,
    /// A render process is unresponsive (hung).
    RenderProcessUnresponsive,
    /// A frame's render process ended unexpectedly.
    FrameRenderProcessExited,
    /// A utility process ended unexpectedly.
    UtilityProcessExited,
    /// A sandbox helper process ended unexpectedly.
    SandboxHelperProcessExited,
    /// The GPU process ended unexpectedly.
    GpuProcessExited,
    /// A PPAPI plugin process ended unexpectedly.
    PpapiPluginProcessExited,
    /// A PPAPI broker process ended unexpectedly.
    PpapiBrokerProcessExited,
    /// An unrecognized process ended unexpectedly.
    UnknownProcessExited,
}

impl ProcessFailedKind {
    fn from_raw(value: COREWEBVIEW2_PROCESS_FAILED_KIND) -> Self {
        match value {
            0 => Self::BrowserProcessExited,
            1 => Self::RenderProcessExited,
            2 => Self::RenderProcessUnresponsive,
            3 => Self::FrameRenderProcessExited,
            4 => Self::UtilityProcessExited,
            5 => Self::SandboxHelperProcessExited,
            6 => Self::GpuProcessExited,
            7 => Self::PpapiPluginProcessExited,
            8 => Self::PpapiBrokerProcessExited,
            9 => Self::UnknownProcessExited,
            _ => Self::Unknown,
        }
    }
}

/// Details about a failed or unresponsive browser process, delivered to a
/// [`WebView::on_process_failed`] handler.
pub struct ProcessFailedArgs(pub(crate) ICoreWebView2ProcessFailedEventArgs);

impl ProcessFailedArgs {
    /// Returns which kind of process failed.
    pub fn kind(&self) -> ProcessFailedKind {
        unsafe { self.0.ProcessFailedKind() }
            .map_or(ProcessFailedKind::Unknown, ProcessFailedKind::from_raw)
    }
}

/// The reason focus is moving, reported by [`MoveFocusRequestedArgs::reason`]
/// and passed to [`Controller::move_focus`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MoveFocusReason {
    /// Focus is being set programmatically.
    Programmatic,
    /// Focus is moving forward (for example Tab).
    Next,
    /// Focus is moving backward (for example Shift+Tab).
    Previous,
}

impl MoveFocusReason {
    fn from_raw(value: COREWEBVIEW2_MOVE_FOCUS_REASON) -> Self {
        match value {
            1 => Self::Next,
            2 => Self::Previous,
            _ => Self::Programmatic,
        }
    }

    pub(crate) fn to_raw(self) -> COREWEBVIEW2_MOVE_FOCUS_REASON {
        match self {
            Self::Programmatic => 0,
            Self::Next => 1,
            Self::Previous => 2,
        }
    }
}

/// A request to move focus out of the browser.
pub struct MoveFocusRequestedArgs(pub(crate) ICoreWebView2MoveFocusRequestedEventArgs);

impl MoveFocusRequestedArgs {
    /// Returns the direction focus is moving.
    pub fn reason(&self) -> MoveFocusReason {
        unsafe { self.0.Reason() }.map_or(MoveFocusReason::Programmatic, MoveFocusReason::from_raw)
    }

    /// Returns `true` if the request has been marked as handled.
    pub fn is_handled(&self) -> bool {
        unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
    }

    /// Marks the request as handled, indicating the host moved focus itself and
    /// WebView2 should not apply its default focus behavior.
    pub fn set_handled(&self, handled: bool) -> Result<()> {
        unsafe { self.0.SetHandled(handled) }.ok()
    }
}

/// The kind of key event reported by [`AcceleratorKeyPressedArgs::key_event_kind`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyEventKind {
    /// A key was pressed.
    KeyDown,
    /// A key was released.
    KeyUp,
    /// A system key was pressed (for example a key combined with Alt).
    SystemKeyDown,
    /// A system key was released.
    SystemKeyUp,
}

impl KeyEventKind {
    fn from_raw(value: COREWEBVIEW2_KEY_EVENT_KIND) -> Self {
        match value {
            1 => Self::KeyUp,
            2 => Self::SystemKeyDown,
            3 => Self::SystemKeyUp,
            _ => Self::KeyDown,
        }
    }
}

/// A browser-level key press delivered before the page sees it.
pub struct AcceleratorKeyPressedArgs(pub(crate) ICoreWebView2AcceleratorKeyPressedEventArgs);

impl AcceleratorKeyPressedArgs {
    /// Returns whether the key was pressed or released, and whether it is a
    /// system key.
    pub fn key_event_kind(&self) -> KeyEventKind {
        unsafe { self.0.KeyEventKind() }.map_or(KeyEventKind::KeyDown, KeyEventKind::from_raw)
    }

    /// Returns the Win32 virtual-key code of the key.
    pub fn virtual_key(&self) -> u32 {
        unsafe { self.0.VirtualKey() }.unwrap_or(0)
    }

    /// Returns `true` if the key has been marked as handled.
    pub fn is_handled(&self) -> bool {
        unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
    }

    /// Marks the key as handled, preventing WebView2's default processing so the
    /// host can act on the shortcut itself.
    pub fn set_handled(&self, handled: bool) -> Result<()> {
        unsafe { self.0.SetHandled(handled) }.ok()
    }
}

/// A Chrome DevTools Protocol event, delivered to a
/// [`WebView::on_dev_tools_protocol_event`] handler. The event's parameters are
/// available as a JSON object string.
pub struct DevToolsProtocolEventReceivedArgs(
    pub(crate) ICoreWebView2DevToolsProtocolEventReceivedEventArgs,
);

impl DevToolsProtocolEventReceivedArgs {
    /// Returns the event's parameter object as a JSON string, matching the
    /// `params` of the corresponding CDP event.
    pub fn parameter_object_as_json(&self) -> String {
        unsafe { string::take_result(self.0.ParameterObjectAsJson()) }
    }
}

/// An event subscription that unsubscribes when dropped.
#[must_use]
pub struct EventRegistration(Option<Box<dyn FnOnce()>>);

impl EventRegistration {
    pub(crate) fn new<F: FnOnce() + 'static>(remove: F) -> Self {
        Self(Some(Box::new(remove)))
    }

    /// Unsubscribes the handler.
    pub fn remove(mut self) {
        if let Some(remove) = self.0.take() {
            remove();
        }
    }
}

impl Drop for EventRegistration {
    fn drop(&mut self) {
        if let Some(remove) = self.0.take() {
            remove();
        }
    }
}