Skip to content

Commit c51d35d

Browse files
committed
One more continuation in firebase-appcheck.
1 parent e6fca8b commit c51d35d

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/DefaultFirebaseAppCheck.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public DefaultFirebaseAppCheck(
7878
new TokenRefreshManager(
7979
firebaseApp.getApplicationContext(),
8080
/* firebaseAppCheck= */ this,
81+
liteExecutor,
8182
scheduledExecutorService);
8283
this.backgroundExecutor = backgroundExecutor;
8384
this.liteExecutor = liteExecutor;

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/DefaultTokenRefresher.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1919
import static java.util.concurrent.TimeUnit.SECONDS;
2020

21-
import android.annotation.SuppressLint;
2221
import androidx.annotation.NonNull;
2322
import androidx.annotation.VisibleForTesting;
2423
import com.google.firebase.annotations.concurrent.Blocking;
24+
import com.google.firebase.annotations.concurrent.Lightweight;
25+
import java.util.concurrent.Executor;
2526
import java.util.concurrent.ScheduledExecutorService;
2627
import java.util.concurrent.ScheduledFuture;
2728

@@ -36,15 +37,18 @@ public class DefaultTokenRefresher {
3637
@VisibleForTesting static final long MAX_DELAY_SECONDS = 16 * 60; // 16 minutes
3738

3839
private final DefaultFirebaseAppCheck firebaseAppCheck;
40+
private final Executor liteExecutor;
3941
private final ScheduledExecutorService scheduledExecutorService;
4042

4143
private volatile ScheduledFuture<?> refreshFuture;
4244
private volatile long delayAfterFailureSeconds;
4345

4446
DefaultTokenRefresher(
4547
@NonNull DefaultFirebaseAppCheck firebaseAppCheck,
48+
@Lightweight Executor liteExecutor,
4649
@Blocking ScheduledExecutorService scheduledExecutorService) {
4750
this.firebaseAppCheck = checkNotNull(firebaseAppCheck);
51+
this.liteExecutor = liteExecutor;
4852
this.scheduledExecutorService = scheduledExecutorService;
4953
this.delayAfterFailureSeconds = UNSET_DELAY;
5054
}
@@ -86,12 +90,10 @@ private long getNextRefreshMillis() {
8690
}
8791
}
8892

89-
// TODO(b/261013814): Use an explicit executor in continuations.
90-
@SuppressLint("TaskMainThread")
9193
private void onRefresh() {
9294
firebaseAppCheck
9395
.fetchTokenFromProvider()
94-
.addOnFailureListener(e -> scheduleRefreshAfterFailure());
96+
.addOnFailureListener(liteExecutor, e -> scheduleRefreshAfterFailure());
9597
}
9698

9799
/** Cancels the in-flight scheduled refresh. */

appcheck/firebase-appcheck/src/main/java/com/google/firebase/appcheck/internal/TokenRefreshManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.google.android.gms.common.api.internal.BackgroundDetector;
2424
import com.google.android.gms.common.api.internal.BackgroundDetector.BackgroundStateChangeListener;
2525
import com.google.firebase.annotations.concurrent.Blocking;
26+
import com.google.firebase.annotations.concurrent.Lightweight;
2627
import com.google.firebase.appcheck.AppCheckToken;
2728
import com.google.firebase.appcheck.internal.util.Clock;
29+
import java.util.concurrent.Executor;
2830
import java.util.concurrent.ScheduledExecutorService;
2931

3032
/** Class to manage whether or not to schedule an {@link AppCheckToken} refresh attempt. */
@@ -46,10 +48,12 @@ public final class TokenRefreshManager {
4648
TokenRefreshManager(
4749
@NonNull Context context,
4850
@NonNull DefaultFirebaseAppCheck firebaseAppCheck,
51+
@Lightweight Executor liteExecutor,
4952
@Blocking ScheduledExecutorService scheduledExecutorService) {
5053
this(
5154
checkNotNull(context),
52-
new DefaultTokenRefresher(checkNotNull(firebaseAppCheck), scheduledExecutorService),
55+
new DefaultTokenRefresher(
56+
checkNotNull(firebaseAppCheck), liteExecutor, scheduledExecutorService),
5357
new Clock.DefaultClock());
5458
}
5559

appcheck/firebase-appcheck/src/test/java/com/google/firebase/appcheck/internal/DefaultTokenRefresherTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.when;
2525

2626
import com.google.android.gms.tasks.Tasks;
27+
import com.google.common.util.concurrent.MoreExecutors;
2728
import com.google.firebase.appcheck.AppCheckToken;
2829
import java.util.concurrent.ScheduledExecutorService;
2930
import org.junit.Before;
@@ -44,7 +45,6 @@ public class DefaultTokenRefresherTest {
4445
private static final long TWO_MINUTES_SECONDS = 2L * 60L;
4546
private static final long FOUR_MINUTES_SECONDS = 4L * 60L;
4647
private static final long EIGHT_MINUTES_SECONDS = 8L * 60L;
47-
private static final String ERROR = "error";
4848

4949
@Mock DefaultFirebaseAppCheck mockFirebaseAppCheck;
5050
@Mock ScheduledExecutorService mockScheduledExecutorService;
@@ -59,8 +59,10 @@ public void setUp() {
5959
when(mockFirebaseAppCheck.fetchTokenFromProvider())
6060
.thenReturn(Tasks.forResult(mockAppCheckToken));
6161

62+
// TODO(b/258273630): Use TestOnlyExecutors.
6263
defaultTokenRefresher =
63-
new DefaultTokenRefresher(mockFirebaseAppCheck, mockScheduledExecutorService);
64+
new DefaultTokenRefresher(
65+
mockFirebaseAppCheck, MoreExecutors.directExecutor(), mockScheduledExecutorService);
6466
}
6567

6668
@Test

0 commit comments

Comments
 (0)