diff options
author | tedchoc@chromium.org <tedchoc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-08 03:58:56 +0000 |
---|---|---|
committer | tedchoc@chromium.org <tedchoc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-08 03:58:56 +0000 |
commit | a65a5500e8c6ab173d4a2e39daf5a423b34c20c3 (patch) | |
tree | 703dead82a05ab56cd603c2b8a74af162fb98dbb | |
parent | 766faca0a3914a7d71a8ec9dccf1273c7524f531 (diff) | |
download | chromium_src-a65a5500e8c6ab173d4a2e39daf5a423b34c20c3.zip chromium_src-a65a5500e8c6ab173d4a2e39daf5a423b34c20c3.tar.gz chromium_src-a65a5500e8c6ab173d4a2e39daf5a423b34c20c3.tar.bz2 |
Expose the API to fetch the largest possible favicon.
BUG=365590
Review URL: https://codereview.chromium.org/269313003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269025 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java | 27 | ||||
-rw-r--r-- | chrome/browser/android/favicon_helper.cc | 67 | ||||
-rw-r--r-- | chrome/browser/android/favicon_helper.h | 7 |
3 files changed, 100 insertions, 1 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java index 445efb0..d04a290 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java @@ -84,6 +84,30 @@ public class FaviconHelper { } /** + * Fetches the first available favicon for a URL that exceeds the minimum size threshold. If + * no favicons are larger (or equal) to the threshold, the largest favicon of any type is + * fetched. + * + * @param profile Profile used for the FaviconService construction. + * @param pageUrl The target Page URL to get the favicon. + * @param iconTypes The list of icon types (each entry can be a bitmasked collection + * of types) that should be fetched in order. As soon as one of + * the buckets exceeds the minimum size threshold, that favicon + * will be returned. + * @param minSizeThresholdPx The size threshold (inclusive) used to early exit out fetching + * subsequent favicon types. + * @param faviconImageCallback The callback to be notified with the best matching favicon. + */ + public void getLargestRawFaviconForUrl( + Profile profile, String pageUrl, int[] iconTypes, int minSizeThresholdPx, + FaviconImageCallback faviconImageCallback) { + assert mNativeFaviconHelper != 0; + nativeGetLargestRawFaviconForUrl( + mNativeFaviconHelper, profile, pageUrl, iconTypes, minSizeThresholdPx - 1, + faviconImageCallback); + } + + /** * Return the dominant color of a given bitmap in {@link Color} format. * @param image The bitmap image to find the dominant color for. * @return The dominant color in {@link Color} format. @@ -111,6 +135,9 @@ public class FaviconHelper { private static native boolean nativeGetLocalFaviconImageForURL(long nativeFaviconHelper, Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip, FaviconImageCallback faviconImageCallback); + private static native void nativeGetLargestRawFaviconForUrl(long nativeFaviconHelper, + Profile profile, String pageUrl, int[] iconTypes, int minSizeThresholdPx, + FaviconImageCallback faviconImageCallback); private static native Bitmap nativeGetSyncedFaviconImageForURL(long nativeFaviconHelper, Profile profile, String pageUrl); private static native int nativeGetDominantColorForBitmap(Bitmap image); diff --git a/chrome/browser/android/favicon_helper.cc b/chrome/browser/android/favicon_helper.cc index f6ae96c..07840d87 100644 --- a/chrome/browser/android/favicon_helper.cc +++ b/chrome/browser/android/favicon_helper.cc @@ -7,6 +7,7 @@ #include <jni.h> #include "base/android/jni_android.h" +#include "base/android/jni_array.h" #include "base/android/jni_string.h" #include "base/android/scoped_java_ref.h" #include "base/bind.h" @@ -23,6 +24,7 @@ #include "jni/FaviconHelper_jni.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/gfx/android/java_bitmap.h" +#include "ui/gfx/codec/png_codec.h" #include "ui/gfx/color_analysis.h" #include "ui/gfx/color_utils.h" @@ -55,6 +57,32 @@ void OnLocalFaviconAvailable( j_icon_url.obj()); } +void OnFaviconBitmapResultAvailable( + ScopedJavaGlobalRef<jobject>* j_favicon_image_callback, + const favicon_base::FaviconBitmapResult& favicon_bitmap_result) { + JNIEnv* env = AttachCurrentThread(); + + // Convert favicon_image_result to java objects. + ScopedJavaLocalRef<jstring> j_icon_url = + ConvertUTF8ToJavaString(env, favicon_bitmap_result.icon_url.spec()); + + SkBitmap favicon_bitmap; + if (favicon_bitmap_result.is_valid()) { + gfx::PNGCodec::Decode(favicon_bitmap_result.bitmap_data->front(), + favicon_bitmap_result.bitmap_data->size(), + &favicon_bitmap); + } + ScopedJavaLocalRef<jobject> j_favicon_bitmap; + if (!favicon_bitmap.isNull()) + j_favicon_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); + + // Call java side OnLocalFaviconAvailable method. + Java_FaviconImageCallback_onFaviconAvailable(env, + j_favicon_image_callback->obj(), + j_favicon_bitmap.obj(), + j_icon_url.obj()); +} + } // namespace static jlong Init(JNIEnv* env, jclass clazz) { @@ -107,6 +135,43 @@ jboolean FaviconHelper::GetLocalFaviconImageForURL( return true; } +void FaviconHelper::GetLargestRawFaviconForUrl( + JNIEnv* env, + jobject obj, + jobject j_profile, + jstring j_page_url, + jintArray j_icon_types, + jint j_min_size_threshold_px, + jobject j_favicon_image_callback) { + Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); + DCHECK(profile); + if (!profile) + return; + + FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( + profile, Profile::EXPLICIT_ACCESS); + DCHECK(favicon_service); + if (!favicon_service) + return; + + std::vector<int> icon_types; + base::android::JavaIntArrayToIntVector(env, j_icon_types, &icon_types); + + ScopedJavaGlobalRef<jobject>* j_scoped_favicon_callback = + new ScopedJavaGlobalRef<jobject>(); + j_scoped_favicon_callback->Reset(env, j_favicon_image_callback); + + FaviconService::FaviconRawCallback callback_runner = base::Bind( + &OnFaviconBitmapResultAvailable, base::Owned(j_scoped_favicon_callback)); + favicon_service->GetLargestRawFaviconForURL( + profile, + GURL(ConvertJavaStringToUTF16(env, j_page_url)), + icon_types, + static_cast<int>(j_min_size_threshold_px), + callback_runner, + cancelable_task_tracker_.get()); +} + ScopedJavaLocalRef<jobject> FaviconHelper::GetSyncedFaviconImageForURL( JNIEnv* env, jobject obj, @@ -129,7 +194,7 @@ ScopedJavaLocalRef<jobject> FaviconHelper::GetSyncedFaviconImageForURL( if (!open_tabs->GetSyncedFaviconForPageURL(page_url, &favicon_png)) return ScopedJavaLocalRef<jobject>(); - // Convert favicon_image_result to java objects. + // Convert favicon_image_result to java objects. gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png); SkBitmap favicon_bitmap = favicon_image.AsBitmap(); diff --git a/chrome/browser/android/favicon_helper.h b/chrome/browser/android/favicon_helper.h index 6c22975..377eaa5 100644 --- a/chrome/browser/android/favicon_helper.h +++ b/chrome/browser/android/favicon_helper.h @@ -22,6 +22,13 @@ class FaviconHelper { jint j_icon_types, jint j_desired_size_in_dip, jobject j_favicon_image_callback); + void GetLargestRawFaviconForUrl(JNIEnv* env, + jobject obj, + jobject j_profile, + jstring j_page_url, + jintArray j_icon_types, + jint j_min_size_threshold_px, + jobject j_favicon_image_callback); base::android::ScopedJavaLocalRef<jobject> GetSyncedFaviconImageForURL( JNIEnv* env, jobject obj, |