From 9662b724ef79e1d095af3b90d8fc9a8241d993fd Mon Sep 17 00:00:00 2001 From: Carol Ng Date: Tue, 10 Jul 2018 14:25:08 -0400 Subject: [PATCH] Closes #402: Synchronize blocks invoking Rust calls --- .../mozilla/components/service/fxa/Config.kt | 30 +++-- .../components/service/fxa/FirefoxAccount.kt | 121 ++++++++++-------- 2 files changed, 84 insertions(+), 67 deletions(-) diff --git a/components/service/fxa/src/main/java/mozilla/components/service/fxa/Config.kt b/components/service/fxa/src/main/java/mozilla/components/service/fxa/Config.kt index dab085f7ed3..368ba4d618e 100644 --- a/components/service/fxa/src/main/java/mozilla/components/service/fxa/Config.kt +++ b/components/service/fxa/src/main/java/mozilla/components/service/fxa/Config.kt @@ -8,19 +8,21 @@ import kotlinx.coroutines.experimental.launch class Config(override var rawPointer: RawConfig?) : RustObject() { override fun destroy(p: RawConfig) { - FxaClient.INSTANCE.fxa_config_free(p) + synchronized(FxaClient.INSTANCE) { FxaClient.INSTANCE.fxa_config_free(p) } } companion object { fun release(): FxaResult { val result = FxaResult() val e = Error.ByReference() - launch(FxaClient.THREAD_CONTEXT) { - val cfg = FxaClient.INSTANCE.fxa_get_release_config(e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(Config(cfg)) + launch { + synchronized(FxaClient.INSTANCE) { + val cfg = FxaClient.INSTANCE.fxa_get_release_config(e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(Config(cfg)) + } } } return result @@ -29,12 +31,14 @@ class Config(override var rawPointer: RawConfig?) : RustObject() { fun custom(content_base: String): FxaResult { val result = FxaResult() val e = Error.ByReference() - launch(FxaClient.THREAD_CONTEXT) { - val cfg = FxaClient.INSTANCE.fxa_get_custom_config(content_base, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(Config(cfg)) + launch { + synchronized(FxaClient.INSTANCE) { + val cfg = FxaClient.INSTANCE.fxa_get_custom_config(content_base, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(Config(cfg)) + } } } return result diff --git a/components/service/fxa/src/main/java/mozilla/components/service/fxa/FirefoxAccount.kt b/components/service/fxa/src/main/java/mozilla/components/service/fxa/FirefoxAccount.kt index 38e954fc854..e83032e268d 100644 --- a/components/service/fxa/src/main/java/mozilla/components/service/fxa/FirefoxAccount.kt +++ b/components/service/fxa/src/main/java/mozilla/components/service/fxa/FirefoxAccount.kt @@ -5,31 +5,34 @@ package mozilla.components.service.fxa import kotlinx.coroutines.experimental.launch -import kotlinx.coroutines.experimental.runBlocking class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject() { constructor(config: Config, clientId: String): this(null) { - val e = Error.ByReference() - val result = FxaClient.INSTANCE.fxa_new(config.consumePointer(), clientId, e) - if (e.isFailure()) throw FxaException.fromConsuming(e) - this.rawPointer = result + synchronized(FxaClient.INSTANCE) { + val e = Error.ByReference() + val result = FxaClient.INSTANCE.fxa_new(config.consumePointer(), clientId, e) + if (e.isFailure()) throw FxaException.fromConsuming(e) + this.rawPointer = result + } } override fun destroy(p: RawFxAccount) { - runBlocking(FxaClient.THREAD_CONTEXT) { FxaClient.INSTANCE.fxa_free(p) } + synchronized(FxaClient.INSTANCE) { FxaClient.INSTANCE.fxa_free(p) } } fun beginOAuthFlow(redirectURI: String, scopes: Array, wantsKeys: Boolean): FxaResult { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val scope = scopes.joinToString(" ") - val e = Error.ByReference() - val p = FxaClient.INSTANCE.fxa_begin_oauth_flow(validPointer(), redirectURI, scope, wantsKeys, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(getAndConsumeString(p)) + launch { + synchronized(FxaClient.INSTANCE) { + val scope = scopes.joinToString(" ") + val e = Error.ByReference() + val p = FxaClient.INSTANCE.fxa_begin_oauth_flow(validPointer(), redirectURI, scope, wantsKeys, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(getAndConsumeString(p)) + } } } return result @@ -37,13 +40,15 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val e = Error.ByReference() - val p = FxaClient.INSTANCE.fxa_profile(validPointer(), ignoreCache, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(Profile(p)) + launch { + synchronized(FxaClient.INSTANCE) { + val e = Error.ByReference() + val p = FxaClient.INSTANCE.fxa_profile(validPointer(), ignoreCache, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(Profile(p)) + } } } return result @@ -54,7 +59,7 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val e = Error.ByReference() - val p = FxaClient.INSTANCE.fxa_complete_oauth_flow(validPointer(), code, state, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(OAuthInfo(p)) + launch { + synchronized(FxaClient.INSTANCE) { + val e = Error.ByReference() + val p = FxaClient.INSTANCE.fxa_complete_oauth_flow(validPointer(), code, state, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(OAuthInfo(p)) + } } } return result @@ -96,14 +103,16 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject): FxaResult { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val scope = scopes.joinToString(" ") - val e = Error.ByReference() - val p = FxaClient.INSTANCE.fxa_get_oauth_token(validPointer(), scope, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(OAuthInfo(p)) + launch { + synchronized(FxaClient.INSTANCE) { + val scope = scopes.joinToString(" ") + val e = Error.ByReference() + val p = FxaClient.INSTANCE.fxa_get_oauth_token(validPointer(), scope, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(OAuthInfo(p)) + } } } return result @@ -112,14 +121,16 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val e = Error.ByReference() - val raw = FxaClient.INSTANCE.fxa_from_credentials(config.consumePointer(), - clientId, webChannelResponse, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(FirefoxAccount(raw)) + launch { + synchronized(FxaClient.INSTANCE) { + val e = Error.ByReference() + val raw = FxaClient.INSTANCE.fxa_from_credentials(config.consumePointer(), + clientId, webChannelResponse, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(FirefoxAccount(raw)) + } } } return result @@ -127,13 +138,15 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject { val result = FxaResult() - launch(FxaClient.THREAD_CONTEXT) { - val e = Error.ByReference() - val raw = FxaClient.INSTANCE.fxa_from_json(json, e) - if (e.isFailure()) { - result.completeExceptionally(FxaException.fromConsuming(e)) - } else { - result.complete(FirefoxAccount(raw)) + launch { + synchronized(FxaClient.INSTANCE) { + val e = Error.ByReference() + val raw = FxaClient.INSTANCE.fxa_from_json(json, e) + if (e.isFailure()) { + result.completeExceptionally(FxaException.fromConsuming(e)) + } else { + result.complete(FirefoxAccount(raw)) + } } } return result