Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/windows-with-profile-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

On Windows, add `WebViewBuilderExtWindows::with_profile_name` to opt the webview into a named WebView2 profile. Webviews with different profile names within the same environment have isolated cookies, storage, IndexedDB, and cache while sharing the runtime — matching WebView2's documented multi-profile pattern.
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
extension_path: Option<PathBuf>,
default_context_menus: bool,
environment: Option<ICoreWebView2Environment>,
profile_name: Option<String>,
}

#[cfg(windows)]
Expand All @@ -1692,6 +1693,7 @@ impl Default for PlatformSpecificWebViewAttributes {
browser_extensions_enabled: false,
extension_path: None,
environment: None,
profile_name: None,
}
}
}
Expand Down Expand Up @@ -1776,6 +1778,19 @@ pub trait WebViewBuilderExtWindows {
/// Set the environment for the webview.
/// Useful if you need to share the same environment, for instance when using the [`WebViewBuilder::with_new_window_req_handler`].
fn with_environment(self, environment: ICoreWebView2Environment) -> Self;

/// Set the WebView2 profile name for this webview. Webviews with different
/// profile names within the same environment have isolated cookies, storage,
/// IndexedDB, cache, and other site data, while sharing the runtime.
///
/// When `None` (the default), the webview uses the unnamed default profile.
///
/// See <https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/multi-profile-support>
/// for the underlying WebView2 multi-profile feature.
///
/// Profile names must follow the WebView2 naming rules (alphanumeric, `.`,
/// `_`, `-`, ` `, up to 64 chars, not starting/ending with `.` or ` `).
fn with_profile_name<S: Into<String>>(self, name: S) -> Self;
}

#[cfg(windows)]
Expand Down Expand Up @@ -1824,6 +1839,11 @@ impl WebViewBuilderExtWindows for WebViewBuilder<'_> {
self.platform_specific.environment.replace(environment);
self
}

fn with_profile_name<S: Into<String>>(mut self, name: S) -> Self {
self.platform_specific.profile_name = Some(name.into());
self
}
}

#[cfg(target_os = "android")]
Expand Down
14 changes: 13 additions & 1 deletion src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ impl InnerWebView {
} else {
Self::create_environment(&attributes, pl_attrs.clone())?
};
let controller = Self::create_controller(hwnd, &env, attributes.incognito, background_color)?;
let controller = Self::create_controller(
hwnd,
&env,
attributes.incognito,
background_color,
pl_attrs.profile_name.as_deref(),
)?;
let webview = Self::init_webview(
parent,
hwnd,
Expand Down Expand Up @@ -369,6 +375,7 @@ impl InnerWebView {
env: &ICoreWebView2Environment,
incognito: bool,
background_color: Option<(u8, u8, u8, u8)>,
profile_name: Option<&str>,
) -> Result<ICoreWebView2Controller> {
let (tx, rx) = mpsc::channel();

Expand Down Expand Up @@ -405,6 +412,11 @@ impl InnerWebView {
}

controller_opts.SetIsInPrivateModeEnabled(incognito)?;

if let Some(name) = profile_name {
controller_opts.SetProfileName(&HSTRING::from(name))?;
}

env10.CreateCoreWebView2ControllerWithOptions(hwnd, &controller_opts, &handler)?;
} else {
env.CreateCoreWebView2Controller(hwnd, &handler)?
Expand Down
Loading