diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-01-07 21:26:53 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-04-20 13:55:40 +0000 |
| commit | 6e9ee9561085e43e1c859ec2d50c00cd740feab6 (patch) | |
| tree | 4803eb4e89ea4b1bae566402798338092a45a429 /android | |
| parent | b3650a7b6a3d5f49090430422ca0710a2322a76c (diff) | |
| download | mullvadvpn-6e9ee9561085e43e1c859ec2d50c00cd740feab6.tar.xz mullvadvpn-6e9ee9561085e43e1c859ec2d50c00cd740feab6.zip | |
Use service side `AuthTokenCache`
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt | 45 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt | 3 |
2 files changed, 27 insertions, 21 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt index 3d0c4299ec..1ea2acec39 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt @@ -1,46 +1,49 @@ package net.mullvad.mullvadvpn.service.endpoint import kotlin.properties.Delegates.observable +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 net.mullvad.mullvadvpn.ipc.Event import net.mullvad.mullvadvpn.ipc.Request -import net.mullvad.mullvadvpn.service.MullvadDaemon class AuthTokenCache(endpoint: ServiceEndpoint) { - private var waitingForDaemon = false + companion object { + private enum class Command { + Fetch + } + } + + private val daemon = endpoint.intermittentDaemon + private val requestQueue = spawnActor() var authToken by observable<String?>(null) { _, _, token -> endpoint.sendEvent(Event.AuthToken(token)) } private set - var daemon by observable<MullvadDaemon?>(null) { _, _, _ -> - synchronized(this@AuthTokenCache) { - if (waitingForDaemon) { - fetchNewToken() - } - } - } - init { endpoint.dispatcher.registerHandler(Request.FetchAuthToken::class) { _ -> - fetchNewToken() + requestQueue.sendBlocking(Command.Fetch) } } fun onDestroy() { - daemon = null + requestQueue.close() } - private fun fetchNewToken() { - synchronized(this) { - val daemon = this.daemon - - if (daemon != null) { - authToken = daemon.getWwwAuthToken() - waitingForDaemon = false - } else { - waitingForDaemon = true + private fun spawnActor() = GlobalScope.actor<Command>(Dispatchers.Default, Channel.UNLIMITED) { + try { + for (command in channel) { + when (command) { + Command.Fetch -> authToken = daemon.await().getWwwAuthToken() + } } + } catch (exception: ClosedReceiveChannelException) { + // Closed sender, so stop the actor } } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt index da0926034b..f251f39340 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt @@ -41,6 +41,7 @@ class ServiceEndpoint( val accountCache = AccountCache(this) val appVersionInfoCache = AppVersionInfoCache(this) + val authTokenCache = AuthTokenCache(this) val customDns = CustomDns(this) val keyStatusListener = KeyStatusListener(this) val locationInfoCache = LocationInfoCache(this) @@ -59,6 +60,7 @@ class ServiceEndpoint( accountCache.onDestroy() appVersionInfoCache.onDestroy() + authTokenCache.onDestroy() connectionProxy.onDestroy() customDns.onDestroy() keyStatusListener.onDestroy() @@ -116,6 +118,7 @@ class ServiceEndpoint( Event.CurrentVersion(appVersionInfoCache.currentVersion), Event.AppVersionInfo(appVersionInfoCache.appVersionInfo), Event.NewRelayList(relayListListener.relayList), + Event.AuthToken(authTokenCache.authToken), Event.ListenerReady ) |
