improve applyPrefs

This commit is contained in:
Fabian Freund
2026-03-11 02:53:51 +01:00
parent 3519f8238b
commit 3d18e97c49
2 changed files with 53 additions and 44 deletions
@@ -25,6 +25,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart'; import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:weblibre/core/logger.dart';
import 'package:weblibre/features/geckoview/features/preferences/data/models/preference_setting.dart'; import 'package:weblibre/features/geckoview/features/preferences/data/models/preference_setting.dart';
import 'package:weblibre/features/geckoview/features/tabs/utils/setting_groups_serializer.dart'; import 'package:weblibre/features/geckoview/features/tabs/utils/setting_groups_serializer.dart';
@@ -75,7 +76,14 @@ class StartupPreferenceEnforcementService
); );
if (prefsToEnforce.isNotEmpty) { if (prefsToEnforce.isNotEmpty) {
await GeckoPrefService().applyPrefs(prefsToEnforce); try {
await GeckoPrefService().applyPrefs(prefsToEnforce);
} catch (e) {
logger.w(
'Failed to enforce startup preferences, will retry on next launch',
error: e,
);
}
} }
} }
@@ -94,80 +94,81 @@ class GeckoPrefApiImpl : GeckoPrefApi, BrowserPrefObserverDelegate {
prefs: Map<String, Any>, prefs: Map<String, Any>,
callback: (Result<Map<String, GeckoPref>>) -> Unit callback: (Result<Map<String, GeckoPref>>) -> Unit
) { ) {
var fault: Boolean = false; if (prefs.isEmpty()) {
callback(Result.success(emptyMap()))
return
}
val entries = prefs.entries.toList()
fun applyNext(index: Int) {
if (index >= entries.size) {
getPrefs(prefs.keys.toList(), callback = callback)
return
}
val pref = entries[index]
fun onSuccess() = applyNext(index + 1)
fun onError(e: Exception) = callback(Result.failure(e))
for (pref in prefs) {
when (pref.value) { when (pref.value) {
is String -> components.core.engine.setBrowserPref( is String -> components.core.engine.setBrowserPref(
pref.key, pref.key,
pref.value as String, pref.value as String,
Branch.USER, Branch.USER,
onSuccess = {}, onSuccess = { onSuccess() },
onError = { onError = { onError(Exception("${it.message} ${it.cause}")) }
callback(Result.failure(Exception("${it.message} ${it.cause}")))
fault = true
}
) )
is Boolean -> components.core.engine.setBrowserPref( is Boolean -> components.core.engine.setBrowserPref(
pref.key, pref.key,
pref.value as Boolean, pref.value as Boolean,
Branch.USER, Branch.USER,
onSuccess = {}, onSuccess = { onSuccess() },
onError = { onError = { onError(Exception("${it.message} ${it.cause}")) }
callback(Result.failure(Exception("${it.message} ${it.cause}")))
fault = true
}
) )
is Long -> components.core.engine.setBrowserPref( is Long -> components.core.engine.setBrowserPref(
pref.key, pref.key,
(pref.value as Long).toInt(), (pref.value as Long).toInt(),
Branch.USER, Branch.USER,
onSuccess = {}, onSuccess = { onSuccess() },
onError = { onError = { onError(Exception("${it.message} ${it.cause}")) }
callback(Result.failure(Exception("${it.message} ${it.cause}")))
fault = true
}
) )
else -> { else -> {
callback(Result.failure(Exception("Unsupported value type: ${pref.value::class.simpleName}"))) onError(Exception("Unsupported value type: ${pref.value::class.simpleName}"))
fault = true
} }
} }
if (fault) {
return;
}
} }
getPrefs( applyNext(0)
prefs.keys.toList(),
callback = callback
)
} }
@OptIn(ExperimentalAndroidComponentsApi::class) @OptIn(ExperimentalAndroidComponentsApi::class)
override fun resetPrefs(preferenceNames: List<String>, callback: (Result<Unit>) -> Unit) { override fun resetPrefs(preferenceNames: List<String>, callback: (Result<Unit>) -> Unit) {
var fault: Boolean = false; if (preferenceNames.isEmpty()) {
callback(Result.success(Unit))
for (pref in preferenceNames) { return
components.core.engine.clearBrowserUserPref(
pref = pref,
onSuccess = { },
onError = {
callback(Result.failure(Exception("${it.message} ${it.cause}")))
fault = true
}
)
if (fault) {
return;
}
} }
callback(Result.success(Unit)) fun resetNext(index: Int) {
if (index >= preferenceNames.size) {
callback(Result.success(Unit))
return
}
components.core.engine.clearBrowserUserPref(
pref = preferenceNames[index],
onSuccess = { resetNext(index + 1) },
onError = {
callback(Result.failure(Exception("${it.message} ${it.cause}")))
}
)
}
resetNext(0)
} }
override fun startObserveChanges() { override fun startObserveChanges() {