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..fd7ec16926a2 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,16 @@ fn handle_user_message( ) .unwrap(); } + + WebviewMessage::CookiesForUrl(url, tx) => { + let webview_cookies = webview.cookies_for_url(url.as_str()).map_err(|_| Error::FailedToSendMessage); + println!("allCookies: {:?}", webview.cookies().unwrap()); + println!("webview_cookies: {:?}", webview_cookies); + 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