diff --git a/.changes/set-cookie-runtime.md b/.changes/set-cookie-runtime.md new file mode 100644 index 000000000000..81ac0ffba280 --- /dev/null +++ b/.changes/set-cookie-runtime.md @@ -0,0 +1,6 @@ +--- +"tauri-runtime": minor:feat +"tauri-runtime-wry": minor:feat +--- + +Added `WebviewDispatch::set_cookie()` and `WebviewDispatch::delete_cookie()`. diff --git a/.changes/set-cookie.md b/.changes/set-cookie.md new file mode 100644 index 000000000000..661a4fc5c052 --- /dev/null +++ b/.changes/set-cookie.md @@ -0,0 +1,5 @@ +--- +"tauri": minor:feat +--- + +Added `Webview::set_cookie()`, `Webview::delete_cookie()`, `WebviewWindow::set_cookie()` and `WebviewWindow::delete_cookie()`. diff --git a/Cargo.lock b/Cargo.lock index 1f4cfa185772..2b30451f57b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8489,6 +8489,7 @@ dependencies = [ "anyhow", "bytes", "cargo_toml", + "cookie", "data-url", "dirs 6.0.0", "dunce", diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 6fc0c2ee9f5b..a9ccdbec1feb 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -1399,6 +1399,8 @@ pub enum WebviewMessage { EvaluateScript(String, Sender<()>, tracing::Span), CookiesForUrl(Url, Sender>>>), Cookies(Sender>>>), + SetCookie(tauri_runtime::Cookie<'static>), + DeleteCookie(tauri_runtime::Cookie<'static>), WebviewEvent(WebviewEvent), SynthesizedWindowEvent(SynthesizedWindowEvent), Navigate(Url), @@ -1688,6 +1690,30 @@ impl WebviewDispatch for WryWebviewDispatcher { webview_getter!(self, WebviewMessage::Cookies)? } + fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()> { + send_user_message( + &self.context, + Message::Webview( + *self.window_id.lock().unwrap(), + self.webview_id, + WebviewMessage::SetCookie(cookie.into_owned()), + ), + )?; + Ok(()) + } + + fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()> { + send_user_message( + &self.context, + Message::Webview( + *self.window_id.lock().unwrap(), + self.webview_id, + WebviewMessage::DeleteCookie(cookie.clone().into_owned()), + ), + )?; + Ok(()) + } + fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { send_user_message( &self.context, @@ -3685,6 +3711,18 @@ fn handle_user_message( .unwrap(); } + WebviewMessage::SetCookie(cookie) => { + if let Err(e) = webview.set_cookie(&cookie) { + log::error!("failed to set webview cookie: {e}"); + } + } + + WebviewMessage::DeleteCookie(cookie) => { + if let Err(e) = webview.delete_cookie(&cookie) { + log::error!("failed to delete webview cookie: {e}"); + } + } + WebviewMessage::CookiesForUrl(url, tx) => { let webview_cookies = webview .cookies_for_url(url.as_str()) diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index 051a82d69723..f2a55ac600dc 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -571,8 +571,7 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' /// /// # Stability /// - /// The return value of this function leverages [`cookie::Cookie`] which re-exports the cookie crate. - /// This dependency might receive updates in minor Tauri releases. + /// See [WebviewDispatch::cookies]. fn cookies_for_url(&self, url: Url) -> Result>>; /// Return all cookies in the cookie store. @@ -583,6 +582,20 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' /// This dependency might receive updates in minor Tauri releases. fn cookies(&self) -> Result>>; + /// Set a cookie for the webview. + /// + /// # Stability + /// + /// See [WebviewDispatch::cookies]. + fn set_cookie(&self, cookie: cookie::Cookie<'_>) -> Result<()>; + + /// Delete a cookie for the webview. + /// + /// # Stability + /// + /// See [WebviewDispatch::cookies]. + fn delete_cookie(&self, cookie: cookie::Cookie<'_>) -> Result<()>; + /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes. fn set_auto_resize(&self, auto_resize: bool) -> Result<()>; diff --git a/crates/tauri/Cargo.toml b/crates/tauri/Cargo.toml index cb6be561f75f..54d4170dce95 100644 --- a/crates/tauri/Cargo.toml +++ b/crates/tauri/Cargo.toml @@ -83,6 +83,8 @@ specta = { version = "^2.0.0-rc.16", optional = true, default-features = false, "function", "derive", ] } +# WARNING: cookie::Cookie is re-exported so bumping this is a breaking change, documented to be done as a minor bump +cookie = "0.18" # desktop [target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "windows", target_os = "macos"))'.dependencies] diff --git a/crates/tauri/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 8662d6d1ae56..cca1da9f3db7 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -638,6 +638,14 @@ impl WebviewDispatch for MockWebviewDispatcher { Ok(Vec::new()) } + fn set_cookie(&self, cookie: tauri_runtime::Cookie<'_>) -> Result<()> { + Ok(()) + } + + fn delete_cookie(&self, cookie: tauri_runtime::Cookie<'_>) -> Result<()> { + Ok(()) + } + fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { Ok(()) } diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index c9f5b02f6f2b..9d47f64d24cc 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -7,13 +7,19 @@ pub(crate) mod plugin; mod webview_window; +use cookie::Cookie; pub use webview_window::{WebviewWindow, WebviewWindowBuilder}; +/// Cookie crate used for [`Webview::set_cookie`] and [`Webview::delete_cookie`]. +/// +/// # Stability +/// +/// This re-exported crate is still on an alpha release and might receive updates in minor Tauri releases. +pub use cookie; use http::HeaderMap; use serde::Serialize; use tauri_macros::default_runtime; pub use tauri_runtime::webview::{NewWindowFeatures, PageLoadEvent}; -pub use tauri_runtime::Cookie; #[cfg(desktop)] use tauri_runtime::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, @@ -2031,15 +2037,11 @@ tauri::Builder::default() /// /// # Stability /// - /// The return value of this function leverages [`tauri_runtime::Cookie`] which re-exports the cookie crate. - /// This dependency might receive updates in minor Tauri releases. + /// See [Self::cookies]. /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue]. - /// You should use `async` commands and separate threads when reading cookies. - /// - /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 + /// See [Self::cookies]. pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { self .webview @@ -2072,6 +2074,32 @@ tauri::Builder::default() pub fn cookies(&self) -> crate::Result>> { self.webview.dispatcher.cookies().map_err(Into::into) } + + /// Set a cookie for the webview. + /// + /// # Stability + /// + /// See [Self::cookies]. + pub fn set_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> { + self + .webview + .dispatcher + .set_cookie(cookie) + .map_err(Into::into) + } + + /// Delete a cookie for the webview. + /// + /// # Stability + /// + /// See [Self::cookies]. + pub fn delete_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> { + self + .webview + .dispatcher + .delete_cookie(cookie) + .map_err(Into::into) + } } impl Listener for Webview { diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index a54271917358..ae47675504e2 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -2387,15 +2387,11 @@ impl WebviewWindow { /// /// # Stability /// - /// The return value of this function leverages [`tauri_runtime::Cookie`] which re-exports the cookie crate. - /// This dependency might receive updates in minor Tauri releases. + /// See [Self::cookies]. /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue]. - /// You should use `async` commands and separate threads when reading cookies. - /// - /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 + /// See [Self::cookies]. pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { self.webview.cookies_for_url(url) } @@ -2424,6 +2420,24 @@ impl WebviewWindow { pub fn cookies(&self) -> crate::Result>> { self.webview.cookies() } + + /// Set a cookie for the webview. + /// + /// # Stability + /// + /// See [Self::cookies]. + pub fn set_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> { + self.webview.set_cookie(cookie) + } + + /// Delete a cookie for the webview. + /// + /// # Stability + /// + /// See [Self::cookies]. + pub fn delete_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> { + self.webview.delete_cookie(cookie) + } } impl Listener for WebviewWindow {