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

desktopMain.com.multiplatform.webview.web.CefRequestExt.kt Maven / Gradle / Ivy

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

import com.multiplatform.webview.setting.WebSettings
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.browser.CefRequestContext
import org.cef.handler.*
import org.cef.misc.BoolRef
import org.cef.network.CefCookie
import org.cef.network.CefRequest
import org.cef.network.CefResponse

internal fun createModifiedRequestContext(
    settings: WebSettings
): CefRequestContext {
    val resourceRequestHandler = object : CefResourceRequestHandlerAdapter() {
        override fun onBeforeResourceLoad(
            browser: CefBrowser?,
            frame: CefFrame?,
            request: CefRequest?
        ): Boolean {
            if (request != null) {
                settings.customUserAgentString?.let(request::setUserAgentString)
            }
            return false
        }
    }

    return CefRequestContext.createContext(
        object : CefRequestContextHandlerAdapter() {
            override fun getResourceRequestHandler(
                browser: CefBrowser?,
                frame: CefFrame?,
                request: CefRequest?,
                isNavigation: Boolean,
                isDownload: Boolean,
                requestInitiator: String?,
                disableDefaultHandling: BoolRef?
            ): CefResourceRequestHandler {
                return resourceRequestHandler
            }
        }
    )
}

internal fun CefRequest.setUserAgentString(userAgent: String) {
    setHeaderByName("User-Agent", userAgent, true)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy