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
6 changes: 6 additions & 0 deletions .changes/set-cookie-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime": minor:feat
"tauri-runtime-wry": minor:feat
---

Added `WebviewDispatch::set_cookie()` and `WebviewDispatch::delete_cookie()`.
5 changes: 5 additions & 0 deletions .changes/set-cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": minor:feat
---

Added `Webview::set_cookie()`, `Webview::delete_cookie()`, `WebviewWindow::set_cookie()` and `WebviewWindow::delete_cookie()`.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ pub enum WebviewMessage {
EvaluateScript(String, Sender<()>, tracing::Span),
CookiesForUrl(Url, Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
Cookies(Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
SetCookie(tauri_runtime::Cookie<'static>),
DeleteCookie(tauri_runtime::Cookie<'static>),
WebviewEvent(WebviewEvent),
SynthesizedWindowEvent(SynthesizedWindowEvent),
Navigate(Url),
Expand Down Expand Up @@ -1688,6 +1690,30 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
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()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cookie.clone() here was forgotten to be removed.

),
)?;
Ok(())
}

fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
send_user_message(
&self.context,
Expand Down Expand Up @@ -3685,6 +3711,18 @@ fn handle_user_message<T: UserEvent>(
.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}");
}
}

Comment on lines +3714 to +3725
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some questions. Why do some methods use a channel to wait for a Result<()> to return, while others simply ignore (log) errors?

WebviewMessage::CookiesForUrl(url, tx) => {
let webview_cookies = webview
.cookies_for_url(url.as_str())
Expand Down
17 changes: 15 additions & 2 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ pub trait WebviewDispatch<T: UserEvent>: 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<Vec<Cookie<'static>>>;

/// Return all cookies in the cookie store.
Expand All @@ -583,6 +582,20 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
/// This dependency might receive updates in minor Tauri releases.
fn cookies(&self) -> Result<Vec<Cookie<'static>>>;

/// 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<()>;

Expand Down
2 changes: 2 additions & 0 deletions crates/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ impl<T: UserEvent> WebviewDispatch<T> 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(())
}
Expand Down
42 changes: 35 additions & 7 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to expose this as a public API. Wouldn't removing it cause some issues?

Copy link
Contributor

@Legend-Master Legend-Master Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should probably bring it back

Edit: done in #14020

#[cfg(desktop)]
use tauri_runtime::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
Expand Down Expand Up @@ -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<Vec<Cookie<'static>>> {
self
.webview
Expand Down Expand Up @@ -2072,6 +2074,32 @@ tauri::Builder::default()
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
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<R: Runtime> Listener<R> for Webview<R> {
Expand Down
26 changes: 20 additions & 6 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,15 +2387,11 @@ impl<R: Runtime> WebviewWindow<R> {
///
/// # 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<Vec<Cookie<'static>>> {
self.webview.cookies_for_url(url)
}
Expand Down Expand Up @@ -2424,6 +2420,24 @@ impl<R: Runtime> WebviewWindow<R> {
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
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<R: Runtime> Listener<R> for WebviewWindow<R> {
Expand Down
Loading