DatL4g / KCEF

Kotlin implementation of jcefmaven with more modern setup and depending on JetBrains/jcef
https://datl4g.github.io/KCEF/
Apache License 2.0
44 stars 6 forks source link

KCEF restart required when not running app as an administrator #4

Open tomastiminskas opened 8 months ago

tomastiminskas commented 8 months ago

Some Windows users are getting the restart required message every time they open the app and the KCEF download doesn't start

After some debug sessions we found that it works perfect when running app as an administrator, so I guess the issue is related with write permissions. Any idea on how to fix this?

Thanks in advance

DatL4g commented 8 months ago

Yes read, write and execute permission is needed, so you have to use an install directory capable of these for the user.

You could use my tooling library, which takes care of it for each platform, take a look at #2

tomastiminskas commented 8 months ago

@DatL4g I'm using your Tooling library. The only difference I see with the example on the ticket are the jvmArgs in the build.gradle file. I was using this:

jvmArgs("--add-opens", "java.desktop/sun.awt=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/java.awt.peer=ALL-UNNAMED")

if (System.getProperty("os.name").contains("Mac")) {
     jvmArgs("--add-opens", "java.desktop/sun.lwawt=ALL-UNNAMED")
     jvmArgs("--add-opens", "java.desktop/sun.lwawt.macosx=ALL-UNNAMED")
}

instead of this:

jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/sun.awt=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/sun.java2d=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/java.awt.peer=ALL-UNNAMED")

if (System.getProperty("os.name").contains("Mac")) {
     jvmArgs("--add-opens", "java.desktop/sun.lwawt=ALL-UNNAMED")
     jvmArgs("--add-opens", "java.desktop/sun.lwawt.macosx=ALL-UNNAMED")
}

Could that be the cause of the issue on Windows?

DatL4g commented 8 months ago

It's possible, can't tell for sure as this are jcef requirements and I don't have a deep insight I recommend to use my implementation as it has no downside

tomastiminskas commented 8 months ago

@DatL4g ok I will test and let you know. I'm actually using your implementation. I'm just confused about one thing, in the ticket you linked in a previous comment you said we should use this:

val appWriteableRootFolder = Tooling.getApplicationWriteableRootFolder("your-app-name") ?: File("./")
val kcefInstallDir = File(appWriteableRootFolder, "kcef-bundle")

But later on the ticket you mentioned a full example of the burningseries project and when looking at the code I'm seeing you are using:

File(AppIO.getWriteableExecutableFolder(), "kcef-bundle")

and inside that class you are not using Tooling.getApplicationWriteableRootFolder. Should I use Tooling library or try a similar implementation than the one on AppIO class?

Thanks

DatL4g commented 8 months ago

The burning series project is just a bit older and the tooling library didn't exist back then. However the implementation is the same.

tomastiminskas commented 8 months ago

@DatL4g got it, thanks. I will test again with the changes on jvmArgs and let you know

tomastiminskas commented 8 months ago

@DatL4g I'm still getting "restart required" callback when not running app as an administrator. If I run as an administrator everything worked perfect. I will copy my code here in case you see something that might be causing the issues:

KCEF initialization

@Composable
fun WebViewInitializing(
    dashboardViewModel: DashboardViewModel
) {
    if (dashboardViewModel.isWebViewLoading()) {
        return
    }

    println("WEBVIEW LOADING")

    // Init WebView
    var restartRequired by remember { mutableStateOf(false) }
    var error by remember { mutableStateOf("") }
    var downloading by remember { mutableStateOf(0F) }
    var initialized by remember { mutableStateOf(false) } // if true, KCEF can be used to create clients, browsers etc
    val isDebug = false

    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) { // IO scope recommended but not required

            val kcefInstallDir = if (isDebug) {
                File("kcef-bundle")
            } else {
                val rootFolder = Tooling.getApplicationWriteableRootFolder("Sphinx") ?: File("./")
                File(rootFolder, "kcef-bundle")
            }

            KCEF.init(
                builder = {
                    installDir(kcefInstallDir)

                    progress {
                        onDownloading {
                            downloading = it
                            dashboardViewModel.setWebViewState(DashboardViewModel.WebViewState.Loading)
                        }
                        onInitialized {
                            dashboardViewModel.setWebViewState(DashboardViewModel.WebViewState.Initialized)
                            toast("Finished loading WebView library")
                            initialized = true
                        }
                    }
                },
                onError = {
                    error = it?.localizedMessage ?: ""
                    dashboardViewModel.setWebViewState(DashboardViewModel.WebViewState.Failed)
                    toast(error)
                },
                onRestartRequired = {
                    restartRequired = true
                    dashboardViewModel.setWebViewState(DashboardViewModel.WebViewState.Failed)
                    toast("Restart Required")
                }
            )
        }
    }
}

build.gradle.kts

import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import java.io.FileInputStream
import java.util.*

plugins {
    kotlin("multiplatform")
    id("org.jetbrains.compose") version "1.5.11"
}

group = "chat.sphinx"
version = "1.0"

repositories {
    mavenCentral()
    maven(url = "https://jitpack.io")
    maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots")
    maven(url = "https://jogamp.org/deployment/maven")
}

kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString()
        }
        withJava()
    }

    sourceSets {
        val jvmMain by getting {
            val kmpTorBinaryVersion = "0.4.7.8"
            val korauVersion = "3.2.0"
            val korioVersion = "3.2.0"

            dependencies {
                implementation(project(":common"))
                implementation(compose.desktop.currentOs)
                api(compose.preview)

                implementation("dev.datlag:kcef:2023.10.13")
                implementation("dev.datlag.tooling:tooling:1.1.0")
                implementation("io.matthewnelson.kotlin-components:kmp-tor-binary-linuxx64:$kmpTorBinaryVersion")
                implementation("io.matthewnelson.kotlin-components:kmp-tor-binary-macosx64:$kmpTorBinaryVersion")
                implementation("io.matthewnelson.kotlin-components:kmp-tor-binary-mingwx64:$kmpTorBinaryVersion")
                implementation("com.soywiz.korlibs.korio:korio:$korioVersion")
                implementation("com.soywiz.korlibs.korau:korau:$korauVersion")
                implementation("org.jetbrains.compose.ui:ui-graphics:1.5.1")
                implementation("uk.co.caprica:vlcj:4.7.1")

//                implementation ("com.github.skydoves:landscapist-glide:1.3.6")
//                implementation ("io.coil-kt:coil-compose:1.4.0")
            }
        }
        val jvmTest by getting
    }
}

compose.desktop {

    application {
        mainClass = "MainKt"

        jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
        jvmArgs("--add-opens", "java.desktop/sun.awt=ALL-UNNAMED")
        jvmArgs("--add-opens", "java.desktop/sun.java2d=ALL-UNNAMED")
        jvmArgs("--add-opens", "java.desktop/java.awt.peer=ALL-UNNAMED")

        if (System.getProperty("os.name").contains("Mac")) {
            jvmArgs("--add-opens", "java.desktop/sun.lwawt=ALL-UNNAMED")
            jvmArgs("--add-opens", "java.desktop/sun.lwawt.macosx=ALL-UNNAMED")
        }

        buildTypes {
            release {
                proguard {
                    configurationFiles.from(file("compose-desktop.pro"))
                }
            }
        }

        nativeDistributions {
            // Modules suggested by suggestRuntimeModules (avoids the ClassNotFoundException)
            modules("java.instrument", "java.management", "java.prefs", "java.sql", "jdk.unsupported")
            includeAllModules = true

            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "Sphinx"
            packageVersion = "1.0.23"

            val sphinxProperties = Properties().apply {
                val localPropertiesFile = project.file("../local.properties")
                if (localPropertiesFile.exists()) {
                    load(FileInputStream(localPropertiesFile))
                }
            }

            val macOsBundleID = sphinxProperties.getProperty("macOs.bundleID")
            val macOsSigningIdentity = sphinxProperties.getProperty("macOs.signing.identity")

            macOS {
                if (macOsBundleID?.isNotEmpty() == true) {
                    bundleID = macOsBundleID
                }

                signing {
                    if (macOsSigningIdentity?.isNotEmpty() == true) {
                        sign.set(true)
                        identity.set(macOsSigningIdentity)
                    }
                }
                iconFile.set(project.file("sphinx-logo.icns"))
            }
            windows {
                iconFile.set(project.file("sphinx-logo-64.ico"))
                dirChooser = true
            }
            linux {
                iconFile.set(project.file("sphinx-logo.png"))
            }
        }
    }
}

Any ideas?

dimaklekchyan commented 3 months ago

I have the same issue with the same settings