Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / profiles / MostVisitedSites.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.profiles;
6
7 import android.graphics.Bitmap;
8
9 import org.chromium.base.CalledByNative;
10
11 /**
12  * Methods to bridge into native history to provide most recent urls, titles and thumbnails.
13  */
14 public class MostVisitedSites {
15
16     private long mNativeMostVisitedSites;
17
18     /**
19      * Interface for receiving the list of most visited urls.
20      */
21     public interface MostVisitedURLsObserver {
22         /**
23          * This is called when the list of most visited URLs is initially available or updated.
24          * Parameters guaranteed to be non-null.
25          *
26          * @param titles Array of most visited url page titles.
27          * @param urls Array of most visited urls.
28          */
29         @CalledByNative("MostVisitedURLsObserver")
30         public void onMostVisitedURLsAvailable(String[] titles, String[] urls);
31     }
32
33     /**
34      * Interface for receiving a thumbnail for a most visited site.
35      */
36     public interface ThumbnailCallback {
37         /**
38          * Callback method for fetching thumbnail of a most visited URL.
39          * Parameter may be null.
40          *
41          * @param thumbnail The bitmap thumbnail for the requested URL.
42          */
43         @CalledByNative("ThumbnailCallback")
44         public void onMostVisitedURLsThumbnailAvailable(Bitmap thumbnail);
45     }
46
47     /**
48      * MostVisitedSites constructor requires a valid user profile object.
49      *
50      * @param profile The profile for which to fetch most visited sites.
51      */
52     public MostVisitedSites(Profile profile) {
53         mNativeMostVisitedSites = nativeInit(profile);
54     }
55
56     /**
57      * Cleans up the C++ side of this class. This instance must not be used after calling destroy().
58      */
59     public void destroy() {
60         assert mNativeMostVisitedSites != 0;
61         nativeDestroy(mNativeMostVisitedSites);
62         mNativeMostVisitedSites = 0;
63     }
64
65     @Override
66     protected void finalize() {
67         // Ensure that destroy() was called.
68         assert mNativeMostVisitedSites == 0;
69     }
70
71     /**
72      * Sets the MostVisitedURLsObserver to receive the list of most visited sites now or soon, and
73      * after any changes to the list. Note: the observer may be notified synchronously or
74      * asynchronously.
75      * @param observer The MostVisitedURLsObserver to be called once when the most visited sites
76      *            are initially available and again whenever the list of most visited sites changes.
77      * @param numSites The maximum number of most visited sites to return.
78      */
79     public void setMostVisitedURLsObserver(final MostVisitedURLsObserver observer, int numSites) {
80         MostVisitedURLsObserver wrappedObserver = new MostVisitedURLsObserver() {
81             @Override
82             public void onMostVisitedURLsAvailable(String[] titles, String[] urls) {
83                 // Don't notify observer if we've already been destroyed.
84                 if (mNativeMostVisitedSites != 0) {
85                     observer.onMostVisitedURLsAvailable(titles, urls);
86                 }
87             }
88         };
89         nativeSetMostVisitedURLsObserver(mNativeMostVisitedSites, wrappedObserver, numSites);
90     }
91
92     /**
93      * Fetches thumbnail bitmap for a url returned by getMostVisitedURLs.
94      *
95      * @param url String representation of url.
96      * @param callback Instance of a callback object.
97      */
98     public void getURLThumbnail(String url, final ThumbnailCallback callback) {
99         ThumbnailCallback wrappedCallback = new ThumbnailCallback() {
100             @Override
101             public void onMostVisitedURLsThumbnailAvailable(Bitmap thumbnail) {
102                 // Don't notify callback if we've already been destroyed.
103                 if (mNativeMostVisitedSites != 0) {
104                     callback.onMostVisitedURLsThumbnailAvailable(thumbnail);
105                 }
106             }
107         };
108         nativeGetURLThumbnail(mNativeMostVisitedSites, url, wrappedCallback);
109     }
110
111     /**
112      * Blacklist a URL from the most visited URLs list.
113      * @param url The URL to be blacklisted.
114      */
115     public void blacklistUrl(String url) {
116         nativeBlacklistUrl(mNativeMostVisitedSites, url);
117     }
118
119     /**
120      * Called when the loading of the Most Visited page is complete.
121      */
122     public void onLoadingComplete() {
123         nativeOnLoadingComplete(mNativeMostVisitedSites);
124     }
125
126     /**
127      * Record the opening of a Most Visited Item.
128      * @param index The index of the item that was opened.
129      */
130     public void recordOpenedMostVisitedItem(int index) {
131         nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSites, index);
132     }
133
134     private native long nativeInit(Profile profile);
135     private native void nativeDestroy(long nativeMostVisitedSites);
136     private native void nativeOnLoadingComplete(long nativeMostVisitedSites);
137     private native void nativeSetMostVisitedURLsObserver(long nativeMostVisitedSites,
138             MostVisitedURLsObserver observer, int numSites);
139     private native void nativeGetURLThumbnail(long nativeMostVisitedSites, String url,
140             ThumbnailCallback callback);
141     private native void nativeBlacklistUrl(long nativeMostVisitedSites, String url);
142     private native void nativeRecordOpenedMostVisitedItem(long nativeMostVisitedSites, int index);
143
144 }