Skip to content

Commit

Permalink
Closes mozilla-mobile#402: Synchronize blocks invoking Rust calls
Browse files Browse the repository at this point in the history
  • Loading branch information
carolkng committed Jul 10, 2018
1 parent 0b7d7f5 commit 9662b72
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import kotlinx.coroutines.experimental.launch

class Config(override var rawPointer: RawConfig?) : RustObject<RawConfig>() {
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<Config> {
val result = FxaResult<Config>()
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
Expand All @@ -29,12 +31,14 @@ class Config(override var rawPointer: RawConfig?) : RustObject<RawConfig>() {
fun custom(content_base: String): FxaResult<Config> {
val result = FxaResult<Config>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,50 @@
package mozilla.components.service.fxa

import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking

class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxAccount>() {

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<String>, wantsKeys: Boolean): FxaResult<String> {
val result = FxaResult<String>()
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
}

fun getProfile(ignoreCache: Boolean): FxaResult<Profile> {
val result = FxaResult<Profile>()
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
Expand All @@ -54,7 +59,7 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxA
}

fun newAssertion(audience: String): String? {
return runBlocking(FxaClient.THREAD_CONTEXT) {
return synchronized(FxaClient.INSTANCE) {
val e = Error.ByReference()
val p = FxaClient.INSTANCE.fxa_assertion_new(validPointer(), audience, e)
if (e.isFailure()) throw FxaException.fromConsuming(e)
Expand All @@ -63,7 +68,7 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxA
}

fun getTokenServerEndpointURL(): String? {
return runBlocking(FxaClient.THREAD_CONTEXT) {
return synchronized(FxaClient.INSTANCE) {
val e = Error.ByReference()
val p = FxaClient.INSTANCE.fxa_get_token_server_endpoint_url(validPointer(), e)
if (e.isFailure()) throw FxaException.fromConsuming(e)
Expand All @@ -72,7 +77,7 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxA
}

fun getSyncKeys(): SyncKeys {
return runBlocking(FxaClient.THREAD_CONTEXT) {
return synchronized(FxaClient.INSTANCE) {
val e = Error.ByReference()
val p = FxaClient.INSTANCE.fxa_get_sync_keys(validPointer(), e)
if (e.isFailure()) throw FxaException.fromConsuming(e)
Expand All @@ -82,28 +87,32 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxA

fun completeOAuthFlow(code: String, state: String): FxaResult<OAuthInfo> {
val result = FxaResult<OAuthInfo>()
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
}

fun getOAuthToken(scopes: Array<String>): FxaResult<OAuthInfo> {
val result = FxaResult<OAuthInfo>()
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
Expand All @@ -112,28 +121,32 @@ class FirefoxAccount(override var rawPointer: RawFxAccount?) : RustObject<RawFxA
companion object {
fun from(config: Config, clientId: String, webChannelResponse: String): FxaResult<FirefoxAccount> {
val result = FxaResult<FirefoxAccount>()
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
}

fun fromJSONString(json: String): FxaResult<FirefoxAccount> {
val result = FxaResult<FirefoxAccount>()
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
Expand Down

0 comments on commit 9662b72

Please sign in to comment.