Skip to content

Commit bc6ec5c

Browse files
committed
Addressing Fred's comments
1 parent f0ff299 commit bc6ec5c

File tree

2 files changed

+32
-50
lines changed

2 files changed

+32
-50
lines changed

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,9 @@ String getName() {
217217
@Override
218218
public Task<String> getId() {
219219
preConditionChecks();
220-
synchronized (lockGetFid) {
221-
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
222-
taskCompletionSource.trySetResult(doGetId());
223-
return taskCompletionSource.getTask();
224-
}
220+
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
221+
taskCompletionSource.trySetResult(doGetId());
222+
return taskCompletionSource.getTask();
225223
}
226224

227225
/**
@@ -321,7 +319,7 @@ private void doGetAuthToken(boolean forceRefresh) {
321319
}
322320

323321
private void doNetworkCall(boolean forceRefresh) {
324-
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
322+
PersistedInstallationEntry prefs = getMultiProcessSafePrefs();
325323
// There are two possible cleanup steps to perform at this stage: the FID may need to
326324
// be registered with the server or the FID is registered but we need a fresh authtoken.
327325
// Registering will also result in a fresh authtoken. Do the appropriate step here.
@@ -509,7 +507,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
509507
* storage.
510508
*/
511509
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException {
512-
PersistedInstallationEntry entry = getPrefsWithGeneratedIdMultiProcessSafe();
510+
PersistedInstallationEntry entry = getMultiProcessSafePrefs();
513511
if (entry.isRegistered()) {
514512
// Call the FIS servers to delete this Firebase Installation Id.
515513
try {
@@ -528,4 +526,31 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
528526
insertOrUpdatePrefs(entry.withNoGeneratedFid());
529527
return null;
530528
}
529+
530+
/**
531+
* Loads the persisted prefs. This operation is made cross-process and cross-thread safe by
532+
* wrapping all the processing first in a java synchronization block and wrapping that in a
533+
* cross-process lock created using FileLocks.
534+
*
535+
* @return a persisted prefs
536+
*/
537+
private PersistedInstallationEntry getMultiProcessSafePrefs() {
538+
synchronized (lockGenerateFid) {
539+
CrossProcessLock lock =
540+
CrossProcessLock.acquire(firebaseApp.getApplicationContext(), LOCKFILE_NAME_GENERATE_FID);
541+
try {
542+
PersistedInstallationEntry prefs =
543+
persistedInstallation.readPersistedInstallationEntryValue();
544+
return prefs;
545+
546+
} finally {
547+
// It is possible that the lock acquisition failed, resulting in lock being null.
548+
// We handle this case by going on with our business even if the acquisition failed
549+
// but we need to be sure to only release if we got a lock.
550+
if (lock != null) {
551+
lock.releaseAndClose();
552+
}
553+
}
554+
}
555+
}
531556
}

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)