package eu.friedcutlet.yokan.services.webview import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.HttpService interface WebViewService { suspend fun open(request: WebViewRequest): Boolean suspend fun resolveCloudflare(request: WebViewRequest): HttpResponse? /** * Headless JS evaluation against [url]. Returns the JSON-encoded result the WebView yielded, * or an empty string if WebView is not available on the current platform. The Android actual * drives `android.webkit.WebView`; desktop/iOS fall through to the stub unless a host app * supplies one. */ suspend fun evaluateScript(url: String, script: String, userAgent: String? = null): String = "" /** * Pull the platform WebView's current cookie set for [url] and persist it * into the shared `CookieJar`, so the next `http.request` for that host * carries the user's session. Called after page loads in the in-app * WebView and after every headless `evaluateScript` round-trip. * * Returns the rendered `name=value; …` cookie string that was harvested * (useful for logging / callers that want to confirm), or null if the * platform doesn't expose a cookie store. */ suspend fun syncCookies(url: String): String? = null } data class WebViewRequest( val url: String, val title: String, val headers: Map = emptyMap(), ) /** Default adopts the shared platform store; extras get their profile's isolated store where the platform has one. */ fun createPlatformWebViewService( http: HttpService, cookieJar: eu.friedcutlet.yokan.services.http.cookies.CookieJar, files: eu.friedcutlet.yokan.services.files.LocalFileService, scope: kotlinx.coroutines.CoroutineScope? = null, ): WebViewService = ProfileWebViewService(cookieJar, scope) { createSharedPlatformWebViewService(http, cookieJar, files) } internal expect fun createSharedPlatformWebViewService( http: HttpService, cookieJar: eu.friedcutlet.yokan.services.http.cookies.CookieJar, files: eu.friedcutlet.yokan.services.files.LocalFileService, ): WebViewService