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