pref observer and fixator

This commit is contained in:
Fabian Freund
2025-10-19 16:45:56 +02:00
parent 64d588ee3e
commit 73dab11113
16 changed files with 598 additions and 72 deletions
@@ -9,15 +9,17 @@ package eu.weblibre.flutter_mozilla_components.api
import eu.weblibre.flutter_mozilla_components.GlobalComponents
import eu.weblibre.flutter_mozilla_components.feature.PrefManagerFeature
import eu.weblibre.flutter_mozilla_components.feature.ResultConsumer
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoPref
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoPrefApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoPrefValue
import mozilla.components.ExperimentalAndroidComponentsApi
import mozilla.components.concept.engine.preferences.Branch
import mozilla.components.concept.engine.preferences.BrowserPrefObserverDelegate
import mozilla.components.concept.engine.preferences.BrowserPreference
import mozilla.components.support.ktx.android.org.json.toList
import org.json.JSONObject
import org.json.JSONArray
class GeckoPrefApiImpl : GeckoPrefApi {
class GeckoPrefApiImpl : GeckoPrefApi, BrowserPrefObserverDelegate {
private val components by lazy {
requireNotNull(GlobalComponents.components) { "Components not initialized" }
}
@@ -46,28 +48,32 @@ class GeckoPrefApiImpl : GeckoPrefApi {
}
override fun getPrefList(callback: (Result<List<String>>) -> Unit) {
PrefManagerFeature.scheduleRequest("getPrefList", Unit, object : ResultConsumer<JSONObject> {
override fun success(result: JSONObject) {
callback(Result.success(result.getJSONArray("result").toList()))
}
PrefManagerFeature.scheduleRequest(
"getPrefList",
Unit,
object : ResultConsumer<JSONObject> {
override fun success(result: JSONObject) {
callback(Result.success(result.getJSONArray("result").toList()))
}
override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
callback(Result.failure(Exception("$errorCode $errorMessage $errorDetails")))
}
})
override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
callback(Result.failure(Exception("$errorCode $errorMessage $errorDetails")))
}
})
}
@OptIn(ExperimentalAndroidComponentsApi::class)
override fun getPrefs(
preferenceFilter: List<String>,
callback: (Result<Map<String, GeckoPrefValue>>) -> Unit
callback: (Result<Map<String, GeckoPref>>) -> Unit
) {
components.core.engine.getBrowserPrefs(
preferenceFilter, onSuccess = {
callback(
Result.success(
it.associate {
it.pref to GeckoPrefValue(
it.pref to GeckoPref(
name = it.pref,
value = it.value,
defaultValue = it.defaultValue,
userValue = it.userValue,
@@ -85,7 +91,7 @@ class GeckoPrefApiImpl : GeckoPrefApi {
@OptIn(ExperimentalAndroidComponentsApi::class)
override fun applyPrefs(
prefs: Map<String, Any>,
callback: (Result<Map<String, GeckoPrefValue>>) -> Unit
callback: (Result<Map<String, GeckoPref>>) -> Unit
) {
var fault: Boolean = false;
@@ -148,7 +154,7 @@ class GeckoPrefApiImpl : GeckoPrefApi {
for (pref in preferenceNames) {
components.core.engine.clearBrowserUserPref(
pref = pref,
onSuccess = {},
onSuccess = { },
onError = {
callback(Result.failure(Exception("${it.message} ${it.cause}")))
fault = true
@@ -159,5 +165,49 @@ class GeckoPrefApiImpl : GeckoPrefApi {
return;
}
}
callback(Result.success(Unit))
}
override fun startObserveChanges() {
components.core.engine.registerPrefObserverDelegate(this);
}
override fun stopObserveChanges() {
components.core.engine.unregisterPrefObserverDelegate();
}
override fun registerPrefForObservation(name: String, callback: (Result<Unit>) -> Unit) {
components.core.engine.registerPrefForObservation(
name,
onSuccess = {
callback(Result.success(Unit))
},
onError = {
callback(Result.failure(Exception("${it.message} ${it.cause}")))
})
}
override fun unregisterPrefForObservation(name: String, callback: (Result<Unit>) -> Unit) {
components.core.engine.unregisterPrefForObservation(
name,
onSuccess = {
callback(Result.success(Unit))
},
onError = {
callback(Result.failure(Exception("${it.message} ${it.cause}")))
})
}
override fun onPreferenceChange(observedPreference: BrowserPreference<*>) {
components.flutterEvents.onPreferenceChange(
System.currentTimeMillis(), GeckoPref(
name = observedPreference.pref,
value = observedPreference.value,
defaultValue = observedPreference.defaultValue,
userValue = observedPreference.userValue,
hasUserChangedValue = observedPreference.hasUserChangedValue,
)
) { _ -> }
}
}
@@ -2196,7 +2196,8 @@ data class AddonCollection (
}
/** Generated class from Pigeon that represents data sent in messages. */
data class GeckoPrefValue (
data class GeckoPref (
val name: String,
val value: Any? = null,
val defaultValue: Any? = null,
val userValue: Any? = null,
@@ -2204,16 +2205,18 @@ data class GeckoPrefValue (
)
{
companion object {
fun fromList(pigeonVar_list: List<Any?>): GeckoPrefValue {
val value = pigeonVar_list[0]
val defaultValue = pigeonVar_list[1]
val userValue = pigeonVar_list[2]
val hasUserChangedValue = pigeonVar_list[3] as Boolean
return GeckoPrefValue(value, defaultValue, userValue, hasUserChangedValue)
fun fromList(pigeonVar_list: List<Any?>): GeckoPref {
val name = pigeonVar_list[0] as String
val value = pigeonVar_list[1]
val defaultValue = pigeonVar_list[2]
val userValue = pigeonVar_list[3]
val hasUserChangedValue = pigeonVar_list[4] as Boolean
return GeckoPref(name, value, defaultValue, userValue, hasUserChangedValue)
}
}
fun toList(): List<Any?> {
return listOf(
name,
value,
defaultValue,
userValue,
@@ -2221,7 +2224,7 @@ data class GeckoPrefValue (
)
}
override fun equals(other: Any?): Boolean {
if (other !is GeckoPrefValue) {
if (other !is GeckoPref) {
return false
}
if (this === other) {
@@ -2546,7 +2549,7 @@ private open class GeckoPigeonCodec : StandardMessageCodec() {
}
191.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
GeckoPrefValue.fromList(it)
GeckoPref.fromList(it)
}
}
else -> super.readValueOfType(type, buffer)
@@ -2802,7 +2805,7 @@ private open class GeckoPigeonCodec : StandardMessageCodec() {
stream.write(190)
writeValue(stream, value.toList())
}
is GeckoPrefValue -> {
is GeckoPref -> {
stream.write(191)
writeValue(stream, value.toList())
}
@@ -3761,9 +3764,13 @@ interface GeckoIconsApi {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface GeckoPrefApi {
fun getPrefList(callback: (Result<List<String>>) -> Unit)
fun getPrefs(preferenceFilter: List<String>, callback: (Result<Map<String, GeckoPrefValue>>) -> Unit)
fun applyPrefs(prefs: Map<String, Any>, callback: (Result<Map<String, GeckoPrefValue>>) -> Unit)
fun getPrefs(preferenceFilter: List<String>, callback: (Result<Map<String, GeckoPref>>) -> Unit)
fun applyPrefs(prefs: Map<String, Any>, callback: (Result<Map<String, GeckoPref>>) -> Unit)
fun resetPrefs(preferenceNames: List<String>, callback: (Result<Unit>) -> Unit)
fun startObserveChanges()
fun stopObserveChanges()
fun registerPrefForObservation(name: String, callback: (Result<Unit>) -> Unit)
fun unregisterPrefForObservation(name: String, callback: (Result<Unit>) -> Unit)
companion object {
/** The codec used by GeckoPrefApi. */
@@ -3798,7 +3805,7 @@ interface GeckoPrefApi {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val preferenceFilterArg = args[0] as List<String>
api.getPrefs(preferenceFilterArg) { result: Result<Map<String, GeckoPrefValue>> ->
api.getPrefs(preferenceFilterArg) { result: Result<Map<String, GeckoPref>> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(GeckoPigeonUtils.wrapError(error))
@@ -3818,7 +3825,7 @@ interface GeckoPrefApi {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val prefsArg = args[0] as Map<String, Any>
api.applyPrefs(prefsArg) { result: Result<Map<String, GeckoPrefValue>> ->
api.applyPrefs(prefsArg) { result: Result<Map<String, GeckoPref>> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(GeckoPigeonUtils.wrapError(error))
@@ -3851,6 +3858,76 @@ interface GeckoPrefApi {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoPrefApi.startObserveChanges$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
api.startObserveChanges()
listOf(null)
} catch (exception: Throwable) {
GeckoPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoPrefApi.stopObserveChanges$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
api.stopObserveChanges()
listOf(null)
} catch (exception: Throwable) {
GeckoPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoPrefApi.registerPrefForObservation$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val nameArg = args[0] as String
api.registerPrefForObservation(nameArg) { result: Result<Unit> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(GeckoPigeonUtils.wrapError(error))
} else {
reply.reply(GeckoPigeonUtils.wrapResult(null))
}
}
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoPrefApi.unregisterPrefForObservation$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val nameArg = args[0] as String
api.unregisterPrefForObservation(nameArg) { result: Result<Unit> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(GeckoPigeonUtils.wrapError(error))
} else {
reply.reply(GeckoPigeonUtils.wrapResult(null))
}
}
}
} else {
channel.setMessageHandler(null)
}
}
}
}
}
@@ -4422,6 +4499,23 @@ class GeckoStateEvents(private val binaryMessenger: BinaryMessenger, private val
}
}
}
fun onPreferenceChange(timestampArg: Long, valueArg: GeckoPref, callback: (Result<Unit>) -> Unit)
{
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
val channelName = "dev.flutter.pigeon.flutter_mozilla_components.GeckoStateEvents.onPreferenceChange$separatedMessageChannelSuffix"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(timestampArg, valueArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
}
} else {
callback(Result.failure(GeckoPigeonUtils.createConnectionError(channelName)))
}
}
}
}
/** Generated class from Pigeon that represents Flutter messages that can be called from Kotlin. */
class GeckoLogging(private val binaryMessenger: BinaryMessenger, private val messageChannelSuffix: String = "") {