summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-01-15 12:46:50 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-31 14:36:30 +0000
commit4532e9a9fa613af18f4b0fa33327ba21403ecbb4 (patch)
treea43e632f08a548a2892be7b35ba1352aefe17bf4 /android/src/main
parent2bda6638351326752254fc0d7af1fccd36437142 (diff)
downloadmullvadvpn-4532e9a9fa613af18f4b0fa33327ba21403ecbb4.tar.xz
mullvadvpn-4532e9a9fa613af18f4b0fa33327ba21403ecbb4.zip
Refactor `AccountCache` login logic into an actor
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt56
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() {