| 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; | |
| 8 import org.chromium.chrome.browser.ntp.MostVisitedTileType.MostVisitedTileTypeEn
um; | 7 import org.chromium.chrome.browser.ntp.MostVisitedTileType.MostVisitedTileTypeEn
um; |
| 9 import org.chromium.chrome.browser.ntp.NTPTileSource.NTPTileSourceEnum; | 8 import org.chromium.chrome.browser.ntp.NTPTileSource.NTPTileSourceEnum; |
| 10 import org.chromium.chrome.browser.profiles.Profile; | 9 import org.chromium.chrome.browser.profiles.Profile; |
| 11 | 10 |
| 12 /** | 11 /** |
| 13 * Methods to bridge into native history to provide most recent urls, titles and
thumbnails. | 12 * Methods to bridge into native history to provide most recent urls, titles and
thumbnails. |
| 14 */ | 13 */ |
| 15 public class MostVisitedSites { | 14 public class MostVisitedSitesBridge implements MostVisitedSites { |
| 16 private long mNativeMostVisitedSitesBridge; | 15 private long mNativeMostVisitedSitesBridge; |
| 17 | 16 |
| 18 /** | 17 /** |
| 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. | 18 * MostVisitedSites constructor requires a valid user profile object. |
| 50 * | 19 * |
| 51 * @param profile The profile for which to fetch most visited sites. | 20 * @param profile The profile for which to fetch most visited sites. |
| 52 */ | 21 */ |
| 53 public MostVisitedSites(Profile profile) { | 22 public MostVisitedSitesBridge(Profile profile) { |
| 54 mNativeMostVisitedSitesBridge = nativeInit(profile); | 23 mNativeMostVisitedSitesBridge = nativeInit(profile); |
| 55 } | 24 } |
| 56 | 25 |
| 57 /** | 26 /** |
| 58 * Cleans up the C++ side of this class. This instance must not be used afte
r calling destroy(). | 27 * Cleans up the C++ side of this class. This instance must not be used afte
r calling destroy(). |
| 59 */ | 28 */ |
| 29 @Override |
| 60 public void destroy() { | 30 public void destroy() { |
| 61 assert mNativeMostVisitedSitesBridge != 0; | 31 assert mNativeMostVisitedSitesBridge != 0; |
| 62 nativeDestroy(mNativeMostVisitedSitesBridge); | 32 nativeDestroy(mNativeMostVisitedSitesBridge); |
| 63 mNativeMostVisitedSitesBridge = 0; | 33 mNativeMostVisitedSitesBridge = 0; |
| 64 } | 34 } |
| 65 | 35 |
| 66 /** | 36 @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) { | 37 public void setObserver(final Observer observer, int numSites) { |
| 73 Observer wrappedObserver = new Observer() { | 38 Observer wrappedObserver = new Observer() { |
| 74 @Override | 39 @Override |
| 75 public void onMostVisitedURLsAvailable( | 40 public void onMostVisitedURLsAvailable( |
| 76 String[] titles, String[] urls, String[] whitelistIconPaths,
int[] sources) { | 41 String[] titles, String[] urls, String[] whitelistIconPaths,
int[] sources) { |
| 77 // Don't notify observer if we've already been destroyed. | 42 // Don't notify observer if we've already been destroyed. |
| 78 if (mNativeMostVisitedSitesBridge != 0) { | 43 if (mNativeMostVisitedSitesBridge != 0) { |
| 79 observer.onMostVisitedURLsAvailable(titles, urls, whitelistI
conPaths, sources); | 44 observer.onMostVisitedURLsAvailable(titles, urls, whitelistI
conPaths, sources); |
| 80 } | 45 } |
| 81 } | 46 } |
| 82 @Override | 47 @Override |
| 83 public void onIconMadeAvailable(String siteUrl) { | 48 public void onIconMadeAvailable(String siteUrl) { |
| 84 // Don't notify observer if we've already been destroyed. | 49 // Don't notify observer if we've already been destroyed. |
| 85 if (mNativeMostVisitedSitesBridge != 0) { | 50 if (mNativeMostVisitedSitesBridge != 0) { |
| 86 observer.onIconMadeAvailable(siteUrl); | 51 observer.onIconMadeAvailable(siteUrl); |
| 87 } | 52 } |
| 88 } | 53 } |
| 89 }; | 54 }; |
| 90 nativeSetObserver(mNativeMostVisitedSitesBridge, wrappedObserver, numSit
es); | 55 nativeSetObserver(mNativeMostVisitedSitesBridge, wrappedObserver, numSit
es); |
| 91 } | 56 } |
| 92 | 57 |
| 93 /** | 58 @Override |
| 94 * Blacklists a URL from the most visited URLs list. | |
| 95 */ | |
| 96 public void addBlacklistedUrl(String url) { | 59 public void addBlacklistedUrl(String url) { |
| 97 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, true
); | 60 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, true
); |
| 98 } | 61 } |
| 99 | 62 |
| 100 /** | 63 @Override |
| 101 * Removes a URL from the most visited URLs blacklist. | |
| 102 */ | |
| 103 public void removeBlacklistedUrl(String url) { | 64 public void removeBlacklistedUrl(String url) { |
| 104 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, fals
e); | 65 nativeAddOrRemoveBlacklistedUrl(mNativeMostVisitedSitesBridge, url, fals
e); |
| 105 } | 66 } |
| 106 | 67 |
| 107 /** | 68 @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) { | 69 public void recordPageImpression(int[] tileTypes, int[] sources, String[] ti
leUrls) { |
| 117 nativeRecordPageImpression(mNativeMostVisitedSitesBridge, tileTypes, sou
rces, tileUrls); | 70 nativeRecordPageImpression(mNativeMostVisitedSitesBridge, tileTypes, sou
rces, tileUrls); |
| 118 } | 71 } |
| 119 | 72 |
| 120 /** | 73 @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( | 74 public void recordOpenedMostVisitedItem( |
| 127 int index, @MostVisitedTileTypeEnum int type, @NTPTileSourceEnum int
source) { | 75 int index, @MostVisitedTileTypeEnum int type, @NTPTileSourceEnum int
source) { |
| 128 nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSitesBridge, index,
type, source); | 76 nativeRecordOpenedMostVisitedItem(mNativeMostVisitedSitesBridge, index,
type, source); |
| 129 } | 77 } |
| 130 | 78 |
| 131 private native long nativeInit(Profile profile); | 79 private native long nativeInit(Profile profile); |
| 132 private native void nativeDestroy(long nativeMostVisitedSitesBridge); | 80 private native void nativeDestroy(long nativeMostVisitedSitesBridge); |
| 81 |
| 82 // TODO(dgn): The second argument is a MostVisitedSites.Observer. We have to
use Object as type |
| 83 // because of https://crbug.com/702703 |
| 133 private native void nativeSetObserver( | 84 private native void nativeSetObserver( |
| 134 long nativeMostVisitedSitesBridge, Observer observer, int numSites); | 85 long nativeMostVisitedSitesBridge, Object observer, int numSites); |
| 135 private native void nativeAddOrRemoveBlacklistedUrl( | 86 private native void nativeAddOrRemoveBlacklistedUrl( |
| 136 long nativeMostVisitedSitesBridge, String url, boolean addUrl); | 87 long nativeMostVisitedSitesBridge, String url, boolean addUrl); |
| 137 private native void nativeRecordPageImpression( | 88 private native void nativeRecordPageImpression( |
| 138 long nativeMostVisitedSitesBridge, int[] tileTypes, int[] sources, S
tring[] tileUrls); | 89 long nativeMostVisitedSitesBridge, int[] tileTypes, int[] sources, S
tring[] tileUrls); |
| 139 private native void nativeRecordOpenedMostVisitedItem( | 90 private native void nativeRecordOpenedMostVisitedItem( |
| 140 long nativeMostVisitedSitesBridge, int index, int tileType, int sour
ce); | 91 long nativeMostVisitedSitesBridge, int index, int tileType, int sour
ce); |
| 141 } | 92 } |
| OLD | NEW |