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.profiles;
7 import android.graphics.Bitmap;
9 import org.chromium.base.CalledByNative;
12 * Methods to bridge into native history to provide most recent urls, titles and thumbnails.
14 public class MostVisitedSites {
16 private long mNativeMostVisitedSites;
19 * Interface for receiving the list of most visited urls.
21 public interface MostVisitedURLsObserver {
23 * This is called when the list of most visited URLs is initially available or updated.
24 * Parameters guaranteed to be non-null.
26 * @param titles Array of most visited url page titles.
27 * @param urls Array of most visited urls.
29 @CalledByNative("MostVisitedURLsObserver")
30 public void onMostVisitedURLsAvailable(String[] titles, String[] urls);
34 * Interface for receiving a thumbnail for a most visited site.
36 public interface ThumbnailCallback {
38 * Callback method for fetching thumbnail of a most visited URL.
39 * Parameter may be null.
41 * @param thumbnail The bitmap thumbnail for the requested URL.
43 @CalledByNative("ThumbnailCallback")
44 public void onMostVisitedURLsThumbnailAvailable(Bitmap thumbnail);
48 * MostVisitedSites constructor requires a valid user profile object.
50 * @param profile The profile for which to fetch most visited sites.
52 public MostVisitedSites(Profile profile) {
53 mNativeMostVisitedSites = nativeInit(profile);
57 * Cleans up the C++ side of this class. This instance must not be used after calling destroy().
59 public void destroy() {
60 assert mNativeMostVisitedSites != 0;
61 nativeDestroy(mNativeMostVisitedSites);
62 mNativeMostVisitedSites = 0;
66 protected void finalize() {
67 // Ensure that destroy() was called.
68 assert mNativeMostVisitedSites == 0;
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
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.
79 public void setMostVisitedURLsObserver(final MostVisitedURLsObserver observer, int numSites) {
80 MostVisitedURLsObserver wrappedObserver = new MostVisitedURLsObserver() {
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);
89 nativeSetMostVisitedURLsObserver(mNativeMostVisitedSites, wrappedObserver, numSites);
93 * Fetches thumbnail bitmap for a url returned by getMostVisitedURLs.
95 * @param url String representation of url.
96 * @param callback Instance of a callback object.
98 public void getURLThumbnail(String url, final ThumbnailCallback callback) {
99 ThumbnailCallback wrappedCallback = new ThumbnailCallback() {
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);
108 nativeGetURLThumbnail(mNativeMostVisitedSites, url, wrappedCallback);
112 * Blacklist a URL from the most visited URLs list.
113 * @param url The URL to be blacklisted.
115 public void blacklistUrl(String url) {
116 nativeBlacklistUrl(mNativeMostVisitedSites, url);
120 * Called when the loading of the Most Visited page is complete.
122 public void onLoadingComplete() {
123 nativeOnLoadingComplete(mNativeMostVisitedSites);
127 * Record the opening of a Most Visited Item.
128 * @param index The index of the item that was opened.
130 public void recordOpenedMostVisitedItem(int index) {
131 nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSites, index);
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);