From bccd107a4b5eef6ae735285ffa015a5f5848882a Mon Sep 17 00:00:00 2001 From: Adam Charron Date: Sun, 9 Feb 2025 14:15:33 -0500 Subject: [PATCH 1/7] Add support for fetching cookies by url --- .changes/cookies-for-url.md | 7 +++++++ Cargo.lock | 1 + crates/tauri-runtime-wry/src/lib.rs | 26 ++++++++++++++++++++++++++ crates/tauri-runtime/Cargo.toml | 1 + crates/tauri-runtime/src/lib.rs | 6 ++++++ crates/tauri/src/test/mock_runtime.rs | 4 ++++ crates/tauri/src/webview/mod.rs | 6 ++++++ 7 files changed, 51 insertions(+) create mode 100644 .changes/cookies-for-url.md diff --git a/.changes/cookies-for-url.md b/.changes/cookies-for-url.md new file mode 100644 index 000000000000..175132a677b0 --- /dev/null +++ b/.changes/cookies-for-url.md @@ -0,0 +1,7 @@ +--- +"tauri": minor:feat +"tauri-runtime": minor:feat +"tauri-runtime-wry": minor:feat +--- + +Added `Webview::cookies_for_url()` and `tauri::Cookie` diff --git a/Cargo.lock b/Cargo.lock index 28031d69f68f..c04db0d671b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9391,6 +9391,7 @@ dependencies = [ name = "tauri-runtime" version = "2.3.0" dependencies = [ + "cookie", "dpi", "gtk", "http 1.2.0", diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 7e8013d6a743..de094b25ea72 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -16,6 +16,7 @@ use http::Request; use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle}; use tauri_runtime::{ + Cookie, dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}, monitor::Monitor, webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler}, @@ -1299,6 +1300,7 @@ pub enum WebviewMessage { EvaluateScript(String), #[cfg(all(feature = "tracing", not(target_os = "android")))] EvaluateScript(String, Sender<()>, tracing::Span), + CookiesForUrl(Url, Sender>>>), WebviewEvent(WebviewEvent), SynthesizedWindowEvent(SynthesizedWindowEvent), Navigate(Url), @@ -1549,6 +1551,22 @@ impl WebviewDispatch for WryWebviewDispatcher { Ok(()) } + fn cookies_for_url(&self, url: Url) -> Result>> { + let current_window_id = self.window_id.lock().unwrap(); + let (tx, rx) = channel(); + send_user_message( + &self.context, + Message::Webview( + *current_window_id, + self.webview_id, + WebviewMessage::CookiesForUrl(url, tx) + ) + )?; + + let res = rx.recv().unwrap(); + res + } + fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { send_user_message( &self.context, @@ -3407,6 +3425,14 @@ fn handle_user_message( ) .unwrap(); } + + WebviewMessage::CookiesForUrl(url, tx) => { + let webview_cookies = webview.cookies_for_url(url.as_str()).map_err(|_| Error::FailedToSendMessage); + tx.send( + webview_cookies + ).unwrap(); + } + WebviewMessage::Bounds(tx) => { tx.send( webview diff --git a/crates/tauri-runtime/Cargo.toml b/crates/tauri-runtime/Cargo.toml index 0e2171b9c574..24e919503dec 100644 --- a/crates/tauri-runtime/Cargo.toml +++ b/crates/tauri-runtime/Cargo.toml @@ -34,6 +34,7 @@ http = "1" raw-window-handle = "0.6" url = { version = "2" } dpi = { version = "0.1", features = ["serde"] } +cookie = { version = "0.18" } [target."cfg(windows)".dependencies.windows] version = "0.58" diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index 8c93a19a0907..d4c464fa7c6e 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -43,6 +43,9 @@ use http::{ /// UI scaling utilities. pub use dpi; +/// Cookie extraction +pub use cookie::Cookie; + pub type WindowEventId = u32; pub type WebviewEventId = u32; @@ -516,6 +519,9 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' /// Moves the webview to the given window. fn reparent(&self, window_id: WindowId) -> Result<()>; + /// Get cookies for a particular url. + fn cookies_for_url(&self, url: Url) -> 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/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 2d45794762ca..c96ba63c482c 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -586,6 +586,10 @@ impl WebviewDispatch for MockWebviewDispatcher { Ok(()) } + fn cookies_for_url(&self, url: Url) -> Result>> { + Ok(Vec::new()) + } + 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 5d1e8c875714..64fcdb562934 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -25,6 +25,7 @@ use tauri_runtime::{ pub use tauri_utils::config::Color; use tauri_utils::config::{BackgroundThrottlingPolicy, WebviewUrl, WindowConfig}; pub use url::Url; +pub use tauri_runtime::Cookie; use crate::{ app::{UriSchemeResponder, WebviewEvent}, @@ -1157,6 +1158,11 @@ impl Webview { Ok(()) } + /// Returns all cookies for a specified URL including HTTP-only and secure cookies. + pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { + self.webview.dispatcher.cookies_for_url(url).map_err(Into::into) + } + /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes. pub fn set_auto_resize(&self, auto_resize: bool) -> crate::Result<()> { self From fdbefdd2fca248dcc9009e68389984a2debc8c99 Mon Sep 17 00:00:00 2001 From: Adam Charron Date: Sun, 9 Feb 2025 18:47:03 -0500 Subject: [PATCH 2/7] Add support for fetching all cookies --- crates/tauri-runtime-wry/src/lib.rs | 32 +++++++++++++++++---------- crates/tauri-runtime/src/lib.rs | 3 +++ crates/tauri/src/test/mock_runtime.rs | 4 ++++ crates/tauri/src/webview/mod.rs | 27 +++++++++++++++++++--- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index de094b25ea72..c07e48b0e995 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -16,7 +16,6 @@ use http::Request; use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle}; use tauri_runtime::{ - Cookie, dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}, monitor::Monitor, webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler}, @@ -24,9 +23,9 @@ use tauri_runtime::{ CursorIcon, DetachedWindow, DetachedWindowWebview, DragDropEvent, PendingWindow, RawWindow, WebviewEvent, WindowBuilder, WindowBuilderBase, WindowEvent, WindowId, WindowSizeConstraints, }, - DeviceEventFilter, Error, EventLoopProxy, ExitRequestedEventAction, Icon, ProgressBarState, - ProgressBarStatus, Result, RunEvent, Runtime, RuntimeHandle, RuntimeInitArgs, UserAttentionType, - UserEvent, WebviewDispatch, WebviewEventId, WindowDispatch, WindowEventId, + Cookie, DeviceEventFilter, Error, EventLoopProxy, ExitRequestedEventAction, Icon, + ProgressBarState, ProgressBarStatus, Result, RunEvent, Runtime, RuntimeHandle, RuntimeInitArgs, + UserAttentionType, UserEvent, WebviewDispatch, WebviewEventId, WindowDispatch, WindowEventId, }; #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -1301,6 +1300,7 @@ pub enum WebviewMessage { #[cfg(all(feature = "tracing", not(target_os = "android")))] EvaluateScript(String, Sender<()>, tracing::Span), CookiesForUrl(Url, Sender>>>), + Cookies(Sender>>>), WebviewEvent(WebviewEvent), SynthesizedWindowEvent(SynthesizedWindowEvent), Navigate(Url), @@ -1559,12 +1559,15 @@ impl WebviewDispatch for WryWebviewDispatcher { Message::Webview( *current_window_id, self.webview_id, - WebviewMessage::CookiesForUrl(url, tx) - ) + WebviewMessage::CookiesForUrl(url, tx), + ), )?; - let res = rx.recv().unwrap(); - res + rx.recv().unwrap() + } + + fn cookies(&self) -> Result>> { + webview_getter!(self, WebviewMessage::Cookies)? } fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { @@ -3426,11 +3429,16 @@ fn handle_user_message( .unwrap(); } + WebviewMessage::Cookies(tx) => { + tx.send(webview.cookies().map_err(|_| Error::FailedToSendMessage)) + .unwrap(); + } + WebviewMessage::CookiesForUrl(url, tx) => { - let webview_cookies = webview.cookies_for_url(url.as_str()).map_err(|_| Error::FailedToSendMessage); - tx.send( - webview_cookies - ).unwrap(); + let webview_cookies = webview + .cookies_for_url(url.as_str()) + .map_err(|_| Error::FailedToSendMessage); + tx.send(webview_cookies).unwrap(); } WebviewMessage::Bounds(tx) => { diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index d4c464fa7c6e..4bf74a7b87fe 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -522,6 +522,9 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' /// Get cookies for a particular url. fn cookies_for_url(&self, url: Url) -> Result>>; + /// Return all cookies in the cookie store. + fn cookies(&self) -> 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/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index c96ba63c482c..30687967727d 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -586,6 +586,10 @@ impl WebviewDispatch for MockWebviewDispatcher { Ok(()) } + fn cookies(&self) -> Result>> { + Ok(Vec::new()) + } + fn cookies_for_url(&self, url: Url) -> Result>> { Ok(Vec::new()) } diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 64fcdb562934..99e8d1e038c4 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -13,6 +13,7 @@ use http::HeaderMap; use serde::Serialize; use tauri_macros::default_runtime; pub use tauri_runtime::webview::PageLoadEvent; +pub use tauri_runtime::Cookie; #[cfg(desktop)] use tauri_runtime::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, @@ -25,7 +26,6 @@ use tauri_runtime::{ pub use tauri_utils::config::Color; use tauri_utils::config::{BackgroundThrottlingPolicy, WebviewUrl, WindowConfig}; pub use url::Url; -pub use tauri_runtime::Cookie; use crate::{ app::{UriSchemeResponder, WebviewEvent}, @@ -1158,9 +1158,30 @@ impl Webview { Ok(()) } - /// Returns all cookies for a specified URL including HTTP-only and secure cookies. + /// Returns all cookies in the runtime's cookie store including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { - self.webview.dispatcher.cookies_for_url(url).map_err(Into::into) + self + .webview + .dispatcher + .cookies_for_url(url) + .map_err(Into::into) + } + + /// Returns all cookies in the runtime's cookie store for all URLs including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// ## Platform-specific + /// + /// - **Android**: Unsupported, always returns an empty [`Vec`]. + pub fn cookies(&self) -> crate::Result>> { + self.webview.dispatcher.cookies().map_err(Into::into) } /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes. From dc50f2c3a9331a8a70e3481e9dcdb55571705136 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 15 Mar 2025 08:47:46 -0300 Subject: [PATCH 3/7] add missing getters, update change file --- .changes/cookies-for-url.md | 4 +--- .changes/webview-cookies-runtime.md | 6 ++++++ crates/tauri/src/webview/webview_window.rs | 25 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 .changes/webview-cookies-runtime.md diff --git a/.changes/cookies-for-url.md b/.changes/cookies-for-url.md index 175132a677b0..498cbc34d083 100644 --- a/.changes/cookies-for-url.md +++ b/.changes/cookies-for-url.md @@ -1,7 +1,5 @@ --- "tauri": minor:feat -"tauri-runtime": minor:feat -"tauri-runtime-wry": minor:feat --- -Added `Webview::cookies_for_url()` and `tauri::Cookie` +Added `Webview::cookies()`, `Webview::cookies_for_url()`, `WebviewWindow::cookies()` and `Webview::cookies_for_url()`. diff --git a/.changes/webview-cookies-runtime.md b/.changes/webview-cookies-runtime.md new file mode 100644 index 000000000000..8beadfbb90fa --- /dev/null +++ b/.changes/webview-cookies-runtime.md @@ -0,0 +1,6 @@ +--- +"tauri-runtime": minor:feat +"tauri-runtime-wry": minor:feat +--- + +Added `WebviewDispatch::cookies()` and `WebviewDispatch::cookies_for_url()`. diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index 22a1e0b74ca0..c466e5f701dd 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -37,8 +37,7 @@ use crate::{ ipc::{CommandArg, CommandItem, InvokeError, OwnedInvokeResponder}, manager::AppManager, sealed::{ManagerBase, RuntimeOrDispatch}, - webview::PageLoadPayload, - webview::WebviewBuilder, + webview::{Cookie, PageLoadPayload, WebviewBuilder}, window::WindowBuilder, AppHandle, Event, EventId, Manager, Runtime, Webview, WindowEvent, }; @@ -2005,6 +2004,28 @@ impl WebviewWindow { pub fn clear_all_browsing_data(&self) -> crate::Result<()> { self.webview.clear_all_browsing_data() } + + /// Returns all cookies in the runtime's cookie store including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. + pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { + self.webview.cookies_for_url(url) + } + + /// Returns all cookies in the runtime's cookie store for all URLs including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// ## Platform-specific + /// + /// - **Android**: Unsupported, always returns an empty [`Vec`]. + pub fn cookies(&self) -> crate::Result>> { + self.webview.cookies() + } } impl Listener for WebviewWindow { From fa1db55d432976ef2ba0399706fa172e8a91096b Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 15 Mar 2025 09:13:24 -0300 Subject: [PATCH 4/7] update docs for windows deadlock --- crates/tauri/src/webview/mod.rs | 22 ++++++++++++++++++---- crates/tauri/src/webview/webview_window.rs | 22 ++++++++++++++++++---- crates/tauri/src/window/mod.rs | 8 ++++---- examples/helloworld/main.rs | 11 ++++++++++- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 99e8d1e038c4..19c0d5d0aedf 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -247,8 +247,8 @@ impl WebviewBuilder { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating windows. + /// 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 creating webviews. /// /// # Examples /// @@ -323,8 +323,8 @@ async fn create_window(app: tauri::AppHandle) { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating webviews. + /// 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 creating webviews. /// /// # Examples /// @@ -1163,6 +1163,13 @@ impl Webview { /// Note that cookies will only be returned for URLs with an http or https scheme. /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// # 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 pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { self .webview @@ -1177,9 +1184,16 @@ impl Webview { /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// ## Platform-specific /// /// - **Android**: Unsupported, always returns an empty [`Vec`]. + /// + /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 pub fn cookies(&self) -> crate::Result>> { self.webview.dispatcher.cookies().map_err(Into::into) } diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index c466e5f701dd..7be0b76ad902 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -60,8 +60,8 @@ impl<'a, R: Runtime, M: Manager> WebviewWindowBuilder<'a, R, M> { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating windows. + /// On Windows, this function deadlocks when used in a synchronous command and event handlers, see [the Webview2 issue]. + /// You should use `async` commands and separate threads when creating windows. /// /// # Examples /// @@ -117,8 +117,8 @@ impl<'a, R: Runtime, M: Manager> WebviewWindowBuilder<'a, R, M> { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating windows. + /// 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 creating windows. /// /// # Examples /// @@ -2010,6 +2010,13 @@ impl WebviewWindow { /// Note that cookies will only be returned for URLs with an http or https scheme. /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// # 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 pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { self.webview.cookies_for_url(url) } @@ -2020,9 +2027,16 @@ impl WebviewWindow { /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// ## Platform-specific /// /// - **Android**: Unsupported, always returns an empty [`Vec`]. + /// + /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 pub fn cookies(&self) -> crate::Result>> { self.webview.cookies() } diff --git a/crates/tauri/src/window/mod.rs b/crates/tauri/src/window/mod.rs index bde970efb2b0..fbf10d9c86b5 100644 --- a/crates/tauri/src/window/mod.rs +++ b/crates/tauri/src/window/mod.rs @@ -140,8 +140,8 @@ impl<'a, R: Runtime, M: Manager> WindowBuilder<'a, R, M> { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating windows. + /// 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 creating windows. /// /// # Examples /// @@ -217,8 +217,8 @@ async fn create_window(app: tauri::AppHandle) { /// /// # Known issues /// - /// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue]. - /// You should use `async` commands when creating windows. + /// 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 creating windows. /// /// # Examples /// diff --git a/examples/helloworld/main.rs b/examples/helloworld/main.rs index ee822803b95c..91389db81643 100644 --- a/examples/helloworld/main.rs +++ b/examples/helloworld/main.rs @@ -5,13 +5,22 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #[tauri::command] -fn greet(name: &str) -> String { +fn greet(app: tauri::AppHandle, name: String) -> String { + use tauri::Manager; + println!("greet {name}"); + let w = app.get_webview_window("main").unwrap(); + println!("{:?}", w.get_webview_window("main").unwrap().cookies()); format!("Hello {name}, You have been greeted from Rust!") } fn main() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![greet]) + .setup(|app| { + use tauri::Manager; + println!("{:?}", app.get_webview_window("main").unwrap().cookies()); + Ok(()) + }) .run(tauri::generate_context!( "../../examples/helloworld/tauri.conf.json" )) From 47c95da60d623b77c3de56cdd6890a36fbe78842 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 15 Mar 2025 09:19:20 -0300 Subject: [PATCH 5/7] fix mobile build --- crates/tauri/src/webview/mod.rs | 80 ++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 19c0d5d0aedf..e542410091a5 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -1158,46 +1158,6 @@ impl Webview { Ok(()) } - /// Returns all cookies in the runtime's cookie store including HTTP-only and secure cookies. - /// - /// Note that cookies will only be returned for URLs with an http or https scheme. - /// Cookies set through javascript for local files - /// (such as those served from the tauri://) protocol are not currently supported. - /// - /// # 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 - pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { - self - .webview - .dispatcher - .cookies_for_url(url) - .map_err(Into::into) - } - - /// Returns all cookies in the runtime's cookie store for all URLs including HTTP-only and secure cookies. - /// - /// Note that cookies will only be returned for URLs with an http or https scheme. - /// Cookies set through javascript for local files - /// (such as those served from the tauri://) protocol are not currently supported. - /// - /// # 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. - /// - /// ## Platform-specific - /// - /// - **Android**: Unsupported, always returns an empty [`Vec`]. - /// - /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 - pub fn cookies(&self) -> crate::Result>> { - self.webview.dispatcher.cookies().map_err(Into::into) - } - /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes. pub fn set_auto_resize(&self, auto_resize: bool) -> crate::Result<()> { self @@ -1744,6 +1704,46 @@ tauri::Builder::default() .clear_all_browsing_data() .map_err(Into::into) } + + /// Returns all cookies in the runtime's cookie store including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// # 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 + pub fn cookies_for_url(&self, url: Url) -> crate::Result>> { + self + .webview + .dispatcher + .cookies_for_url(url) + .map_err(Into::into) + } + + /// Returns all cookies in the runtime's cookie store for all URLs including HTTP-only and secure cookies. + /// + /// Note that cookies will only be returned for URLs with an http or https scheme. + /// Cookies set through javascript for local files + /// (such as those served from the tauri://) protocol are not currently supported. + /// + /// # 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. + /// + /// ## Platform-specific + /// + /// - **Android**: Unsupported, always returns an empty [`Vec`]. + /// + /// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583 + pub fn cookies(&self) -> crate::Result>> { + self.webview.dispatcher.cookies().map_err(Into::into) + } } impl Listener for Webview { From b53ebb2611c692df3ded328de23ee60c355f4fe0 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 15 Mar 2025 10:15:59 -0300 Subject: [PATCH 6/7] Update crates/tauri-runtime/Cargo.toml --- crates/tauri-runtime/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime/Cargo.toml b/crates/tauri-runtime/Cargo.toml index 24e919503dec..2891c2c07bd6 100644 --- a/crates/tauri-runtime/Cargo.toml +++ b/crates/tauri-runtime/Cargo.toml @@ -34,7 +34,8 @@ http = "1" raw-window-handle = "0.6" url = { version = "2" } dpi = { version = "0.1", features = ["serde"] } -cookie = { version = "0.18" } +# WARNING: cookie::Cookie is re-exported so bumping this is a breaking change +cookie = "0.18" [target."cfg(windows)".dependencies.windows] version = "0.58" From 3a0aee3e2416d644daf5d37df3f510bffb3fa207 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 15 Mar 2025 12:32:09 -0300 Subject: [PATCH 7/7] add docs for stability [skip ci] --- crates/tauri-runtime/Cargo.toml | 2 +- crates/tauri-runtime/src/lib.rs | 10 ++++++++++ crates/tauri/src/webview/mod.rs | 10 ++++++++++ crates/tauri/src/webview/webview_window.rs | 10 ++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime/Cargo.toml b/crates/tauri-runtime/Cargo.toml index 2891c2c07bd6..f2698fe9d31f 100644 --- a/crates/tauri-runtime/Cargo.toml +++ b/crates/tauri-runtime/Cargo.toml @@ -34,7 +34,7 @@ http = "1" raw-window-handle = "0.6" url = { version = "2" } dpi = { version = "0.1", features = ["serde"] } -# WARNING: cookie::Cookie is re-exported so bumping this is a breaking change +# WARNING: cookie::Cookie is re-exported so bumping this is a breaking change, documented to be done as a minor bump cookie = "0.18" [target."cfg(windows)".dependencies.windows] diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index 4bf74a7b87fe..3ed1be0b0ec6 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -520,9 +520,19 @@ pub trait WebviewDispatch: Debug + Clone + Send + Sync + Sized + ' fn reparent(&self, window_id: WindowId) -> Result<()>; /// Get cookies for a particular url. + /// + /// # 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. fn cookies_for_url(&self, url: Url) -> Result>>; /// Return all cookies in the cookie store. + /// + /// # 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. fn cookies(&self) -> Result>>; /// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes. diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index e542410091a5..9cd706ca9eea 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -1711,6 +1711,11 @@ tauri::Builder::default() /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// # Known issues /// /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue]. @@ -1731,6 +1736,11 @@ tauri::Builder::default() /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// # Known issues /// /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue]. diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index 7be0b76ad902..95ca0117b25c 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -2011,6 +2011,11 @@ impl WebviewWindow { /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// # Known issues /// /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue]. @@ -2027,6 +2032,11 @@ impl WebviewWindow { /// Cookies set through javascript for local files /// (such as those served from the tauri://) protocol are not currently supported. /// + /// # 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. + /// /// # Known issues /// /// On Windows, this function deadlocks when used in a synchronous command or event handlers, see [the Webview2 issue].