Skip to content

Commit dfadcb7

Browse files
WSH032lucasfernog
andauthored
feat: add WebView::set_cookie and WebView::delete_cookie (#13661)
* chore: patch wry * feat: added `Webview::set_cookie` and `Webview::delete_cookie` * chore: changes-files * fmt * owned cookie, re-export crate --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 22d6bca commit dfadcb7

File tree

9 files changed

+130
-15
lines changed

9 files changed

+130
-15
lines changed

.changes/set-cookie-runtime.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": minor:feat
3+
"tauri-runtime-wry": minor:feat
4+
---
5+
6+
Added `WebviewDispatch::set_cookie()` and `WebviewDispatch::delete_cookie()`.

.changes/set-cookie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor:feat
3+
---
4+
5+
Added `Webview::set_cookie()`, `Webview::delete_cookie()`, `WebviewWindow::set_cookie()` and `WebviewWindow::delete_cookie()`.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ pub enum WebviewMessage {
13991399
EvaluateScript(String, Sender<()>, tracing::Span),
14001400
CookiesForUrl(Url, Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
14011401
Cookies(Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
1402+
SetCookie(tauri_runtime::Cookie<'static>),
1403+
DeleteCookie(tauri_runtime::Cookie<'static>),
14021404
WebviewEvent(WebviewEvent),
14031405
SynthesizedWindowEvent(SynthesizedWindowEvent),
14041406
Navigate(Url),
@@ -1688,6 +1690,30 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
16881690
webview_getter!(self, WebviewMessage::Cookies)?
16891691
}
16901692

1693+
fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
1694+
send_user_message(
1695+
&self.context,
1696+
Message::Webview(
1697+
*self.window_id.lock().unwrap(),
1698+
self.webview_id,
1699+
WebviewMessage::SetCookie(cookie.into_owned()),
1700+
),
1701+
)?;
1702+
Ok(())
1703+
}
1704+
1705+
fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
1706+
send_user_message(
1707+
&self.context,
1708+
Message::Webview(
1709+
*self.window_id.lock().unwrap(),
1710+
self.webview_id,
1711+
WebviewMessage::DeleteCookie(cookie.clone().into_owned()),
1712+
),
1713+
)?;
1714+
Ok(())
1715+
}
1716+
16911717
fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
16921718
send_user_message(
16931719
&self.context,
@@ -3685,6 +3711,18 @@ fn handle_user_message<T: UserEvent>(
36853711
.unwrap();
36863712
}
36873713

3714+
WebviewMessage::SetCookie(cookie) => {
3715+
if let Err(e) = webview.set_cookie(&cookie) {
3716+
log::error!("failed to set webview cookie: {e}");
3717+
}
3718+
}
3719+
3720+
WebviewMessage::DeleteCookie(cookie) => {
3721+
if let Err(e) = webview.delete_cookie(&cookie) {
3722+
log::error!("failed to delete webview cookie: {e}");
3723+
}
3724+
}
3725+
36883726
WebviewMessage::CookiesForUrl(url, tx) => {
36893727
let webview_cookies = webview
36903728
.cookies_for_url(url.as_str())

crates/tauri-runtime/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
571571
///
572572
/// # Stability
573573
///
574-
/// The return value of this function leverages [`cookie::Cookie`] which re-exports the cookie crate.
575-
/// This dependency might receive updates in minor Tauri releases.
574+
/// See [WebviewDispatch::cookies].
576575
fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>>;
577576

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

585+
/// Set a cookie for the webview.
586+
///
587+
/// # Stability
588+
///
589+
/// See [WebviewDispatch::cookies].
590+
fn set_cookie(&self, cookie: cookie::Cookie<'_>) -> Result<()>;
591+
592+
/// Delete a cookie for the webview.
593+
///
594+
/// # Stability
595+
///
596+
/// See [WebviewDispatch::cookies].
597+
fn delete_cookie(&self, cookie: cookie::Cookie<'_>) -> Result<()>;
598+
586599
/// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
587600
fn set_auto_resize(&self, auto_resize: bool) -> Result<()>;
588601

crates/tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ specta = { version = "^2.0.0-rc.16", optional = true, default-features = false,
8383
"function",
8484
"derive",
8585
] }
86+
# WARNING: cookie::Cookie is re-exported so bumping this is a breaking change, documented to be done as a minor bump
87+
cookie = "0.18"
8688

8789
# desktop
8890
[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]

crates/tauri/src/test/mock_runtime.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
638638
Ok(Vec::new())
639639
}
640640

641+
fn set_cookie(&self, cookie: tauri_runtime::Cookie<'_>) -> Result<()> {
642+
Ok(())
643+
}
644+
645+
fn delete_cookie(&self, cookie: tauri_runtime::Cookie<'_>) -> Result<()> {
646+
Ok(())
647+
}
648+
641649
fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
642650
Ok(())
643651
}

crates/tauri/src/webview/mod.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
pub(crate) mod plugin;
88
mod webview_window;
99

10+
use cookie::Cookie;
1011
pub use webview_window::{WebviewWindow, WebviewWindowBuilder};
1112

13+
/// Cookie crate used for [`Webview::set_cookie`] and [`Webview::delete_cookie`].
14+
///
15+
/// # Stability
16+
///
17+
/// This re-exported crate is still on an alpha release and might receive updates in minor Tauri releases.
18+
pub use cookie;
1219
use http::HeaderMap;
1320
use serde::Serialize;
1421
use tauri_macros::default_runtime;
1522
pub use tauri_runtime::webview::{NewWindowFeatures, PageLoadEvent};
16-
pub use tauri_runtime::Cookie;
1723
#[cfg(desktop)]
1824
use tauri_runtime::{
1925
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
@@ -2031,15 +2037,11 @@ tauri::Builder::default()
20312037
///
20322038
/// # Stability
20332039
///
2034-
/// The return value of this function leverages [`tauri_runtime::Cookie`] which re-exports the cookie crate.
2035-
/// This dependency might receive updates in minor Tauri releases.
2040+
/// See [Self::cookies].
20362041
///
20372042
/// # Known issues
20382043
///
2039-
/// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue].
2040-
/// You should use `async` commands and separate threads when reading cookies.
2041-
///
2042-
/// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
2044+
/// See [Self::cookies].
20432045
pub fn cookies_for_url(&self, url: Url) -> crate::Result<Vec<Cookie<'static>>> {
20442046
self
20452047
.webview
@@ -2072,6 +2074,32 @@ tauri::Builder::default()
20722074
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
20732075
self.webview.dispatcher.cookies().map_err(Into::into)
20742076
}
2077+
2078+
/// Set a cookie for the webview.
2079+
///
2080+
/// # Stability
2081+
///
2082+
/// See [Self::cookies].
2083+
pub fn set_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> {
2084+
self
2085+
.webview
2086+
.dispatcher
2087+
.set_cookie(cookie)
2088+
.map_err(Into::into)
2089+
}
2090+
2091+
/// Delete a cookie for the webview.
2092+
///
2093+
/// # Stability
2094+
///
2095+
/// See [Self::cookies].
2096+
pub fn delete_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> {
2097+
self
2098+
.webview
2099+
.dispatcher
2100+
.delete_cookie(cookie)
2101+
.map_err(Into::into)
2102+
}
20752103
}
20762104

20772105
impl<R: Runtime> Listener<R> for Webview<R> {

crates/tauri/src/webview/webview_window.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,15 +2387,11 @@ impl<R: Runtime> WebviewWindow<R> {
23872387
///
23882388
/// # Stability
23892389
///
2390-
/// The return value of this function leverages [`tauri_runtime::Cookie`] which re-exports the cookie crate.
2391-
/// This dependency might receive updates in minor Tauri releases.
2390+
/// See [Self::cookies].
23922391
///
23932392
/// # Known issues
23942393
///
2395-
/// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue].
2396-
/// You should use `async` commands and separate threads when reading cookies.
2397-
///
2398-
/// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
2394+
/// See [Self::cookies].
23992395
pub fn cookies_for_url(&self, url: Url) -> crate::Result<Vec<Cookie<'static>>> {
24002396
self.webview.cookies_for_url(url)
24012397
}
@@ -2424,6 +2420,24 @@ impl<R: Runtime> WebviewWindow<R> {
24242420
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
24252421
self.webview.cookies()
24262422
}
2423+
2424+
/// Set a cookie for the webview.
2425+
///
2426+
/// # Stability
2427+
///
2428+
/// See [Self::cookies].
2429+
pub fn set_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> {
2430+
self.webview.set_cookie(cookie)
2431+
}
2432+
2433+
/// Delete a cookie for the webview.
2434+
///
2435+
/// # Stability
2436+
///
2437+
/// See [Self::cookies].
2438+
pub fn delete_cookie(&self, cookie: Cookie<'_>) -> crate::Result<()> {
2439+
self.webview.delete_cookie(cookie)
2440+
}
24272441
}
24282442

24292443
impl<R: Runtime> Listener<R> for WebviewWindow<R> {

0 commit comments

Comments
 (0)