Yokan / rebuild
Course 1 8624561

WebViewService.desktop.kt

composeApp/src/desktopMain/kotlin/eu/friedcutlet/yokan/services/webview/WebViewService.desktop.kt

implementation54 lines
1package eu.friedcutlet.yokan.services.webview23import eu.friedcutlet.yokan.services.logging.AppLogger4import eu.friedcutlet.yokan.services.http.HttpResponse5import eu.friedcutlet.yokan.services.files.LocalFileService6import eu.friedcutlet.yokan.services.http.HttpService7import eu.friedcutlet.yokan.services.http.cookies.CookieJar8import eu.friedcutlet.yokan.services.notifications.InAppNotifications9import eu.friedcutlet.yokan.services.system.openSystemBrowser10import java.util.concurrent.atomic.AtomicBoolean11import kotlinx.coroutines.Dispatchers12import kotlinx.coroutines.runInterruptible1314internal actual fun createSharedPlatformWebViewService(http: HttpService, cookieJar: CookieJar, files: LocalFileService): WebViewService =15    DesktopWebViewService(cookieJar, files)1617private class DesktopWebViewService(18    private val cookieJar: CookieJar,19    private val files: LocalFileService,20) : WebViewService {21    override suspend fun open(request: WebViewRequest): Boolean {22        if (request.url.isBlank()) return false23        return openSystemBrowser(request.url)24    }2526    override suspend fun resolveCloudflare(request: WebViewRequest): HttpResponse? {27        if (request.url.isBlank()) return null28        // solve() blocks for up to minutes; keep it off the caller's thread and let29        // cancellation interrupt it so its finally block kills the browser process.30        val result = runInterruptible(Dispatchers.IO) { DesktopBrowserChallengeSession.solve(request.url, cookieJar, files.cacheRoot) }31            ?: return null32        cookieJar.rememberUserAgent(request.url, result.userAgent)33        return result.response34    }3536    override suspend fun evaluateScript(url: String, script: String, userAgent: String?): String {37        if (url.isBlank() || script.isBlank()) return ""38        if (scriptWarningShown.compareAndSet(false, true)) {39            val message = "Desktop WebView scripting is unavailable in this runtime. " +40                "Use the Cloudflare resolver or import cookies if the source needs a browser session."41            AppLogger.warn(message)42            InAppNotifications.post(43                title = "WebView script unavailable",44                message = message,45            )46        }47        return ""48    }4950    override suspend fun syncCookies(url: String): String? =51        cookieJar.cookieHeader(url)52}5354private val scriptWarningShown = AtomicBoolean(false)55

Search chapters, reference and source filenames.