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

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

Issue 1919823002: Update MostVisitedSites observer interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: iMove move to .cc. 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"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h" 13 #include "base/metrics/sparse_histogram.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "chrome/browser/android/ntp/popular_sites.h"
20 #include "chrome/browser/history/top_sites_factory.h" 19 #include "chrome/browser/history/top_sites_factory.h"
21 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
23 #include "chrome/browser/supervised_user/supervised_user_service.h" 22 #include "chrome/browser/supervised_user/supervised_user_service.h"
24 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" 23 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
25 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" 24 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 25 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
27 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
29 #include "components/history/core/browser/top_sites.h" 28 #include "components/history/core/browser/top_sites.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 141 }
143 // The whole grid is already filled with personal suggestions, no point in 142 // The whole grid is already filled with personal suggestions, no point in
144 // bothering with popular ones. 143 // bothering with popular ones.
145 return false; 144 return false;
146 } 145 }
147 146
148 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { 147 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) {
149 return url1.host() == url2.host() && url1.path() == url2.path(); 148 return url1.host() == url2.host() && url1.path() == url2.path();
150 } 149 }
151 150
152 } // namespace 151 std::string GetSourceHistogramName(
153 152 const MostVisitedSites::Suggestion& suggestion) {
154 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} 153 switch (suggestion.source) {
155
156 MostVisitedSites::Suggestion::~Suggestion() {}
157
158 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const {
159 switch (source) {
160 case MostVisitedSites::TOP_SITES: 154 case MostVisitedSites::TOP_SITES:
161 return kHistogramClientName; 155 return kHistogramClientName;
162 case MostVisitedSites::POPULAR: 156 case MostVisitedSites::POPULAR:
163 return kHistogramPopularName; 157 return kHistogramPopularName;
164 case MostVisitedSites::WHITELIST: 158 case MostVisitedSites::WHITELIST:
165 return kHistogramWhitelistName; 159 return kHistogramWhitelistName;
166 case MostVisitedSites::SUGGESTIONS_SERVICE: 160 case MostVisitedSites::SUGGESTIONS_SERVICE:
167 return provider_index >= 0 161 return suggestion.provider_index >= 0
168 ? base::StringPrintf(kHistogramServerFormat, provider_index) 162 ? base::StringPrintf(kHistogramServerFormat,
163 suggestion.provider_index)
169 : kHistogramServerName; 164 : kHistogramServerName;
170 } 165 }
171 NOTREACHED(); 166 NOTREACHED();
172 return std::string(); 167 return std::string();
173 } 168 }
174 169
170 } // namespace
171
172 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {}
173
174 MostVisitedSites::Suggestion::~Suggestion() {}
175
176 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default;
177 MostVisitedSites::Suggestion&
178 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default;
179
175 MostVisitedSites::MostVisitedSites(Profile* profile) 180 MostVisitedSites::MostVisitedSites(Profile* profile)
176 : profile_(profile), observer_(nullptr), num_sites_(0), 181 : profile_(profile), observer_(nullptr), num_sites_(0),
177 received_most_visited_sites_(false), received_popular_sites_(false), 182 received_most_visited_sites_(false), received_popular_sites_(false),
178 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) { 183 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) {
179 // Register the thumbnails debugging page. 184 // Register the thumbnails debugging page.
180 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 185 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
181 186
182 SupervisedUserService* supervised_user_service = 187 SupervisedUserService* supervised_user_service =
183 SupervisedUserServiceFactory::GetForProfile(profile_); 188 SupervisedUserServiceFactory::GetForProfile(profile_);
184 supervised_user_service->AddObserver(this); 189 supervised_user_service->AddObserver(this);
185 } 190 }
186 191
187 MostVisitedSites::~MostVisitedSites() { 192 MostVisitedSites::~MostVisitedSites() {
188 SupervisedUserService* supervised_user_service = 193 SupervisedUserService* supervised_user_service =
189 SupervisedUserServiceFactory::GetForProfile(profile_); 194 SupervisedUserServiceFactory::GetForProfile(profile_);
190 supervised_user_service->RemoveObserver(this); 195 supervised_user_service->RemoveObserver(this);
191 } 196 }
192 197
193 void MostVisitedSites::SetMostVisitedURLsObserver( 198 void MostVisitedSites::SetMostVisitedURLsObserver(
194 MostVisitedSitesObserver* observer, int num_sites) { 199 MostVisitedSites::Observer* observer, int num_sites) {
195 observer_ = observer; 200 observer_ = observer;
196 num_sites_ = num_sites; 201 num_sites_ = num_sites;
197 202
198 if (ShouldShowPopularSites() && 203 if (ShouldShowPopularSites() &&
199 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { 204 NeedPopularSites(profile_->GetPrefs(), num_sites_)) {
200 popular_sites_.reset(new PopularSites( 205 popular_sites_.reset(new PopularSites(
201 profile_, 206 profile_,
202 GetPopularSitesCountry(), 207 GetPopularSitesCountry(),
203 GetPopularSitesVersion(), 208 GetPopularSitesVersion(),
204 false, 209 false,
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 320
316 void MostVisitedSites::RecordTileTypeMetrics( 321 void MostVisitedSites::RecordTileTypeMetrics(
317 const std::vector<int>& tile_types) { 322 const std::vector<int>& tile_types) {
318 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); 323 DCHECK_EQ(current_suggestions_.size(), tile_types.size());
319 int counts_per_type[NUM_TILE_TYPES] = {0}; 324 int counts_per_type[NUM_TILE_TYPES] = {0};
320 for (size_t i = 0; i < tile_types.size(); ++i) { 325 for (size_t i = 0; i < tile_types.size(); ++i) {
321 int tile_type = tile_types[i]; 326 int tile_type = tile_types[i];
322 ++counts_per_type[tile_type]; 327 ++counts_per_type[tile_type];
323 std::string histogram = base::StringPrintf( 328 std::string histogram = base::StringPrintf(
324 "NewTabPage.TileType.%s", 329 "NewTabPage.TileType.%s",
325 current_suggestions_[i]->GetSourceHistogramName().c_str()); 330 GetSourceHistogramName(current_suggestions_[i]).c_str());
326 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); 331 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES);
327 } 332 }
328 333
329 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", 334 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal",
330 counts_per_type[ICON_REAL]); 335 counts_per_type[ICON_REAL]);
331 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", 336 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor",
332 counts_per_type[ICON_COLOR]); 337 counts_per_type[ICON_COLOR]);
333 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", 338 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray",
334 counts_per_type[ICON_DEFAULT]); 339 counts_per_type[ICON_DEFAULT]);
335 } 340 }
336 341
337 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { 342 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) {
338 DCHECK_GE(index, 0); 343 DCHECK_GE(index, 0);
339 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); 344 DCHECK_LT(index, static_cast<int>(current_suggestions_.size()));
340 std::string histogram = base::StringPrintf( 345 std::string histogram = base::StringPrintf(
341 "NewTabPage.MostVisited.%s", 346 "NewTabPage.MostVisited.%s",
342 current_suggestions_[index]->GetSourceHistogramName().c_str()); 347 GetSourceHistogramName(current_suggestions_[index]).c_str());
343 LogHistogramEvent(histogram, index, num_sites_); 348 LogHistogramEvent(histogram, index, num_sites_);
344 349
345 histogram = base::StringPrintf( 350 histogram = base::StringPrintf(
346 "NewTabPage.TileTypeClicked.%s", 351 "NewTabPage.TileTypeClicked.%s",
347 current_suggestions_[index]->GetSourceHistogramName().c_str()); 352 GetSourceHistogramName(current_suggestions_[index]).c_str());
348 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); 353 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES);
349 } 354 }
350 355
351 void MostVisitedSites::OnURLFilterChanged() { 356 void MostVisitedSites::OnURLFilterChanged() {
352 QueryMostVisitedURLs(); 357 QueryMostVisitedURLs();
353 } 358 }
354 359
355 // static 360 // static
356 void MostVisitedSites::RegisterProfilePrefs( 361 void MostVisitedSites::RegisterProfilePrefs(
357 user_prefs::PrefRegistrySyncable* registry) { 362 user_prefs::PrefRegistrySyncable* registry) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 } 400 }
396 return base::FilePath(); 401 return base::FilePath();
397 } 402 }
398 403
399 void MostVisitedSites::OnMostVisitedURLsAvailable( 404 void MostVisitedSites::OnMostVisitedURLsAvailable(
400 const history::MostVisitedURLList& visited_list) { 405 const history::MostVisitedURLList& visited_list) {
401 SupervisedUserURLFilter* url_filter = 406 SupervisedUserURLFilter* url_filter =
402 SupervisedUserServiceFactory::GetForProfile(profile_) 407 SupervisedUserServiceFactory::GetForProfile(profile_)
403 ->GetURLFilterForUIThread(); 408 ->GetURLFilterForUIThread();
404 409
405 MostVisitedSites::SuggestionsVector suggestions; 410 MostVisitedSites::SuggestionsPtrVector suggestions;
406 size_t num_tiles = 411 size_t num_tiles =
407 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); 412 std::min(visited_list.size(), static_cast<size_t>(num_sites_));
408 for (size_t i = 0; i < num_tiles; ++i) { 413 for (size_t i = 0; i < num_tiles; ++i) {
409 const history::MostVisitedURL& visited = visited_list[i]; 414 const history::MostVisitedURL& visited = visited_list[i];
410 if (visited.url.is_empty()) { 415 if (visited.url.is_empty()) {
411 num_tiles = i; 416 num_tiles = i;
412 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.
413 } 418 }
414 if (url_filter->GetFilteringBehaviorForURL(visited.url) == 419 if (url_filter->GetFilteringBehaviorForURL(visited.url) ==
415 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { 420 SupervisedUserURLFilter::FilteringBehavior::BLOCK) {
(...skipping 22 matching lines...) Expand all
438 if (num_tiles == 0) { 443 if (num_tiles == 0) {
439 InitiateTopSitesQuery(); 444 InitiateTopSitesQuery();
440 return; 445 return;
441 } 446 }
442 if (num_sites_ < num_tiles) 447 if (num_sites_ < num_tiles)
443 num_tiles = num_sites_; 448 num_tiles = num_sites_;
444 449
445 SupervisedUserURLFilter* url_filter = 450 SupervisedUserURLFilter* url_filter =
446 SupervisedUserServiceFactory::GetForProfile(profile_) 451 SupervisedUserServiceFactory::GetForProfile(profile_)
447 ->GetURLFilterForUIThread(); 452 ->GetURLFilterForUIThread();
448 MostVisitedSites::SuggestionsVector suggestions; 453 MostVisitedSites::SuggestionsPtrVector suggestions;
449 for (int i = 0; i < num_tiles; ++i) { 454 for (int i = 0; i < num_tiles; ++i) {
450 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); 455 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
451 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == 456 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) ==
452 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { 457 SupervisedUserURLFilter::FilteringBehavior::BLOCK) {
453 continue; 458 continue;
454 } 459 }
455 460
456 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); 461 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion());
457 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); 462 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title());
458 generated_suggestion->url = GURL(suggestion.url()); 463 generated_suggestion->url = GURL(suggestion.url());
459 generated_suggestion->source = SUGGESTIONS_SERVICE; 464 generated_suggestion->source = SUGGESTIONS_SERVICE;
460 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath( 465 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath(
461 GURL(suggestion.url())); 466 GURL(suggestion.url()));
462 if (suggestion.providers_size() > 0) 467 if (suggestion.providers_size() > 0)
463 generated_suggestion->provider_index = suggestion.providers(0); 468 generated_suggestion->provider_index = suggestion.providers(0);
464 469
465 suggestions.push_back(std::move(generated_suggestion)); 470 suggestions.push_back(std::move(generated_suggestion));
466 } 471 }
467 472
468 received_most_visited_sites_ = true; 473 received_most_visited_sites_ = true;
469 mv_source_ = SUGGESTIONS_SERVICE; 474 mv_source_ = SUGGESTIONS_SERVICE;
470 SaveNewNTPSuggestions(&suggestions); 475 SaveNewNTPSuggestions(&suggestions);
471 NotifyMostVisitedURLsObserver(); 476 NotifyMostVisitedURLsObserver();
472 } 477 }
473 478
474 MostVisitedSites::SuggestionsVector 479 MostVisitedSites::SuggestionsPtrVector
475 MostVisitedSites::CreateWhitelistEntryPointSuggestions( 480 MostVisitedSites::CreateWhitelistEntryPointSuggestions(
476 const MostVisitedSites::SuggestionsVector& personal_suggestions) { 481 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions) {
477 size_t num_personal_suggestions = personal_suggestions.size(); 482 size_t num_personal_suggestions = personal_suggestions.size();
478 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); 483 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_));
479 484
480 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; 485 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions;
481 MostVisitedSites::SuggestionsVector whitelist_suggestions; 486 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions;
482 487
483 SupervisedUserService* supervised_user_service = 488 SupervisedUserService* supervised_user_service =
484 SupervisedUserServiceFactory::GetForProfile(profile_); 489 SupervisedUserServiceFactory::GetForProfile(profile_);
485 SupervisedUserURLFilter* url_filter = 490 SupervisedUserURLFilter* url_filter =
486 supervised_user_service->GetURLFilterForUIThread(); 491 supervised_user_service->GetURLFilterForUIThread();
487 492
488 std::set<std::string> personal_hosts; 493 std::set<std::string> personal_hosts;
489 for (const auto& suggestion : personal_suggestions) 494 for (const auto& suggestion : personal_suggestions)
490 personal_hosts.insert(suggestion->url.host()); 495 personal_hosts.insert(suggestion->url.host());
491 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 496 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
(...skipping 21 matching lines...) Expand all
513 suggestion->whitelist_icon_path = whitelist->large_icon_path(); 518 suggestion->whitelist_icon_path = whitelist->large_icon_path();
514 519
515 whitelist_suggestions.push_back(std::move(suggestion)); 520 whitelist_suggestions.push_back(std::move(suggestion));
516 if (whitelist_suggestions.size() >= num_whitelist_suggestions) 521 if (whitelist_suggestions.size() >= num_whitelist_suggestions)
517 break; 522 break;
518 } 523 }
519 524
520 return whitelist_suggestions; 525 return whitelist_suggestions;
521 } 526 }
522 527
523 MostVisitedSites::SuggestionsVector 528 MostVisitedSites::SuggestionsPtrVector
524 MostVisitedSites::CreatePopularSitesSuggestions( 529 MostVisitedSites::CreatePopularSitesSuggestions(
525 const MostVisitedSites::SuggestionsVector& personal_suggestions, 530 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions,
526 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { 531 const MostVisitedSites::SuggestionsPtrVector& whitelist_suggestions) {
527 // For child accounts popular sites suggestions will not be added. 532 // For child accounts popular sites suggestions will not be added.
528 if (profile_->IsChild()) 533 if (profile_->IsChild())
529 return MostVisitedSites::SuggestionsVector(); 534 return MostVisitedSites::SuggestionsPtrVector();
530 535
531 size_t num_suggestions = 536 size_t num_suggestions =
532 personal_suggestions.size() + whitelist_suggestions.size(); 537 personal_suggestions.size() + whitelist_suggestions.size();
533 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); 538 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_));
534 539
535 // Collect non-blacklisted popular suggestions, skipping those already present 540 // Collect non-blacklisted popular suggestions, skipping those already present
536 // in the personal suggestions. 541 // in the personal suggestions.
537 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; 542 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions;
538 MostVisitedSites::SuggestionsVector popular_sites_suggestions; 543 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions;
539 544
540 if (num_popular_sites_suggestions > 0 && popular_sites_) { 545 if (num_popular_sites_suggestions > 0 && popular_sites_) {
541 std::set<std::string> hosts; 546 std::set<std::string> hosts;
542 for (const auto& suggestion : personal_suggestions) 547 for (const auto& suggestion : personal_suggestions)
543 hosts.insert(suggestion->url.host()); 548 hosts.insert(suggestion->url.host());
544 for (const auto& suggestion : whitelist_suggestions) 549 for (const auto& suggestion : whitelist_suggestions)
545 hosts.insert(suggestion->url.host()); 550 hosts.insert(suggestion->url.host());
546 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 551 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
547 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 552 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
548 // Skip blacklisted sites. 553 // Skip blacklisted sites.
(...skipping 11 matching lines...) Expand all
560 565
561 popular_sites_suggestions.push_back(std::move(suggestion)); 566 popular_sites_suggestions.push_back(std::move(suggestion));
562 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) 567 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions)
563 break; 568 break;
564 } 569 }
565 } 570 }
566 return popular_sites_suggestions; 571 return popular_sites_suggestions;
567 } 572 }
568 573
569 void MostVisitedSites::SaveNewNTPSuggestions( 574 void MostVisitedSites::SaveNewNTPSuggestions(
570 MostVisitedSites::SuggestionsVector* personal_suggestions) { 575 MostVisitedSites::SuggestionsPtrVector* personal_suggestions) {
571 MostVisitedSites::SuggestionsVector whitelist_suggestions = 576 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions =
572 CreateWhitelistEntryPointSuggestions(*personal_suggestions); 577 CreateWhitelistEntryPointSuggestions(*personal_suggestions);
573 MostVisitedSites::SuggestionsVector popular_sites_suggestions = 578 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions =
574 CreatePopularSitesSuggestions(*personal_suggestions, 579 CreatePopularSitesSuggestions(*personal_suggestions,
575 whitelist_suggestions); 580 whitelist_suggestions);
576 size_t num_actual_tiles = personal_suggestions->size() + 581 size_t num_actual_tiles = personal_suggestions->size() +
577 whitelist_suggestions.size() + 582 whitelist_suggestions.size() +
578 popular_sites_suggestions.size(); 583 popular_sites_suggestions.size();
579 std::vector<std::string> old_sites_url; 584 std::vector<std::string> old_sites_url;
580 std::vector<bool> old_sites_is_personal; 585 std::vector<bool> old_sites_is_personal;
581 // TODO(treib): We used to call |GetPreviousNTPSites| here to populate 586 // TODO(treib): We used to call |GetPreviousNTPSites| here to populate
582 // |old_sites_url| and |old_sites_is_personal|, but that caused problems 587 // |old_sites_url| and |old_sites_is_personal|, but that caused problems
583 // (crbug.com/585391). Either figure out a way to fix them and re-enable, 588 // (crbug.com/585391). Either figure out a way to fix them and re-enable,
584 // or properly remove the order-persisting code. crbug.com/601734 589 // or properly remove the order-persisting code. crbug.com/601734
585 MostVisitedSites::SuggestionsVector merged_suggestions = MergeSuggestions( 590 MostVisitedSites::SuggestionsPtrVector merged_suggestions = MergeSuggestions(
586 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions, 591 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions,
587 old_sites_url, old_sites_is_personal); 592 old_sites_url, old_sites_is_personal);
588 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); 593 DCHECK_EQ(num_actual_tiles, merged_suggestions.size());
589 current_suggestions_.swap(merged_suggestions); 594 current_suggestions_.resize(merged_suggestions.size());
595 for (size_t i = 0; i < merged_suggestions.size(); ++i)
596 std::swap(*merged_suggestions[i], current_suggestions_[i]);
590 if (received_popular_sites_) 597 if (received_popular_sites_)
591 SaveCurrentNTPSites(); 598 SaveCurrentNTPSites();
592 } 599 }
593 600
594 // static 601 // static
595 MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions( 602 MostVisitedSites::SuggestionsPtrVector MostVisitedSites::MergeSuggestions(
596 MostVisitedSites::SuggestionsVector* personal_suggestions, 603 MostVisitedSites::SuggestionsPtrVector* personal_suggestions,
597 MostVisitedSites::SuggestionsVector* whitelist_suggestions, 604 MostVisitedSites::SuggestionsPtrVector* whitelist_suggestions,
598 MostVisitedSites::SuggestionsVector* popular_suggestions, 605 MostVisitedSites::SuggestionsPtrVector* popular_suggestions,
599 const std::vector<std::string>& old_sites_url, 606 const std::vector<std::string>& old_sites_url,
600 const std::vector<bool>& old_sites_is_personal) { 607 const std::vector<bool>& old_sites_is_personal) {
601 size_t num_personal_suggestions = personal_suggestions->size(); 608 size_t num_personal_suggestions = personal_suggestions->size();
602 size_t num_whitelist_suggestions = whitelist_suggestions->size(); 609 size_t num_whitelist_suggestions = whitelist_suggestions->size();
603 size_t num_popular_suggestions = popular_suggestions->size(); 610 size_t num_popular_suggestions = popular_suggestions->size();
604 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions + 611 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions +
605 num_personal_suggestions; 612 num_personal_suggestions;
606 MostVisitedSites::SuggestionsVector merged_suggestions; 613 MostVisitedSites::SuggestionsPtrVector merged_suggestions;
607 merged_suggestions.resize(num_tiles); 614 merged_suggestions.resize(num_tiles);
608 615
609 size_t num_old_tiles = old_sites_url.size(); 616 size_t num_old_tiles = old_sites_url.size();
610 DCHECK_LE(num_old_tiles, num_tiles); 617 DCHECK_LE(num_old_tiles, num_tiles);
611 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size()); 618 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size());
612 std::vector<std::string> old_sites_host; 619 std::vector<std::string> old_sites_host;
613 old_sites_host.reserve(num_old_tiles); 620 old_sites_host.reserve(num_old_tiles);
614 // Only populate the hosts for popular suggestions as only they can be 621 // Only populate the hosts for popular suggestions as only they can be
615 // replaced by host. Personal suggestions require an exact url match to be 622 // replaced by host. Personal suggestions require an exact url match to be
616 // replaced. 623 // replaced.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 success = source_list->GetBoolean(i, &is_personal); 676 success = source_list->GetBoolean(i, &is_personal);
670 DCHECK(success); 677 DCHECK(success);
671 old_sites_is_personal->push_back(is_personal); 678 old_sites_is_personal->push_back(is_personal);
672 } 679 }
673 } 680 }
674 681
675 void MostVisitedSites::SaveCurrentNTPSites() { 682 void MostVisitedSites::SaveCurrentNTPSites() {
676 base::ListValue url_list; 683 base::ListValue url_list;
677 base::ListValue source_list; 684 base::ListValue source_list;
678 for (const auto& suggestion : current_suggestions_) { 685 for (const auto& suggestion : current_suggestions_) {
679 url_list.AppendString(suggestion->url.spec()); 686 url_list.AppendString(suggestion.url.spec());
680 source_list.AppendBoolean(suggestion->source != MostVisitedSites::POPULAR); 687 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR);
681 } 688 }
682 PrefService* prefs = profile_->GetPrefs(); 689 PrefService* prefs = profile_->GetPrefs();
683 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list); 690 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list);
684 prefs->Set(prefs::kNTPSuggestionsURL, url_list); 691 prefs->Set(prefs::kNTPSuggestionsURL, url_list);
685 } 692 }
686 693
687 // static 694 // static
688 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( 695 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions(
689 MostVisitedSites::SuggestionsVector* src_suggestions, 696 MostVisitedSites::SuggestionsPtrVector* src_suggestions,
690 MostVisitedSites::SuggestionsVector* dst_suggestions, 697 MostVisitedSites::SuggestionsPtrVector* dst_suggestions,
691 const std::vector<std::string>& match_urls, 698 const std::vector<std::string>& match_urls,
692 const std::vector<std::string>& match_hosts) { 699 const std::vector<std::string>& match_hosts) {
693 std::vector<size_t> unmatched_suggestions; 700 std::vector<size_t> unmatched_suggestions;
694 size_t num_src_suggestions = src_suggestions->size(); 701 size_t num_src_suggestions = src_suggestions->size();
695 size_t num_matchers = match_urls.size(); 702 size_t num_matchers = match_urls.size();
696 for (size_t i = 0; i < num_src_suggestions; ++i) { 703 for (size_t i = 0; i < num_src_suggestions; ++i) {
697 size_t position; 704 size_t position;
698 for (position = 0; position < num_matchers; ++position) { 705 for (position = 0; position < num_matchers; ++position) {
699 if ((*dst_suggestions)[position] != nullptr) 706 if ((*dst_suggestions)[position] != nullptr)
700 continue; 707 continue;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 if (received_most_visited_sites_ && received_popular_sites_ && 750 if (received_most_visited_sites_ && received_popular_sites_ &&
744 !recorded_uma_) { 751 !recorded_uma_) {
745 RecordImpressionUMAMetrics(); 752 RecordImpressionUMAMetrics();
746 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); 753 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions);
747 recorded_uma_ = true; 754 recorded_uma_ = true;
748 } 755 }
749 756
750 if (!observer_) 757 if (!observer_)
751 return; 758 return;
752 759
753 std::vector<base::string16> titles; 760 observer_->OnMostVisitedURLsAvailable(current_suggestions_);
754 std::vector<std::string> urls;
755 std::vector<std::string> whitelist_icon_paths;
756 titles.reserve(num_suggestions);
757 urls.reserve(num_suggestions);
758 for (const auto& suggestion : current_suggestions_) {
759 titles.push_back(suggestion->title);
760 urls.push_back(suggestion->url.spec());
761 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value());
762 }
763
764 observer_->OnMostVisitedURLsAvailable(titles, urls, whitelist_icon_paths);
765 } 761 }
766 762
767 void MostVisitedSites::OnPopularSitesAvailable(bool success) { 763 void MostVisitedSites::OnPopularSitesAvailable(bool success) {
768 received_popular_sites_ = true; 764 received_popular_sites_ = true;
769 765
770 if (!success) { 766 if (!success) {
771 LOG(WARNING) << "Download of popular sites failed"; 767 LOG(WARNING) << "Download of popular sites failed";
772 return; 768 return;
773 } 769 }
774 770
775 if (!observer_) 771 if (!observer_)
776 return; 772 return;
777 773
778 std::vector<std::string> urls; 774 observer_->OnPopularURLsAvailable(popular_sites_->sites());
779 std::vector<std::string> favicon_urls;
780 std::vector<std::string> large_icon_urls;
781 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
782 urls.push_back(popular_site.url.spec());
783 favicon_urls.push_back(popular_site.favicon_url.spec());
784 large_icon_urls.push_back(popular_site.large_icon_url.spec());
785 }
786 observer_->OnPopularURLsAvailable(urls, favicon_urls, large_icon_urls);
787 QueryMostVisitedURLs(); 775 QueryMostVisitedURLs();
788 } 776 }
789 777
790 void MostVisitedSites::RecordImpressionUMAMetrics() { 778 void MostVisitedSites::RecordImpressionUMAMetrics() {
791 for (size_t i = 0; i < current_suggestions_.size(); i++) { 779 for (size_t i = 0; i < current_suggestions_.size(); i++) {
792 std::string histogram = base::StringPrintf( 780 std::string histogram = base::StringPrintf(
793 "NewTabPage.SuggestionsImpression.%s", 781 "NewTabPage.SuggestionsImpression.%s",
794 current_suggestions_[i]->GetSourceHistogramName().c_str()); 782 GetSourceHistogramName(current_suggestions_[i]).c_str());
795 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); 783 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_);
796 } 784 }
797 } 785 }
798 786
799 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 787 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
800 788
801 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 789 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
802 ChangeReason change_reason) { 790 ChangeReason change_reason) {
803 if (mv_source_ == TOP_SITES) { 791 if (mv_source_ == TOP_SITES) {
804 // The displayed suggestions are invalidated. 792 // The displayed suggestions are invalidated.
805 InitiateTopSitesQuery(); 793 InitiateTopSitesQuery();
806 } 794 }
807 } 795 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698