-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: add WebView::set_cookie and WebView::delete_cookie
#13661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2243582
d3ffa22
295f638
f7516b7
e54a70f
1511888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()`. |
| 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()`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1399,6 +1399,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), | ||
|
|
@@ -1688,6 +1690,30 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> { | |
| webview_getter!(self, WebviewMessage::Cookies)? | ||
| } | ||
|
|
||
| fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()> { | ||
| send_user_message( | ||
| &self.context, | ||
| Message::Webview( | ||
| *self.window_id.lock().unwrap(), | ||
| self.webview_id, | ||
| WebviewMessage::SetCookie(cookie.into_owned()), | ||
| ), | ||
| )?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()> { | ||
| send_user_message( | ||
| &self.context, | ||
| Message::Webview( | ||
| *self.window_id.lock().unwrap(), | ||
| self.webview_id, | ||
| WebviewMessage::DeleteCookie(cookie.clone().into_owned()), | ||
| ), | ||
| )?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { | ||
| send_user_message( | ||
| &self.context, | ||
|
|
@@ -3685,6 +3711,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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have some questions. Why do some methods use a |
||
| WebviewMessage::CookiesForUrl(url, tx) => { | ||
| let webview_cookies = webview | ||
| .cookies_for_url(url.as_str()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,19 @@ | |
| pub(crate) mod plugin; | ||
| mod webview_window; | ||
|
|
||
| use cookie::Cookie; | ||
| pub use webview_window::{WebviewWindow, WebviewWindowBuilder}; | ||
|
|
||
| /// Cookie crate used for [`Webview::set_cookie`] and [`Webview::delete_cookie`]. | ||
| /// | ||
| /// # Stability | ||
| /// | ||
| /// This re-exported crate is still on an alpha release and might receive updates in minor Tauri releases. | ||
| pub use cookie; | ||
| use http::HeaderMap; | ||
| use serde::Serialize; | ||
| use tauri_macros::default_runtime; | ||
| pub use tauri_runtime::webview::{NewWindowFeatures, PageLoadEvent}; | ||
| pub use tauri_runtime::Cookie; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to expose this as a public API. Wouldn't removing it cause some issues?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we should probably bring it back Edit: done in #14020 |
||
| #[cfg(desktop)] | ||
| use tauri_runtime::{ | ||
| dpi::{PhysicalPosition, PhysicalSize, Position, Size}, | ||
|
|
@@ -2031,15 +2037,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 | ||
|
|
@@ -2072,6 +2074,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> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
cookie.clone()here was forgotten to be removed.