Skip to content

Commit d494d4e

Browse files
committed
fix guarded by issues
1 parent 389782d commit d494d4e

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,20 @@ synchronized void removeOrCancelDownload(String modelName, Long downloadId) {
172172

173173
private synchronized DownloadBroadcastReceiver getReceiverInstance(
174174
long downloadId, String modelName) {
175-
DownloadBroadcastReceiver receiver = receiverMaps.get(downloadId);
175+
DownloadBroadcastReceiver receiver = this.receiverMaps.get(downloadId);
176176
if (receiver == null) {
177177
receiver =
178178
new DownloadBroadcastReceiver(
179179
downloadId, modelName, getTaskCompletionSourceInstance(downloadId));
180-
receiverMaps.put(downloadId, receiver);
180+
this.receiverMaps.put(downloadId, receiver);
181181
}
182182
return receiver;
183183
}
184184

185+
private synchronized void removeReceiverInstance(long downloadId) {
186+
this.receiverMaps.remove(downloadId);
187+
}
188+
185189
private Task<Void> registerReceiverForDownloadId(long downloadId, String modelName) {
186190
BroadcastReceiver broadcastReceiver = getReceiverInstance(downloadId, modelName);
187191
// It is okay to always register here. Since the broadcast receiver is the same via the lookup
@@ -194,15 +198,25 @@ private Task<Void> registerReceiverForDownloadId(long downloadId, String modelNa
194198

195199
@VisibleForTesting
196200
synchronized TaskCompletionSource<Void> getTaskCompletionSourceInstance(long downloadId) {
197-
TaskCompletionSource<Void> taskCompletionSource = taskCompletionSourceMaps.get(downloadId);
201+
TaskCompletionSource<Void> taskCompletionSource = this.taskCompletionSourceMaps.get(downloadId);
198202
if (taskCompletionSource == null) {
199203
taskCompletionSource = new TaskCompletionSource<>();
200-
taskCompletionSourceMaps.put(downloadId, taskCompletionSource);
204+
this.taskCompletionSourceMaps.put(downloadId, taskCompletionSource);
201205
}
202206

203207
return taskCompletionSource;
204208
}
205209

210+
@VisibleForTesting
211+
synchronized boolean existTaskCompletionSourceInstance(long downloadId) {
212+
TaskCompletionSource<Void> taskCompletionSource = this.taskCompletionSourceMaps.get(downloadId);
213+
return (taskCompletionSource != null);
214+
}
215+
216+
private synchronized void removeTaskCompletionSourceInstance(long downloadId) {
217+
this.taskCompletionSourceMaps.remove(downloadId);
218+
}
219+
206220
@VisibleForTesting
207221
synchronized Long scheduleModelDownload(@NonNull CustomModel customModel) {
208222
if (downloadManager == null) {
@@ -441,8 +455,8 @@ public void onReceive(Context context, Intent intent) {
441455
Integer statusCode = getDownloadingModelStatusCode(downloadId);
442456
// check to prevent DuplicateTaskCompletionException - this was already updated and removed.
443457
// Just return.
444-
if (taskCompletionSourceMaps.get(downloadId) == null) {
445-
receiverMaps.remove(downloadId);
458+
if (!existTaskCompletionSourceInstance(downloadId)) {
459+
removeReceiverInstance(downloadId);
446460
return;
447461
}
448462

@@ -457,8 +471,8 @@ public void onReceive(Context context, Intent intent) {
457471
// move on.
458472
}
459473

460-
receiverMaps.remove(downloadId);
461-
taskCompletionSourceMaps.remove(downloadId);
474+
removeReceiverInstance(downloadId);
475+
removeTaskCompletionSourceInstance(downloadId);
462476
}
463477

464478
if (statusCode != null) {

0 commit comments

Comments
 (0)