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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 | 277 |
275 // static | 278 // static |
276 void MostVisitedSites::RegisterProfilePrefs( | 279 void MostVisitedSites::RegisterProfilePrefs( |
277 user_prefs::PrefRegistrySyncable* registry) { | 280 user_prefs::PrefRegistrySyncable* registry) { |
278 registry->RegisterIntegerPref(prefs::kNumPersonalTiles, 0); | 281 registry->RegisterIntegerPref(prefs::kNumPersonalTiles, 0); |
279 // TODO(treib): Remove after M55. | 282 // TODO(treib): Remove after M55. |
280 registry->RegisterListPref(prefs::kDeprecatedNTPTilesURL); | 283 registry->RegisterListPref(prefs::kDeprecatedNTPTilesURL); |
281 registry->RegisterListPref(prefs::kDeprecatedNTPTilesIsPersonal); | 284 registry->RegisterListPref(prefs::kDeprecatedNTPTilesIsPersonal); |
282 } | 285 } |
283 | 286 |
287 void MostVisitedSites::OnIconMadeAvailable(const GURL& site_url, | |
288 bool newly_available) { | |
289 if (newly_available) | |
290 observer_->OnIconMadeAvailable(site_url); | |
291 } | |
292 | |
284 void MostVisitedSites::BuildCurrentTiles() { | 293 void MostVisitedSites::BuildCurrentTiles() { |
285 // Get the current suggestions from cache. If the cache is empty, this will | 294 // Get the current suggestions from cache. If the cache is empty, this will |
286 // fall back to TopSites. | 295 // fall back to TopSites. |
287 OnSuggestionsProfileAvailable( | 296 OnSuggestionsProfileAvailable( |
288 suggestions_service_->GetSuggestionsDataFromCache()); | 297 suggestions_service_->GetSuggestionsDataFromCache()); |
289 } | 298 } |
290 | 299 |
291 void MostVisitedSites::InitiateTopSitesQuery() { | 300 void MostVisitedSites::InitiateTopSitesQuery() { |
292 if (!top_sites_) | 301 if (!top_sites_) |
293 return; | 302 return; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
511 } | 520 } |
512 | 521 |
513 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 522 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
514 waiting_for_popular_sites_ = false; | 523 waiting_for_popular_sites_ = false; |
515 | 524 |
516 if (!success) { | 525 if (!success) { |
517 LOG(WARNING) << "Download of popular sites failed"; | 526 LOG(WARNING) << "Download of popular sites failed"; |
518 return; | 527 return; |
519 } | 528 } |
520 | 529 |
521 // Pass the popular sites to the observer. This will cause it to fetch any | 530 for (const auto& site : popular_sites_->sites()) { |
522 // missing icons, but will *not* cause it to display the popular sites. | 531 icon_cacher_->StartFetch( |
523 observer_->OnPopularURLsAvailable(popular_sites_->sites()); | 532 site, base::Bind(&MostVisitedSites::OnIconMadeAvailable, |
533 weak_ptr_factory_.GetWeakPtr(), site.url)); | |
Marc Treib
2016/10/12 09:11:28
Is the WeakPtr necessary? Since we own the IconCac
sfiera
2016/10/13 09:06:46
No, I suppose it isn't. Switched for base::Unretai
| |
534 } | |
524 | 535 |
525 // Re-build the tile list. Once done, this will notify the observer. | 536 // Re-build the tile list. Once done, this will notify the observer. |
526 BuildCurrentTiles(); | 537 BuildCurrentTiles(); |
527 } | 538 } |
528 | 539 |
529 void MostVisitedSites::RecordImpressionUMAMetrics() { | 540 void MostVisitedSites::RecordImpressionUMAMetrics() { |
530 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", | 541 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", |
531 current_tiles_.size()); | 542 current_tiles_.size()); |
532 | 543 |
533 for (size_t i = 0; i < current_tiles_.size(); i++) { | 544 for (size_t i = 0; i < current_tiles_.size(); i++) { |
(...skipping 11 matching lines...) Expand all Loading... | |
545 | 556 |
546 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 557 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
547 ChangeReason change_reason) { | 558 ChangeReason change_reason) { |
548 if (mv_source_ == NTPTileSource::TOP_SITES) { | 559 if (mv_source_ == NTPTileSource::TOP_SITES) { |
549 // The displayed tiles are invalidated. | 560 // The displayed tiles are invalidated. |
550 InitiateTopSitesQuery(); | 561 InitiateTopSitesQuery(); |
551 } | 562 } |
552 } | 563 } |
553 | 564 |
554 } // namespace ntp_tiles | 565 } // namespace ntp_tiles |
OLD | NEW |