Skip to content

Commit 6f6006e

Browse files
authored
Patch caching fid in memory on successful registration. (#1764)
* Revert "Completing getId call with the disk reads on the caller thread. (#1570)" This reverts commit 1a36dd7 * Updating caching FID implementation due to the revert PR. * Addressing Rayo's comments. * Minor spacing. * Addressing Rayo's comments.
1 parent 0ce007a commit 6f6006e

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
6666
private final Object lock = new Object();
6767
private final ExecutorService backgroundExecutor;
6868
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;
6973

7074
@GuardedBy("lock")
7175
private final List<StateListener> listeners = new ArrayList<>();
@@ -221,8 +225,15 @@ String getName() {
221225
@Override
222226
public Task<String> getId() {
223227
preConditionChecks();
228+
229+
// Return cached fid if available.
230+
String fid = getCacheFid();
231+
if (fid != null) {
232+
return Tasks.forResult(fid);
233+
}
234+
224235
Task<String> task = addGetIdListener();
225-
backgroundExecutor.execute(this::doGetId);
236+
backgroundExecutor.execute(() -> doRegistrationOrRefresh(false));
226237
return task;
227238
}
228239

@@ -239,11 +250,7 @@ public Task<String> getId() {
239250
public Task<InstallationTokenResult> getToken(boolean forceRefresh) {
240251
preConditionChecks();
241252
Task<InstallationTokenResult> task = addGetAuthTokenListener();
242-
if (forceRefresh) {
243-
backgroundExecutor.execute(this::doGetAuthTokenForceRefresh);
244-
} else {
245-
backgroundExecutor.execute(this::doGetAuthTokenWithoutForceRefresh);
246-
}
253+
backgroundExecutor.execute(() -> doRegistrationOrRefresh(forceRefresh));
247254
return task;
248255
}
249256

@@ -261,20 +268,22 @@ public Task<Void> delete() {
261268
private Task<String> addGetIdListener() {
262269
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
263270
StateListener l = new GetIdListener(taskCompletionSource);
264-
synchronized (lock) {
265-
listeners.add(l);
266-
}
271+
addStateListeners(l);
267272
return taskCompletionSource.getTask();
268273
}
269274

270275
private Task<InstallationTokenResult> addGetAuthTokenListener() {
271276
TaskCompletionSource<InstallationTokenResult> taskCompletionSource =
272277
new TaskCompletionSource<>();
273278
StateListener l = new GetAuthTokenListener(utils, taskCompletionSource);
279+
addStateListeners(l);
280+
return taskCompletionSource.getTask();
281+
}
282+
283+
private void addStateListeners(StateListener l) {
274284
synchronized (lock) {
275285
listeners.add(l);
276286
}
277-
return taskCompletionSource.getTask();
278287
}
279288

280289
private void triggerOnStateReached(PersistedInstallationEntry persistedInstallationEntry) {
@@ -303,16 +312,12 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
303312
}
304313
}
305314

306-
private final void doGetId() {
307-
doRegistrationInternal(false);
308-
}
309-
310-
private final void doGetAuthTokenWithoutForceRefresh() {
311-
doRegistrationInternal(false);
315+
private synchronized void updateCacheFid(String cachedFid) {
316+
this.cachedFid = cachedFid;
312317
}
313318

314-
private final void doGetAuthTokenForceRefresh() {
315-
doRegistrationInternal(true);
319+
private synchronized String getCacheFid() {
320+
return cachedFid;
316321
}
317322

318323
/**
@@ -324,7 +329,8 @@ private final void doGetAuthTokenForceRefresh() {
324329
* @param forceRefresh true if this is for a getAuthToken call and if the caller wants to fetch a
325330
* new auth token from the server even if an unexpired auth token exists on the client.
326331
*/
327-
private final void doRegistrationInternal(boolean forceRefresh) {
332+
private final void doRegistrationOrRefresh(boolean forceRefresh) {
333+
328334
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
329335

330336
// Since the caller wants to force an authtoken refresh remove the authtoken from the
@@ -361,6 +367,11 @@ private void doNetworkCallIfNecessary(boolean forceRefresh) {
361367
// Store the prefs to persist the result of the previous step.
362368
insertOrUpdatePrefs(prefs);
363369

370+
// Update cachedFID, if FID is successfully REGISTERED and persisted.
371+
if (prefs.isRegistered()) {
372+
updateCacheFid(prefs.getFirebaseInstallationId());
373+
}
374+
364375
// Let the caller know about the result.
365376
if (prefs.isErrored()) {
366377
triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG));
@@ -520,6 +531,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
520531
case AUTH_ERROR:
521532
// The the server refused to generate a new auth token due to bad credentials, clear the
522533
// FID to force the generation of a new one.
534+
updateCacheFid(null);
523535
return prefs.withNoGeneratedFid();
524536
default:
525537
throw new FirebaseInstallationsException(
@@ -533,6 +545,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
533545
* storage.
534546
*/
535547
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException {
548+
updateCacheFid(null);
536549
PersistedInstallationEntry entry = getMultiProcessSafePrefs();
537550
if (entry.isRegistered()) {
538551
// Call the FIS servers to delete this Firebase Installation Id.
@@ -542,7 +555,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
542555
/*projectID= */ getProjectIdentifier(),
543556
/*refreshToken= */ entry.getRefreshToken());
544557
}
545-
546558
insertOrUpdatePrefs(entry.withNoGeneratedFid());
547559
return null;
548560
}

firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import com.google.android.gms.tasks.TaskCompletionSource;
1818
import com.google.firebase.installations.local.PersistedInstallationEntry;
1919

20+
/**
21+
* This class manages {@link PersistedInstallationEntry} state transitions valid for getId() and
22+
* updates the caller once the id is generated.
23+
*/
2024
class GetIdListener implements StateListener {
2125
final TaskCompletionSource<String> taskCompletionSource;
2226

@@ -32,6 +36,8 @@ public boolean onStateReached(PersistedInstallationEntry persistedInstallationEn
3236
taskCompletionSource.trySetResult(persistedInstallationEntry.getFirebaseInstallationId());
3337
return true;
3438
}
39+
// Don't update the caller if the PersistedInstallationEntry registration status is
40+
// ATTEMPT_MIGRATION or NOT_GENERATED.
3541
return false;
3642
}
3743

0 commit comments

Comments
 (0)