Skip to content

Commit 1842845

Browse files
committed
feat: add functionality to retrieve all cookies from the webview.
1 parent 9b0b2a4 commit 1842845

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

wrywebview/src/main/kotlin/io/github/kdroidfilter/webview/wry/WryWebViewPanel.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ class WryWebViewPanel(
273273
} ?: emptyList()
274274
}
275275

276+
fun getCookies(): List<WebViewCookie> {
277+
return webviewId?.let {
278+
try {
279+
NativeBindings.getCookies(it)
280+
} catch (e: Exception) {
281+
log("getCookies failed: ${e.message}")
282+
emptyList()
283+
}
284+
} ?: emptyList()
285+
}
286+
276287
fun clearCookiesForUrl(url: String) {
277288
val action = { webviewId?.let { NativeBindings.clearCookiesForUrl(it, url) } }
278289
if (SwingUtilities.isEventDispatchThread()) {
@@ -792,6 +803,10 @@ private object NativeBindings {
792803
return io.github.kdroidfilter.webview.wry.getCookiesForUrl(id, url)
793804
}
794805

806+
fun getCookies(id: ULong): List<WebViewCookie> {
807+
return io.github.kdroidfilter.webview.wry.getCookies(id)
808+
}
809+
795810
fun clearCookiesForUrl(id: ULong, url: String) {
796811
io.github.kdroidfilter.webview.wry.clearCookiesForUrl(id, url)
797812
}

wrywebview/src/main/rust/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,25 @@ pub fn drain_ipc_messages(id: u64) -> Result<Vec<String>, WebViewError> {
763763
// Cookies
764764
// ============================================================================
765765

766+
fn get_cookies_inner(id: u64) -> Result<Vec<WebViewCookie>, WebViewError> {
767+
wry_log!("[wrywebview] get_cookies id={}", id);
768+
with_webview(id, |webview| {
769+
let cookies = webview.cookies().map_err(WebViewError::from)?;
770+
Ok(cookies.iter().map(cookie_record_from).collect())
771+
})
772+
}
773+
774+
#[uniffi::export]
775+
pub fn get_cookies(id: u64) -> Result<Vec<WebViewCookie>, WebViewError> {
776+
#[cfg(target_os = "linux")]
777+
{
778+
return run_on_gtk_thread(move || get_cookies_inner(id));
779+
}
780+
781+
#[cfg(not(target_os = "linux"))]
782+
run_on_main_thread(move || get_cookies_inner(id))
783+
}
784+
766785
fn get_cookies_for_url_inner(id: u64, url: String) -> Result<Vec<WebViewCookie>, WebViewError> {
767786
wry_log!("[wrywebview] get_cookies_for_url id={} url={}", id, url);
768787
with_webview(id, |webview| {

0 commit comments

Comments
 (0)