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

Side by Side Diff: components/ntp_tiles/most_visited_sites.cc

Issue 2069263003: MostVisitedSites cleanup: kill SuggestionsPtrVector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 6 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 "components/ntp_tiles/most_visited_sites.h" 5 #include "components/ntp_tiles/most_visited_sites.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 case MostVisitedSites::SUGGESTIONS_SERVICE: 157 case MostVisitedSites::SUGGESTIONS_SERVICE:
158 return suggestion.provider_index >= 0 158 return suggestion.provider_index >= 0
159 ? base::StringPrintf(kHistogramServerFormat, 159 ? base::StringPrintf(kHistogramServerFormat,
160 suggestion.provider_index) 160 suggestion.provider_index)
161 : kHistogramServerName; 161 : kHistogramServerName;
162 } 162 }
163 NOTREACHED(); 163 NOTREACHED();
164 return std::string(); 164 return std::string();
165 } 165 }
166 166
167 void AppendSuggestions(MostVisitedSites::SuggestionsVector src,
168 MostVisitedSites::SuggestionsVector* dst) {
169 dst->insert(dst->end(),
170 std::make_move_iterator(src.begin()),
171 std::make_move_iterator(src.end()));
172 }
173
167 } // namespace 174 } // namespace
168 175
169 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} 176 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {}
170 177
171 MostVisitedSites::Suggestion::~Suggestion() {} 178 MostVisitedSites::Suggestion::~Suggestion() {}
172 179
173 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; 180 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default;
174 MostVisitedSites::Suggestion& 181 MostVisitedSites::Suggestion&
175 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; 182 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default;
176 183
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { 400 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) {
394 for (const auto& whitelist : supervisor_->whitelists()) { 401 for (const auto& whitelist : supervisor_->whitelists()) {
395 if (AreURLsEquivalent(whitelist.entry_point, url)) 402 if (AreURLsEquivalent(whitelist.entry_point, url))
396 return whitelist.large_icon_path; 403 return whitelist.large_icon_path;
397 } 404 }
398 return base::FilePath(); 405 return base::FilePath();
399 } 406 }
400 407
401 void MostVisitedSites::OnMostVisitedURLsAvailable( 408 void MostVisitedSites::OnMostVisitedURLsAvailable(
402 const history::MostVisitedURLList& visited_list) { 409 const history::MostVisitedURLList& visited_list) {
403 SuggestionsPtrVector suggestions; 410 SuggestionsVector suggestions;
404 size_t num_tiles = 411 size_t num_tiles =
405 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); 412 std::min(visited_list.size(), static_cast<size_t>(num_sites_));
406 for (size_t i = 0; i < num_tiles; ++i) { 413 for (size_t i = 0; i < num_tiles; ++i) {
407 const history::MostVisitedURL& visited = visited_list[i]; 414 const history::MostVisitedURL& visited = visited_list[i];
408 if (visited.url.is_empty()) { 415 if (visited.url.is_empty()) {
409 num_tiles = i; 416 num_tiles = i;
410 break; // This is the signal that there are no more real visited sites. 417 break; // This is the signal that there are no more real visited sites.
411 } 418 }
412 if (supervisor_->IsBlocked(visited.url)) 419 if (supervisor_->IsBlocked(visited.url))
413 continue; 420 continue;
414 421
415 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 422 Suggestion suggestion;
416 suggestion->title = visited.title; 423 suggestion.title = visited.title;
417 suggestion->url = visited.url; 424 suggestion.url = visited.url;
418 suggestion->source = TOP_SITES; 425 suggestion.source = TOP_SITES;
419 suggestion->whitelist_icon_path = GetWhitelistLargeIconPath(visited.url); 426 suggestion.whitelist_icon_path = GetWhitelistLargeIconPath(visited.url);
420 427
421 suggestions.push_back(std::move(suggestion)); 428 suggestions.push_back(std::move(suggestion));
422 } 429 }
423 430
424 received_most_visited_sites_ = true; 431 received_most_visited_sites_ = true;
425 mv_source_ = TOP_SITES; 432 mv_source_ = TOP_SITES;
426 SaveNewSuggestions(&suggestions); 433 SaveNewSuggestions(std::move(suggestions));
427 NotifyMostVisitedURLsObserver(); 434 NotifyMostVisitedURLsObserver();
428 } 435 }
429 436
430 void MostVisitedSites::OnSuggestionsProfileAvailable( 437 void MostVisitedSites::OnSuggestionsProfileAvailable(
431 const SuggestionsProfile& suggestions_profile) { 438 const SuggestionsProfile& suggestions_profile) {
432 int num_tiles = suggestions_profile.suggestions_size(); 439 int num_tiles = suggestions_profile.suggestions_size();
433 // With no server suggestions, fall back to local TopSites. 440 // With no server suggestions, fall back to local TopSites.
434 if (num_tiles == 0 || 441 if (num_tiles == 0 ||
435 !base::FeatureList::IsEnabled(kDisplaySuggestionsServiceTiles)) { 442 !base::FeatureList::IsEnabled(kDisplaySuggestionsServiceTiles)) {
436 InitiateTopSitesQuery(); 443 InitiateTopSitesQuery();
437 return; 444 return;
438 } 445 }
439 if (num_sites_ < num_tiles) 446 if (num_sites_ < num_tiles)
440 num_tiles = num_sites_; 447 num_tiles = num_sites_;
441 448
442 SuggestionsPtrVector suggestions; 449 SuggestionsVector suggestions;
443 for (int i = 0; i < num_tiles; ++i) { 450 for (int i = 0; i < num_tiles; ++i) {
444 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); 451 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
445 if (supervisor_->IsBlocked(GURL(suggestion.url()))) 452 if (supervisor_->IsBlocked(GURL(suggestion.url())))
446 continue; 453 continue;
447 454
448 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); 455 Suggestion generated_suggestion;
449 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); 456 generated_suggestion.title = base::UTF8ToUTF16(suggestion.title());
450 generated_suggestion->url = GURL(suggestion.url()); 457 generated_suggestion.url = GURL(suggestion.url());
451 generated_suggestion->source = SUGGESTIONS_SERVICE; 458 generated_suggestion.source = SUGGESTIONS_SERVICE;
452 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath( 459 generated_suggestion.whitelist_icon_path =
453 GURL(suggestion.url())); 460 GetWhitelistLargeIconPath(GURL(suggestion.url()));
454 if (suggestion.providers_size() > 0) 461 if (suggestion.providers_size() > 0)
455 generated_suggestion->provider_index = suggestion.providers(0); 462 generated_suggestion.provider_index = suggestion.providers(0);
456 463
457 suggestions.push_back(std::move(generated_suggestion)); 464 suggestions.push_back(std::move(generated_suggestion));
458 } 465 }
459 466
460 received_most_visited_sites_ = true; 467 received_most_visited_sites_ = true;
461 mv_source_ = SUGGESTIONS_SERVICE; 468 mv_source_ = SUGGESTIONS_SERVICE;
462 SaveNewSuggestions(&suggestions); 469 SaveNewSuggestions(std::move(suggestions));
463 NotifyMostVisitedURLsObserver(); 470 NotifyMostVisitedURLsObserver();
464 } 471 }
465 472
466 MostVisitedSites::SuggestionsPtrVector 473 MostVisitedSites::SuggestionsVector
467 MostVisitedSites::CreateWhitelistEntryPointSuggestions( 474 MostVisitedSites::CreateWhitelistEntryPointSuggestions(
468 const SuggestionsPtrVector& personal_suggestions) { 475 const SuggestionsVector& personal_suggestions) {
469 size_t num_personal_suggestions = personal_suggestions.size(); 476 size_t num_personal_suggestions = personal_suggestions.size();
470 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); 477 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_));
471 478
472 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; 479 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions;
473 SuggestionsPtrVector whitelist_suggestions; 480 SuggestionsVector whitelist_suggestions;
474 481
475 std::set<std::string> personal_hosts; 482 std::set<std::string> personal_hosts;
476 for (const auto& suggestion : personal_suggestions) 483 for (const auto& suggestion : personal_suggestions)
477 personal_hosts.insert(suggestion->url.host()); 484 personal_hosts.insert(suggestion.url.host());
478 485
479 for (const auto& whitelist : supervisor_->whitelists()) { 486 for (const auto& whitelist : supervisor_->whitelists()) {
480 // Skip blacklisted sites. 487 // Skip blacklisted sites.
481 if (top_sites_->IsBlacklisted(whitelist.entry_point)) 488 if (top_sites_->IsBlacklisted(whitelist.entry_point))
482 continue; 489 continue;
483 490
484 // Skip suggestions already present. 491 // Skip suggestions already present.
485 if (personal_hosts.find(whitelist.entry_point.host()) != 492 if (personal_hosts.find(whitelist.entry_point.host()) !=
486 personal_hosts.end()) 493 personal_hosts.end())
487 continue; 494 continue;
488 495
489 // Skip whitelist entry points that are manually blocked. 496 // Skip whitelist entry points that are manually blocked.
490 if (supervisor_->IsBlocked(whitelist.entry_point)) 497 if (supervisor_->IsBlocked(whitelist.entry_point))
491 continue; 498 continue;
492 499
493 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 500 Suggestion suggestion;
494 suggestion->title = whitelist.title; 501 suggestion.title = whitelist.title;
495 suggestion->url = whitelist.entry_point; 502 suggestion.url = whitelist.entry_point;
496 suggestion->source = WHITELIST; 503 suggestion.source = WHITELIST;
497 suggestion->whitelist_icon_path = whitelist.large_icon_path; 504 suggestion.whitelist_icon_path = whitelist.large_icon_path;
498 505
499 whitelist_suggestions.push_back(std::move(suggestion)); 506 whitelist_suggestions.push_back(std::move(suggestion));
500 if (whitelist_suggestions.size() >= num_whitelist_suggestions) 507 if (whitelist_suggestions.size() >= num_whitelist_suggestions)
501 break; 508 break;
502 } 509 }
503 510
504 return whitelist_suggestions; 511 return whitelist_suggestions;
505 } 512 }
506 513
507 MostVisitedSites::SuggestionsPtrVector 514 MostVisitedSites::SuggestionsVector
508 MostVisitedSites::CreatePopularSitesSuggestions( 515 MostVisitedSites::CreatePopularSitesSuggestions(
509 const SuggestionsPtrVector& personal_suggestions, 516 const SuggestionsVector& personal_suggestions,
510 const SuggestionsPtrVector& whitelist_suggestions) { 517 const SuggestionsVector& whitelist_suggestions) {
511 // For child accounts popular sites suggestions will not be added. 518 // For child accounts popular sites suggestions will not be added.
512 if (supervisor_->IsChildProfile()) 519 if (supervisor_->IsChildProfile())
513 return SuggestionsPtrVector(); 520 return SuggestionsVector();
514 521
515 size_t num_suggestions = 522 size_t num_suggestions =
516 personal_suggestions.size() + whitelist_suggestions.size(); 523 personal_suggestions.size() + whitelist_suggestions.size();
517 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); 524 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_));
518 525
519 // Collect non-blacklisted popular suggestions, skipping those already present 526 // Collect non-blacklisted popular suggestions, skipping those already present
520 // in the personal suggestions. 527 // in the personal suggestions.
521 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; 528 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions;
522 SuggestionsPtrVector popular_sites_suggestions; 529 SuggestionsVector popular_sites_suggestions;
523 530
524 if (num_popular_sites_suggestions > 0 && popular_sites_) { 531 if (num_popular_sites_suggestions > 0 && popular_sites_) {
525 std::set<std::string> hosts; 532 std::set<std::string> hosts;
526 for (const auto& suggestion : personal_suggestions) 533 for (const auto& suggestion : personal_suggestions)
527 hosts.insert(suggestion->url.host()); 534 hosts.insert(suggestion.url.host());
528 for (const auto& suggestion : whitelist_suggestions) 535 for (const auto& suggestion : whitelist_suggestions)
529 hosts.insert(suggestion->url.host()); 536 hosts.insert(suggestion.url.host());
530 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 537 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
531 // Skip blacklisted sites. 538 // Skip blacklisted sites.
532 if (top_sites_->IsBlacklisted(popular_site.url)) 539 if (top_sites_->IsBlacklisted(popular_site.url))
533 continue; 540 continue;
534 std::string host = popular_site.url.host(); 541 std::string host = popular_site.url.host();
535 // Skip suggestions already present in personal or whitelists. 542 // Skip suggestions already present in personal or whitelists.
536 if (hosts.find(host) != hosts.end()) 543 if (hosts.find(host) != hosts.end())
537 continue; 544 continue;
538 545
539 std::unique_ptr<Suggestion> suggestion(new Suggestion()); 546 Suggestion suggestion;
540 suggestion->title = popular_site.title; 547 suggestion.title = popular_site.title;
541 suggestion->url = GURL(popular_site.url); 548 suggestion.url = GURL(popular_site.url);
542 suggestion->source = POPULAR; 549 suggestion.source = POPULAR;
543 550
544 popular_sites_suggestions.push_back(std::move(suggestion)); 551 popular_sites_suggestions.push_back(std::move(suggestion));
545 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) 552 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions)
546 break; 553 break;
547 } 554 }
548 } 555 }
549 return popular_sites_suggestions; 556 return popular_sites_suggestions;
550 } 557 }
551 558
552 void MostVisitedSites::SaveNewSuggestions( 559 void MostVisitedSites::SaveNewSuggestions(
553 SuggestionsPtrVector* personal_suggestions) { 560 SuggestionsVector personal_suggestions) {
554 SuggestionsPtrVector whitelist_suggestions = 561 SuggestionsVector whitelist_suggestions =
555 CreateWhitelistEntryPointSuggestions(*personal_suggestions); 562 CreateWhitelistEntryPointSuggestions(personal_suggestions);
556 SuggestionsPtrVector popular_sites_suggestions = 563 SuggestionsVector popular_sites_suggestions =
557 CreatePopularSitesSuggestions(*personal_suggestions, 564 CreatePopularSitesSuggestions(personal_suggestions,
558 whitelist_suggestions); 565 whitelist_suggestions);
559 566
560 size_t num_actual_tiles = personal_suggestions->size() + 567 size_t num_actual_tiles = personal_suggestions.size() +
561 whitelist_suggestions.size() + 568 whitelist_suggestions.size() +
562 popular_sites_suggestions.size(); 569 popular_sites_suggestions.size();
563 DCHECK_LE(num_actual_tiles, static_cast<size_t>(num_sites_)); 570 DCHECK_LE(num_actual_tiles, static_cast<size_t>(num_sites_));
564 571
565 SuggestionsPtrVector merged_suggestions = MergeSuggestions( 572 current_suggestions_ = MergeSuggestions(std::move(personal_suggestions),
566 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions); 573 std::move(whitelist_suggestions),
567 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); 574 std::move(popular_sites_suggestions));
568 575 DCHECK_EQ(num_actual_tiles, current_suggestions_.size());
569 current_suggestions_.resize(merged_suggestions.size());
570 for (size_t i = 0; i < merged_suggestions.size(); ++i)
571 std::swap(*merged_suggestions[i], current_suggestions_[i]);
572 576
573 if (received_popular_sites_) 577 if (received_popular_sites_)
574 SaveCurrentSuggestionsToPrefs(); 578 SaveCurrentSuggestionsToPrefs();
575 } 579 }
576 580
577 // static 581 // static
578 MostVisitedSites::SuggestionsPtrVector MostVisitedSites::MergeSuggestions( 582 MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions(
579 SuggestionsPtrVector* personal_suggestions, 583 SuggestionsVector personal_suggestions,
580 SuggestionsPtrVector* whitelist_suggestions, 584 SuggestionsVector whitelist_suggestions,
581 SuggestionsPtrVector* popular_suggestions) { 585 SuggestionsVector popular_suggestions) {
582 size_t num_personal_suggestions = personal_suggestions->size(); 586 SuggestionsVector merged_suggestions;
583 size_t num_whitelist_suggestions = whitelist_suggestions->size(); 587 AppendSuggestions(std::move(personal_suggestions), &merged_suggestions);
584 size_t num_popular_suggestions = popular_suggestions->size(); 588 AppendSuggestions(std::move(whitelist_suggestions), &merged_suggestions);
585 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions + 589 AppendSuggestions(std::move(popular_suggestions), &merged_suggestions);
586 num_personal_suggestions;
587 SuggestionsPtrVector merged_suggestions;
588 AppendSuggestions(personal_suggestions, &merged_suggestions);
589 AppendSuggestions(whitelist_suggestions, &merged_suggestions);
590 AppendSuggestions(popular_suggestions, &merged_suggestions);
591 DCHECK_EQ(num_tiles, merged_suggestions.size());
592
593 return merged_suggestions; 590 return merged_suggestions;
594 } 591 }
595 592
596 // TODO(treib): Once we use SuggestionsVector (non-Ptr) everywhere, move this
597 // into an anonymous namespace.
598 // static
599 void MostVisitedSites::AppendSuggestions(SuggestionsPtrVector* src,
600 SuggestionsPtrVector* dst) {
601 dst->insert(dst->end(),
602 std::make_move_iterator(src->begin()),
603 std::make_move_iterator(src->end()));
604 }
605
606 void MostVisitedSites::SaveCurrentSuggestionsToPrefs() { 593 void MostVisitedSites::SaveCurrentSuggestionsToPrefs() {
607 base::ListValue url_list; 594 base::ListValue url_list;
608 base::ListValue source_list; 595 base::ListValue source_list;
609 for (const auto& suggestion : current_suggestions_) { 596 for (const auto& suggestion : current_suggestions_) {
610 url_list.AppendString(suggestion.url.spec()); 597 url_list.AppendString(suggestion.url.spec());
611 source_list.AppendBoolean(suggestion.source != POPULAR); 598 source_list.AppendBoolean(suggestion.source != POPULAR);
612 } 599 }
613 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list); 600 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list);
614 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list); 601 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list);
615 } 602 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 645
659 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 646 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
660 ChangeReason change_reason) { 647 ChangeReason change_reason) {
661 if (mv_source_ == TOP_SITES) { 648 if (mv_source_ == TOP_SITES) {
662 // The displayed suggestions are invalidated. 649 // The displayed suggestions are invalidated.
663 InitiateTopSitesQuery(); 650 InitiateTopSitesQuery();
664 } 651 }
665 } 652 }
666 653
667 } // namespace ntp_tiles 654 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/most_visited_sites.h ('k') | components/ntp_tiles/most_visited_sites_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698