Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3740)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarksModel.java

Issue 1257103003: Add Java-side LRU cache for bookmark favicons (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up spacing Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..dd7d1a8c071216e3940425bb1225aa1d2df71f67 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;
}
/**
@@ -135,10 +161,32 @@ public class EnhancedBookmarksModel extends BookmarksBridge {
}
/**
+ * Retrieves a favicon and fallback color for the given |url|. An LRU cache is used to store the
+ * favicons. If the favicon is not already present in the cache, it is retrieved using
+ * LargeIconBridge#getLargeIconForUrl().
+ *
* @see LargeIconBridge#getLargeIconForUrl(Profile, String, int, LargeIconCallback)
*/
- 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;
+
+ 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);
}
/**
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698