All Downloads are FREE. Search and download functionalities are using the official Maven repository.

desktopMain.com.multiplatform.webview.cookie.DesktopCookieManager.kt Maven / Gradle / Ivy

There is a newer version: 1.9.40
Show newest version
package com.multiplatform.webview.cookie

import com.multiplatform.webview.util.KLogger
import dev.datlag.kcef.KCEFCookieManager
import org.cef.network.CefCookie
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

object DesktopCookieManager : CookieManager {
    override suspend fun setCookie(
        url: String,
        cookie: Cookie,
    ) {
        val currentTime = System.currentTimeMillis()
        val cefCookie =
            CefCookie(
                cookie.name,
                cookie.value,
                cookie.domain,
                cookie.path,
                cookie.isSecure ?: false,
                cookie.isHttpOnly ?: false,
                Date(currentTime),
                Date(currentTime),
                Date(cookie.expiresDate ?: currentTime).before(Date(currentTime)),
                Date(cookie.expiresDate ?: System.currentTimeMillis()),
            )
        val addedCookie = KCEFCookieManager.instance.setCookie(url, cefCookie)
        KLogger.i(tag = "DesktopCookieManager") { "Added Cookie: $addedCookie" }
    }

    override suspend fun getCookies(url: String): List {
        KLogger.i(tag = "DesktopCookieManager") { "DesktopCookieManager getCookies: $url" }

        return KCEFCookieManager.instance.getCookiesWhile(url, true).map {
            Cookie(
                name = it.name,
                value = it.value,
                domain = it.domain,
                path = it.path,
                expiresDate = it.expires?.time,
                sameSite = null,
                isSecure = it.secure,
                isHttpOnly = it.httponly,
                maxAge = null,
            )
        }
    }

    override suspend fun removeAllCookies() {
        KCEFCookieManager.instance.deleteAllCookies()
    }

    override suspend fun removeCookies(url: String) {
        KCEFCookieManager.instance.deleteCookies(url)
    }
}

actual fun getCookieExpirationDate(expiresDate: Long): String {
    val sdf =
        SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US).apply {
            timeZone = TimeZone.getTimeZone("GMT")
        }
    return sdf.format(Date(expiresDate))
}

/**
 * Returns an instance of [DesktopCookieManager] for Desktop.
 */
@Suppress("FunctionName") // Builder Function
actual fun WebViewCookieManager(): CookieManager = DesktopCookieManager




© 2015 - 2025 Weber Informatics LLC | Privacy Policy