Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: chrome/browser/android/ntp/most_visited_sites.h

Issue 1983063002: Move classes to //components/ntp_tiles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #ifndef CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_
6 #define CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_
7
8 #include <stddef.h>
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/macros.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/scoped_observer.h"
19 #include "chrome/browser/android/ntp/popular_sites.h"
20 #include "components/history/core/browser/history_types.h"
21 #include "components/history/core/browser/top_sites_observer.h"
22 #include "components/suggestions/proto/suggestions.pb.h"
23 #include "components/suggestions/suggestions_service.h"
24 #include "url/gurl.h"
25
26 namespace history {
27 class TopSites;
28 }
29
30 namespace suggestions {
31 class SuggestionsService;
32 }
33
34 namespace user_prefs {
35 class PrefRegistrySyncable;
36 }
37
38 namespace variations {
39 class VariationsService;
40 }
41
42 // Shim interface for SupervisedUserService.
43 class MostVisitedSitesSupervisor {
44 public:
45 struct Whitelist {
46 base::string16 title;
47 GURL entry_point;
48 base::FilePath large_icon_path;
49 };
50
51 class Observer {
52 public:
53 virtual void OnBlockedSitesChanged() = 0;
54
55 protected:
56 ~Observer() {}
57 };
58
59 // Pass non-null to set observer, or null to remove observer.
60 // If setting observer, there must not yet be an observer set.
61 // If removing observer, there must already be one to remove.
62 // Does not take ownership. Observer must outlive this object.
63 virtual void SetObserver(Observer* new_observer) = 0;
64
65 // If true, |url| should not be shown on the NTP.
66 virtual bool IsBlocked(const GURL& url) = 0;
67
68 // Explicit suggestions for sites to show on NTP.
69 virtual std::vector<Whitelist> whitelists() = 0;
70
71 // If true, be conservative about suggesting sites from outside sources.
72 virtual bool IsChildProfile() = 0;
73
74 protected:
75 virtual ~MostVisitedSitesSupervisor() {}
76 };
77
78 // Tracks the list of most visited sites and their thumbnails.
79 //
80 // Do not use, except from MostVisitedSitesBridge. The interface is in flux
81 // while we are extracting the functionality of the Java class to make available
82 // in C++.
83 //
84 // TODO(sfiera): finalize interface.
85 class MostVisitedSites : public history::TopSitesObserver,
86 public MostVisitedSitesSupervisor::Observer {
87 public:
88 struct Suggestion;
89 using SuggestionsVector = std::vector<Suggestion>;
90 using PopularSitesVector = std::vector<PopularSites::Site>;
91
92 // The source of the Most Visited sites.
93 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST };
94
95 // The observer to be notified when the list of most visited sites changes.
96 class Observer {
97 public:
98 virtual void OnMostVisitedURLsAvailable(
99 const SuggestionsVector& suggestions) = 0;
100 virtual void OnPopularURLsAvailable(const PopularSitesVector& sites) = 0;
101
102 protected:
103 virtual ~Observer() {}
104 };
105
106 struct Suggestion {
107 base::string16 title;
108 GURL url;
109 MostVisitedSource source;
110
111 // Only valid for source == WHITELIST (empty otherwise).
112 base::FilePath whitelist_icon_path;
113
114 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise).
115 int provider_index;
116
117 Suggestion();
118 ~Suggestion();
119
120 Suggestion(Suggestion&&);
121 Suggestion& operator=(Suggestion&&);
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(Suggestion);
125 };
126
127 MostVisitedSites(PrefService* prefs,
128 const TemplateURLService* template_url_service,
129 variations::VariationsService* variations_service,
130 net::URLRequestContextGetter* download_context,
131 const base::FilePath& popular_sites_directory,
132 scoped_refptr<history::TopSites> top_sites,
133 suggestions::SuggestionsService* suggestions,
134 MostVisitedSitesSupervisor* supervisor);
135
136 ~MostVisitedSites() override;
137
138 // Does not take ownership of |observer|, which must outlive this object and
139 // must not be null.
140 void SetMostVisitedURLsObserver(Observer* observer, int num_sites);
141
142 using ThumbnailCallback = base::Callback<
143 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>;
144 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback);
145 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url);
146 void RecordTileTypeMetrics(const std::vector<int>& tile_types);
147 void RecordOpenedMostVisitedItem(int index, int tile_type);
148
149 // MostVisitedSitesSupervisor::Observer implementation.
150 void OnBlockedSitesChanged() override;
151
152 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
153
154 private:
155 friend class MostVisitedSitesTest;
156
157 // TODO(treib): use SuggestionsVector in internal functions. crbug.com/601734
158 using SuggestionsPtrVector = std::vector<std::unique_ptr<Suggestion>>;
159
160 void BuildCurrentSuggestions();
161
162 // Initialize the query to Top Sites. Called if the SuggestionsService is not
163 // enabled, or if it returns no data.
164 void InitiateTopSitesQuery();
165
166 // If there's a whitelist entry point for the URL, return the large icon path.
167 base::FilePath GetWhitelistLargeIconPath(const GURL& url);
168
169 // Callback for when data is available from TopSites.
170 void OnMostVisitedURLsAvailable(
171 const history::MostVisitedURLList& visited_list);
172
173 // Callback for when data is available from the SuggestionsService.
174 void OnSuggestionsProfileAvailable(
175 const suggestions::SuggestionsProfile& suggestions_profile);
176
177 // Takes the personal suggestions and creates whitelist entry point
178 // suggestions if necessary.
179 SuggestionsPtrVector CreateWhitelistEntryPointSuggestions(
180 const SuggestionsPtrVector& personal_suggestions);
181
182 // Takes the personal and whitelist suggestions and creates popular
183 // suggestions if necessary.
184 SuggestionsPtrVector CreatePopularSitesSuggestions(
185 const SuggestionsPtrVector& personal_suggestions,
186 const SuggestionsPtrVector& whitelist_suggestions);
187
188 // Takes the personal suggestions, creates and merges in whitelist and popular
189 // suggestions if appropriate, and saves the new suggestions.
190 void SaveNewNTPSuggestions(SuggestionsPtrVector* personal_suggestions);
191
192 // Workhorse for SaveNewNTPSuggestions above. Implemented as a separate static
193 // method for ease of testing.
194 static SuggestionsPtrVector MergeSuggestions(
195 SuggestionsPtrVector* personal_suggestions,
196 SuggestionsPtrVector* whitelist_suggestions,
197 SuggestionsPtrVector* popular_suggestions,
198 const std::vector<std::string>& old_sites_url,
199 const std::vector<bool>& old_sites_is_personal);
200
201 void GetPreviousNTPSites(size_t num_tiles,
202 std::vector<std::string>* old_sites_url,
203 std::vector<bool>* old_sites_source) const;
204
205 void SaveCurrentNTPSites();
206
207 // Takes suggestions from |src_suggestions| and moves them to
208 // |dst_suggestions| if the suggestion's url/host matches
209 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from
210 // |src_suggestions| are returned for ease of insertion later.
211 static std::vector<size_t> InsertMatchingSuggestions(
212 SuggestionsPtrVector* src_suggestions,
213 SuggestionsPtrVector* dst_suggestions,
214 const std::vector<std::string>& match_urls,
215 const std::vector<std::string>& match_hosts);
216
217 // Inserts suggestions from |src_suggestions| at positions |insert_positions|
218 // into |dst_suggestions| where ever empty starting from |start_position|.
219 // Returns the last filled position so that future insertions can start from
220 // there.
221 static size_t InsertAllSuggestions(
222 size_t start_position,
223 const std::vector<size_t>& insert_positions,
224 SuggestionsPtrVector* src_suggestions,
225 SuggestionsPtrVector* dst_suggestions);
226
227 // Notifies the observer about the availability of suggestions.
228 // Also records impressions UMA if not done already.
229 void NotifyMostVisitedURLsObserver();
230
231 void OnPopularSitesAvailable(bool success);
232
233 // Runs on the UI Thread.
234 void OnLocalThumbnailFetched(
235 const GURL& url,
236 const ThumbnailCallback& callback,
237 std::unique_ptr<SkBitmap> bitmap);
238
239 // Callback for when the thumbnail lookup is complete.
240 // Runs on the UI Thread.
241 void OnObtainedThumbnail(
242 bool is_local_thumbnail,
243 const ThumbnailCallback& callback,
244 const GURL& url,
245 const SkBitmap* bitmap);
246
247 // Records thumbnail-related UMA histogram metrics.
248 void RecordThumbnailUMAMetrics();
249
250 // Records UMA histogram metrics related to the number of impressions.
251 void RecordImpressionUMAMetrics();
252
253 // history::TopSitesObserver implementation.
254 void TopSitesLoaded(history::TopSites* top_sites) override;
255 void TopSitesChanged(history::TopSites* top_sites,
256 ChangeReason change_reason) override;
257
258 PrefService* prefs_;
259 const TemplateURLService* template_url_service_;
260 variations::VariationsService* variations_service_;
261 net::URLRequestContextGetter* download_context_;
262 base::FilePath popular_sites_directory_;
263 scoped_refptr<history::TopSites> top_sites_;
264 suggestions::SuggestionsService* suggestions_service_;
265 MostVisitedSitesSupervisor* supervisor_;
266
267 Observer* observer_;
268
269 // The maximum number of most visited sites to return.
270 int num_sites_;
271
272 // Whether we have received an initial set of most visited sites (from either
273 // TopSites or the SuggestionsService).
274 bool received_most_visited_sites_;
275
276 // Whether we have received the set of popular sites. Immediately set to true
277 // if popular sites are disabled.
278 bool received_popular_sites_;
279
280 // Whether we have recorded one-shot UMA metrics such as impressions. They are
281 // recorded once both the previous flags are true.
282 bool recorded_uma_;
283
284 std::unique_ptr<
285 suggestions::SuggestionsService::ResponseCallbackList::Subscription>
286 suggestions_subscription_;
287
288 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_;
289
290 MostVisitedSource mv_source_;
291
292 std::unique_ptr<PopularSites> popular_sites_;
293
294 SuggestionsVector current_suggestions_;
295
296 // For callbacks may be run after destruction.
297 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
298
299 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
300 };
301
302 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698