| 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. |
| 4 | |
| 5 | package org.chromium.chrome.browser.favicon; |
| 6 | |
| 7 | import android.graphics.Bitmap; |
| 8 | |
| 9 | import org.chromium.base.CalledByNative; |
| 10 | import org.chromium.chrome.browser.profiles.Profile; |
| 11 | |
| 12 | /** |
| 13 | * This is a helper class to use favicon_service.cc's functionality. |
| 14 | * |
| 15 | * You can request a favicon image by web page URL. Note that an instance of this class should be |
| 16 | * created & used & destroyed (by destroy()) in the same thread due to the C++ CancelableTaskTracker |
| 17 | * class requirement. |
| 18 | */ |
| 19 | public class FaviconHelper { |
| 20 | |
| 21 | // Please keep in sync with favicon_types.h's IconType. |
| 22 | public static final int INVALID_ICON = 0; |
| 23 | public static final int FAVICON = 1 << 0; |
| 24 | public static final int TOUCH_ICON = 1 << 1; |
| 25 | public static final int TOUCH_PRECOMPOSED_ICON = 1 << 2; |
| 26 | |
| 27 | private int mNativeFaviconHelper; |
| 28 | |
| 29 | /** |
| 30 | * Callback interface for getting the result from getFaviconImageForURL method. |
| 31 | */ |
| 32 | public interface FaviconImageCallback { |
| 33 | /** |
| 34 | * This method will be called when the result favicon is ready. |
| 35 | * @param image Favicon image. |
| 36 | * @param iconUrl Favicon image's icon url. |
| 37 | */ |
| 38 | @CalledByNative("FaviconImageCallback") |
| 39 | public void onFaviconAvailable(Bitmap image, String iconUrl); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Allocate and initialize the C++ side of this class. |
| 44 | */ |
| 45 | public FaviconHelper() { |
| 46 | mNativeFaviconHelper = nativeInit(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Clean up the C++ side of this class. After the call, this class instance shouldn't be used. |
| 51 | */ |
| 52 | public void destroy() { |
| 53 | nativeDestroy(mNativeFaviconHelper); |
| 54 | mNativeFaviconHelper = 0; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void finalize() { |
| 59 | // It is not O.K. to call nativeDestroy() here because garbage collection can be |
| 60 | // performed in another thread, and CancelableTaskTrack should be destroyed in the |
| 61 | // same thread where it was created. So we just make sure that destroy() is called before |
| 62 | // garbage collection picks up by the following assertion. |
| 63 | assert mNativeFaviconHelper == 0; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get Favicon bitmap for the requested arguments. |
| 68 | * @param profile Profile used for the FaviconService construction. |
| 69 | * @param pageUrl The target Page URL to get the favicon. |
| 70 | * @param iconTypes One of the IconType class values. |
| 71 | * @param desiredSizeInDip The size of the favicon in dip we want to get. |
| 72 | * @param faviconImageCallback A method to be called back when the result is available. |
| 73 | * Note that this callback is not called if this method returns |
| 74 | * false. |
| 75 | * @return True if we GetFaviconImageForURL is successfully called. |
| 76 | */ |
| 77 | public boolean getFaviconImageForURL( |
| 78 | Profile profile, String pageUrl, int iconTypes, |
| 79 | int desiredSizeInDip, FaviconImageCallback faviconImageCallback) { |
| 80 | assert mNativeFaviconHelper != 0; |
| 81 | return nativeGetFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl, iconTypes, |
| 82 | desiredSizeInDip, faviconImageCallback); |
| 83 | } |
| 84 | |
| 85 | private static native int nativeInit(); |
| 86 | private static native void nativeDestroy(int nativeFaviconHelper); |
| 87 | private static native boolean nativeGetFaviconImageForURL(int nativeFaviconHelper, |
| 88 | Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip, |
| 89 | FaviconImageCallback faviconImageCallback); |
| 90 | } |