| 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 "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 <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #if defined(OS_ANDROID) | 12 #if defined(OS_ANDROID) |
| 13 #include <jni.h> | 13 #include <jni.h> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/feature_list.h" | 18 #include "base/feature_list.h" |
| 19 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
| 20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
| 21 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/metrics/sparse_histogram.h" | 22 #include "base/metrics/sparse_histogram.h" |
| 23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 27 #include "components/history/core/browser/top_sites.h" | 27 #include "components/history/core/browser/top_sites.h" |
| 28 #include "components/ntp_tiles/constants.h" | 28 #include "components/ntp_tiles/constants.h" |
| 29 #include "components/ntp_tiles/icon_cacher.h" |
| 29 #include "components/ntp_tiles/pref_names.h" | 30 #include "components/ntp_tiles/pref_names.h" |
| 30 #include "components/ntp_tiles/switches.h" | 31 #include "components/ntp_tiles/switches.h" |
| 31 #include "components/pref_registry/pref_registry_syncable.h" | 32 #include "components/pref_registry/pref_registry_syncable.h" |
| 32 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 33 | 34 |
| 34 #if defined(OS_ANDROID) | 35 #if defined(OS_ANDROID) |
| 35 #include "base/android/jni_android.h" | 36 #include "base/android/jni_android.h" |
| 36 #include "jni/MostVisitedSites_jni.h" | 37 #include "jni/MostVisitedSites_jni.h" |
| 37 #endif | 38 #endif |
| 38 | 39 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 NOTREACHED(); | 138 NOTREACHED(); |
| 138 return std::string(); | 139 return std::string(); |
| 139 } | 140 } |
| 140 | 141 |
| 141 } // namespace | 142 } // namespace |
| 142 | 143 |
| 143 MostVisitedSites::MostVisitedSites(PrefService* prefs, | 144 MostVisitedSites::MostVisitedSites(PrefService* prefs, |
| 144 scoped_refptr<history::TopSites> top_sites, | 145 scoped_refptr<history::TopSites> top_sites, |
| 145 SuggestionsService* suggestions, | 146 SuggestionsService* suggestions, |
| 146 std::unique_ptr<PopularSites> popular_sites, | 147 std::unique_ptr<PopularSites> popular_sites, |
| 148 std::unique_ptr<IconCacher> icon_cacher, |
| 147 MostVisitedSitesSupervisor* supervisor) | 149 MostVisitedSitesSupervisor* supervisor) |
| 148 : prefs_(prefs), | 150 : prefs_(prefs), |
| 149 top_sites_(top_sites), | 151 top_sites_(top_sites), |
| 150 suggestions_service_(suggestions), | 152 suggestions_service_(suggestions), |
| 151 popular_sites_(std::move(popular_sites)), | 153 popular_sites_(std::move(popular_sites)), |
| 154 icon_cacher_(std::move(icon_cacher)), |
| 152 supervisor_(supervisor), | 155 supervisor_(supervisor), |
| 153 observer_(nullptr), | 156 observer_(nullptr), |
| 154 num_sites_(0), | 157 num_sites_(0), |
| 155 waiting_for_most_visited_sites_(true), | 158 waiting_for_most_visited_sites_(true), |
| 156 waiting_for_popular_sites_(true), | 159 waiting_for_popular_sites_(true), |
| 157 recorded_uma_(false), | 160 recorded_uma_(false), |
| 158 top_sites_observer_(this), | 161 top_sites_observer_(this), |
| 159 mv_source_(NTPTileSource::SUGGESTIONS_SERVICE), | 162 mv_source_(NTPTileSource::SUGGESTIONS_SERVICE), |
| 160 weak_ptr_factory_(this) { | 163 weak_ptr_factory_(this) { |
| 161 DCHECK(prefs_); | 164 DCHECK(prefs_); |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 // Skip tiles already present in personal or whitelists. | 446 // Skip tiles already present in personal or whitelists. |
| 444 if (hosts.find(host) != hosts.end()) | 447 if (hosts.find(host) != hosts.end()) |
| 445 continue; | 448 continue; |
| 446 | 449 |
| 447 NTPTile tile; | 450 NTPTile tile; |
| 448 tile.title = popular_site.title; | 451 tile.title = popular_site.title; |
| 449 tile.url = GURL(popular_site.url); | 452 tile.url = GURL(popular_site.url); |
| 450 tile.source = NTPTileSource::POPULAR; | 453 tile.source = NTPTileSource::POPULAR; |
| 451 | 454 |
| 452 popular_sites_tiles.push_back(std::move(tile)); | 455 popular_sites_tiles.push_back(std::move(tile)); |
| 456 icon_cacher_->StartFetch( |
| 457 popular_site, base::Bind(&MostVisitedSites::OnIconMadeAvailable, |
| 458 base::Unretained(this), popular_site.url)); |
| 453 if (popular_sites_tiles.size() >= num_popular_sites_tiles) | 459 if (popular_sites_tiles.size() >= num_popular_sites_tiles) |
| 454 break; | 460 break; |
| 455 } | 461 } |
| 456 } | 462 } |
| 457 return popular_sites_tiles; | 463 return popular_sites_tiles; |
| 458 } | 464 } |
| 459 | 465 |
| 460 void MostVisitedSites::SaveNewTiles(NTPTilesVector personal_tiles) { | 466 void MostVisitedSites::SaveNewTiles(NTPTilesVector personal_tiles) { |
| 461 NTPTilesVector whitelist_tiles = | 467 NTPTilesVector whitelist_tiles = |
| 462 CreateWhitelistEntryPointTiles(personal_tiles); | 468 CreateWhitelistEntryPointTiles(personal_tiles); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 } | 517 } |
| 512 | 518 |
| 513 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 519 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
| 514 waiting_for_popular_sites_ = false; | 520 waiting_for_popular_sites_ = false; |
| 515 | 521 |
| 516 if (!success) { | 522 if (!success) { |
| 517 LOG(WARNING) << "Download of popular sites failed"; | 523 LOG(WARNING) << "Download of popular sites failed"; |
| 518 return; | 524 return; |
| 519 } | 525 } |
| 520 | 526 |
| 521 // Pass the popular sites to the observer. This will cause it to fetch any | |
| 522 // missing icons, but will *not* cause it to display the popular sites. | |
| 523 observer_->OnPopularURLsAvailable(popular_sites_->sites()); | |
| 524 | |
| 525 // Re-build the tile list. Once done, this will notify the observer. | 527 // Re-build the tile list. Once done, this will notify the observer. |
| 526 BuildCurrentTiles(); | 528 BuildCurrentTiles(); |
| 527 } | 529 } |
| 528 | 530 |
| 531 void MostVisitedSites::OnIconMadeAvailable(const GURL& site_url, |
| 532 bool newly_available) { |
| 533 if (newly_available) |
| 534 observer_->OnIconMadeAvailable(site_url); |
| 535 } |
| 536 |
| 529 void MostVisitedSites::RecordImpressionUMAMetrics() { | 537 void MostVisitedSites::RecordImpressionUMAMetrics() { |
| 530 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", | 538 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", |
| 531 current_tiles_.size()); | 539 current_tiles_.size()); |
| 532 | 540 |
| 533 for (size_t i = 0; i < current_tiles_.size(); i++) { | 541 for (size_t i = 0; i < current_tiles_.size(); i++) { |
| 534 UMA_HISTOGRAM_ENUMERATION( | 542 UMA_HISTOGRAM_ENUMERATION( |
| 535 "NewTabPage.SuggestionsImpression", static_cast<int>(i), num_sites_); | 543 "NewTabPage.SuggestionsImpression", static_cast<int>(i), num_sites_); |
| 536 | 544 |
| 537 std::string histogram = base::StringPrintf( | 545 std::string histogram = base::StringPrintf( |
| 538 "NewTabPage.SuggestionsImpression.%s", | 546 "NewTabPage.SuggestionsImpression.%s", |
| 539 GetSourceHistogramName(current_tiles_[i].source).c_str()); | 547 GetSourceHistogramName(current_tiles_[i].source).c_str()); |
| 540 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); | 548 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); |
| 541 } | 549 } |
| 542 } | 550 } |
| 543 | 551 |
| 544 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 552 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 545 | 553 |
| 546 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 554 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 547 ChangeReason change_reason) { | 555 ChangeReason change_reason) { |
| 548 if (mv_source_ == NTPTileSource::TOP_SITES) { | 556 if (mv_source_ == NTPTileSource::TOP_SITES) { |
| 549 // The displayed tiles are invalidated. | 557 // The displayed tiles are invalidated. |
| 550 InitiateTopSitesQuery(); | 558 InitiateTopSitesQuery(); |
| 551 } | 559 } |
| 552 } | 560 } |
| 553 | 561 |
| 554 } // namespace ntp_tiles | 562 } // namespace ntp_tiles |
| OLD | NEW |