diff options
author | newt <newt@chromium.org> | 2015-04-30 14:57:12 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-30 21:57:43 +0000 |
commit | 57e24108a8d9afa498566137ad1997977f668781 (patch) | |
tree | 53f1e253b9b5af3a88fd2e0d57a6cdfbdbf51418 | |
parent | 10a866ab06ab72fa161328cb922052876c49c3a8 (diff) | |
download | chromium_src-57e24108a8d9afa498566137ad1997977f668781.zip chromium_src-57e24108a8d9afa498566137ad1997977f668781.tar.gz chromium_src-57e24108a8d9afa498566137ad1997977f668781.tar.bz2 |
Add LargeIconBridge to expose LargeIconService to Java.
This provides a single method LargeIconBridge.getLargeIconForUrl(),
which fetches and returns a large icon (e.g. favicon or touch icon) for
a given URL.
BUG=479026
Review URL: https://codereview.chromium.org/1110063008
Cr-Commit-Position: refs/heads/master@{#327802}
6 files changed, 233 insertions, 0 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java new file mode 100644 index 0000000..b8fb571 --- /dev/null +++ b/chrome/android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java @@ -0,0 +1,75 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.chrome.browser.favicon; + +import android.graphics.Bitmap; + +import org.chromium.base.CalledByNative; +import org.chromium.chrome.browser.profiles.Profile; + +/** + * A Java API for using the C++ LargeIconService. + * + * An instance of this class must be created, used, and destroyed on the same thread. + */ +public class LargeIconBridge { + + private long mNativeLargeIconBridge; + + /** + * Callback for use with GetLargeIconForUrl(). + */ + public interface LargeIconCallback { + /** + * Called when the icon or fallback color is available. + * + * @param icon The icon, or null if none is available. + * @param fallbackColor The fallback color to use if icon is null. + */ + @CalledByNative("LargeIconCallback") + void onLargeIconAvailable(Bitmap icon, int fallbackColor); + } + + /** + * Initializes the C++ side of this class. + */ + public LargeIconBridge() { + mNativeLargeIconBridge = nativeInit(); + } + + /** + * Deletes the C++ side of this class. This must be called when this object is no longer needed. + */ + public void destroy() { + assert mNativeLargeIconBridge != 0; + nativeDestroy(mNativeLargeIconBridge); + mNativeLargeIconBridge = 0; + } + + /** + * Given a URL, returns a large icon for that URL if one is available (e.g. a favicon or + * touch icon). If none is available, a fallback color is returned, based on the dominant color + * of any small icons for the URL, or a default gray if no small icons are available. The icon + * and fallback color are returned asynchronously to the given callback. + * + * @param profile Profile to use when fetching icons. + * @param pageUrl The URL of the page whose icon will be fetched. + * @param desiredSizePx The desired size of the icon in pixels. + * @param callback The method to call asynchronously when the result is available. This callback + * will not be called if this method returns false. + * @return True if a callback should be expected. + */ + public boolean getLargeIconForUrl(Profile profile, String pageUrl, + int desiredSizePx, LargeIconCallback callback) { + assert mNativeLargeIconBridge != 0; + return nativeGetLargeIconForURL(mNativeLargeIconBridge, profile, pageUrl, + desiredSizePx, callback); + } + + private static native long nativeInit(); + private static native void nativeDestroy(long nativeLargeIconBridge); + private static native boolean nativeGetLargeIconForURL(long nativeLargeIconBridge, + Profile profile, String pageUrl, int desiredSizePx, LargeIconCallback callback); +} diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java index e60b2f1..868763c 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/RoundedIconGenerator.java @@ -93,6 +93,13 @@ public class RoundedIconGenerator { } /** + * Sets the background color to use when generating icons. + */ + public void setBackgroundColor(int color) { + mBackgroundPaint.setColor(color); + } + + /** * Generates an icon based on |text|. * * @param text The text to render the first character of on the icon. diff --git a/chrome/browser/android/chrome_jni_registrar.cc b/chrome/browser/android/chrome_jni_registrar.cc index 9d2a4ea..dfde400 100644 --- a/chrome/browser/android/chrome_jni_registrar.cc +++ b/chrome/browser/android/chrome_jni_registrar.cc @@ -31,6 +31,7 @@ #include "chrome/browser/android/foreign_session_helper.h" #include "chrome/browser/android/fullscreen/fullscreen_infobar_delegate.h" #include "chrome/browser/android/intent_helper.h" +#include "chrome/browser/android/large_icon_bridge.h" #include "chrome/browser/android/location_settings_impl.h" #include "chrome/browser/android/logo_bridge.h" #include "chrome/browser/android/metrics/launch_metrics.h" @@ -213,6 +214,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = { {"IntentHelper", RegisterIntentHelper}, {"JavascriptAppModalDialog", JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog}, + {"LargeIconBridge", LargeIconBridge::RegisterLargeIconBridge}, {"LaunchMetrics", metrics::RegisterLaunchMetrics}, {"LayerTitleCache", chrome::android::RegisterLayerTitleCache}, {"LocationSettings", LocationSettingsImpl::Register}, diff --git a/chrome/browser/android/large_icon_bridge.cc b/chrome/browser/android/large_icon_bridge.cc new file mode 100644 index 0000000..0408cb2 --- /dev/null +++ b/chrome/browser/android/large_icon_bridge.cc @@ -0,0 +1,111 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/android/large_icon_bridge.h" + +#include <jni.h> + +#include "base/android/jni_android.h" +#include "base/android/jni_string.h" +#include "base/android/scoped_java_ref.h" +#include "base/bind.h" +#include "base/strings/utf_string_conversions.h" +#include "chrome/browser/favicon/large_icon_service_factory.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_android.h" +#include "components/favicon/core/large_icon_service.h" +#include "components/favicon_base/fallback_icon_style.h" +#include "components/favicon_base/favicon_types.h" +#include "jni/LargeIconBridge_jni.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "ui/gfx/android/java_bitmap.h" +#include "ui/gfx/codec/png_codec.h" + +using base::android::ScopedJavaGlobalRef; +using base::android::ScopedJavaLocalRef; +using base::android::AttachCurrentThread; +using base::android::ConvertJavaStringToUTF16; + +namespace { + +const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78); + +void OnLargeIconAvailable( + ScopedJavaGlobalRef<jobject>* j_callback, + const favicon_base::LargeIconResult& result) { + JNIEnv* env = AttachCurrentThread(); + + // Convert the result to a Java Bitmap. + SkBitmap bitmap; + ScopedJavaLocalRef<jobject> j_bitmap; + if (result.bitmap.is_valid()) { + gfx::PNGCodec::Decode(result.bitmap.bitmap_data->front(), + result.bitmap.bitmap_data->size(), + &bitmap); + if (!bitmap.isNull()) + j_bitmap = gfx::ConvertToJavaBitmap(&bitmap); + } + + jint background_color = kDefaultBackgroundColor; + if (result.fallback_icon_style) + background_color = result.fallback_icon_style->background_color; + + Java_LargeIconCallback_onLargeIconAvailable(env, + j_callback->obj(), + j_bitmap.obj(), + background_color); +} + +} // namespace + +static jlong Init(JNIEnv* env, jclass clazz) { + return reinterpret_cast<intptr_t>(new LargeIconBridge()); +} + +LargeIconBridge::LargeIconBridge() { +} + +LargeIconBridge::~LargeIconBridge() { +} + +void LargeIconBridge::Destroy(JNIEnv* env, jobject obj) { + delete this; +} + +jboolean LargeIconBridge::GetLargeIconForURL( + JNIEnv* env, + jobject obj, + jobject j_profile, + jstring j_page_url, + jint desired_size_px, + jobject j_callback) { + Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); + if (!profile) + return false; + + favicon::LargeIconService* large_icon_service = + LargeIconServiceFactory::GetForBrowserContext(profile); + if (!large_icon_service) + return false; + + ScopedJavaGlobalRef<jobject>* j_global_callback = + new ScopedJavaGlobalRef<jobject>(); + j_global_callback->Reset(env, j_callback); + + favicon_base::LargeIconCallback callback_runner = + base::Bind(&OnLargeIconAvailable, base::Owned(j_global_callback)); + + large_icon_service->GetLargeIconOrFallbackStyle( + GURL(ConvertJavaStringToUTF16(env, j_page_url)), + desired_size_px, + callback_runner, + &cancelable_task_tracker_); + + return true; +} + +// static +bool LargeIconBridge::RegisterLargeIconBridge(JNIEnv* env) { + return RegisterNativesImpl(env); +} diff --git a/chrome/browser/android/large_icon_bridge.h b/chrome/browser/android/large_icon_bridge.h new file mode 100644 index 0000000..777dfe0 --- /dev/null +++ b/chrome/browser/android/large_icon_bridge.h @@ -0,0 +1,35 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_ANDROID_LARGE_ICON_BRIDGE_H_ +#define CHROME_BROWSER_ANDROID_LARGE_ICON_BRIDGE_H_ + +#include <jni.h> + +#include "base/memory/scoped_ptr.h" +#include "base/task/cancelable_task_tracker.h" + +// The C++ counterpart to Java's LargeIconBridge. Together these classes expose +// LargeIconService to Java. +class LargeIconBridge { + public: + LargeIconBridge(); + void Destroy(JNIEnv* env, jobject obj); + jboolean GetLargeIconForURL(JNIEnv* env, + jobject obj, + jobject j_profile, + jstring j_page_url, + jint desired_size_px, + jobject j_callback); + static bool RegisterLargeIconBridge(JNIEnv* env); + + private: + virtual ~LargeIconBridge(); + + base::CancelableTaskTracker cancelable_task_tracker_; + + DISALLOW_COPY_AND_ASSIGN(LargeIconBridge); +}; + +#endif // CHROME_BROWSER_ANDROID_LARGE_ICON_BRIDGE_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 6b2cbec..05eb252 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -106,6 +106,8 @@ 'browser/android/intent_helper.h', 'browser/android/intercept_download_resource_throttle.cc', 'browser/android/intercept_download_resource_throttle.h', + 'browser/android/large_icon_bridge.cc', + 'browser/android/large_icon_bridge.h', 'browser/android/location_settings.h', 'browser/android/location_settings_impl.cc', 'browser/android/location_settings_impl.h', @@ -1660,6 +1662,7 @@ 'android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java', 'android/java/src/org/chromium/chrome/browser/enhanced_bookmarks/EnhancedBookmarksBridge.java', 'android/java/src/org/chromium/chrome/browser/favicon/FaviconHelper.java', + 'android/java/src/org/chromium/chrome/browser/favicon/LargeIconBridge.java', 'android/java/src/org/chromium/chrome/browser/findinpage/FindInPageBridge.java', 'android/java/src/org/chromium/chrome/browser/fullscreen/FullscreenInfoBarDelegate.java', 'android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java', |