Commit 9186ba75 authored by Grisha Kruglov's avatar Grisha Kruglov Committed by Grisha Kruglov
Browse files

Part 4: Make AccountStorage non-configurable in FxaAccountManager

In order to sync, a background worker needs to have access to the current account.
We could serialize the account information as part of the Data object that the worker
will operate over, but that's potentially problematic, as we'll inevitably risk our
serialized account information will become stale by the time worker runs.

This means that it's much better if we can just create an instance of the current account
inside the worker. For that, it needs to have access to the account storage object mechanism
that's used within the AccountManager. Rather than dealing with making SyncManager
configurable with different types of account storage implementations, let's assume
we're going to use a single one, defined in the services-fxa module. This makes
our life easier for now, and is easy enough to re-visit in the future if we need to.
parent 016538ce
......@@ -38,7 +38,7 @@ class TestableFxaAccountManager(
scopes: Array<String>,
accountStorage: AccountStorage = SharedPrefAccountStorage(context),
val block: () -> OAuthAccount = { mock() }
) : FxaAccountManager(context, config, scopes, null, accountStorage) {
) : FxaAccountManager(context, config, scopes, null) {
override fun createAccount(config: Config): OAuthAccount {
return block()
}
......
......@@ -108,8 +108,7 @@ open class FxaAccountManager(
private val context: Context,
private val config: Config,
private val scopes: Array<String>,
syncManager: SyncManager? = null,
private val accountStorage: AccountStorage = SharedPrefAccountStorage(context)
syncManager: SyncManager? = null
) : Closeable, Observable<AccountObserver> by ObserverRegistry() {
private val logTag = "FirefoxAccountStateMachine"
......@@ -290,7 +289,7 @@ open class FxaAccountManager(
Event.Init -> {
// Locally corrupt accounts are simply treated as 'absent'.
val savedAccount = try {
accountStorage.read()
getAccountStorage().read()
} catch (e: FxaException) {
Log.log(
tag = logTag,
......@@ -321,7 +320,7 @@ open class FxaAccountManager(
profile = null
account.close()
// Delete persisted state.
accountStorage.clear()
getAccountStorage().clear()
// Re-initialize account.
account = createAccount(config)
......@@ -353,7 +352,7 @@ open class FxaAccountManager(
when (via) {
is Event.Authenticated -> {
account.completeOAuthFlow(via.code, via.state).await()
accountStorage.write(account)
getAccountStorage().write(account)
notifyObservers { onAuthenticated(account) }
......@@ -406,6 +405,11 @@ open class FxaAccountManager(
return FirefoxAccount(config)
}
@VisibleForTesting
open fun getAccountStorage(): AccountStorage {
return SharedPrefAccountStorage(context)
}
private class SyncManagerIntegration(private val syncManager: SyncManager) : AccountObserver {
override fun onLoggedOut() {
syncManager.loggedOut()
......
......@@ -38,12 +38,16 @@ class TestableFxaAccountManager(
context: Context,
config: Config,
scopes: Array<String>,
accountStorage: AccountStorage = SharedPrefAccountStorage(context),
private val storage: AccountStorage,
val block: () -> OAuthAccount = { mock() }
) : FxaAccountManager(context, config, scopes, null, accountStorage) {
) : FxaAccountManager(context, config, scopes, null) {
override fun createAccount(config: Config): OAuthAccount {
return block()
}
override fun getAccountStorage(): AccountStorage {
return storage
}
}
@RunWith(RobolectricTestRunner::class)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment