@@ -217,11 +217,9 @@ String getName() {
217
217
@ Override
218
218
public Task <String > getId () {
219
219
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 ();
225
223
}
226
224
227
225
/**
@@ -321,7 +319,7 @@ private void doGetAuthToken(boolean forceRefresh) {
321
319
}
322
320
323
321
private void doNetworkCall (boolean forceRefresh ) {
324
- PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
322
+ PersistedInstallationEntry prefs = getMultiProcessSafePrefs ();
325
323
// There are two possible cleanup steps to perform at this stage: the FID may need to
326
324
// be registered with the server or the FID is registered but we need a fresh authtoken.
327
325
// Registering will also result in a fresh authtoken. Do the appropriate step here.
@@ -509,7 +507,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
509
507
* storage.
510
508
*/
511
509
private Void deleteFirebaseInstallationId () throws FirebaseInstallationsException , IOException {
512
- PersistedInstallationEntry entry = getPrefsWithGeneratedIdMultiProcessSafe ();
510
+ PersistedInstallationEntry entry = getMultiProcessSafePrefs ();
513
511
if (entry .isRegistered ()) {
514
512
// Call the FIS servers to delete this Firebase Installation Id.
515
513
try {
@@ -528,4 +526,31 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
528
526
insertOrUpdatePrefs (entry .withNoGeneratedFid ());
529
527
return null ;
530
528
}
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
+ }
531
556
}
0 commit comments