16
16
17
17
import static com .google .android .gms .common .internal .Preconditions .checkNotNull ;
18
18
19
- import android .annotation .SuppressLint ;
20
19
import androidx .annotation .NonNull ;
21
20
import androidx .annotation .Nullable ;
22
21
import androidx .annotation .VisibleForTesting ;
27
26
import com .google .firebase .FirebaseException ;
28
27
import com .google .firebase .annotations .concurrent .Background ;
29
28
import com .google .firebase .annotations .concurrent .Blocking ;
29
+ import com .google .firebase .annotations .concurrent .Lightweight ;
30
30
import com .google .firebase .appcheck .AppCheckProvider ;
31
31
import com .google .firebase .appcheck .AppCheckProviderFactory ;
32
32
import com .google .firebase .appcheck .AppCheckToken ;
@@ -52,6 +52,7 @@ public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
52
52
private final StorageHelper storageHelper ;
53
53
private final TokenRefreshManager tokenRefreshManager ;
54
54
private final Executor backgroundExecutor ;
55
+ private final Executor liteExecutor ;
55
56
private final Task <Void > retrieveStoredTokenTask ;
56
57
private final Clock clock ;
57
58
@@ -63,6 +64,7 @@ public DefaultFirebaseAppCheck(
63
64
@ NonNull FirebaseApp firebaseApp ,
64
65
@ NonNull Provider <HeartBeatController > heartBeatController ,
65
66
@ Background Executor backgroundExecutor ,
67
+ @ Lightweight Executor liteExecutor ,
66
68
@ Blocking ScheduledExecutorService scheduledExecutorService ) {
67
69
checkNotNull (firebaseApp );
68
70
checkNotNull (heartBeatController );
@@ -78,6 +80,7 @@ public DefaultFirebaseAppCheck(
78
80
/* firebaseAppCheck= */ this ,
79
81
scheduledExecutorService );
80
82
this .backgroundExecutor = backgroundExecutor ;
83
+ this .liteExecutor = liteExecutor ;
81
84
this .retrieveStoredTokenTask = retrieveStoredAppCheckTokenInBackground (backgroundExecutor );
82
85
this .clock = new Clock .DefaultClock ();
83
86
}
@@ -169,12 +172,11 @@ public void removeAppCheckListener(@NonNull AppCheckListener listener) {
169
172
appCheckTokenListenerList .size () + appCheckListenerList .size ());
170
173
}
171
174
172
- // TODO(b/261013814): Use an explicit executor in continuations.
173
- @ SuppressLint ("TaskMainThread" )
174
175
@ NonNull
175
176
@ Override
176
177
public Task <AppCheckTokenResult > getToken (boolean forceRefresh ) {
177
178
return retrieveStoredTokenTask .continueWithTask (
179
+ liteExecutor ,
178
180
unused -> {
179
181
if (!forceRefresh && hasValidToken ()) {
180
182
return Tasks .forResult (
@@ -188,6 +190,7 @@ public Task<AppCheckTokenResult> getToken(boolean forceRefresh) {
188
190
// TODO: Cache the in-flight task.
189
191
return fetchTokenFromProvider ()
190
192
.continueWithTask (
193
+ liteExecutor ,
191
194
appCheckTokenTask -> {
192
195
if (appCheckTokenTask .isSuccessful ()) {
193
196
return Tasks .forResult (
@@ -205,12 +208,11 @@ public Task<AppCheckTokenResult> getToken(boolean forceRefresh) {
205
208
});
206
209
}
207
210
208
- // TODO(b/261013814): Use an explicit executor in continuations.
209
- @ SuppressLint ("TaskMainThread" )
210
211
@ NonNull
211
212
@ Override
212
213
public Task <AppCheckToken > getAppCheckToken (boolean forceRefresh ) {
213
214
return retrieveStoredTokenTask .continueWithTask (
215
+ liteExecutor ,
214
216
unused -> {
215
217
if (!forceRefresh && hasValidToken ()) {
216
218
return Tasks .forResult (cachedToken );
@@ -223,26 +225,22 @@ public Task<AppCheckToken> getAppCheckToken(boolean forceRefresh) {
223
225
}
224
226
225
227
/** Fetches an {@link AppCheckToken} via the installed {@link AppCheckProvider}. */
226
- // TODO(b/261013814): Use an explicit executor in continuations.
227
- @ SuppressLint ("TaskMainThread" )
228
228
Task <AppCheckToken > fetchTokenFromProvider () {
229
229
return appCheckProvider
230
230
.getToken ()
231
- .continueWithTask (
232
- task -> {
233
- if (task .isSuccessful ()) {
234
- AppCheckToken token = task .getResult ();
235
- updateStoredToken (token );
236
- for (AppCheckListener listener : appCheckListenerList ) {
237
- listener .onAppCheckTokenChanged (token );
238
- }
239
- AppCheckTokenResult tokenResult =
240
- DefaultAppCheckTokenResult .constructFromAppCheckToken (token );
241
- for (AppCheckTokenListener listener : appCheckTokenListenerList ) {
242
- listener .onAppCheckTokenChanged (tokenResult );
243
- }
231
+ .onSuccessTask (
232
+ liteExecutor ,
233
+ token -> {
234
+ updateStoredToken (token );
235
+ for (AppCheckListener listener : appCheckListenerList ) {
236
+ listener .onAppCheckTokenChanged (token );
244
237
}
245
- return task ;
238
+ AppCheckTokenResult tokenResult =
239
+ DefaultAppCheckTokenResult .constructFromAppCheckToken (token );
240
+ for (AppCheckTokenListener listener : appCheckTokenListenerList ) {
241
+ listener .onAppCheckTokenChanged (tokenResult );
242
+ }
243
+ return Tasks .forResult (token );
246
244
});
247
245
}
248
246
0 commit comments