Skip to content

Commit

Permalink
Add support for fetching cookies by url
Browse files Browse the repository at this point in the history
  • Loading branch information
acharron-hl committed Feb 9, 2025
1 parent 8e9339e commit e2ca406
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changes/cookies-for-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": minor:feat
"tauri-runtime": minor:feat
"tauri-runtime-wry": minor:feat
---

Added `Webview::cookies_for_url()` and `tauri::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.

28 changes: 28 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Result<Vec<tauri_runtime::Cookie<'static>>>>),
WebviewEvent(WebviewEvent),
SynthesizedWindowEvent(SynthesizedWindowEvent),
Navigate(Url),
Expand Down Expand Up @@ -1549,6 +1551,22 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
Ok(())
}

fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>> {
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,
Expand Down Expand Up @@ -3407,6 +3425,16 @@ fn handle_user_message<T: UserEvent>(
)
.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
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -516,6 +519,9 @@ pub trait WebviewDispatch<T: UserEvent>: 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<Vec<Cookie<'static>>>;

/// 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
4 changes: 4 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(())
}

fn cookies_for_url(&self, url: Url) -> Result<Vec<tauri_runtime::Cookie<'static>>> {
Ok(Vec::new())
}

fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -1157,6 +1158,11 @@ impl<R: Runtime> Webview<R> {
Ok(())
}

/// Returns all cookies for a specified URL including HTTP-only and secure cookies.
pub fn cookies_for_url(&self, url: Url) -> crate::Result<Vec<Cookie<'static>>> {
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
Expand Down

0 comments on commit e2ca406

Please sign in to comment.