Skip to content

Commit

Permalink
Ignore callbacks from cancelled or otherwise invalid requests in Sour…
Browse files Browse the repository at this point in the history
…ceGenerator.

PiperOrigin-RevId: 277765705
  • Loading branch information
sjudd authored and glide-copybara-robot committed Oct 31, 2019
1 parent 0acd87c commit 7e750ca
Showing 1 changed file with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.data.DataFetcher.DataCallback;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoader.LoadData;
import com.bumptech.glide.util.LogTime;
import com.bumptech.glide.util.Synthetic;
import java.util.Collections;

/**
Expand All @@ -19,10 +22,7 @@
* <p>Depending on the disk cache strategy, source data may first be written to disk and then loaded
* from the cache file rather than returned directly.
*/
class SourceGenerator
implements DataFetcherGenerator,
DataFetcher.DataCallback<Object>,
DataFetcherGenerator.FetcherReadyCallback {
class SourceGenerator implements DataFetcherGenerator, DataFetcherGenerator.FetcherReadyCallback {
private static final String TAG = "SourceGenerator";

private final DecodeHelper<?> helper;
Expand Down Expand Up @@ -60,12 +60,40 @@ public boolean startNext() {
&& (helper.getDiskCacheStrategy().isDataCacheable(loadData.fetcher.getDataSource())
|| helper.hasLoadPath(loadData.fetcher.getDataClass()))) {
started = true;
loadData.fetcher.loadData(helper.getPriority(), this);
startNextLoad(loadData);
}
}
return started;
}

private void startNextLoad(final LoadData<?> toStart) {
loadData.fetcher.loadData(
helper.getPriority(),
new DataCallback<Object>() {
@Override
public void onDataReady(@Nullable Object data) {
if (isCurrentRequest(toStart)) {
onDataReadyInternal(toStart, data);
}
}

@Override
public void onLoadFailed(@NonNull Exception e) {
if (isCurrentRequest(toStart)) {
onLoadFailedInternal(toStart, e);
}
}
});
}

// We want reference equality explicitly to make sure we ignore results from old requests.
@SuppressWarnings({"PMD.CompareObjectsWithEquals", "WeakerAccess"})
@Synthetic
boolean isCurrentRequest(LoadData<?> requestLoadData) {
LoadData<?> currentLoadData = loadData;
return currentLoadData != null && currentLoadData == requestLoadData;
}

private boolean hasNextModelLoader() {
return loadDataListIndex < helper.getLoadData().size();
}
Expand Down Expand Up @@ -107,8 +135,9 @@ public void cancel() {
}
}

@Override
public void onDataReady(Object data) {
@SuppressWarnings("WeakerAccess")
@Synthetic
void onDataReadyInternal(LoadData<?> loadData, Object data) {
DiskCacheStrategy diskCacheStrategy = helper.getDiskCacheStrategy();
if (data != null && diskCacheStrategy.isDataCacheable(loadData.fetcher.getDataSource())) {
dataToCache = data;
Expand All @@ -125,8 +154,9 @@ public void onDataReady(Object data) {
}
}

@Override
public void onLoadFailed(@NonNull Exception e) {
@SuppressWarnings("WeakerAccess")
@Synthetic
void onLoadFailedInternal(LoadData<?> loadData, @NonNull Exception e) {
cb.onDataFetcherFailed(originalKey, e, loadData.fetcher, loadData.fetcher.getDataSource());
}

Expand Down

0 comments on commit 7e750ca

Please sign in to comment.