| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.suggestions; | 5 package org.chromium.chrome.browser.suggestions; |
| 6 | 6 |
| 7 import org.chromium.base.annotations.CalledByNative; | 7 import org.chromium.base.annotations.JNIAdditionalImport; |
| 8 import org.chromium.chrome.browser.ntp.MostVisitedTileType.MostVisitedTileTypeEn
um; | 8 import org.chromium.chrome.browser.ntp.MostVisitedTileType.MostVisitedTileTypeEn
um; |
| 9 import org.chromium.chrome.browser.ntp.NTPTileSource.NTPTileSourceEnum; | 9 import org.chromium.chrome.browser.ntp.NTPTileSource.NTPTileSourceEnum; |
| 10 import org.chromium.chrome.browser.profiles.Profile; | 10 import org.chromium.chrome.browser.profiles.Profile; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Methods to bridge into native history to provide most recent urls, titles and
thumbnails. | 13 * Methods to bridge into native history to provide most recent urls, titles and
thumbnails. |
| 14 */ | 14 */ |
| 15 public class MostVisitedSites { | 15 @JNIAdditionalImport(MostVisitedSites.class) // Needed for the Observer usage in
the native calls. |
| 16 public class MostVisitedSitesBridge implements MostVisitedSites { |
| 16 private long mNativeMostVisitedSitesBridge; | 17 private long mNativeMostVisitedSitesBridge; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * An interface for handling events in {@link MostVisitedSites}. | |
| 20 */ | |
| 21 public interface Observer { | |
| 22 /** | |
| 23 * This is called when the list of most visited URLs is initially availa
ble 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, including popular URLs if | |
| 28 * available and necessary (i.e. there aren't enough most | |
| 29 * visited URLs). | |
| 30 * @param whitelistIconPaths The paths to the icon image files for white
listed tiles, empty | |
| 31 * strings otherwise. | |
| 32 * @param sources For each tile, the {@code NTPTileSource} that generate
d the tile. | |
| 33 */ | |
| 34 @CalledByNative("Observer") | |
| 35 void onMostVisitedURLsAvailable( | |
| 36 String[] titles, String[] urls, String[] whitelistIconPaths, int
[] sources); | |
| 37 | |
| 38 /** | |
| 39 * This is called when a previously uncached icon has been fetched. | |
| 40 * Parameters guaranteed to be non-null. | |
| 41 * | |
| 42 * @param siteUrl URL of site with newly-cached icon. | |
| 43 */ | |
| 44 @CalledByNative("Observer") | |
| 45 void onIconMadeAvailable(String siteUrl); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * MostVisitedSites constructor requires a valid user profile object. | 20 * MostVisitedSites constructor requires a valid user profile object. |
| 50 * | 21 * |
| 51 * @param profile The profile for which to fetch most visited sites. | 22 * @param profile The profile for which to fetch most visited sites. |
| 52 */ | 23 */ |
| 53 public MostVisitedSites(Profile profile) { | 24 public MostVisitedSitesBridge(Profile profile) { |
| 54 mNativeMostVisitedSitesBridge = nativeInit(profile); | 25 mNativeMostVisitedSitesBridge = nativeInit(profile); |
| 55 } | 26 } |
| 56 | 27 |
| 57 /** | 28 /** |
| 58 * Cleans up the C++ side of this class. This instance must not be used afte
r calling destroy(). | 29 * Cleans up the C++ side of this class. This instance must not be used afte
r calling destroy(). |
| 59 */ | 30 */ |
| 31 @Override |
| 60 public void destroy() { | 32 public void destroy() { |
| 61 assert mNativeMostVisitedSitesBridge != 0; | 33 assert mNativeMostVisitedSitesBridge != 0; |
| 62 nativeDestroy(mNativeMostVisitedSitesBridge); | 34 nativeDestroy(mNativeMostVisitedSitesBridge); |
| 63 mNativeMostVisitedSitesBridge = 0; | 35 mNativeMostVisitedSitesBridge = 0; |
| 64 } | 36 } |
| 65 | 37 |
| 66 /** | 38 @Override |
| 67 * Sets the recipient for events from {@link MostVisitedSites}. The observer
may be notified | |
| 68 * synchronously or asynchronously. | |
| 69 * @param observer The observer to be notified. | |
| 70 * @param numSites The maximum number of sites to return. | |
| 71 */ | |
| 72 public void setObserver(final Observer observer, int numSites) { | 39 public void setObserver(final Observer observer, int numSites) { |
| 73 Observer wrappedObserver = new Observer() { | 40 Observer wrappedObserver = new Observer() { |
| 74 @Override | 41 @Override |
| 75 public void onMostVisitedURLsAvailable( | 42 public void onMostVisitedURLsAvailable( |
| 76 String[] titles, String[] urls, String[] whitelistIconPaths,
int[] sources) { | 43 String[] titles, String[] urls, String[] whitelistIconPaths,
int[] sources) { |
| 77 // Don't notify observer if we've already been destroyed. | 44 // Don't notify observer if we've already been destroyed. |
| 78 if (mNativeMostVisitedSitesBridge != 0) { | 45 if (mNativeMostVisitedSitesBridge != 0) { |
| 79 observer.onMostVisitedURLsAvailable(titles, urls, whitelistI
conPaths, sources); | 46 observer.onMostVisitedURLsAvailable(titles, urls, whitelistI
conPaths, sources); |
| 80 } | 47 } |
| 81 } | 48 } |
| 82 @Override | 49 @Override |
| 83 public void onIconMadeAvailable(String siteUrl) { | 50 public void onIconMadeAvailable(String siteUrl) { |
| 84 // Don't notify observer if we've already been destroyed. | 51 // Don't notify observer if we've already been destroyed. |
| 85 if (mNativeMostVisitedSitesBridge != 0) { | 52 if (mNativeMostVisitedSitesBridge != 0) { |
| 86 observer.onIconMadeAvailable(siteUrl); | 53 observer.onIconMadeAvailable(siteUrl); |
| 87 } | 54 } |
| 88 } | 55 } |
| 89 }; | 56 }; |
| 90 nativeSetObserver(mNativeMostVisitedSitesBridge, wrappedObserver, numSit
es); | 57 nativeSetObserver(mNativeMostVisitedSitesBridge, wrappedObserver, numSit
es); |
| 91 } | 58 } |
| 92 | 59 |
| 93 /** | 60 @Override |
| 94 * Blacklists a URL from the most visited URLs list. | |
| 95 */ | |
| 96 public void addBlacklistedUrl(String url) { | 61 public void addBlacklistedUrl(String url) { |
| 97 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, true
); | 62 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, true
); |
| 98 } | 63 } |
| 99 | 64 |
| 100 /** | 65 @Override |
| 101 * Removes a URL from the most visited URLs blacklist. | |
| 102 */ | |
| 103 public void removeBlacklistedUrl(String url) { | 66 public void removeBlacklistedUrl(String url) { |
| 104 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, fals
e); | 67 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, fals
e); |
| 105 } | 68 } |
| 106 | 69 |
| 107 /** | 70 @Override |
| 108 * Records metrics about an impression, including the sources (local, server
, ...) and visual | |
| 109 * types of the tiles that are shown. | |
| 110 * @param tileTypes An array of values from MostVisitedTileType indicating t
he type of each | |
| 111 * tile that's currently showing. | |
| 112 * @param sources An array of values from NTPTileSource indicating the sourc
e of each tile | |
| 113 * that's currently showing. | |
| 114 * @param tileUrls An array of strings indicating the URL of each tile. | |
| 115 */ | |
| 116 public void recordPageImpression(int[] tileTypes, int[] sources, String[] ti
leUrls) { | 71 public void recordPageImpression(int[] tileTypes, int[] sources, String[] ti
leUrls) { |
| 117 nativeRecordPageImpression(mNativeMostVisitedSitesBridge, tileTypes, sou
rces, tileUrls); | 72 nativeRecordPageImpression(mNativeMostVisitedSitesBridge, tileTypes, sou
rces, tileUrls); |
| 118 } | 73 } |
| 119 | 74 |
| 120 /** | 75 @Override |
| 121 * Records the opening of a Most Visited Item. | |
| 122 * @param index The index of the item that was opened. | |
| 123 * @param type The visual type of the item as defined in {@code MostVisitedT
ileType}. | |
| 124 * @param source The {@code NTPTileSource} that generated this item. | |
| 125 */ | |
| 126 public void recordOpenedMostVisitedItem( | 76 public void recordOpenedMostVisitedItem( |
| 127 int index, @MostVisitedTileTypeEnum int type, @NTPTileSourceEnum int
source) { | 77 int index, @MostVisitedTileTypeEnum int type, @NTPTileSourceEnum int
source) { |
| 128 nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSitesBridge, index,
type, source); | 78 nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSitesBridge, index,
type, source); |
| 129 } | 79 } |
| 130 | 80 |
| 131 private native long nativeInit(Profile profile); | 81 private native long nativeInit(Profile profile); |
| 132 private native void nativeDestroy(long nativeMostVisitedSitesBridge); | 82 private native void nativeDestroy(long nativeMostVisitedSitesBridge); |
| 133 private native void nativeSetObserver( | 83 private native void nativeSetObserver( |
| 134 long nativeMostVisitedSitesBridge, Observer observer, int numSites); | 84 long nativeMostVisitedSitesBridge, MostVisitedSites.Observer observe
r, int numSites); |
| 135 private native void nativeAddOrRemoveBlacklistedUrl( | 85 private native void nativeAddOrRemoveBlacklistedUrl( |
| 136 long nativeMostVisitedSitesBridge, String url, boolean addUrl); | 86 long nativeMostVisitedSitesBridge, String url, boolean addUrl); |
| 137 private native void nativeRecordPageImpression( | 87 private native void nativeRecordPageImpression( |
| 138 long nativeMostVisitedSitesBridge, int[] tileTypes, int[] sources, S
tring[] tileUrls); | 88 long nativeMostVisitedSitesBridge, int[] tileTypes, int[] sources, S
tring[] tileUrls); |
| 139 private native void nativeRecordOpenedMostVisitedItem( | 89 private native void nativeRecordOpenedMostVisitedItem( |
| 140 long nativeMostVisitedSitesBridge, int index, int tileType, int sour
ce); | 90 long nativeMostVisitedSitesBridge, int index, int tileType, int sour
ce); |
| 141 } | 91 } |
| OLD | NEW |