Skip to main content

windows_webview/
profile.rs

1use super::*;
2
3/// The color scheme a [`Profile`] reports to pages through the
4/// `prefers-color-scheme` media query.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum PreferredColorScheme {
7    /// Follow the operating system's setting.
8    Auto,
9    Light,
10    Dark,
11}
12
13impl PreferredColorScheme {
14    fn from_raw(value: COREWEBVIEW2_PREFERRED_COLOR_SCHEME) -> Self {
15        match value {
16            1 => Self::Light,
17            2 => Self::Dark,
18            _ => Self::Auto,
19        }
20    }
21
22    fn to_raw(self) -> COREWEBVIEW2_PREFERRED_COLOR_SCHEME {
23        match self {
24            Self::Auto => 0,
25            Self::Light => 1,
26            Self::Dark => 2,
27        }
28    }
29}
30
31/// A WebView2 browser profile - the on-disk identity (name, path, private mode)
32/// shared by every `WebView` created with it, plus its color scheme, download
33/// folder, and browsing-data controls. Obtain it with [`WebView::profile`].
34pub struct Profile(pub(crate) ICoreWebView2Profile);
35
36impl Profile {
37    /// Returns the profile's name.
38    pub fn name(&self) -> String {
39        unsafe { string::take_result(self.0.ProfileName()) }
40    }
41
42    /// Returns `true` if the profile runs in private (incognito) mode.
43    pub fn is_in_private_mode(&self) -> bool {
44        unsafe { self.0.IsInPrivateModeEnabled() }.is_ok_and(|value| value.as_bool())
45    }
46
47    /// Returns the absolute path of the profile's on-disk directory.
48    pub fn path(&self) -> String {
49        unsafe { string::take_result(self.0.ProfilePath()) }
50    }
51
52    /// Returns the folder downloads are saved to by default.
53    pub fn default_download_folder_path(&self) -> String {
54        unsafe { string::take_result(self.0.DefaultDownloadFolderPath()) }
55    }
56
57    /// Sets the folder downloads are saved to by default.
58    pub fn set_default_download_folder_path(&self, path: &str) -> Result<()> {
59        let path = HSTRING::from(path);
60        unsafe { self.0.SetDefaultDownloadFolderPath(&path) }.ok()
61    }
62
63    /// Returns the color scheme reported to pages.
64    pub fn preferred_color_scheme(&self) -> PreferredColorScheme {
65        unsafe { self.0.PreferredColorScheme() }
66            .map_or(PreferredColorScheme::Auto, PreferredColorScheme::from_raw)
67    }
68
69    /// Sets the color scheme reported to pages through `prefers-color-scheme`,
70    /// driving a site's light/dark theme.
71    pub fn set_preferred_color_scheme(&self, scheme: PreferredColorScheme) -> Result<()> {
72        unsafe { self.0.SetPreferredColorScheme(scheme.to_raw()) }.ok()
73    }
74
75    /// Asynchronously clears all of the profile's browsing data (cookies, cache,
76    /// history, storage, and so on). The `handler` closure runs on the UI thread
77    /// once the data has been cleared.
78    pub fn clear_browsing_data_all<F: FnOnce(Result<()>) + 'static>(
79        &self,
80        handler: F,
81    ) -> Result<()> {
82        let source: ICoreWebView2Profile2 = self.0.cast()?;
83        let handler = handler::ClearBrowsingDataCompleted::create(handler);
84        unsafe { source.ClearBrowsingDataAll(&handler) }.ok()
85    }
86}