Skip to content

Commit

Permalink
🚑 [kmp/dwebview] 修复android的profile向下兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Jul 4, 2024
1 parent bbbf666 commit 7093c07
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.annotation.SuppressLint
import androidx.webkit.ProfileStore
import androidx.webkit.WebViewCompat
import org.dweb_browser.dwebview.engine.DWebViewEngine
import org.dweb_browser.helper.compose.ENV_SWITCH_KEY
import org.dweb_browser.helper.compose.envSwitch
import org.dweb_browser.helper.platform.keyValueStore
import org.dweb_browser.helper.withMainContext

Expand All @@ -21,6 +19,7 @@ class ChromiumWebProfileStore(private val profileStore: ProfileStore) : AndroidW
*/
init {
for (name in profileStore.allProfileNames.toList()) {
/// 上个版本的隐私模式使用 incognito_ 前缀,但几乎没有生效过
if (name.endsWith(IncognitoSuffix)) {
profileStore.deleteProfile(name)
}
Expand All @@ -38,24 +37,22 @@ class ChromiumWebProfileStore(private val profileStore: ProfileStore) : AndroidW

override suspend fun getAllProfileNames() = allProfiles.keys.toList()
override val isSupportIncognitoProfile: Boolean = true
override fun getOrCreateProfile(engine: DWebViewEngine, profileName: String): DWebProfile = when {
envSwitch.isEnabled(ENV_SWITCH_KEY.DWEBVIEW_PROFILE) ->
(allProfiles[profileName] ?: profileStore.getOrCreateProfile(profileName)).let { profile ->
WebViewCompat.setProfile(engine, profileName)
ChromiumWebProfile(profile).also {
allProfiles[profileName] = it
keyValueStore.setValues(DwebProfilesKey, allProfiles.keys)
}
override fun getOrCreateProfile(engine: DWebViewEngine, profileName: String): DWebProfile =
/// 上个版本(240622)起,已经在使用 profileStore 来创建并使用 profile 了
(allProfiles[profileName] ?: profileStore.getOrCreateProfile(profileName)).let { profile ->
WebViewCompat.setProfile(engine, profileName)
ChromiumWebProfile(profile).also {
allProfiles[profileName] = it
keyValueStore.setValues(DwebProfilesKey, allProfiles.keys)
}

else -> CompactDWebProfile("*")
}
}

override fun getOrCreateIncognitoProfile(
engine: DWebViewEngine,
sessionId: String,
profileName: String,
): ChromiumWebProfile {
/// 上个版本(240622)的 incognito 模式使用更简单的 prefix 模式,但几乎没有生效过
val incognitoProfileName = "$profileName@$sessionId$IncognitoSuffix"
return (profileStore.getOrCreateProfile(incognitoProfileName)).let { profile ->
WebViewCompat.setProfile(engine, incognitoProfileName)
Expand Down

0 comments on commit 7093c07

Please sign in to comment.