Skip to content

Conversation

@charrondev
Copy link
Contributor

@charrondev charrondev commented Feb 9, 2025

Closes

Related

Related issues not fully handled by this PR:

Changes

  • Added Webview::cookies_for_url() and tauri::Cookie

Implementation notes

I'm open to any requested changes here, but here's how I've gone about this:

  • Expose cookie::Cookie as a re-export from tauri_runtime::Cookie and tauri::Cookie. Notably I did not re-export the wry::Cookie which is a re-export of cookie::Cookie since tauri_runtime does not have a dependency on wry.
  • Add cookies_for_url() on the webview dispatcher and add an implementation for wry. I used WebviewDispatcher::reparent as my template for a dispatch the takes a parameter and returns a result.
  • I chose not to expose wry::webview::cookies() as it seemed to have an implementation note that it didn't work on Android at the moment.

Notes from my testing

Notably the wry support seems to only work when a site served over http (which is what I need it for). In my manual testing there doesn't seem to be a clear story for cookies on the tauri:// protocol (and there probably shouldn't be?). In any case by linking this PR into my own project I am able to extract the cookies from content served over http/https.

@charrondev charrondev requested a review from a team as a code owner February 9, 2025 19:26
@charrondev charrondev force-pushed the feat/cookies-for-url branch from e2ca406 to bccd107 Compare February 9, 2025 19:32
@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2025

Package Changes Through b53ebb2

There are 8 changes which include tauri with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-utils with patch, tauri-cli with minor, @tauri-apps/cli with minor, @tauri-apps/api with minor, tauri-bundler with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.3.0 2.4.0
tauri-utils 2.2.0 2.2.1
tauri-bundler 2.2.4 2.2.5
tauri-runtime 2.4.0 2.5.0
tauri-runtime-wry 2.4.1 2.5.0
tauri-codegen 2.0.5 2.0.6
tauri-macros 2.0.5 2.0.6
tauri-plugin 2.0.5 2.0.6
tauri-build 2.0.6 2.0.7
tauri 2.3.1 2.4.0
@tauri-apps/cli 2.3.1 2.4.0
tauri-cli 2.3.1 2.4.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

Copy link
Member

@FabianLars FabianLars left a comment

Choose a reason for hiding this comment

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

thank you!!

I chose not to expose wry::webview::cookies() as it seemed to have an implementation note that it didn't work on Android at the moment.

It would be fine to do the same in tauri as well (having this function return an empty vec with a platform-specific note)

Ok(())
}

/// Returns all cookies for a specified URL including HTTP-only and secure cookies.
Copy link
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
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.

@charrondev
Copy link
Contributor Author

I've added the relevant note and the cookies() function.

I've also gone in and fixed up my fustfmt and clippy issues.

@charrondev
Copy link
Contributor Author

As I'm testing this more deeply, I'm finding some concerning going. Fetching cookies is occasionally hanging the entire process. I will try to dig in some more.

@charrondev
Copy link
Contributor Author

So after testing a bit more I've narrowed down the source of the hangs. It seems to happen in the MacOS implemenation specifically getting caught up in wry's wait_for_blocking_operation().

https://github.com/tauri-apps/wry/blob/cf181943444bca172d969615471997e48e10b611/src/wkwebview/mod.rs#L1057-L1077

I've gone and made a PR over on wry to resolve it tauri-apps/wry#1486

@Bubbet
Copy link

Bubbet commented Feb 26, 2025

Where are you meant to use this? I've tried using the pr in a on_page_load event and inside a command and in both circumstances it just gets hung or something and never actually returns the cookies. The process doesn't lock up but its like it waiting forever. Edit: I'm also on windows.

@AlloryDante
Copy link

Where are you meant to use this? I've tried using the pr in a on_page_load event and inside a command and in both circumstances it just gets hung or something and never actually returns the cookies. The process doesn't lock up but its like it waiting forever.

I get the same hunging when I do it on windows....

@lucasfernog lucasfernog changed the title feat: add tauri::Webview::cookies_by_url() and tauri::Cookie feat: add Webview::cookies and Webview::cookies_for_url() Mar 15, 2025
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Mar 15, 2025
@lucasfernog
Copy link
Member

the issue on Windows is similar to window creation, which must be done on a separate thread because we need to wait for an async operation on the webview side. see tauri-apps/wry#583
i've updated the docs to mention this behavior

@AlloryDante
Copy link

the issue on Windows is similar to window creation, which must be done on a separate thread because we need to wait for an async operation on the webview side. see tauri-apps/wry#583 i've updated the docs to mention this behavior

Got it. Thank you for the docs and for the example file.

@lucasfernog
Copy link
Member

the only gotcha here is that we're re-exporting the cookie crate which is on 0.18 meaning we can't update it to 0.19 for instance without it being flagged as a breaking change, i'll at least update our toml for this

lucasfernog
lucasfernog previously approved these changes Mar 15, 2025
@FabianLars
Copy link
Member

alternatively we could do the same we do in with_webview and document that this may change between minor versions. Seeing the crate's activity we probably don't have to worry about new 0.x versions in the near future.

@AlloryDante
Copy link

AlloryDante commented Mar 15, 2025

Finally we will have a way to get the cookies for the websites. If wry allows maybe in the future it would be nice to also have a set cookie method. I know for a fact that I am gonna use it...

Thank you so much for adding support for this @lucasfernog . I know it doesnt seem much but for my use case I am heavly dependant on cookie access and I was actually deciding if it would be better to switch to electron and you added this nice PR.

@lucasfernog
Copy link
Member

thanks to @charrondev for bringing the bindings here and @amrbashir for working on the wry implementations

@lucasfernog lucasfernog merged commit cedb24d into tauri-apps:dev Mar 15, 2025
3 checks passed
@github-project-automation github-project-automation bot moved this from 📬Proposal to 🔎 In audit in Roadmap Mar 15, 2025
WofWca added a commit to WofWca/tauri that referenced this pull request Mar 15, 2025
The issue was introduced in cedb24d
(tauri-apps#12665).

As the `.cookies()` docs state, the command should be async.
WofWca added a commit to WofWca/tauri that referenced this pull request Mar 16, 2025
Remove the cookies code from it.
Firtly, they are not necessary: "Hello, world!" should be simple.
Secondly, it's broken on Windows: `.cookies()` hangs.

The cookies stuff was introduced recently,
in cedb24d
(tauri-apps#12665).

This supersedes tauri-apps#12992.
WofWca added a commit to WofWca/tauri that referenced this pull request Mar 16, 2025
Remove the cookies code from it.
Firtly, they are not necessary: "Hello, world!" should be simple.
Secondly, it's broken on Windows: `.cookies()` hangs.

The cookies stuff was introduced recently,
in cedb24d
(tauri-apps#12665).
This commit reverts the changes to the example.

This supersedes tauri-apps#12992.
WofWca added a commit to WofWca/tauri that referenced this pull request Mar 16, 2025
Remove the cookies code from it.
Firtly, they are not necessary: "Hello, world!" should be simple.
Secondly, it's broken on Windows: `.cookies()` hangs.

The cookies stuff was introduced recently,
in cedb24d
(tauri-apps#12665).
This commit reverts the changes to the example.

This supersedes tauri-apps#12992.
amrbashir pushed a commit that referenced this pull request Mar 16, 2025
Remove the cookies code from it.
Firtly, they are not necessary: "Hello, world!" should be simple.
Secondly, it's broken on Windows: `.cookies()` hangs.

The cookies stuff was introduced recently,
in cedb24d
(#12665).
This commit reverts the changes to the example.

This supersedes #12992.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 In audit

Development

Successfully merging this pull request may close these issues.

6 participants