1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.favicon;
7 import android.graphics.Bitmap;
8 import android.graphics.Color;
10 import org.chromium.base.CalledByNative;
11 import org.chromium.chrome.browser.profiles.Profile;
14 * This is a helper class to use favicon_service.cc's functionality.
16 * You can request a favicon image by web page URL. Note that an instance of this class should be
17 * created & used & destroyed (by destroy()) in the same thread due to the C++ CancelableTaskTracker
20 public class FaviconHelper {
22 // Please keep in sync with favicon_types.h's IconType.
23 public static final int INVALID_ICON = 0;
24 public static final int FAVICON = 1 << 0;
25 public static final int TOUCH_ICON = 1 << 1;
26 public static final int TOUCH_PRECOMPOSED_ICON = 1 << 2;
28 private int mNativeFaviconHelper;
31 * Callback interface for getting the result from getLocalFaviconImageForURL method.
33 public interface FaviconImageCallback {
35 * This method will be called when the result favicon is ready.
36 * @param image Favicon image.
37 * @param iconUrl Favicon image's icon url.
39 @CalledByNative("FaviconImageCallback")
40 public void onFaviconAvailable(Bitmap image, String iconUrl);
44 * Allocate and initialize the C++ side of this class.
46 public FaviconHelper() {
47 mNativeFaviconHelper = nativeInit();
51 protected void finalize() {
52 // Ensure that destroy() was called.
53 assert mNativeFaviconHelper == 0;
57 * Clean up the C++ side of this class. After the call, this class instance shouldn't be used.
59 public void destroy() {
60 nativeDestroy(mNativeFaviconHelper);
61 mNativeFaviconHelper = 0;
65 * Get Favicon bitmap for the requested arguments. Retrieves favicons only for pages the user
66 * has visited on the current device.
67 * @param profile Profile used for the FaviconService construction.
68 * @param pageUrl The target Page URL to get the favicon.
69 * @param iconTypes One of the IconType class values.
70 * @param desiredSizeInDip The size of the favicon in dip we want to get.
71 * @param faviconImageCallback A method to be called back when the result is available.
72 * Note that this callback is not called if this method returns
74 * @return True if GetLocalFaviconImageForURL is successfully called.
76 public boolean getLocalFaviconImageForURL(
77 Profile profile, String pageUrl, int iconTypes,
78 int desiredSizeInDip, FaviconImageCallback faviconImageCallback) {
79 assert mNativeFaviconHelper != 0;
80 return nativeGetLocalFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl, iconTypes,
81 desiredSizeInDip, faviconImageCallback);
85 * Return the dominant color of a given bitmap in {@link Color} format.
86 * @param image The bitmap image to find the dominant color for.
87 * @return The dominant color in {@link Color} format.
89 public int getDominantColorForBitmap(Bitmap image) {
90 return nativeGetDominantColorForBitmap(mNativeFaviconHelper, image);
94 * Get 16x16 Favicon bitmap for the requested arguments. Only retrives favicons in synced
95 * session storage. (e.g. favicons synced from other devices). TODO (apiccion) provide a way
96 * to obtain higher resolution favicons.
97 * @param profile Profile used for the FaviconService construction.
98 * @param pageUrl The target Page URL to get the favicon.
100 * @return 16x16 favicon Bitmap corresponding to the pageUrl.
102 public Bitmap getSyncedFaviconImageForURL(Profile profile, String pageUrl) {
103 assert mNativeFaviconHelper != 0;
104 return nativeGetSyncedFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl);
107 private static native int nativeInit();
108 private static native void nativeDestroy(int nativeFaviconHelper);
109 private static native boolean nativeGetLocalFaviconImageForURL(int nativeFaviconHelper,
110 Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip,
111 FaviconImageCallback faviconImageCallback);
112 private static native Bitmap nativeGetSyncedFaviconImageForURL(int nativeFaviconHelper,
113 Profile profile, String pageUrl);
114 private static native int nativeGetDominantColorForBitmap(int nativeFaviconHelper,