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

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

Issue 1972923002: MostVisitedSites: Remove unused order-persisting code (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
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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 SupervisedUserServiceFactory::GetForProfile(profile_); 193 SupervisedUserServiceFactory::GetForProfile(profile_);
194 supervised_user_service->AddObserver(this); 194 supervised_user_service->AddObserver(this);
195 } 195 }
196 196
197 MostVisitedSites::~MostVisitedSites() { 197 MostVisitedSites::~MostVisitedSites() {
198 SupervisedUserService* supervised_user_service = 198 SupervisedUserService* supervised_user_service =
199 SupervisedUserServiceFactory::GetForProfile(profile_); 199 SupervisedUserServiceFactory::GetForProfile(profile_);
200 supervised_user_service->RemoveObserver(this); 200 supervised_user_service->RemoveObserver(this);
201 } 201 }
202 202
203 void MostVisitedSites::SetMostVisitedURLsObserver( 203 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
204 MostVisitedSites::Observer* observer, int num_sites) { 204 int num_sites) {
205 DCHECK(observer); 205 DCHECK(observer);
206 observer_ = observer; 206 observer_ = observer;
207 num_sites_ = num_sites; 207 num_sites_ = num_sites;
208 208
209 if (ShouldShowPopularSites() && 209 if (ShouldShowPopularSites() &&
210 NeedPopularSites(prefs_, num_sites_)) { 210 NeedPopularSites(prefs_, num_sites_)) {
211 popular_sites_.reset(new PopularSites( 211 popular_sites_.reset(new PopularSites(
212 prefs_, 212 prefs_,
213 template_url_service_, 213 template_url_service_,
214 variations_service_, 214 variations_service_,
215 download_context_, 215 download_context_,
216 GetPopularSitesCountry(), 216 GetPopularSitesCountry(),
217 GetPopularSitesVersion(), 217 GetPopularSitesVersion(),
218 false, 218 false,
219 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, 219 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
220 base::Unretained(this)))); 220 base::Unretained(this))));
221 } else { 221 } else {
222 received_popular_sites_ = true; 222 received_popular_sites_ = true;
223 } 223 }
224 224
225 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks.
225 if (top_sites_) { 226 if (top_sites_) {
226 // TopSites updates itself after a delay. To ensure up-to-date results, 227 // TopSites updates itself after a delay. To ensure up-to-date results,
227 // force an update now. 228 // force an update now.
228 top_sites_->SyncWithHistory(); 229 top_sites_->SyncWithHistory();
229 230
230 // Register as TopSitesObserver so that we can update ourselves when the 231 // Register as TopSitesObserver so that we can update ourselves when the
231 // TopSites changes. 232 // TopSites changes.
232 scoped_observer_.Add(top_sites_.get()); 233 scoped_observer_.Add(top_sites_.get());
233 } 234 }
234 235
235 suggestions_subscription_ = suggestions_service_->AddCallback( 236 suggestions_subscription_ = suggestions_service_->AddCallback(
236 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, 237 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable,
237 base::Unretained(this))); 238 base::Unretained(this)));
238 239
239 // Immediately build the current suggestions, getting personal suggestions 240 // Immediately build the current suggestions, getting personal suggestions
240 // from the SuggestionsService's cache or, if that is empty, from TopSites. 241 // from the SuggestionsService's cache or, if that is empty, from TopSites.
241 BuildCurrentSuggestions(); 242 BuildCurrentSuggestions();
242 // Also start a request for fresh suggestions. 243 // Also start a request for fresh suggestions.
243 suggestions_service_->FetchSuggestionsData(); 244 suggestions_service_->FetchSuggestionsData();
244 } 245 }
245 246
246 void MostVisitedSites::GetURLThumbnail( 247 void MostVisitedSites::GetURLThumbnail(const GURL& url,
247 const GURL& url, 248 const ThumbnailCallback& callback) {
248 const ThumbnailCallback& callback) {
249 DCHECK_CURRENTLY_ON(BrowserThread::UI); 249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
250 250
251 // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB.
251 BrowserThread::PostTaskAndReplyWithResult( 252 BrowserThread::PostTaskAndReplyWithResult(
252 BrowserThread::DB, FROM_HERE, 253 BrowserThread::DB, FROM_HERE,
253 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_), 254 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_),
254 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, 255 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
255 weak_ptr_factory_.GetWeakPtr(), url, callback)); 256 weak_ptr_factory_.GetWeakPtr(), url, callback));
256 } 257 }
257 258
258 void MostVisitedSites::OnLocalThumbnailFetched( 259 void MostVisitedSites::OnLocalThumbnailFetched(
259 const GURL& url, 260 const GURL& url,
260 const ThumbnailCallback& callback, 261 const ThumbnailCallback& callback,
(...skipping 20 matching lines...) Expand all
281 } 282 }
282 if (mv_source_ == SUGGESTIONS_SERVICE) { 283 if (mv_source_ == SUGGESTIONS_SERVICE) {
283 return suggestions_service_->GetPageThumbnail( 284 return suggestions_service_->GetPageThumbnail(
284 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, 285 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail,
285 weak_ptr_factory_.GetWeakPtr(), false, callback)); 286 weak_ptr_factory_.GetWeakPtr(), false, callback));
286 } 287 }
287 } 288 }
288 OnObtainedThumbnail(true, callback, url, bitmap.get()); 289 OnObtainedThumbnail(true, callback, url, bitmap.get());
289 } 290 }
290 291
291 void MostVisitedSites::OnObtainedThumbnail( 292 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail,
292 bool is_local_thumbnail, 293 const ThumbnailCallback& callback,
293 const ThumbnailCallback& callback, 294 const GURL& url,
294 const GURL& url, 295 const SkBitmap* bitmap) {
295 const SkBitmap* bitmap) {
296 DCHECK_CURRENTLY_ON(BrowserThread::UI); 296 DCHECK_CURRENTLY_ON(BrowserThread::UI);
297 callback.Run(is_local_thumbnail, bitmap); 297 callback.Run(is_local_thumbnail, bitmap);
298 } 298 }
299 299
300 void MostVisitedSites::AddOrRemoveBlacklistedUrl( 300 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url,
301 const GURL& url, bool add_url) { 301 bool add_url) {
302 // Always blacklist in the local TopSites. 302 // Always blacklist in the local TopSites.
303 if (top_sites_) { 303 if (top_sites_) {
304 if (add_url) 304 if (add_url)
305 top_sites_->AddBlacklistedURL(url); 305 top_sites_->AddBlacklistedURL(url);
306 else 306 else
307 top_sites_->RemoveBlacklistedURL(url); 307 top_sites_->RemoveBlacklistedURL(url);
308 } 308 }
309 309
310 // Only blacklist in the server-side suggestions service if it's active. 310 // Only blacklist in the server-side suggestions service if it's active.
311 if (mv_source_ == SUGGESTIONS_SERVICE) { 311 if (mv_source_ == SUGGESTIONS_SERVICE) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); 351 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES);
352 } 352 }
353 353
354 void MostVisitedSites::OnURLFilterChanged() { 354 void MostVisitedSites::OnURLFilterChanged() {
355 BuildCurrentSuggestions(); 355 BuildCurrentSuggestions();
356 } 356 }
357 357
358 // static 358 // static
359 void MostVisitedSites::RegisterProfilePrefs( 359 void MostVisitedSites::RegisterProfilePrefs(
360 user_prefs::PrefRegistrySyncable* registry) { 360 user_prefs::PrefRegistrySyncable* registry) {
361 // TODO(treib): Remove both of these.
sfiera 2016/05/12 14:24:05 What prevents us from doing that now?
Marc Treib 2016/05/12 14:30:33 The second one is still used, for determining if w
sfiera 2016/05/12 14:44:36 OK. It would be nice to have more of that detail i
Marc Treib 2016/05/12 16:51:07 Done.
361 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL); 362 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL);
362 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); 363 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal);
363 } 364 }
364 365
365 void MostVisitedSites::BuildCurrentSuggestions() { 366 void MostVisitedSites::BuildCurrentSuggestions() {
366 // Get the current suggestions from cache. If the cache is empty, this will 367 // Get the current suggestions from cache. If the cache is empty, this will
367 // fall back to TopSites. 368 // fall back to TopSites.
368 OnSuggestionsProfileAvailable( 369 OnSuggestionsProfileAvailable(
369 suggestions_service_->GetSuggestionsDataFromCache()); 370 suggestions_service_->GetSuggestionsDataFromCache());
370 } 371 }
(...skipping 18 matching lines...) Expand all
389 } 390 }
390 return base::FilePath(); 391 return base::FilePath();
391 } 392 }
392 393
393 void MostVisitedSites::OnMostVisitedURLsAvailable( 394 void MostVisitedSites::OnMostVisitedURLsAvailable(
394 const history::MostVisitedURLList& visited_list) { 395 const history::MostVisitedURLList& visited_list) {
395 SupervisedUserURLFilter* url_filter = 396 SupervisedUserURLFilter* url_filter =
396 SupervisedUserServiceFactory::GetForProfile(profile_) 397 SupervisedUserServiceFactory::GetForProfile(profile_)
397 ->GetURLFilterForUIThread(); 398 ->GetURLFilterForUIThread();
398 399
399 MostVisitedSites::SuggestionsPtrVector suggestions; 400 SuggestionsPtrVector suggestions;
400 size_t num_tiles = 401 size_t num_tiles =
401 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); 402 std::min(visited_list.size(), static_cast<size_t>(num_sites_));
402 for (size_t i = 0; i < num_tiles; ++i) { 403 for (size_t i = 0; i < num_tiles; ++i) {
403 const history::MostVisitedURL& visited = visited_list[i]; 404 const history::MostVisitedURL& visited = visited_list[i];
404 if (visited.url.is_empty()) { 405 if (visited.url.is_empty()) {
405 num_tiles = i; 406 num_tiles = i;
406 break; // This is the signal that there are no more real visited sites. 407 break; // This is the signal that there are no more real visited sites.
407 } 408 }
408 if (url_filter->GetFilteringBehaviorForURL(visited.url) == 409 if (url_filter->GetFilteringBehaviorForURL(visited.url) ==
409 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { 410 SupervisedUserURLFilter::FilteringBehavior::BLOCK) {
410 continue; 411 continue;
411 } 412 }
412 413
413 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 414 std::unique_ptr<Suggestion> suggestion(new Suggestion());
414 suggestion->title = visited.title; 415 suggestion->title = visited.title;
415 suggestion->url = visited.url; 416 suggestion->url = visited.url;
416 suggestion->source = TOP_SITES; 417 suggestion->source = TOP_SITES;
417 suggestion->whitelist_icon_path = GetWhitelistLargeIconPath(visited.url); 418 suggestion->whitelist_icon_path = GetWhitelistLargeIconPath(visited.url);
418 419
419 suggestions.push_back(std::move(suggestion)); 420 suggestions.push_back(std::move(suggestion));
420 } 421 }
421 422
422 received_most_visited_sites_ = true; 423 received_most_visited_sites_ = true;
423 mv_source_ = TOP_SITES; 424 mv_source_ = TOP_SITES;
424 SaveNewNTPSuggestions(&suggestions); 425 SaveNewSuggestions(&suggestions);
425 NotifyMostVisitedURLsObserver(); 426 NotifyMostVisitedURLsObserver();
426 } 427 }
427 428
428 void MostVisitedSites::OnSuggestionsProfileAvailable( 429 void MostVisitedSites::OnSuggestionsProfileAvailable(
429 const SuggestionsProfile& suggestions_profile) { 430 const SuggestionsProfile& suggestions_profile) {
430 int num_tiles = suggestions_profile.suggestions_size(); 431 int num_tiles = suggestions_profile.suggestions_size();
431 // With no server suggestions, fall back to local TopSites. 432 // With no server suggestions, fall back to local TopSites.
432 if (num_tiles == 0) { 433 if (num_tiles == 0) {
433 InitiateTopSitesQuery(); 434 InitiateTopSitesQuery();
434 return; 435 return;
435 } 436 }
436 if (num_sites_ < num_tiles) 437 if (num_sites_ < num_tiles)
437 num_tiles = num_sites_; 438 num_tiles = num_sites_;
438 439
439 SupervisedUserURLFilter* url_filter = 440 SupervisedUserURLFilter* url_filter =
440 SupervisedUserServiceFactory::GetForProfile(profile_) 441 SupervisedUserServiceFactory::GetForProfile(profile_)
441 ->GetURLFilterForUIThread(); 442 ->GetURLFilterForUIThread();
442 MostVisitedSites::SuggestionsPtrVector suggestions; 443 SuggestionsPtrVector suggestions;
443 for (int i = 0; i < num_tiles; ++i) { 444 for (int i = 0; i < num_tiles; ++i) {
444 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); 445 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
445 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == 446 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) ==
446 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { 447 SupervisedUserURLFilter::FilteringBehavior::BLOCK) {
447 continue; 448 continue;
448 } 449 }
449 450
450 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); 451 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion());
451 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); 452 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title());
452 generated_suggestion->url = GURL(suggestion.url()); 453 generated_suggestion->url = GURL(suggestion.url());
453 generated_suggestion->source = SUGGESTIONS_SERVICE; 454 generated_suggestion->source = SUGGESTIONS_SERVICE;
454 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath( 455 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath(
455 GURL(suggestion.url())); 456 GURL(suggestion.url()));
456 if (suggestion.providers_size() > 0) 457 if (suggestion.providers_size() > 0)
457 generated_suggestion->provider_index = suggestion.providers(0); 458 generated_suggestion->provider_index = suggestion.providers(0);
458 459
459 suggestions.push_back(std::move(generated_suggestion)); 460 suggestions.push_back(std::move(generated_suggestion));
460 } 461 }
461 462
462 received_most_visited_sites_ = true; 463 received_most_visited_sites_ = true;
463 mv_source_ = SUGGESTIONS_SERVICE; 464 mv_source_ = SUGGESTIONS_SERVICE;
464 SaveNewNTPSuggestions(&suggestions); 465 SaveNewSuggestions(&suggestions);
465 NotifyMostVisitedURLsObserver(); 466 NotifyMostVisitedURLsObserver();
466 } 467 }
467 468
468 MostVisitedSites::SuggestionsPtrVector 469 MostVisitedSites::SuggestionsPtrVector
469 MostVisitedSites::CreateWhitelistEntryPointSuggestions( 470 MostVisitedSites::CreateWhitelistEntryPointSuggestions(
470 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions) { 471 const SuggestionsPtrVector& personal_suggestions) {
471 size_t num_personal_suggestions = personal_suggestions.size(); 472 size_t num_personal_suggestions = personal_suggestions.size();
472 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); 473 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_));
473 474
474 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; 475 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions;
475 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; 476 SuggestionsPtrVector whitelist_suggestions;
476 477
477 SupervisedUserService* supervised_user_service = 478 SupervisedUserService* supervised_user_service =
478 SupervisedUserServiceFactory::GetForProfile(profile_); 479 SupervisedUserServiceFactory::GetForProfile(profile_);
479 SupervisedUserURLFilter* url_filter = 480 SupervisedUserURLFilter* url_filter =
480 supervised_user_service->GetURLFilterForUIThread(); 481 supervised_user_service->GetURLFilterForUIThread();
481 482
482 std::set<std::string> personal_hosts; 483 std::set<std::string> personal_hosts;
483 for (const auto& suggestion : personal_suggestions) 484 for (const auto& suggestion : personal_suggestions)
484 personal_hosts.insert(suggestion->url.host()); 485 personal_hosts.insert(suggestion->url.host());
485 486
(...skipping 22 matching lines...) Expand all
508 whitelist_suggestions.push_back(std::move(suggestion)); 509 whitelist_suggestions.push_back(std::move(suggestion));
509 if (whitelist_suggestions.size() >= num_whitelist_suggestions) 510 if (whitelist_suggestions.size() >= num_whitelist_suggestions)
510 break; 511 break;
511 } 512 }
512 513
513 return whitelist_suggestions; 514 return whitelist_suggestions;
514 } 515 }
515 516
516 MostVisitedSites::SuggestionsPtrVector 517 MostVisitedSites::SuggestionsPtrVector
517 MostVisitedSites::CreatePopularSitesSuggestions( 518 MostVisitedSites::CreatePopularSitesSuggestions(
518 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions, 519 const SuggestionsPtrVector& personal_suggestions,
519 const MostVisitedSites::SuggestionsPtrVector& whitelist_suggestions) { 520 const SuggestionsPtrVector& whitelist_suggestions) {
520 // For child accounts popular sites suggestions will not be added. 521 // For child accounts popular sites suggestions will not be added.
521 if (is_child_profile_) 522 if (is_child_profile_)
522 return MostVisitedSites::SuggestionsPtrVector(); 523 return SuggestionsPtrVector();
523 524
524 size_t num_suggestions = 525 size_t num_suggestions =
525 personal_suggestions.size() + whitelist_suggestions.size(); 526 personal_suggestions.size() + whitelist_suggestions.size();
526 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); 527 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_));
527 528
528 // Collect non-blacklisted popular suggestions, skipping those already present 529 // Collect non-blacklisted popular suggestions, skipping those already present
529 // in the personal suggestions. 530 // in the personal suggestions.
530 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; 531 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions;
531 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions; 532 SuggestionsPtrVector popular_sites_suggestions;
532 533
533 if (num_popular_sites_suggestions > 0 && popular_sites_) { 534 if (num_popular_sites_suggestions > 0 && popular_sites_) {
534 std::set<std::string> hosts; 535 std::set<std::string> hosts;
535 for (const auto& suggestion : personal_suggestions) 536 for (const auto& suggestion : personal_suggestions)
536 hosts.insert(suggestion->url.host()); 537 hosts.insert(suggestion->url.host());
537 for (const auto& suggestion : whitelist_suggestions) 538 for (const auto& suggestion : whitelist_suggestions)
538 hosts.insert(suggestion->url.host()); 539 hosts.insert(suggestion->url.host());
539 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 540 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
540 // Skip blacklisted sites. 541 // Skip blacklisted sites.
541 if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url)) 542 if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url))
542 continue; 543 continue;
543 std::string host = popular_site.url.host(); 544 std::string host = popular_site.url.host();
544 // Skip suggestions already present in personal or whitelists. 545 // Skip suggestions already present in personal or whitelists.
545 if (hosts.find(host) != hosts.end()) 546 if (hosts.find(host) != hosts.end())
546 continue; 547 continue;
547 548
548 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 549 std::unique_ptr<Suggestion> suggestion(new Suggestion());
549 suggestion->title = popular_site.title; 550 suggestion->title = popular_site.title;
550 suggestion->url = GURL(popular_site.url); 551 suggestion->url = GURL(popular_site.url);
551 suggestion->source = POPULAR; 552 suggestion->source = POPULAR;
552 553
553 popular_sites_suggestions.push_back(std::move(suggestion)); 554 popular_sites_suggestions.push_back(std::move(suggestion));
554 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) 555 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions)
555 break; 556 break;
556 } 557 }
557 } 558 }
558 return popular_sites_suggestions; 559 return popular_sites_suggestions;
559 } 560 }
560 561
561 void MostVisitedSites::SaveNewNTPSuggestions( 562 void MostVisitedSites::SaveNewSuggestions(
562 MostVisitedSites::SuggestionsPtrVector* personal_suggestions) { 563 SuggestionsPtrVector* personal_suggestions) {
563 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions = 564 SuggestionsPtrVector whitelist_suggestions =
564 CreateWhitelistEntryPointSuggestions(*personal_suggestions); 565 CreateWhitelistEntryPointSuggestions(*personal_suggestions);
565 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions = 566 SuggestionsPtrVector popular_sites_suggestions =
566 CreatePopularSitesSuggestions(*personal_suggestions, 567 CreatePopularSitesSuggestions(*personal_suggestions,
567 whitelist_suggestions); 568 whitelist_suggestions);
569
568 size_t num_actual_tiles = personal_suggestions->size() + 570 size_t num_actual_tiles = personal_suggestions->size() +
569 whitelist_suggestions.size() + 571 whitelist_suggestions.size() +
570 popular_sites_suggestions.size(); 572 popular_sites_suggestions.size();
571 std::vector<std::string> old_sites_url; 573 DCHECK_LE(num_actual_tiles, static_cast<size_t>(num_sites_));
572 std::vector<bool> old_sites_is_personal; 574
573 // TODO(treib): We used to call |GetPreviousNTPSites| here to populate 575 SuggestionsPtrVector merged_suggestions = MergeSuggestions(
sfiera 2016/05/12 14:24:05 Creating three vectors and them merging them into
Marc Treib 2016/05/12 14:30:33 The reason I left it like this is that we have som
sfiera 2016/05/12 14:44:36 Removing testing in this directory is a sure way t
Marc Treib 2016/05/12 16:51:07 Alright. Well, if the MergeSuggestions tests stay,
574 // |old_sites_url| and |old_sites_is_personal|, but that caused problems 576 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions);
575 // (crbug.com/585391). Either figure out a way to fix them and re-enable,
576 // or properly remove the order-persisting code. crbug.com/601734
577 MostVisitedSites::SuggestionsPtrVector merged_suggestions = MergeSuggestions(
578 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions,
579 old_sites_url, old_sites_is_personal);
580 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); 577 DCHECK_EQ(num_actual_tiles, merged_suggestions.size());
578
581 current_suggestions_.resize(merged_suggestions.size()); 579 current_suggestions_.resize(merged_suggestions.size());
582 for (size_t i = 0; i < merged_suggestions.size(); ++i) 580 for (size_t i = 0; i < merged_suggestions.size(); ++i)
583 std::swap(*merged_suggestions[i], current_suggestions_[i]); 581 std::swap(*merged_suggestions[i], current_suggestions_[i]);
582
584 if (received_popular_sites_) 583 if (received_popular_sites_)
585 SaveCurrentNTPSites(); 584 SaveCurrentSuggestionsToPrefs();
586 } 585 }
587 586
588 // static 587 // static
589 MostVisitedSites::SuggestionsPtrVector MostVisitedSites::MergeSuggestions( 588 MostVisitedSites::SuggestionsPtrVector MostVisitedSites::MergeSuggestions(
590 MostVisitedSites::SuggestionsPtrVector* personal_suggestions, 589 SuggestionsPtrVector* personal_suggestions,
591 MostVisitedSites::SuggestionsPtrVector* whitelist_suggestions, 590 SuggestionsPtrVector* whitelist_suggestions,
592 MostVisitedSites::SuggestionsPtrVector* popular_suggestions, 591 SuggestionsPtrVector* popular_suggestions) {
593 const std::vector<std::string>& old_sites_url,
594 const std::vector<bool>& old_sites_is_personal) {
595 size_t num_personal_suggestions = personal_suggestions->size(); 592 size_t num_personal_suggestions = personal_suggestions->size();
596 size_t num_whitelist_suggestions = whitelist_suggestions->size(); 593 size_t num_whitelist_suggestions = whitelist_suggestions->size();
597 size_t num_popular_suggestions = popular_suggestions->size(); 594 size_t num_popular_suggestions = popular_suggestions->size();
598 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions + 595 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions +
599 num_personal_suggestions; 596 num_personal_suggestions;
600 MostVisitedSites::SuggestionsPtrVector merged_suggestions; 597 SuggestionsPtrVector merged_suggestions;
601 merged_suggestions.resize(num_tiles); 598 AppendSuggestions(personal_suggestions, &merged_suggestions);
599 AppendSuggestions(whitelist_suggestions, &merged_suggestions);
600 AppendSuggestions(popular_suggestions, &merged_suggestions);
601 DCHECK_EQ(num_tiles, merged_suggestions.size());
602 602
603 size_t num_old_tiles = old_sites_url.size();
604 DCHECK_LE(num_old_tiles, num_tiles);
605 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size());
606 std::vector<std::string> old_sites_host;
607 old_sites_host.reserve(num_old_tiles);
608 // Only populate the hosts for popular suggestions as only they can be
609 // replaced by host. Personal suggestions require an exact url match to be
610 // replaced.
611 for (size_t i = 0; i < num_old_tiles; ++i) {
612 old_sites_host.push_back(old_sites_is_personal[i]
613 ? std::string()
614 : GURL(old_sites_url[i]).host());
615 }
616
617 // Insert personal suggestions if they existed previously.
618 std::vector<size_t> new_personal_suggestions = InsertMatchingSuggestions(
619 personal_suggestions, &merged_suggestions, old_sites_url, old_sites_host);
620 // Insert whitelist suggestions if they existed previously.
621 std::vector<size_t> new_whitelist_suggestions =
622 InsertMatchingSuggestions(whitelist_suggestions, &merged_suggestions,
623 old_sites_url, old_sites_host);
624 // Insert popular suggestions if they existed previously.
625 std::vector<size_t> new_popular_suggestions = InsertMatchingSuggestions(
626 popular_suggestions, &merged_suggestions, old_sites_url, old_sites_host);
627 // Insert leftover personal suggestions.
628 size_t filled_so_far = InsertAllSuggestions(
629 0, new_personal_suggestions, personal_suggestions, &merged_suggestions);
630 // Insert leftover whitelist suggestions.
631 filled_so_far =
632 InsertAllSuggestions(filled_so_far, new_whitelist_suggestions,
633 whitelist_suggestions, &merged_suggestions);
634 // Insert leftover popular suggestions.
635 InsertAllSuggestions(filled_so_far, new_popular_suggestions,
636 popular_suggestions, &merged_suggestions);
637 return merged_suggestions; 603 return merged_suggestions;
638 } 604 }
639 605
640 void MostVisitedSites::GetPreviousNTPSites( 606 // static
641 size_t num_tiles, 607 void MostVisitedSites::AppendSuggestions(SuggestionsPtrVector* src,
642 std::vector<std::string>* old_sites_url, 608 SuggestionsPtrVector* dst) {
643 std::vector<bool>* old_sites_is_personal) const { 609 dst->insert(dst->end(),
644 const base::ListValue* url_list = prefs_->GetList( 610 std::make_move_iterator(src->begin()),
645 ntp_tiles::prefs::kNTPSuggestionsURL); 611 std::make_move_iterator(src->end()));
646 const base::ListValue* source_list =
647 prefs_->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal);
648 DCHECK_EQ(url_list->GetSize(), source_list->GetSize());
649 if (url_list->GetSize() < num_tiles)
650 num_tiles = url_list->GetSize();
651 if (num_tiles == 0) {
652 // No fallback required as Personal suggestions take precedence anyway.
653 return;
654 }
655 old_sites_url->reserve(num_tiles);
656 old_sites_is_personal->reserve(num_tiles);
657 for (size_t i = 0; i < num_tiles; ++i) {
658 std::string url_string;
659 bool success = url_list->GetString(i, &url_string);
660 DCHECK(success);
661 old_sites_url->push_back(url_string);
662 bool is_personal;
663 success = source_list->GetBoolean(i, &is_personal);
664 DCHECK(success);
665 old_sites_is_personal->push_back(is_personal);
666 }
667 } 612 }
668 613
669 void MostVisitedSites::SaveCurrentNTPSites() { 614 void MostVisitedSites::SaveCurrentSuggestionsToPrefs() {
670 base::ListValue url_list; 615 base::ListValue url_list;
671 base::ListValue source_list; 616 base::ListValue source_list;
672 for (const auto& suggestion : current_suggestions_) { 617 for (const auto& suggestion : current_suggestions_) {
673 url_list.AppendString(suggestion.url.spec()); 618 url_list.AppendString(suggestion.url.spec());
674 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); 619 source_list.AppendBoolean(suggestion.source != POPULAR);
675 } 620 }
676 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list); 621 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list);
677 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list); 622 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list);
678 } 623 }
679 624
680 // static
681 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions(
682 MostVisitedSites::SuggestionsPtrVector* src_suggestions,
683 MostVisitedSites::SuggestionsPtrVector* dst_suggestions,
684 const std::vector<std::string>& match_urls,
685 const std::vector<std::string>& match_hosts) {
686 std::vector<size_t> unmatched_suggestions;
687 size_t num_src_suggestions = src_suggestions->size();
688 size_t num_matchers = match_urls.size();
689 for (size_t i = 0; i < num_src_suggestions; ++i) {
690 size_t position;
691 for (position = 0; position < num_matchers; ++position) {
692 if ((*dst_suggestions)[position] != nullptr)
693 continue;
694 if (match_urls[position] == (*src_suggestions)[i]->url.spec())
695 break;
696 // match_hosts is only populated for suggestions which can be replaced by
697 // host matching like popular suggestions.
698 if (match_hosts[position] == (*src_suggestions)[i]->url.host())
699 break;
700 }
701 if (position == num_matchers) {
702 unmatched_suggestions.push_back(i);
703 } else {
704 // A move is required as the source and destination containers own the
705 // elements.
706 std::swap((*dst_suggestions)[position], (*src_suggestions)[i]);
707 }
708 }
709 return unmatched_suggestions;
710 }
711
712 // static
713 size_t MostVisitedSites::InsertAllSuggestions(
714 size_t start_position,
715 const std::vector<size_t>& insert_positions,
716 std::vector<std::unique_ptr<Suggestion>>* src_suggestions,
717 std::vector<std::unique_ptr<Suggestion>>* dst_suggestions) {
718 size_t num_inserts = insert_positions.size();
719 size_t num_dests = dst_suggestions->size();
720
721 size_t src_pos = 0;
722 size_t i = start_position;
723 for (; i < num_dests && src_pos < num_inserts; ++i) {
724 if ((*dst_suggestions)[i] != nullptr)
725 continue;
726 size_t src = insert_positions[src_pos++];
727 std::swap((*dst_suggestions)[i], (*src_suggestions)[src]);
728 }
729 // Return destination positions filled so far which becomes the start_position
730 // for future runs.
731 return i;
732 }
733
734 void MostVisitedSites::NotifyMostVisitedURLsObserver() { 625 void MostVisitedSites::NotifyMostVisitedURLsObserver() {
735 size_t num_suggestions = current_suggestions_.size();
736 if (received_most_visited_sites_ && received_popular_sites_ && 626 if (received_most_visited_sites_ && received_popular_sites_ &&
737 !recorded_uma_) { 627 !recorded_uma_) {
738 RecordImpressionUMAMetrics(); 628 RecordImpressionUMAMetrics();
739 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); 629 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles",
630 current_suggestions_.size());
740 recorded_uma_ = true; 631 recorded_uma_ = true;
741 } 632 }
742 633
743 if (!observer_) 634 if (!observer_)
744 return; 635 return;
745 636
746 observer_->OnMostVisitedURLsAvailable(current_suggestions_); 637 observer_->OnMostVisitedURLsAvailable(current_suggestions_);
747 } 638 }
748 639
749 void MostVisitedSites::OnPopularSitesAvailable(bool success) { 640 void MostVisitedSites::OnPopularSitesAvailable(bool success) {
(...skipping 23 matching lines...) Expand all
773 664
774 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 665 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
775 666
776 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 667 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
777 ChangeReason change_reason) { 668 ChangeReason change_reason) {
778 if (mv_source_ == TOP_SITES) { 669 if (mv_source_ == TOP_SITES) {
779 // The displayed suggestions are invalidated. 670 // The displayed suggestions are invalidated.
780 InitiateTopSitesQuery(); 671 InitiateTopSitesQuery();
781 } 672 }
782 } 673 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/most_visited_sites.h ('k') | chrome/browser/android/ntp/most_visited_sites_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698