diff options
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt index 4b9fd7b498..3793774e58 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt @@ -1,5 +1,11 @@ package net.mullvad.mullvadvpn.service.endpoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.ClosedReceiveChannelException +import kotlinx.coroutines.channels.actor +import kotlinx.coroutines.channels.sendBlocking import kotlinx.coroutines.delay import net.mullvad.mullvadvpn.model.GetAccountDataResult import net.mullvad.mullvadvpn.model.LoginStatus @@ -19,8 +25,15 @@ class AccountCache(private val endpoint: ServiceEndpoint) { // This is only used if the expiry was invalidated and fetching a new expiry returns the // same value as before the invalidation. private const val MAX_INVALIDATED_RETRIES = 7 + + private sealed class Command { + object CreateAccount : Command() + data class Login(val account: String) : Command() + } } + private val commandChannel = spawnActor() + private val daemon get() = endpoint.intermittentDaemon @@ -50,18 +63,12 @@ class AccountCache(private val endpoint: ServiceEndpoint) { } } - suspend fun createNewAccount() { - newlyCreatedAccount = true - createdAccountExpiry = null - - daemon.await().createNewAccount() + fun createNewAccount() { + commandChannel.sendBlocking(Command.CreateAccount) } - suspend fun login(account: String) { - if (account != accountNumber) { - markAccountAsNotNew() - daemon.await().setAccount(account) - } + fun login(account: String) { + commandChannel.sendBlocking(Command.Login(account)) } fun fetchAccountExpiry() { @@ -117,6 +124,35 @@ class AccountCache(private val endpoint: ServiceEndpoint) { onAccountExpiryChange.unsubscribeAll() onAccountHistoryChange.unsubscribeAll() onLoginStatusChange.unsubscribeAll() + + commandChannel.close() + } + + private fun spawnActor() = GlobalScope.actor<Command>(Dispatchers.Default, Channel.UNLIMITED) { + try { + val command = channel.receive() + + when (command) { + is Command.CreateAccount -> doCreateAccount() + is Command.Login -> doLogin(command.account) + } + } catch (exception: ClosedReceiveChannelException) { + // Command channel was closed, stop the actor + } + } + + private suspend fun doCreateAccount() { + newlyCreatedAccount = true + createdAccountExpiry = null + + daemon.await().createNewAccount() + } + + private suspend fun doLogin(account: String) { + if (account != accountNumber) { + markAccountAsNotNew() + daemon.await().setAccount(account) + } } private fun fetchAccountHistory() { |
