Skip to content

Commit 5aeb268

Browse files
authored
Merge pull request #38 from KRTirtho/fix-windows-get-cookies
fix(wry): WryCookieManager.getCookies always throw WrongThread exception on Windows
2 parents 58314a9 + a1001f6 commit 5aeb268

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

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

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,41 @@ class WryWebViewPanel(
275275
}
276276

277277
fun getCookiesForUrl(url: String): List<WebViewCookie> {
278-
return webviewId?.let {
279-
try {
280-
NativeBindings.getCookiesForUrl(it, url)
281-
} catch (e: Exception) {
282-
log("getCookiesForUrl failed: ${e.message}")
283-
emptyList()
284-
}
285-
} ?: emptyList()
278+
var result: List<WebViewCookie> = emptyList()
279+
val id = webviewId ?: run {
280+
log("getCookiesForUrl webviewId is null")
281+
return result
282+
}
283+
284+
val action = {
285+
result = runCatching { NativeBindings.getCookiesForUrl(id, url) }
286+
.onFailure { log("getCookiesForUrl failed: ${it.message}"); it.printStackTrace() }
287+
.getOrDefault(emptyList())
288+
}
289+
290+
if (SwingUtilities.isEventDispatchThread()) action() else SwingUtilities.invokeAndWait(
291+
action
292+
)
293+
return result
286294
}
287295

288296
fun getCookies(): List<WebViewCookie> {
289-
return webviewId?.let {
290-
try {
291-
NativeBindings.getCookies(it)
292-
} catch (e: Exception) {
293-
log("getCookies failed: ${e.message}")
294-
emptyList()
295-
}
296-
} ?: emptyList()
297+
var result: List<WebViewCookie> = emptyList()
298+
val id = webviewId ?: run {
299+
log("getCookies webviewId is null")
300+
return result
301+
}
302+
303+
val action = {
304+
result = runCatching { NativeBindings.getCookies(id) }
305+
.onFailure { log("getCookies failed: ${it.message}"); it.printStackTrace() }
306+
.getOrDefault(emptyList())
307+
}
308+
309+
if (SwingUtilities.isEventDispatchThread()) action() else SwingUtilities.invokeAndWait(
310+
action
311+
)
312+
return result
297313
}
298314

299315
fun clearCookiesForUrl(url: String) {
@@ -545,7 +561,13 @@ class WryWebViewPanel(
545561
if (toSend != lastBounds) {
546562
lastBounds = toSend
547563
log("setBounds id=$currentId pos=(${toSend.x}, ${toSend.y}) size=${toSend.width}x${toSend.height}")
548-
NativeBindings.setBounds(currentId, toSend.x, toSend.y, toSend.width, toSend.height)
564+
NativeBindings.setBounds(
565+
currentId,
566+
toSend.x,
567+
toSend.y,
568+
toSend.width,
569+
toSend.height
570+
)
549571
}
550572
if (pendingBounds == null) {
551573
stopBoundsTimer()
@@ -736,7 +758,8 @@ class WryWebViewPanel(
736758
private val IS_MAC = OS_NAME.contains("mac")
737759
private val IS_WINDOWS = OS_NAME.contains("windows")
738760
var LOG_ENABLED = run {
739-
val raw = System.getProperty("composewebview.wry.log") ?: System.getenv("WRYWEBVIEW_LOG")
761+
val raw =
762+
System.getProperty("composewebview.wry.log") ?: System.getenv("WRYWEBVIEW_LOG")
740763
when {
741764
raw == null -> false
742765
raw == "1" -> true

0 commit comments

Comments
 (0)