Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java b/chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java |
| index 6399c4363e8c2ff93dc343826e061191046826d8..f3c5a2d1f3dd4c704fa7e4a14aff7aba1f9188d8 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java |
| @@ -4,6 +4,13 @@ |
| package org.chromium.chrome.browser.enhancedbookmarks; |
| +import android.app.ActivityManager; |
| +import android.content.Context; |
| +import android.graphics.Bitmap; |
| +import android.util.LruCache; |
| +import android.util.Pair; |
| + |
| +import org.chromium.base.ApplicationStatus; |
| import org.chromium.base.ObserverList; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.browser.BookmarksBridge; |
| @@ -22,6 +29,8 @@ import java.util.List; |
| * the UI to acquire data from the backend. |
| */ |
| public class EnhancedBookmarksModel extends BookmarksBridge { |
| + private static final int FAVICON_MAX_CACHE_SIZE = 10 * 1024 * 1024; // 10MB |
| + |
| /** |
| * Observer that listens to delete event. This interface is used by undo controllers to know |
| * which bookmarks were deleted. Note this observer only listens to events that go through |
| @@ -39,6 +48,7 @@ public class EnhancedBookmarksModel extends BookmarksBridge { |
| private LargeIconBridge mLargeIconBridge; |
| private ObserverList<EnhancedBookmarkDeleteObserver> mDeleteObservers = new ObserverList<>(); |
| + private LruCache<String, Pair<Bitmap, Integer>> mFaviconCache; |
| /** |
| * Initialize enhanced bookmark model for last used non-incognito profile. |
| @@ -51,6 +61,21 @@ public class EnhancedBookmarksModel extends BookmarksBridge { |
| public EnhancedBookmarksModel(Profile profile) { |
| super(profile); |
| mLargeIconBridge = new LargeIconBridge(); |
| + |
| + ActivityManager activityManager = ((ActivityManager) ApplicationStatus |
| + .getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)); |
| + int maxSize = Math.min(activityManager.getMemoryClass() / 4 * 1024 * 1024, |
| + FAVICON_MAX_CACHE_SIZE); |
| + mFaviconCache = new LruCache<String, Pair<Bitmap, Integer>>(maxSize) { |
| + @Override |
| + protected int sizeOf(String key, Pair<Bitmap, Integer> icon) { |
| + int size = Integer.SIZE; |
| + if (icon.first != null) { |
| + size += icon.first.getByteCount(); |
| + } |
| + return size; |
| + } |
| + }; |
| } |
| /** |
| @@ -60,6 +85,7 @@ public class EnhancedBookmarksModel extends BookmarksBridge { |
| public void destroy() { |
| super.destroy(); |
| mLargeIconBridge.destroy(); |
| + mFaviconCache = null; |
| } |
| /** |
| @@ -137,8 +163,28 @@ public class EnhancedBookmarksModel extends BookmarksBridge { |
| /** |
| * @see LargeIconBridge#getLargeIconForUrl(Profile, String, int, LargeIconCallback) |
|
Ian Wen
2015/07/27 20:22:39
Update the comments and mention we have java cache
Theresa
2015/07/27 20:33:14
Done.
|
| */ |
| - public void getLargeIcon(String url, int minSize, LargeIconCallback callback) { |
| - mLargeIconBridge.getLargeIconForUrl(Profile.getLastUsedProfile(), url, minSize, callback); |
| + public void getLargeIcon(final String url, int minSize, final LargeIconCallback callback) { |
| + assert callback != null; |
| + LargeIconCallback callbackWrapper = callback; |
| + |
| + if (mFaviconCache != null) { |
|
Ian Wen
2015/07/27 20:22:39
Is this null check still necessary? If destroy() i
Theresa
2015/07/27 20:33:14
Done.
|
| + Pair<Bitmap, Integer> cached = mFaviconCache.get(url); |
| + if (cached != null) { |
| + callback.onLargeIconAvailable(cached.first, cached.second); |
| + return; |
| + } |
| + |
| + callbackWrapper = new LargeIconCallback() { |
| + @Override |
| + public void onLargeIconAvailable(Bitmap icon, int fallbackColor) { |
| + mFaviconCache.put(url, new Pair<Bitmap, Integer>(icon, fallbackColor)); |
| + callback.onLargeIconAvailable(icon, fallbackColor); |
| + } |
| + }; |
| + } |
| + |
| + mLargeIconBridge.getLargeIconForUrl(Profile.getLastUsedProfile(), url, minSize, |
| + callbackWrapper); |
| } |
| /** |