Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / favicon / FaviconHelper.java
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 import android.graphics.Color;
9
10 import org.chromium.base.CalledByNative;
11 import org.chromium.chrome.browser.profiles.Profile;
12
13 /**
14  * This is a helper class to use favicon_service.cc's functionality.
15  *
16  * You can request a favicon image by web page URL. Note that an instance of
17  * this class should be created & used & destroyed (by destroy()) in the same
18  * thread due to the C++ base::CancelableTaskTracker class
19  * requirement.
20  */
21 public class FaviconHelper {
22
23     // Please keep in sync with favicon_types.h's IconType.
24     public static final int INVALID_ICON = 0;
25     public static final int FAVICON = 1 << 0;
26     public static final int TOUCH_ICON = 1 << 1;
27     public static final int TOUCH_PRECOMPOSED_ICON = 1 << 2;
28
29     private long mNativeFaviconHelper;
30
31     /**
32      * Callback interface for getting the result from getLocalFaviconImageForURL method.
33      */
34     public interface FaviconImageCallback {
35         /**
36          * This method will be called when the result favicon is ready.
37          * @param image   Favicon image.
38          * @param iconUrl Favicon image's icon url.
39          */
40         @CalledByNative("FaviconImageCallback")
41         public void onFaviconAvailable(Bitmap image, String iconUrl);
42     }
43
44     /**
45      * Allocate and initialize the C++ side of this class.
46      */
47     public FaviconHelper() {
48         mNativeFaviconHelper = nativeInit();
49     }
50
51     @Override
52     protected void finalize() {
53         // Ensure that destroy() was called.
54         assert mNativeFaviconHelper == 0;
55     }
56
57     /**
58      * Clean up the C++ side of this class. After the call, this class instance shouldn't be used.
59      */
60     public void destroy() {
61         assert mNativeFaviconHelper != 0;
62         nativeDestroy(mNativeFaviconHelper);
63         mNativeFaviconHelper = 0;
64     }
65
66     /**
67      * Get Favicon bitmap for the requested arguments. Retrieves favicons only for pages the user
68      * has visited on the current device.
69      * @param profile               Profile used for the FaviconService construction.
70      * @param pageUrl               The target Page URL to get the favicon.
71      * @param iconTypes             One of the IconType class values.
72      * @param desiredSizeInDip      The size of the favicon in dip we want to get.
73      * @param faviconImageCallback  A method to be called back when the result is available.
74      *                              Note that this callback is not called if this method returns
75      *                              false.
76      * @return                      True if GetLocalFaviconImageForURL is successfully called.
77      */
78     public boolean getLocalFaviconImageForURL(
79             Profile profile, String pageUrl, int iconTypes,
80             int desiredSizeInDip, FaviconImageCallback faviconImageCallback) {
81         assert mNativeFaviconHelper != 0;
82         return nativeGetLocalFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl, iconTypes,
83                 desiredSizeInDip, faviconImageCallback);
84     }
85
86     /**
87      * Return the dominant color of a given bitmap in {@link Color} format.
88      * @param image The bitmap image to find the dominant color for.
89      * @return The dominant color in {@link Color} format.
90      */
91     public int getDominantColorForBitmap(Bitmap image) {
92         return nativeGetDominantColorForBitmap(mNativeFaviconHelper, image);
93     }
94
95     /**
96      * Get 16x16 Favicon bitmap for the requested arguments. Only retrives favicons in synced
97      * session storage. (e.g. favicons synced from other devices).
98      * TODO(apiccion): provide a way to obtain higher resolution favicons.
99      * @param profile   Profile used for the FaviconService construction.
100      * @param pageUrl   The target Page URL to get the favicon.
101      *
102      * @return          16x16 favicon Bitmap corresponding to the pageUrl.
103      */
104     public Bitmap getSyncedFaviconImageForURL(Profile profile, String pageUrl) {
105         assert mNativeFaviconHelper != 0;
106         return nativeGetSyncedFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl);
107     }
108
109     private static native long nativeInit();
110     private static native void nativeDestroy(long nativeFaviconHelper);
111     private static native boolean nativeGetLocalFaviconImageForURL(long nativeFaviconHelper,
112             Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip,
113             FaviconImageCallback faviconImageCallback);
114     private static native Bitmap nativeGetSyncedFaviconImageForURL(long nativeFaviconHelper,
115             Profile profile, String pageUrl);
116     private static native int nativeGetDominantColorForBitmap(long nativeFaviconHelper,
117             Bitmap image);
118 }