OLD | NEW |
---|---|
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 Loading... | |
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 | |
175 MostVisitedSites::MostVisitedSites(Profile* profile) | 176 MostVisitedSites::MostVisitedSites(Profile* profile) |
176 : profile_(profile), observer_(nullptr), num_sites_(0), | 177 : profile_(profile), observer_(nullptr), num_sites_(0), |
177 received_most_visited_sites_(false), received_popular_sites_(false), | 178 received_most_visited_sites_(false), received_popular_sites_(false), |
178 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) { | 179 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) { |
179 // Register the thumbnails debugging page. | 180 // Register the thumbnails debugging page. |
180 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 181 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
181 | 182 |
182 SupervisedUserService* supervised_user_service = | 183 SupervisedUserService* supervised_user_service = |
183 SupervisedUserServiceFactory::GetForProfile(profile_); | 184 SupervisedUserServiceFactory::GetForProfile(profile_); |
184 supervised_user_service->AddObserver(this); | 185 supervised_user_service->AddObserver(this); |
185 } | 186 } |
186 | 187 |
187 MostVisitedSites::~MostVisitedSites() { | 188 MostVisitedSites::~MostVisitedSites() { |
188 SupervisedUserService* supervised_user_service = | 189 SupervisedUserService* supervised_user_service = |
189 SupervisedUserServiceFactory::GetForProfile(profile_); | 190 SupervisedUserServiceFactory::GetForProfile(profile_); |
190 supervised_user_service->RemoveObserver(this); | 191 supervised_user_service->RemoveObserver(this); |
191 } | 192 } |
192 | 193 |
193 void MostVisitedSites::SetMostVisitedURLsObserver( | 194 void MostVisitedSites::SetMostVisitedURLsObserver( |
194 MostVisitedSitesObserver* observer, int num_sites) { | 195 MostVisitedSites::Observer* observer, int num_sites) { |
195 observer_ = observer; | 196 observer_ = observer; |
196 num_sites_ = num_sites; | 197 num_sites_ = num_sites; |
197 | 198 |
198 if (ShouldShowPopularSites() && | 199 if (ShouldShowPopularSites() && |
199 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { | 200 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { |
200 popular_sites_.reset(new PopularSites( | 201 popular_sites_.reset(new PopularSites( |
201 profile_, | 202 profile_, |
202 GetPopularSitesCountry(), | 203 GetPopularSitesCountry(), |
203 GetPopularSitesVersion(), | 204 GetPopularSitesVersion(), |
204 false, | 205 false, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 | 316 |
316 void MostVisitedSites::RecordTileTypeMetrics( | 317 void MostVisitedSites::RecordTileTypeMetrics( |
317 const std::vector<int>& tile_types) { | 318 const std::vector<int>& tile_types) { |
318 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); | 319 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); |
319 int counts_per_type[NUM_TILE_TYPES] = {0}; | 320 int counts_per_type[NUM_TILE_TYPES] = {0}; |
320 for (size_t i = 0; i < tile_types.size(); ++i) { | 321 for (size_t i = 0; i < tile_types.size(); ++i) { |
321 int tile_type = tile_types[i]; | 322 int tile_type = tile_types[i]; |
322 ++counts_per_type[tile_type]; | 323 ++counts_per_type[tile_type]; |
323 std::string histogram = base::StringPrintf( | 324 std::string histogram = base::StringPrintf( |
324 "NewTabPage.TileType.%s", | 325 "NewTabPage.TileType.%s", |
325 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 326 GetSourceHistogramName(current_suggestions_[i]).c_str()); |
326 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 327 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
327 } | 328 } |
328 | 329 |
329 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", | 330 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", |
330 counts_per_type[ICON_REAL]); | 331 counts_per_type[ICON_REAL]); |
331 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", | 332 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", |
332 counts_per_type[ICON_COLOR]); | 333 counts_per_type[ICON_COLOR]); |
333 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", | 334 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", |
334 counts_per_type[ICON_DEFAULT]); | 335 counts_per_type[ICON_DEFAULT]); |
335 } | 336 } |
336 | 337 |
337 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { | 338 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { |
338 DCHECK_GE(index, 0); | 339 DCHECK_GE(index, 0); |
339 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); | 340 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); |
340 std::string histogram = base::StringPrintf( | 341 std::string histogram = base::StringPrintf( |
341 "NewTabPage.MostVisited.%s", | 342 "NewTabPage.MostVisited.%s", |
342 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 343 GetSourceHistogramName(current_suggestions_[index]).c_str()); |
343 LogHistogramEvent(histogram, index, num_sites_); | 344 LogHistogramEvent(histogram, index, num_sites_); |
344 | 345 |
345 histogram = base::StringPrintf( | 346 histogram = base::StringPrintf( |
346 "NewTabPage.TileTypeClicked.%s", | 347 "NewTabPage.TileTypeClicked.%s", |
347 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 348 GetSourceHistogramName(current_suggestions_[index]).c_str()); |
348 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 349 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
349 } | 350 } |
350 | 351 |
351 void MostVisitedSites::OnURLFilterChanged() { | 352 void MostVisitedSites::OnURLFilterChanged() { |
352 QueryMostVisitedURLs(); | 353 QueryMostVisitedURLs(); |
353 } | 354 } |
354 | 355 |
355 // static | 356 // static |
356 void MostVisitedSites::RegisterProfilePrefs( | 357 void MostVisitedSites::RegisterProfilePrefs( |
357 user_prefs::PrefRegistrySyncable* registry) { | 358 user_prefs::PrefRegistrySyncable* registry) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 } | 396 } |
396 return base::FilePath(); | 397 return base::FilePath(); |
397 } | 398 } |
398 | 399 |
399 void MostVisitedSites::OnMostVisitedURLsAvailable( | 400 void MostVisitedSites::OnMostVisitedURLsAvailable( |
400 const history::MostVisitedURLList& visited_list) { | 401 const history::MostVisitedURLList& visited_list) { |
401 SupervisedUserURLFilter* url_filter = | 402 SupervisedUserURLFilter* url_filter = |
402 SupervisedUserServiceFactory::GetForProfile(profile_) | 403 SupervisedUserServiceFactory::GetForProfile(profile_) |
403 ->GetURLFilterForUIThread(); | 404 ->GetURLFilterForUIThread(); |
404 | 405 |
405 MostVisitedSites::SuggestionsVector suggestions; | 406 MostVisitedSites::SuggestionsPtrVector suggestions; |
406 size_t num_tiles = | 407 size_t num_tiles = |
407 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); | 408 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); |
408 for (size_t i = 0; i < num_tiles; ++i) { | 409 for (size_t i = 0; i < num_tiles; ++i) { |
409 const history::MostVisitedURL& visited = visited_list[i]; | 410 const history::MostVisitedURL& visited = visited_list[i]; |
410 if (visited.url.is_empty()) { | 411 if (visited.url.is_empty()) { |
411 num_tiles = i; | 412 num_tiles = i; |
412 break; // This is the signal that there are no more real visited sites. | 413 break; // This is the signal that there are no more real visited sites. |
413 } | 414 } |
414 if (url_filter->GetFilteringBehaviorForURL(visited.url) == | 415 if (url_filter->GetFilteringBehaviorForURL(visited.url) == |
415 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 416 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
(...skipping 22 matching lines...) Expand all Loading... | |
438 if (num_tiles == 0) { | 439 if (num_tiles == 0) { |
439 InitiateTopSitesQuery(); | 440 InitiateTopSitesQuery(); |
440 return; | 441 return; |
441 } | 442 } |
442 if (num_sites_ < num_tiles) | 443 if (num_sites_ < num_tiles) |
443 num_tiles = num_sites_; | 444 num_tiles = num_sites_; |
444 | 445 |
445 SupervisedUserURLFilter* url_filter = | 446 SupervisedUserURLFilter* url_filter = |
446 SupervisedUserServiceFactory::GetForProfile(profile_) | 447 SupervisedUserServiceFactory::GetForProfile(profile_) |
447 ->GetURLFilterForUIThread(); | 448 ->GetURLFilterForUIThread(); |
448 MostVisitedSites::SuggestionsVector suggestions; | 449 MostVisitedSites::SuggestionsPtrVector suggestions; |
449 for (int i = 0; i < num_tiles; ++i) { | 450 for (int i = 0; i < num_tiles; ++i) { |
450 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 451 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
451 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == | 452 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == |
452 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 453 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
453 continue; | 454 continue; |
454 } | 455 } |
455 | 456 |
456 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); | 457 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); |
457 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); | 458 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); |
458 generated_suggestion->url = GURL(suggestion.url()); | 459 generated_suggestion->url = GURL(suggestion.url()); |
459 generated_suggestion->source = SUGGESTIONS_SERVICE; | 460 generated_suggestion->source = SUGGESTIONS_SERVICE; |
460 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath( | 461 generated_suggestion->whitelist_icon_path = GetWhitelistLargeIconPath( |
461 GURL(suggestion.url())); | 462 GURL(suggestion.url())); |
462 if (suggestion.providers_size() > 0) | 463 if (suggestion.providers_size() > 0) |
463 generated_suggestion->provider_index = suggestion.providers(0); | 464 generated_suggestion->provider_index = suggestion.providers(0); |
464 | 465 |
465 suggestions.push_back(std::move(generated_suggestion)); | 466 suggestions.push_back(std::move(generated_suggestion)); |
466 } | 467 } |
467 | 468 |
468 received_most_visited_sites_ = true; | 469 received_most_visited_sites_ = true; |
469 mv_source_ = SUGGESTIONS_SERVICE; | 470 mv_source_ = SUGGESTIONS_SERVICE; |
470 SaveNewNTPSuggestions(&suggestions); | 471 SaveNewNTPSuggestions(&suggestions); |
471 NotifyMostVisitedURLsObserver(); | 472 NotifyMostVisitedURLsObserver(); |
472 } | 473 } |
473 | 474 |
474 MostVisitedSites::SuggestionsVector | 475 MostVisitedSites::SuggestionsPtrVector |
475 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 476 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
476 const MostVisitedSites::SuggestionsVector& personal_suggestions) { | 477 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions) { |
477 size_t num_personal_suggestions = personal_suggestions.size(); | 478 size_t num_personal_suggestions = personal_suggestions.size(); |
478 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); | 479 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
479 | 480 |
480 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 481 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
481 MostVisitedSites::SuggestionsVector whitelist_suggestions; | 482 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; |
482 | 483 |
483 SupervisedUserService* supervised_user_service = | 484 SupervisedUserService* supervised_user_service = |
484 SupervisedUserServiceFactory::GetForProfile(profile_); | 485 SupervisedUserServiceFactory::GetForProfile(profile_); |
485 SupervisedUserURLFilter* url_filter = | 486 SupervisedUserURLFilter* url_filter = |
486 supervised_user_service->GetURLFilterForUIThread(); | 487 supervised_user_service->GetURLFilterForUIThread(); |
487 | 488 |
488 std::set<std::string> personal_hosts; | 489 std::set<std::string> personal_hosts; |
489 for (const auto& suggestion : personal_suggestions) | 490 for (const auto& suggestion : personal_suggestions) |
490 personal_hosts.insert(suggestion->url.host()); | 491 personal_hosts.insert(suggestion->url.host()); |
491 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 492 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
(...skipping 21 matching lines...) Expand all Loading... | |
513 suggestion->whitelist_icon_path = whitelist->large_icon_path(); | 514 suggestion->whitelist_icon_path = whitelist->large_icon_path(); |
514 | 515 |
515 whitelist_suggestions.push_back(std::move(suggestion)); | 516 whitelist_suggestions.push_back(std::move(suggestion)); |
516 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 517 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
517 break; | 518 break; |
518 } | 519 } |
519 | 520 |
520 return whitelist_suggestions; | 521 return whitelist_suggestions; |
521 } | 522 } |
522 | 523 |
523 MostVisitedSites::SuggestionsVector | 524 MostVisitedSites::SuggestionsPtrVector |
524 MostVisitedSites::CreatePopularSitesSuggestions( | 525 MostVisitedSites::CreatePopularSitesSuggestions( |
525 const MostVisitedSites::SuggestionsVector& personal_suggestions, | 526 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions, |
526 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { | 527 const MostVisitedSites::SuggestionsPtrVector& whitelist_suggestions) { |
527 // For child accounts popular sites suggestions will not be added. | 528 // For child accounts popular sites suggestions will not be added. |
528 if (profile_->IsChild()) | 529 if (profile_->IsChild()) |
529 return MostVisitedSites::SuggestionsVector(); | 530 return MostVisitedSites::SuggestionsPtrVector(); |
530 | 531 |
531 size_t num_suggestions = | 532 size_t num_suggestions = |
532 personal_suggestions.size() + whitelist_suggestions.size(); | 533 personal_suggestions.size() + whitelist_suggestions.size(); |
533 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); | 534 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); |
534 | 535 |
535 // Collect non-blacklisted popular suggestions, skipping those already present | 536 // Collect non-blacklisted popular suggestions, skipping those already present |
536 // in the personal suggestions. | 537 // in the personal suggestions. |
537 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; | 538 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; |
538 MostVisitedSites::SuggestionsVector popular_sites_suggestions; | 539 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions; |
539 | 540 |
540 if (num_popular_sites_suggestions > 0 && popular_sites_) { | 541 if (num_popular_sites_suggestions > 0 && popular_sites_) { |
541 std::set<std::string> hosts; | 542 std::set<std::string> hosts; |
542 for (const auto& suggestion : personal_suggestions) | 543 for (const auto& suggestion : personal_suggestions) |
543 hosts.insert(suggestion->url.host()); | 544 hosts.insert(suggestion->url.host()); |
544 for (const auto& suggestion : whitelist_suggestions) | 545 for (const auto& suggestion : whitelist_suggestions) |
545 hosts.insert(suggestion->url.host()); | 546 hosts.insert(suggestion->url.host()); |
546 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 547 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
547 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 548 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
548 // Skip blacklisted sites. | 549 // Skip blacklisted sites. |
(...skipping 11 matching lines...) Expand all Loading... | |
560 | 561 |
561 popular_sites_suggestions.push_back(std::move(suggestion)); | 562 popular_sites_suggestions.push_back(std::move(suggestion)); |
562 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) | 563 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) |
563 break; | 564 break; |
564 } | 565 } |
565 } | 566 } |
566 return popular_sites_suggestions; | 567 return popular_sites_suggestions; |
567 } | 568 } |
568 | 569 |
569 void MostVisitedSites::SaveNewNTPSuggestions( | 570 void MostVisitedSites::SaveNewNTPSuggestions( |
570 MostVisitedSites::SuggestionsVector* personal_suggestions) { | 571 MostVisitedSites::SuggestionsPtrVector* personal_suggestions) { |
571 MostVisitedSites::SuggestionsVector whitelist_suggestions = | 572 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions = |
572 CreateWhitelistEntryPointSuggestions(*personal_suggestions); | 573 CreateWhitelistEntryPointSuggestions(*personal_suggestions); |
573 MostVisitedSites::SuggestionsVector popular_sites_suggestions = | 574 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions = |
574 CreatePopularSitesSuggestions(*personal_suggestions, | 575 CreatePopularSitesSuggestions(*personal_suggestions, |
575 whitelist_suggestions); | 576 whitelist_suggestions); |
576 size_t num_actual_tiles = personal_suggestions->size() + | 577 size_t num_actual_tiles = personal_suggestions->size() + |
577 whitelist_suggestions.size() + | 578 whitelist_suggestions.size() + |
578 popular_sites_suggestions.size(); | 579 popular_sites_suggestions.size(); |
579 std::vector<std::string> old_sites_url; | 580 std::vector<std::string> old_sites_url; |
580 std::vector<bool> old_sites_is_personal; | 581 std::vector<bool> old_sites_is_personal; |
581 // TODO(treib): We used to call |GetPreviousNTPSites| here to populate | 582 // TODO(treib): We used to call |GetPreviousNTPSites| here to populate |
582 // |old_sites_url| and |old_sites_is_personal|, but that caused problems | 583 // |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, | 584 // (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 | 585 // or properly remove the order-persisting code. crbug.com/601734 |
585 MostVisitedSites::SuggestionsVector merged_suggestions = MergeSuggestions( | 586 MostVisitedSites::SuggestionsPtrVector merged_suggestions = MergeSuggestions( |
586 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions, | 587 personal_suggestions, &whitelist_suggestions, &popular_sites_suggestions, |
587 old_sites_url, old_sites_is_personal); | 588 old_sites_url, old_sites_is_personal); |
588 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); | 589 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); |
589 current_suggestions_.swap(merged_suggestions); | 590 current_suggestions_.resize(merged_suggestions.size()); |
591 for (size_t i = 0; i < merged_suggestions.size(); ++i) { | |
592 std::swap(*merged_suggestions[i], current_suggestions_[i]); | |
Marc Treib
2016/04/26 09:57:55
Ah, this took me a while to get :D
I think somethi
sfiera
2016/04/26 10:17:30
Personally, I find std::swap() a lot easier to thi
Marc Treib
2016/04/26 10:59:22
What tripped me up wasn't the swap per se, but rat
| |
593 } | |
Marc Treib
2016/04/26 09:57:55
nit: no braces required
sfiera
2016/04/26 10:17:30
Done.
| |
590 if (received_popular_sites_) | 594 if (received_popular_sites_) |
591 SaveCurrentNTPSites(); | 595 SaveCurrentNTPSites(); |
592 } | 596 } |
593 | 597 |
594 // static | 598 // static |
595 MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions( | 599 MostVisitedSites::SuggestionsPtrVector MostVisitedSites::MergeSuggestions( |
596 MostVisitedSites::SuggestionsVector* personal_suggestions, | 600 MostVisitedSites::SuggestionsPtrVector* personal_suggestions, |
597 MostVisitedSites::SuggestionsVector* whitelist_suggestions, | 601 MostVisitedSites::SuggestionsPtrVector* whitelist_suggestions, |
598 MostVisitedSites::SuggestionsVector* popular_suggestions, | 602 MostVisitedSites::SuggestionsPtrVector* popular_suggestions, |
599 const std::vector<std::string>& old_sites_url, | 603 const std::vector<std::string>& old_sites_url, |
600 const std::vector<bool>& old_sites_is_personal) { | 604 const std::vector<bool>& old_sites_is_personal) { |
601 size_t num_personal_suggestions = personal_suggestions->size(); | 605 size_t num_personal_suggestions = personal_suggestions->size(); |
602 size_t num_whitelist_suggestions = whitelist_suggestions->size(); | 606 size_t num_whitelist_suggestions = whitelist_suggestions->size(); |
603 size_t num_popular_suggestions = popular_suggestions->size(); | 607 size_t num_popular_suggestions = popular_suggestions->size(); |
604 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions + | 608 size_t num_tiles = num_popular_suggestions + num_whitelist_suggestions + |
605 num_personal_suggestions; | 609 num_personal_suggestions; |
606 MostVisitedSites::SuggestionsVector merged_suggestions; | 610 MostVisitedSites::SuggestionsPtrVector merged_suggestions; |
607 merged_suggestions.resize(num_tiles); | 611 merged_suggestions.resize(num_tiles); |
608 | 612 |
609 size_t num_old_tiles = old_sites_url.size(); | 613 size_t num_old_tiles = old_sites_url.size(); |
610 DCHECK_LE(num_old_tiles, num_tiles); | 614 DCHECK_LE(num_old_tiles, num_tiles); |
611 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size()); | 615 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size()); |
612 std::vector<std::string> old_sites_host; | 616 std::vector<std::string> old_sites_host; |
613 old_sites_host.reserve(num_old_tiles); | 617 old_sites_host.reserve(num_old_tiles); |
614 // Only populate the hosts for popular suggestions as only they can be | 618 // 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 | 619 // replaced by host. Personal suggestions require an exact url match to be |
616 // replaced. | 620 // replaced. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 success = source_list->GetBoolean(i, &is_personal); | 673 success = source_list->GetBoolean(i, &is_personal); |
670 DCHECK(success); | 674 DCHECK(success); |
671 old_sites_is_personal->push_back(is_personal); | 675 old_sites_is_personal->push_back(is_personal); |
672 } | 676 } |
673 } | 677 } |
674 | 678 |
675 void MostVisitedSites::SaveCurrentNTPSites() { | 679 void MostVisitedSites::SaveCurrentNTPSites() { |
676 base::ListValue url_list; | 680 base::ListValue url_list; |
677 base::ListValue source_list; | 681 base::ListValue source_list; |
678 for (const auto& suggestion : current_suggestions_) { | 682 for (const auto& suggestion : current_suggestions_) { |
679 url_list.AppendString(suggestion->url.spec()); | 683 url_list.AppendString(suggestion.url.spec()); |
680 source_list.AppendBoolean(suggestion->source != MostVisitedSites::POPULAR); | 684 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); |
681 } | 685 } |
682 PrefService* prefs = profile_->GetPrefs(); | 686 PrefService* prefs = profile_->GetPrefs(); |
683 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list); | 687 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list); |
684 prefs->Set(prefs::kNTPSuggestionsURL, url_list); | 688 prefs->Set(prefs::kNTPSuggestionsURL, url_list); |
685 } | 689 } |
686 | 690 |
687 // static | 691 // static |
688 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( | 692 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( |
689 MostVisitedSites::SuggestionsVector* src_suggestions, | 693 MostVisitedSites::SuggestionsPtrVector* src_suggestions, |
690 MostVisitedSites::SuggestionsVector* dst_suggestions, | 694 MostVisitedSites::SuggestionsPtrVector* dst_suggestions, |
691 const std::vector<std::string>& match_urls, | 695 const std::vector<std::string>& match_urls, |
692 const std::vector<std::string>& match_hosts) { | 696 const std::vector<std::string>& match_hosts) { |
693 std::vector<size_t> unmatched_suggestions; | 697 std::vector<size_t> unmatched_suggestions; |
694 size_t num_src_suggestions = src_suggestions->size(); | 698 size_t num_src_suggestions = src_suggestions->size(); |
695 size_t num_matchers = match_urls.size(); | 699 size_t num_matchers = match_urls.size(); |
696 for (size_t i = 0; i < num_src_suggestions; ++i) { | 700 for (size_t i = 0; i < num_src_suggestions; ++i) { |
697 size_t position; | 701 size_t position; |
698 for (position = 0; position < num_matchers; ++position) { | 702 for (position = 0; position < num_matchers; ++position) { |
699 if ((*dst_suggestions)[position] != nullptr) | 703 if ((*dst_suggestions)[position] != nullptr) |
700 continue; | 704 continue; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
743 if (received_most_visited_sites_ && received_popular_sites_ && | 747 if (received_most_visited_sites_ && received_popular_sites_ && |
744 !recorded_uma_) { | 748 !recorded_uma_) { |
745 RecordImpressionUMAMetrics(); | 749 RecordImpressionUMAMetrics(); |
746 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); | 750 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); |
747 recorded_uma_ = true; | 751 recorded_uma_ = true; |
748 } | 752 } |
749 | 753 |
750 if (!observer_) | 754 if (!observer_) |
751 return; | 755 return; |
752 | 756 |
753 std::vector<base::string16> titles; | 757 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 } | 758 } |
766 | 759 |
767 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 760 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
768 received_popular_sites_ = true; | 761 received_popular_sites_ = true; |
769 | 762 |
770 if (!success) { | 763 if (!success) { |
771 LOG(WARNING) << "Download of popular sites failed"; | 764 LOG(WARNING) << "Download of popular sites failed"; |
772 return; | 765 return; |
773 } | 766 } |
774 | 767 |
775 if (!observer_) | 768 if (!observer_) |
776 return; | 769 return; |
777 | 770 |
778 std::vector<std::string> urls; | 771 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(); | 772 QueryMostVisitedURLs(); |
788 } | 773 } |
789 | 774 |
790 void MostVisitedSites::RecordImpressionUMAMetrics() { | 775 void MostVisitedSites::RecordImpressionUMAMetrics() { |
791 for (size_t i = 0; i < current_suggestions_.size(); i++) { | 776 for (size_t i = 0; i < current_suggestions_.size(); i++) { |
792 std::string histogram = base::StringPrintf( | 777 std::string histogram = base::StringPrintf( |
793 "NewTabPage.SuggestionsImpression.%s", | 778 "NewTabPage.SuggestionsImpression.%s", |
794 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 779 GetSourceHistogramName(current_suggestions_[i]).c_str()); |
795 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); | 780 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); |
796 } | 781 } |
797 } | 782 } |
798 | 783 |
799 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 784 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
800 | 785 |
801 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 786 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
802 ChangeReason change_reason) { | 787 ChangeReason change_reason) { |
803 if (mv_source_ == TOP_SITES) { | 788 if (mv_source_ == TOP_SITES) { |
804 // The displayed suggestions are invalidated. | 789 // The displayed suggestions are invalidated. |
805 InitiateTopSitesQuery(); | 790 InitiateTopSitesQuery(); |
806 } | 791 } |
807 } | 792 } |
OLD | NEW |