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

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

Issue 1930353002: Invoke factories once in MostVisitedSites(). (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
« no previous file with comments | « chrome/browser/android/ntp/most_visited_sites.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "chrome/browser/android/ntp/most_visited_sites.h" 5 #include "chrome/browser/android/ntp/most_visited_sites.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 22 matching lines...) Expand all
33 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/url_data_source.h" 34 #include "content/public/browser/url_data_source.h"
35 #include "third_party/skia/include/core/SkBitmap.h" 35 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "ui/gfx/codec/jpeg_codec.h" 36 #include "ui/gfx/codec/jpeg_codec.h"
37 #include "url/gurl.h" 37 #include "url/gurl.h"
38 38
39 using content::BrowserThread; 39 using content::BrowserThread;
40 using history::TopSites; 40 using history::TopSites;
41 using suggestions::ChromeSuggestion; 41 using suggestions::ChromeSuggestion;
42 using suggestions::SuggestionsProfile; 42 using suggestions::SuggestionsProfile;
43 using suggestions::SuggestionsService;
44 using suggestions::SuggestionsServiceFactory; 43 using suggestions::SuggestionsServiceFactory;
45 44
46 namespace { 45 namespace {
47 46
48 // Identifiers for the various tile sources. 47 // Identifiers for the various tile sources.
49 const char kHistogramClientName[] = "client"; 48 const char kHistogramClientName[] = "client";
50 const char kHistogramServerName[] = "server"; 49 const char kHistogramServerName[] = "server";
51 const char kHistogramServerFormat[] = "server%d"; 50 const char kHistogramServerFormat[] = "server%d";
52 const char kHistogramPopularName[] = "popular"; 51 const char kHistogramPopularName[] = "popular";
53 const char kHistogramWhitelistName[] = "whitelist"; 52 const char kHistogramWhitelistName[] = "whitelist";
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 171
173 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} 172 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {}
174 173
175 MostVisitedSites::Suggestion::~Suggestion() {} 174 MostVisitedSites::Suggestion::~Suggestion() {}
176 175
177 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; 176 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default;
178 MostVisitedSites::Suggestion& 177 MostVisitedSites::Suggestion&
179 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; 178 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default;
180 179
181 MostVisitedSites::MostVisitedSites(Profile* profile) 180 MostVisitedSites::MostVisitedSites(Profile* profile)
182 : profile_(profile), observer_(nullptr), num_sites_(0), 181 : profile_(profile), top_sites_(TopSitesFactory::GetForProfile(profile)),
182 suggestions_service_(
183 SuggestionsServiceFactory::GetForProfile(profile_)),
Marc Treib 2016/04/29 10:12:34 nit: Doesn't this fit on the previous line?
sfiera 2016/04/29 10:15:32 Oh, yes, it does. Silly irrelevant editor thing.
184 observer_(nullptr), num_sites_(0),
183 received_most_visited_sites_(false), received_popular_sites_(false), 185 received_most_visited_sites_(false), received_popular_sites_(false),
184 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) { 186 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) {
185 // Register the thumbnails debugging page. 187 // Register the thumbnails debugging page.
186 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 188 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
187 189
188 SupervisedUserService* supervised_user_service = 190 SupervisedUserService* supervised_user_service =
189 SupervisedUserServiceFactory::GetForProfile(profile_); 191 SupervisedUserServiceFactory::GetForProfile(profile_);
190 supervised_user_service->AddObserver(this); 192 supervised_user_service->AddObserver(this);
191 } 193 }
192 194
(...skipping 16 matching lines...) Expand all
209 profile_->GetRequestContext(), 211 profile_->GetRequestContext(),
210 GetPopularSitesCountry(), 212 GetPopularSitesCountry(),
211 GetPopularSitesVersion(), 213 GetPopularSitesVersion(),
212 false, 214 false,
213 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, 215 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
214 base::Unretained(this)))); 216 base::Unretained(this))));
215 } else { 217 } else {
216 received_popular_sites_ = true; 218 received_popular_sites_ = true;
217 } 219 }
218 220
219 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); 221 if (top_sites_) {
220 if (top_sites) {
221 // TopSites updates itself after a delay. To ensure up-to-date results, 222 // TopSites updates itself after a delay. To ensure up-to-date results,
222 // force an update now. 223 // force an update now.
223 top_sites->SyncWithHistory(); 224 top_sites_->SyncWithHistory();
224 225
225 // Register as TopSitesObserver so that we can update ourselves when the 226 // Register as TopSitesObserver so that we can update ourselves when the
226 // TopSites changes. 227 // TopSites changes.
227 scoped_observer_.Add(top_sites.get()); 228 scoped_observer_.Add(top_sites_.get());
228 } 229 }
229 230
230 SuggestionsService* suggestions_service = 231 suggestions_subscription_ = suggestions_service_->AddCallback(
231 SuggestionsServiceFactory::GetForProfile(profile_);
232 suggestions_subscription_ = suggestions_service->AddCallback(
233 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, 232 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable,
234 base::Unretained(this))); 233 base::Unretained(this)));
235 234
236 // Immediately get the current suggestions from the cache. If the cache is 235 // Immediately get the current suggestions from the cache. If the cache is
237 // empty, this will fall back to TopSites. 236 // empty, this will fall back to TopSites.
238 OnSuggestionsProfileAvailable( 237 OnSuggestionsProfileAvailable(
239 suggestions_service->GetSuggestionsDataFromCache()); 238 suggestions_service_->GetSuggestionsDataFromCache());
240 // Also start a request for fresh suggestions. 239 // Also start a request for fresh suggestions.
241 suggestions_service->FetchSuggestionsData(); 240 suggestions_service_->FetchSuggestionsData();
242 } 241 }
243 242
244 void MostVisitedSites::GetURLThumbnail( 243 void MostVisitedSites::GetURLThumbnail(
245 const GURL& url, 244 const GURL& url,
246 const ThumbnailCallback& callback) { 245 const ThumbnailCallback& callback) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI); 246 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
249 247
250 BrowserThread::PostTaskAndReplyWithResult( 248 BrowserThread::PostTaskAndReplyWithResult(
251 BrowserThread::DB, FROM_HERE, 249 BrowserThread::DB, FROM_HERE,
252 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), 250 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_),
253 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, 251 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
254 weak_ptr_factory_.GetWeakPtr(), url, callback)); 252 weak_ptr_factory_.GetWeakPtr(), url, callback));
255 } 253 }
256 254
257 void MostVisitedSites::OnLocalThumbnailFetched( 255 void MostVisitedSites::OnLocalThumbnailFetched(
258 const GURL& url, 256 const GURL& url,
259 const ThumbnailCallback& callback, 257 const ThumbnailCallback& callback,
260 std::unique_ptr<SkBitmap> bitmap) { 258 std::unique_ptr<SkBitmap> bitmap) {
261 DCHECK_CURRENTLY_ON(BrowserThread::UI); 259 DCHECK_CURRENTLY_ON(BrowserThread::UI);
262 if (!bitmap.get()) { 260 if (!bitmap.get()) {
263 // A thumbnail is not locally available for |url|. Make sure it is put in 261 // A thumbnail is not locally available for |url|. Make sure it is put in
264 // the list to be fetched at the next visit to this site. 262 // the list to be fetched at the next visit to this site.
265 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 263 if (top_sites_)
266 if (top_sites) 264 top_sites_->AddForcedURL(url, base::Time::Now());
267 top_sites->AddForcedURL(url, base::Time::Now());
268 // Also fetch a remote thumbnail if possible. PopularSites or the 265 // Also fetch a remote thumbnail if possible. PopularSites or the
269 // SuggestionsService can supply a thumbnail download URL. 266 // SuggestionsService can supply a thumbnail download URL.
270 SuggestionsService* suggestions_service =
271 SuggestionsServiceFactory::GetForProfile(profile_);
272 if (popular_sites_) { 267 if (popular_sites_) {
273 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); 268 const std::vector<PopularSites::Site>& sites = popular_sites_->sites();
274 auto it = std::find_if( 269 auto it = std::find_if(
275 sites.begin(), sites.end(), 270 sites.begin(), sites.end(),
276 [&url](const PopularSites::Site& site) { return site.url == url; }); 271 [&url](const PopularSites::Site& site) { return site.url == url; });
277 if (it != sites.end() && it->thumbnail_url.is_valid()) { 272 if (it != sites.end() && it->thumbnail_url.is_valid()) {
278 return suggestions_service->GetPageThumbnailWithURL( 273 return suggestions_service_->GetPageThumbnailWithURL(
279 url, it->thumbnail_url, 274 url, it->thumbnail_url,
280 base::Bind(&MostVisitedSites::OnObtainedThumbnail, 275 base::Bind(&MostVisitedSites::OnObtainedThumbnail,
281 weak_ptr_factory_.GetWeakPtr(), false, callback)); 276 weak_ptr_factory_.GetWeakPtr(), false, callback));
282 } 277 }
283 } 278 }
284 if (mv_source_ == SUGGESTIONS_SERVICE) { 279 if (mv_source_ == SUGGESTIONS_SERVICE) {
285 return suggestions_service->GetPageThumbnail( 280 return suggestions_service_->GetPageThumbnail(
286 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, 281 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail,
287 weak_ptr_factory_.GetWeakPtr(), false, callback)); 282 weak_ptr_factory_.GetWeakPtr(), false, callback));
288 } 283 }
289 } 284 }
290 OnObtainedThumbnail(true, callback, url, bitmap.get()); 285 OnObtainedThumbnail(true, callback, url, bitmap.get());
291 } 286 }
292 287
293 void MostVisitedSites::OnObtainedThumbnail( 288 void MostVisitedSites::OnObtainedThumbnail(
294 bool is_local_thumbnail, 289 bool is_local_thumbnail,
295 const ThumbnailCallback& callback, 290 const ThumbnailCallback& callback,
296 const GURL& url, 291 const GURL& url,
297 const SkBitmap* bitmap) { 292 const SkBitmap* bitmap) {
298 DCHECK_CURRENTLY_ON(BrowserThread::UI); 293 DCHECK_CURRENTLY_ON(BrowserThread::UI);
299 callback.Run(is_local_thumbnail, bitmap); 294 callback.Run(is_local_thumbnail, bitmap);
300 } 295 }
301 296
302 void MostVisitedSites::AddOrRemoveBlacklistedUrl( 297 void MostVisitedSites::AddOrRemoveBlacklistedUrl(
303 const GURL& url, bool add_url) { 298 const GURL& url, bool add_url) {
304 // Always blacklist in the local TopSites. 299 // Always blacklist in the local TopSites.
305 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); 300 if (top_sites_) {
306 if (top_sites) {
307 if (add_url) 301 if (add_url)
308 top_sites->AddBlacklistedURL(url); 302 top_sites_->AddBlacklistedURL(url);
309 else 303 else
310 top_sites->RemoveBlacklistedURL(url); 304 top_sites_->RemoveBlacklistedURL(url);
311 } 305 }
312 306
313 // Only blacklist in the server-side suggestions service if it's active. 307 // Only blacklist in the server-side suggestions service if it's active.
314 if (mv_source_ == SUGGESTIONS_SERVICE) { 308 if (mv_source_ == SUGGESTIONS_SERVICE) {
315 SuggestionsService* suggestions_service =
316 SuggestionsServiceFactory::GetForProfile(profile_);
317 if (add_url) 309 if (add_url)
318 suggestions_service->BlacklistURL(url); 310 suggestions_service_->BlacklistURL(url);
319 else 311 else
320 suggestions_service->UndoBlacklistURL(url); 312 suggestions_service_->UndoBlacklistURL(url);
321 } 313 }
322 } 314 }
323 315
324 void MostVisitedSites::RecordTileTypeMetrics( 316 void MostVisitedSites::RecordTileTypeMetrics(
325 const std::vector<int>& tile_types) { 317 const std::vector<int>& tile_types) {
326 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); 318 DCHECK_EQ(current_suggestions_.size(), tile_types.size());
327 int counts_per_type[NUM_TILE_TYPES] = {0}; 319 int counts_per_type[NUM_TILE_TYPES] = {0};
328 for (size_t i = 0; i < tile_types.size(); ++i) { 320 for (size_t i = 0; i < tile_types.size(); ++i) {
329 int tile_type = tile_types[i]; 321 int tile_type = tile_types[i];
330 ++counts_per_type[tile_type]; 322 ++counts_per_type[tile_type];
(...skipping 30 matching lines...) Expand all
361 } 353 }
362 354
363 // static 355 // static
364 void MostVisitedSites::RegisterProfilePrefs( 356 void MostVisitedSites::RegisterProfilePrefs(
365 user_prefs::PrefRegistrySyncable* registry) { 357 user_prefs::PrefRegistrySyncable* registry) {
366 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL); 358 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL);
367 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); 359 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal);
368 } 360 }
369 361
370 void MostVisitedSites::QueryMostVisitedURLs() { 362 void MostVisitedSites::QueryMostVisitedURLs() {
371 SuggestionsService* suggestions_service = 363 if (suggestions_service_->FetchSuggestionsData()) {
372 SuggestionsServiceFactory::GetForProfile(profile_);
373 if (suggestions_service->FetchSuggestionsData()) {
374 // A suggestions network request is on its way. We'll be called back via 364 // A suggestions network request is on its way. We'll be called back via
375 // OnSuggestionsProfileAvailable. 365 // OnSuggestionsProfileAvailable.
376 return; 366 return;
377 } 367 }
378 // If no network request could be sent, try to get suggestions from the 368 // If no network request could be sent, try to get suggestions from the
379 // cache. If that also returns nothing, OnSuggestionsProfileAvailable will 369 // cache. If that also returns nothing, OnSuggestionsProfileAvailable will
380 // call InitiateTopSitesQuery. 370 // call InitiateTopSitesQuery.
381 OnSuggestionsProfileAvailable( 371 OnSuggestionsProfileAvailable(
382 suggestions_service->GetSuggestionsDataFromCache()); 372 suggestions_service_->GetSuggestionsDataFromCache());
383 } 373 }
384 374
385 void MostVisitedSites::InitiateTopSitesQuery() { 375 void MostVisitedSites::InitiateTopSitesQuery() {
386 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); 376 if (!top_sites_)
387 if (!top_sites)
388 return; 377 return;
389 378
390 top_sites->GetMostVisitedURLs( 379 top_sites_->GetMostVisitedURLs(
391 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, 380 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable,
392 weak_ptr_factory_.GetWeakPtr()), 381 weak_ptr_factory_.GetWeakPtr()),
393 false); 382 false);
394 } 383 }
395 384
396 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { 385 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) {
397 SupervisedUserService* supervised_user_service = 386 SupervisedUserService* supervised_user_service =
398 SupervisedUserServiceFactory::GetForProfile(profile_); 387 SupervisedUserServiceFactory::GetForProfile(profile_);
399 388
400 for (const auto& whitelist : supervised_user_service->whitelists()) { 389 for (const auto& whitelist : supervised_user_service->whitelists()) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; 478 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions;
490 479
491 SupervisedUserService* supervised_user_service = 480 SupervisedUserService* supervised_user_service =
492 SupervisedUserServiceFactory::GetForProfile(profile_); 481 SupervisedUserServiceFactory::GetForProfile(profile_);
493 SupervisedUserURLFilter* url_filter = 482 SupervisedUserURLFilter* url_filter =
494 supervised_user_service->GetURLFilterForUIThread(); 483 supervised_user_service->GetURLFilterForUIThread();
495 484
496 std::set<std::string> personal_hosts; 485 std::set<std::string> personal_hosts;
497 for (const auto& suggestion : personal_suggestions) 486 for (const auto& suggestion : personal_suggestions)
498 personal_hosts.insert(suggestion->url.host()); 487 personal_hosts.insert(suggestion->url.host());
499 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
500 488
501 for (const auto& whitelist : supervised_user_service->whitelists()) { 489 for (const auto& whitelist : supervised_user_service->whitelists()) {
502 // Skip blacklisted sites. 490 // Skip blacklisted sites.
503 if (top_sites && top_sites->IsBlacklisted(whitelist->entry_point())) 491 if (top_sites_ && top_sites_->IsBlacklisted(whitelist->entry_point()))
504 continue; 492 continue;
505 493
506 // Skip suggestions already present. 494 // Skip suggestions already present.
507 if (personal_hosts.find(whitelist->entry_point().host()) != 495 if (personal_hosts.find(whitelist->entry_point().host()) !=
508 personal_hosts.end()) 496 personal_hosts.end())
509 continue; 497 continue;
510 498
511 // Skip whitelist entry points that are manually blocked. 499 // Skip whitelist entry points that are manually blocked.
512 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == 500 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) ==
513 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { 501 SupervisedUserURLFilter::FilteringBehavior::BLOCK) {
(...skipping 30 matching lines...) Expand all
544 // in the personal suggestions. 532 // in the personal suggestions.
545 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; 533 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions;
546 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions; 534 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions;
547 535
548 if (num_popular_sites_suggestions > 0 && popular_sites_) { 536 if (num_popular_sites_suggestions > 0 && popular_sites_) {
549 std::set<std::string> hosts; 537 std::set<std::string> hosts;
550 for (const auto& suggestion : personal_suggestions) 538 for (const auto& suggestion : personal_suggestions)
551 hosts.insert(suggestion->url.host()); 539 hosts.insert(suggestion->url.host());
552 for (const auto& suggestion : whitelist_suggestions) 540 for (const auto& suggestion : whitelist_suggestions)
553 hosts.insert(suggestion->url.host()); 541 hosts.insert(suggestion->url.host());
554 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
555 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 542 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
556 // Skip blacklisted sites. 543 // Skip blacklisted sites.
557 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) 544 if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url))
558 continue; 545 continue;
559 std::string host = popular_site.url.host(); 546 std::string host = popular_site.url.host();
560 // Skip suggestions already present in personal or whitelists. 547 // Skip suggestions already present in personal or whitelists.
561 if (hosts.find(host) != hosts.end()) 548 if (hosts.find(host) != hosts.end())
562 continue; 549 continue;
563 550
564 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 551 std::unique_ptr<Suggestion> suggestion(new Suggestion());
565 suggestion->title = popular_site.title; 552 suggestion->title = popular_site.title;
566 suggestion->url = GURL(popular_site.url); 553 suggestion->url = GURL(popular_site.url);
567 suggestion->source = POPULAR; 554 suggestion->source = POPULAR;
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 777
791 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 778 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
792 779
793 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 780 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
794 ChangeReason change_reason) { 781 ChangeReason change_reason) {
795 if (mv_source_ == TOP_SITES) { 782 if (mv_source_ == TOP_SITES) {
796 // The displayed suggestions are invalidated. 783 // The displayed suggestions are invalidated.
797 InitiateTopSitesQuery(); 784 InitiateTopSitesQuery();
798 } 785 }
799 } 786 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/most_visited_sites.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698