Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
6 changes: 6 additions & 0 deletions .changes/set-cookie-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime": minor:feat
"tauri-runtime-wry": minor:feat
---

Added `WebviewDispatch::set_cookie()` and `WebviewDispatch::delete_cookie()`.
5 changes: 5 additions & 0 deletions .changes/set-cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": minor:feat
---

Added `Webview::set_cookie()`, `Webview::delete_cookie()`, `WebviewWindow::set_cookie()` and `WebviewWindow::delete_cookie()`.
19 changes: 1 addition & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ opt-level = "s"
schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch = 'feat/preserve-description-newlines' }
tauri = { path = "./crates/tauri" }
tauri-plugin = { path = "./crates/tauri-plugin" }

wry = { git = "https://github.com/WSH032/wry.git", branch = "feat/set-cookies" }
48 changes: 47 additions & 1 deletion crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,8 @@ pub enum WebviewMessage {
EvaluateScript(String, Sender<()>, tracing::Span),
CookiesForUrl(Url, Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
Cookies(Sender<Result<Vec<tauri_runtime::Cookie<'static>>>>),
SetCookie(tauri_runtime::Cookie<'static>),
DeleteCookie(tauri_runtime::Cookie<'static>),
WebviewEvent(WebviewEvent),
SynthesizedWindowEvent(SynthesizedWindowEvent),
Navigate(Url),
Expand Down Expand Up @@ -1681,6 +1683,38 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
webview_getter!(self, WebviewMessage::Cookies)?
}

fn set_cookie(&self, cookie: &Cookie<'_>) -> Result<()> {
// TODO, FIXME: Because [Message] requires `T: 'static`,
// we currently need to clone the cookie.
let cookie = cookie.clone().into_owned();
let current_window_id = self.window_id.lock().unwrap();
send_user_message(
&self.context,
Message::Webview(
*current_window_id,
self.webview_id,
WebviewMessage::SetCookie(cookie),
),
)?;
Ok(())
}

fn delete_cookie(&self, cookie: &Cookie<'_>) -> Result<()> {
// TODO, FIXME: Because [Message] requires `T: 'static`,
// we currently need to clone the cookie.
let cookie = cookie.clone().into_owned();
let current_window_id = self.window_id.lock().unwrap();
send_user_message(
&self.context,
Message::Webview(
*current_window_id,
self.webview_id,
WebviewMessage::DeleteCookie(cookie),
),
)?;
Ok(())
}

fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
send_user_message(
&self.context,
Expand Down Expand Up @@ -3649,6 +3683,18 @@ fn handle_user_message<T: UserEvent>(
.unwrap();
}

WebviewMessage::SetCookie(cookie) => {
if let Err(e) = webview.set_cookie(&cookie) {
log::error!("failed to set webview cookie: {e}");
}
}

WebviewMessage::DeleteCookie(cookie) => {
if let Err(e) = webview.delete_cookie(&cookie) {
log::error!("failed to delete webview cookie: {e}");
}
}

Comment on lines +3714 to +3725

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 have some questions. Why do some methods use a channel to wait for a Result<()> to return, while others simply ignore (log) errors?

WebviewMessage::CookiesForUrl(url, tx) => {
let webview_cookies = webview
.cookies_for_url(url.as_str())
Expand Down Expand Up @@ -4473,7 +4519,7 @@ You may have it installed on another user account, but it is not available for t
}
};

let mut webview_builder = WebViewBuilder::with_web_context(&mut web_context.inner)
let mut webview_builder = WebViewBuilder::new_with_web_context(&mut web_context.inner)
.with_id(&label)
.with_focused(webview_attributes.focus)
.with_url(&url)
Expand Down
18 changes: 16 additions & 2 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
///
/// # 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.
/// See [WebviewDispatch::cookies].
fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>>;

/// Return all cookies in the cookie store.
Expand All @@ -571,6 +570,21 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
/// This dependency might receive updates in minor Tauri releases.
fn cookies(&self) -> Result<Vec<Cookie<'static>>>;

/// Set a cookie for the webview.
///
/// # Stability
///
/// See [WebviewDispatch::cookies].
fn set_cookie(&self, cookie: &cookie::Cookie<'_>) -> Result<()>;

/// Delete a cookie for the webview.
///
/// # Stability
///
/// See [WebviewDispatch::cookies].
fn delete_cookie(&self, cookie: &cookie::Cookie<'_>) -> 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<()>;

Expand Down
8 changes: 8 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(Vec::new())
}

fn set_cookie(&self, cookie: &tauri_runtime::Cookie<'_>) -> Result<()> {
Ok(())
}

fn delete_cookie(&self, cookie: &tauri_runtime::Cookie<'_>) -> Result<()> {
Ok(())
}

fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
Ok(())
}
Expand Down
34 changes: 28 additions & 6 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,15 +1850,11 @@ tauri::Builder::default()
///
/// # 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.
/// See [Self::cookies].
///
/// # 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
/// See [Self::cookies].
pub fn cookies_for_url(&self, url: Url) -> crate::Result<Vec<Cookie<'static>>> {
self
.webview
Expand Down Expand Up @@ -1891,6 +1887,32 @@ tauri::Builder::default()
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
self.webview.dispatcher.cookies().map_err(Into::into)
}

/// Set a cookie for the webview.
///
/// # Stability
///
/// See [Self::cookies].
pub fn set_cookie(&self, cookie: &Cookie<'_>) -> crate::Result<()> {
self
.webview
.dispatcher
.set_cookie(cookie)
.map_err(Into::into)
}

/// Delete a cookie for the webview.
///
/// # Stability
///
/// See [Self::cookies].
pub fn delete_cookie(&self, cookie: &Cookie<'_>) -> crate::Result<()> {
self
.webview
.dispatcher
.delete_cookie(cookie)
.map_err(Into::into)
}
}

impl<R: Runtime> Listener<R> for Webview<R> {
Expand Down
26 changes: 20 additions & 6 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,15 +2211,11 @@ impl<R: Runtime> WebviewWindow<R> {
///
/// # 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.
/// See [Self::cookies].
///
/// # 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
/// See [Self::cookies].
pub fn cookies_for_url(&self, url: Url) -> crate::Result<Vec<Cookie<'static>>> {
self.webview.cookies_for_url(url)
}
Expand Down Expand Up @@ -2248,6 +2244,24 @@ impl<R: Runtime> WebviewWindow<R> {
pub fn cookies(&self) -> crate::Result<Vec<Cookie<'static>>> {
self.webview.cookies()
}

/// Set a cookie for the webview.
///
/// # Stability
///
/// See [Self::cookies].
pub fn set_cookie(&self, cookie: &Cookie<'_>) -> crate::Result<()> {
self.webview.set_cookie(cookie)
}

/// Delete a cookie for the webview.
///
/// # Stability
///
/// See [Self::cookies].
pub fn delete_cookie(&self, cookie: &Cookie<'_>) -> crate::Result<()> {
self.webview.delete_cookie(cookie)
}
}

impl<R: Runtime> Listener<R> for WebviewWindow<R> {
Expand Down