Closes #15882: check for account state abnormalities after account manager is ready

This commit is contained in:
Grigory Kruglov 2022-03-03 18:42:19 -08:00 committed by Grisha Kruglov
parent a320d75b61
commit 6c793e035b
2 changed files with 12 additions and 15 deletions

View File

@ -89,17 +89,7 @@ class AccountAbnormalities(
hadAccountPrior = prefPair.second
}
/**
* Once [accountManager] is initialized, queries it to detect abnormal account states.
* Call this right after running [FxaAccountManager.initAsync].
*
* @param accountManager An instance of [FxaAccountManager].
* @param initResult A deferred result of initializing [accountManager].
* @return A [Unit] deferred, resolved once [initResult] is resolved and state is processed for abnormalities.
*/
fun accountManagerStarted(
accountManager: FxaAccountManager
) {
override fun onReady(authenticatedAccount: OAuthAccount?) {
check(!accountManagerConfigured) { "accountManagerStarted called twice" }
accountManagerConfigured = true
@ -110,7 +100,7 @@ class AccountAbnormalities(
// account. This works because our account state is persisted in the application's
// directory, same as SharedPreferences. If user clears application data, both the
// fxa state and our flag will be removed.
val hasAccountNow = accountManager.authenticatedAccount() != null
val hasAccountNow = authenticatedAccount != null
if (hadAccountPrior && !hasAccountNow) {
prefs.edit().putBoolean(KEY_HAS_ACCOUNT, false).apply()

View File

@ -177,6 +177,8 @@ class BackgroundServices(
// unexpected logouts.
accountManager.register(accountAbnormalities)
accountManager.register(AccountManagerReadyObserver(accountManagerAvailableQueue))
// Enable push if it's configured.
push.feature?.let { autoPushFeature ->
FxaPushSupportFeature(context, accountManager, autoPushFeature, crashReporter)
@ -190,10 +192,7 @@ class BackgroundServices(
MainScope().launch {
accountManager.start()
accountAbnormalities.accountManagerStarted(accountManager)
}
}.also {
accountManagerAvailableQueue.ready()
}
/**
@ -204,6 +203,14 @@ class BackgroundServices(
}
}
private class AccountManagerReadyObserver(
private val accountManagerAvailableQueue: RunWhenReadyQueue
) : AccountObserver {
override fun onReady(authenticatedAccount: OAuthAccount?) {
accountManagerAvailableQueue.ready()
}
}
@VisibleForTesting(otherwise = PRIVATE)
internal class TelemetryAccountObserver(
private val settings: Settings,