Skip to content

Commit f0ff299

Browse files
committed
Completing getId call with the disk reads on the caller thread.
Also, fixing delete API to access persisted FID using cross process safe locks.
1 parent 9519e70 commit f0ff299

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
7373

7474
/* used for thread-level synchronization of generating and persisting fids */
7575
private static final Object lockGenerateFid = new Object();
76+
77+
/* used for thread-level synchronization of getting a fid. */
78+
private static final Object lockGetFid = new Object();
79+
7680
/* file used for process-level synchronization of generating and persisting fids */
7781
private static final String LOCKFILE_NAME_GENERATE_FID = "generatefid.lock";
7882
private static final String CHIME_FIREBASE_APP_NAME = "CHIME_ANDROID_SDK";
@@ -213,9 +217,11 @@ String getName() {
213217
@Override
214218
public Task<String> getId() {
215219
preConditionChecks();
216-
Task<String> task = addGetIdListener();
217-
backgroundExecutor.execute(this::doGetId);
218-
return task;
220+
synchronized (lockGetFid) {
221+
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
222+
taskCompletionSource.trySetResult(doGetId());
223+
return taskCompletionSource.getTask();
224+
}
219225
}
220226

221227
/**
@@ -231,11 +237,7 @@ public Task<String> getId() {
231237
public Task<InstallationTokenResult> getToken(boolean forceRefresh) {
232238
preConditionChecks();
233239
Task<InstallationTokenResult> task = addGetAuthTokenListener();
234-
if (forceRefresh) {
235-
backgroundExecutor.execute(this::doGetAuthTokenForceRefresh);
236-
} else {
237-
backgroundExecutor.execute(this::doGetAuthTokenWithoutForceRefresh);
238-
}
240+
backgroundExecutor.execute(() -> doGetAuthToken(forceRefresh));
239241
return task;
240242
}
241243

@@ -250,15 +252,6 @@ public Task<Void> delete() {
250252
return Tasks.call(backgroundExecutor, this::deleteFirebaseInstallationId);
251253
}
252254

253-
private Task<String> addGetIdListener() {
254-
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
255-
StateListener l = new GetIdListener(taskCompletionSource);
256-
synchronized (lock) {
257-
listeners.add(l);
258-
}
259-
return taskCompletionSource.getTask();
260-
}
261-
262255
private Task<InstallationTokenResult> addGetAuthTokenListener() {
263256
TaskCompletionSource<InstallationTokenResult> taskCompletionSource =
264257
new TaskCompletionSource<>();
@@ -295,16 +288,12 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
295288
}
296289
}
297290

298-
private final void doGetId() {
299-
doRegistrationInternal(false);
300-
}
301-
302-
private final void doGetAuthTokenWithoutForceRefresh() {
303-
doRegistrationInternal(false);
304-
}
305-
306-
private final void doGetAuthTokenForceRefresh() {
307-
doRegistrationInternal(true);
291+
private String doGetId() {
292+
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
293+
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
294+
// i.e networkExecutor
295+
networkExecutor.execute(() -> doNetworkCall(false));
296+
return prefs.getFirebaseInstallationId();
308297
}
309298

310299
/**
@@ -316,7 +305,7 @@ private final void doGetAuthTokenForceRefresh() {
316305
* @param forceRefresh true if this is for a getAuthToken call and if the caller wants to fetch a
317306
* new auth token from the server even if an unexpired auth token exists on the client.
318307
*/
319-
private final void doRegistrationInternal(boolean forceRefresh) {
308+
private void doGetAuthToken(boolean forceRefresh) {
320309
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
321310

322311
// Since the caller wants to force an authtoken refresh remove the authtoken from the
@@ -520,7 +509,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
520509
* storage.
521510
*/
522511
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException {
523-
PersistedInstallationEntry entry = persistedInstallation.readPersistedInstallationEntryValue();
512+
PersistedInstallationEntry entry = getPrefsWithGeneratedIdMultiProcessSafe();
524513
if (entry.isRegistered()) {
525514
// Call the FIS servers to delete this Firebase Installation Id.
526515
try {

0 commit comments

Comments
 (0)