- add sources.
[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 this class should be
17  * created & used & destroyed (by destroy()) in the same thread due to the C++ CancelableTaskTracker
18  * class requirement.
19  */
20 public class FaviconHelper {
21
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;
27
28     private int mNativeFaviconHelper;
29
30     /**
31      * Callback interface for getting the result from getLocalFaviconImageForURL method.
32      */
33     public interface FaviconImageCallback {
34         /**
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.
38          */
39         @CalledByNative("FaviconImageCallback")
40         public void onFaviconAvailable(Bitmap image, String iconUrl);
41     }
42
43     /**
44      * Allocate and initialize the C++ side of this class.
45      */
46     public FaviconHelper() {
47         mNativeFaviconHelper = nativeInit();
48     }
49
50     @Override
51     protected void finalize() {
52         // Ensure that destroy() was called.
53         assert mNativeFaviconHelper == 0;
54     }
55
56     /**
57      * Clean up the C++ side of this class. After the call, this class instance shouldn't be used.
58      */
59     public void destroy() {
60         nativeDestroy(mNativeFaviconHelper);
61         mNativeFaviconHelper = 0;
62     }
63
64     /**
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
73      *                              false.
74      * @return                      True if GetLocalFaviconImageForURL is successfully called.
75      */
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);
82     }
83
84     /**
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.
88      */
89     public int getDominantColorForBitmap(Bitmap image) {
90         return nativeGetDominantColorForBitmap(mNativeFaviconHelper, image);
91     }
92
93     /**
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.
99      *
100      * @return          16x16 favicon Bitmap corresponding to the pageUrl.
101      */
102     public Bitmap getSyncedFaviconImageForURL(Profile profile, String pageUrl) {
103         assert mNativeFaviconHelper != 0;
104         return nativeGetSyncedFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl);
105     }
106
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,
115             Bitmap image);
116 }