Commit 9e26abc0 authored by David Trainor's avatar David Trainor Committed by Commit Bot

Early-out of previously failed thumbnail requests.

If we've failed to grab a thumbnail for a particular content id, don't
try again to avoid lots of wasted file access and computation.

BUG=892320

Change-Id: I9d980c0cce1644f2c0da3d8699c5e25e220af8f0
Reviewed-on: https://chromium-review.googlesource.com/c/1263416
Commit-Queue: David Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597336}
parent 388470da
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.widget;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import org.chromium.base.DiscardableReferencePool;
......@@ -33,8 +34,21 @@ public class ThumbnailProviderImpl implements ThumbnailProvider, ThumbnailStorag
/** 5 MB of thumbnails should be enough for everyone. */
private static final int MAX_CACHE_BYTES = 5 * ConversionUtils.BYTES_PER_MEGABYTE;
/**
* Helper object to store in the LruCache when we don't really need a value but can't use null.
*/
private static final Object NO_BITMAP_PLACEHOLDER = new Object();
private BitmapCache mBitmapCache;
/**
* Tracks a set of Content Ids where thumbnail generation or retrieval failed. This should
* prevent making subsequent (potentially expensive) thumbnail generation requests when there
* would be no point.
*/
private LruCache<String /* Content Id */, Object /* Placeholder */> mNoBitmapCache =
new LruCache<>(100);
/** Queue of files to retrieve thumbnails for. */
private final Deque<ThumbnailRequest> mRequestQueue = new ArrayDeque<>();
......@@ -70,6 +84,11 @@ public class ThumbnailProviderImpl implements ThumbnailProvider, ThumbnailStorag
return;
}
if (mNoBitmapCache.get(request.getContentId()) != null) {
request.onThumbnailRetrieved(request.getContentId(), null);
return;
}
Bitmap cachedBitmap = getBitmapFromCache(request.getContentId(), request.getIconSize());
if (cachedBitmap != null) {
request.onThumbnailRetrieved(request.getContentId(), cachedBitmap);
......@@ -169,8 +188,10 @@ public class ThumbnailProviderImpl implements ThumbnailProvider, ThumbnailStorag
// fetches of this thumbnail can recognise the key in the cache.
String key = getKey(contentId, mCurrentRequest.getIconSize());
mBitmapCache.putBitmap(key, bitmap);
mNoBitmapCache.remove(contentId);
mCurrentRequest.onThumbnailRetrieved(contentId, bitmap);
} else {
mNoBitmapCache.put(contentId, NO_BITMAP_PLACEHOLDER);
mCurrentRequest.onThumbnailRetrieved(contentId, null);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment