Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
7172: Close mozilla-mobile#7164: Add getSubscription to AutoPushFeature if one exists r=grigoryk a=jonalmeida



Co-authored-by: Jonathan Almeida <[email protected]>
  • Loading branch information
MozLando and jonalmeida committed May 29, 2020
2 parents cb312ff + aa4e35d commit 811e8ab
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onGetSubscription(scope) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Retrieving subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand All @@ -39,11 +35,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onSubscribe(scope, appServerKey) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Creating subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ class GeckoWebPushDelegateTest {

val nullResult = geckoDelegate.onGetSubscription("test")

nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Retrieving subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down Expand Up @@ -108,9 +107,8 @@ class GeckoWebPushDelegateTest {
subscription = null

val nullResult = geckoDelegate.onSubscribe("test", null)
nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Creating subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onGetSubscription(scope) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Retrieving subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand All @@ -39,11 +35,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onSubscribe(scope, appServerKey) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Creating subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ class GeckoWebPushDelegateTest {

val nullResult = geckoDelegate.onGetSubscription("test")

nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Retrieving subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down Expand Up @@ -108,9 +107,8 @@ class GeckoWebPushDelegateTest {
subscription = null

val nullResult = geckoDelegate.onSubscribe("test", null)
nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Creating subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onGetSubscription(scope) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Retrieving subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand All @@ -39,11 +35,7 @@ internal class GeckoWebPushDelegate(private val delegate: WebPushDelegate) : Gec
val result: GeckoResult<GeckoWebPushSubscription> = GeckoResult()

delegate.onSubscribe(scope, appServerKey) { subscription ->
if (subscription != null) {
result.complete(subscription.toGeckoSubscription())
} else {
result.completeExceptionally(WebPushException("Creating subscription failed."))
}
result.complete(subscription?.toGeckoSubscription())
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ class GeckoWebPushDelegateTest {

val nullResult = geckoDelegate.onGetSubscription("test")

nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Retrieving subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down Expand Up @@ -108,9 +107,8 @@ class GeckoWebPushDelegateTest {
subscription = null

val nullResult = geckoDelegate.onSubscribe("test", null)
nullResult?.exceptionally { throwable: Throwable ->
assertTrue(throwable.localizedMessage == "Creating subscription failed.")
GeckoResult.fromValue(null)
nullResult?.accept { sub ->
assertNull(sub)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,32 @@ class AutoPushFeature(
}
}

/**
* Checks if a subscription for the [scope] already exists.
*
* @param scope The subscription identifier which usually represents the website's URI.
* @param appServerKey An optional key provided by the application server.
* @param block The callback invoked when a subscription for the [scope] is found, otherwise null. Note: this will
* not execute on the calls thread.
*/
fun getSubscription(
scope: String,
appServerKey: String? = null,
block: (AutoPushSubscription?) -> Unit
) {
connection.ifInitialized {
coroutineScope.launchAndTry {
if (containsSubscription(scope)) {
// If we have a subscription, calling subscribe will give us the existing subscription.
// We do this because we do not have API symmetry across the different layers in this stack.
subscribe(scope, appServerKey, {}, block)
} else {
block(null)
}
}
}
}

/**
* Deletes the registration token locally so that it forces the service to get a new one the
* next time hits it's messaging server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ interface PushConnection : Closeable {
*/
suspend fun unsubscribeAll(): Boolean

/**
* Returns `true` if the specified [scope] has a subscription.
*/
suspend fun containsSubscription(scope: PushScope): Boolean

/**
* Updates the registration token to the native Push API if it changes.
*
Expand Down Expand Up @@ -154,6 +159,14 @@ internal class RustPushConnection(
return pushApi.unsubscribeAll()
}

@GuardedBy("this")
override suspend fun containsSubscription(scope: PushScope): Boolean = synchronized(this) {
val pushApi = api
check(pushApi != null) { "Rust API is not initiated; updateToken hasn't been called yet." }

return pushApi.dispatchInfoForChid(scope.toChannelId()) != null
}

@GuardedBy("this")
override suspend fun updateToken(token: String): Boolean = synchronized(this) {
val pushApi = api
Expand Down
Loading

0 comments on commit 811e8ab

Please sign in to comment.