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

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