@@ -66,10 +66,6 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
66
66
private final Object lock = new Object ();
67
67
private final ExecutorService backgroundExecutor ;
68
68
private final ExecutorService networkExecutor ;
69
- /* FID of this Firebase Installations instance. Cached after successfully registering and
70
- persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/
71
- @ GuardedBy ("this" )
72
- private String cachedFid ;
73
69
74
70
@ GuardedBy ("lock" )
75
71
private final List <StateListener > listeners = new ArrayList <>();
@@ -225,7 +221,9 @@ String getName() {
225
221
@ Override
226
222
public Task <String > getId () {
227
223
preConditionChecks ();
228
- return Tasks .forResult (doGetId ());
224
+ Task <String > task = addGetIdListener ();
225
+ backgroundExecutor .execute (this ::doGetId );
226
+ return task ;
229
227
}
230
228
231
229
/**
@@ -241,7 +239,11 @@ public Task<String> getId() {
241
239
public Task <InstallationTokenResult > getToken (boolean forceRefresh ) {
242
240
preConditionChecks ();
243
241
Task <InstallationTokenResult > task = addGetAuthTokenListener ();
244
- backgroundExecutor .execute (() -> doGetAuthToken (forceRefresh ));
242
+ if (forceRefresh ) {
243
+ backgroundExecutor .execute (this ::doGetAuthTokenForceRefresh );
244
+ } else {
245
+ backgroundExecutor .execute (this ::doGetAuthTokenWithoutForceRefresh );
246
+ }
245
247
return task ;
246
248
}
247
249
@@ -256,6 +258,15 @@ public Task<Void> delete() {
256
258
return Tasks .call (backgroundExecutor , this ::deleteFirebaseInstallationId );
257
259
}
258
260
261
+ private Task <String > addGetIdListener () {
262
+ TaskCompletionSource <String > taskCompletionSource = new TaskCompletionSource <>();
263
+ StateListener l = new GetIdListener (taskCompletionSource );
264
+ synchronized (lock ) {
265
+ listeners .add (l );
266
+ }
267
+ return taskCompletionSource .getTask ();
268
+ }
269
+
259
270
private Task <InstallationTokenResult > addGetAuthTokenListener () {
260
271
TaskCompletionSource <InstallationTokenResult > taskCompletionSource =
261
272
new TaskCompletionSource <>();
@@ -292,25 +303,16 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
292
303
}
293
304
}
294
305
295
- private synchronized void updateCacheFid ( String cachedFid ) {
296
- this . cachedFid = cachedFid ;
306
+ private final void doGetId ( ) {
307
+ doRegistrationInternal ( false ) ;
297
308
}
298
309
299
- private synchronized String getCacheFid () {
300
- return cachedFid ;
310
+ private final void doGetAuthTokenWithoutForceRefresh () {
311
+ doRegistrationInternal ( false ) ;
301
312
}
302
313
303
- private String doGetId () {
304
-
305
- String fid = getCacheFid ();
306
- if (fid != null ) {
307
- return fid ;
308
- }
309
- PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
310
- // Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
311
- // i.e networkExecutor
312
- networkExecutor .execute (() -> doNetworkCallIfNecessary (false ));
313
- return prefs .getFirebaseInstallationId ();
314
+ private final void doGetAuthTokenForceRefresh () {
315
+ doRegistrationInternal (true );
314
316
}
315
317
316
318
/**
@@ -322,7 +324,7 @@ private String doGetId() {
322
324
* @param forceRefresh true if this is for a getAuthToken call and if the caller wants to fetch a
323
325
* new auth token from the server even if an unexpired auth token exists on the client.
324
326
*/
325
- private void doGetAuthToken (boolean forceRefresh ) {
327
+ private final void doRegistrationInternal (boolean forceRefresh ) {
326
328
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
327
329
328
330
// Since the caller wants to force an authtoken refresh remove the authtoken from the
@@ -359,11 +361,6 @@ private void doNetworkCallIfNecessary(boolean forceRefresh) {
359
361
// Store the prefs to persist the result of the previous step.
360
362
insertOrUpdatePrefs (prefs );
361
363
362
- // Update cachedFID, if FID is successfully REGISTERED and persisted.
363
- if (prefs .isRegistered ()) {
364
- updateCacheFid (prefs .getFirebaseInstallationId ());
365
- }
366
-
367
364
// Let the caller know about the result.
368
365
if (prefs .isErrored ()) {
369
366
triggerOnException (prefs , new FirebaseInstallationsException (Status .BAD_CONFIG ));
@@ -523,7 +520,6 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
523
520
case AUTH_ERROR :
524
521
// The the server refused to generate a new auth token due to bad credentials, clear the
525
522
// FID to force the generation of a new one.
526
- updateCacheFid (null );
527
523
return prefs .withNoGeneratedFid ();
528
524
default :
529
525
throw new FirebaseInstallationsException (
@@ -537,7 +533,6 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
537
533
* storage.
538
534
*/
539
535
private Void deleteFirebaseInstallationId () throws FirebaseInstallationsException {
540
- updateCacheFid (null );
541
536
PersistedInstallationEntry entry = getMultiProcessSafePrefs ();
542
537
if (entry .isRegistered ()) {
543
538
// Call the FIS servers to delete this Firebase Installation Id.
@@ -547,6 +542,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
547
542
/*projectID= */ getProjectIdentifier (),
548
543
/*refreshToken= */ entry .getRefreshToken ());
549
544
}
545
+
550
546
insertOrUpdatePrefs (entry .withNoGeneratedFid ());
551
547
return null ;
552
548
}
0 commit comments