Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.

26 changes: 26 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,14 @@ 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);
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" }
Comment thread
lucasfernog marked this conversation as resolved.
Resolved

[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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a note about the scheme requirements here? imo quite important

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added a note. This actually seems more of a bug/runtime limitation than anything. I would imagine this is because the tauri:// protocol behaves like the the file:// protocol in browsers and doesn't actually expose a functional cookie store.

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