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 <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 } | 45 } |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 MostVisitedSites::MostVisitedSites( | 49 MostVisitedSites::MostVisitedSites( |
50 PrefService* prefs, | 50 PrefService* prefs, |
51 scoped_refptr<history::TopSites> top_sites, | 51 scoped_refptr<history::TopSites> top_sites, |
52 SuggestionsService* suggestions, | 52 SuggestionsService* suggestions, |
53 std::unique_ptr<PopularSites> popular_sites, | 53 std::unique_ptr<PopularSites> popular_sites, |
54 std::unique_ptr<IconCacher> icon_cacher, | 54 std::unique_ptr<IconCacher> icon_cacher, |
55 std::unique_ptr<MostVisitedSitesSupervisor> supervisor, | 55 std::unique_ptr<MostVisitedSitesSupervisor> supervisor) |
56 std::unique_ptr<HomePageClient> home_page_client) | |
57 : prefs_(prefs), | 56 : prefs_(prefs), |
58 top_sites_(top_sites), | 57 top_sites_(top_sites), |
59 suggestions_service_(suggestions), | 58 suggestions_service_(suggestions), |
60 popular_sites_(std::move(popular_sites)), | 59 popular_sites_(std::move(popular_sites)), |
61 icon_cacher_(std::move(icon_cacher)), | 60 icon_cacher_(std::move(icon_cacher)), |
62 supervisor_(std::move(supervisor)), | 61 supervisor_(std::move(supervisor)), |
63 home_page_client_(std::move(home_page_client)), | 62 home_page_client_(nullptr), |
mastiz
2017/06/01 08:18:44
Nit: no need for this line, unique_ptr constructs
fhorschig
2017/06/01 10:12:56
Gone.
| |
64 observer_(nullptr), | 63 observer_(nullptr), |
65 num_sites_(0u), | 64 num_sites_(0u), |
66 top_sites_observer_(this), | 65 top_sites_observer_(this), |
67 mv_source_(TileSource::TOP_SITES), | 66 mv_source_(TileSource::TOP_SITES), |
68 top_sites_weak_ptr_factory_(this) { | 67 top_sites_weak_ptr_factory_(this) { |
69 DCHECK(prefs_); | 68 DCHECK(prefs_); |
70 // top_sites_ can be null in tests. | 69 // top_sites_ can be null in tests. |
71 // TODO(sfiera): have iOS use a dummy TopSites in its tests. | 70 // TODO(sfiera): have iOS use a dummy TopSites in its tests. |
72 DCHECK(suggestions_service_); | 71 DCHECK(suggestions_service_); |
73 if (supervisor_) | 72 if (supervisor_) |
74 supervisor_->SetObserver(this); | 73 supervisor_->SetObserver(this); |
75 } | 74 } |
76 | 75 |
77 MostVisitedSites::MostVisitedSites( | |
78 PrefService* prefs, | |
79 scoped_refptr<history::TopSites> top_sites, | |
80 SuggestionsService* suggestions, | |
81 std::unique_ptr<PopularSites> popular_sites, | |
82 std::unique_ptr<IconCacher> icon_cacher, | |
83 std::unique_ptr<MostVisitedSitesSupervisor> supervisor) | |
84 : MostVisitedSites(prefs, | |
85 top_sites, | |
86 suggestions, | |
87 std::move(popular_sites), | |
88 std::move(icon_cacher), | |
89 std::move(supervisor), | |
90 nullptr) {} | |
91 | |
92 MostVisitedSites::~MostVisitedSites() { | 76 MostVisitedSites::~MostVisitedSites() { |
93 if (supervisor_) | 77 if (supervisor_) |
94 supervisor_->SetObserver(nullptr); | 78 supervisor_->SetObserver(nullptr); |
95 } | 79 } |
96 | 80 |
97 bool MostVisitedSites::DoesSourceExist(TileSource source) const { | 81 bool MostVisitedSites::DoesSourceExist(TileSource source) const { |
98 switch (source) { | 82 switch (source) { |
99 case TileSource::TOP_SITES: | 83 case TileSource::TOP_SITES: |
100 return top_sites_ != nullptr; | 84 return top_sites_ != nullptr; |
101 case TileSource::SUGGESTIONS_SERVICE: | 85 case TileSource::SUGGESTIONS_SERVICE: |
102 return suggestions_service_ != nullptr; | 86 return suggestions_service_ != nullptr; |
103 case TileSource::POPULAR: | 87 case TileSource::POPULAR: |
104 return popular_sites_ != nullptr; | 88 return popular_sites_ != nullptr; |
105 case TileSource::WHITELIST: | 89 case TileSource::WHITELIST: |
106 return supervisor_ != nullptr; | 90 return supervisor_ != nullptr; |
107 case TileSource::HOMEPAGE: | 91 case TileSource::HOMEPAGE: |
108 return home_page_client_ != nullptr; | 92 return home_page_client_ != nullptr; |
109 } | 93 } |
110 NOTREACHED(); | 94 NOTREACHED(); |
111 return false; | 95 return false; |
112 } | 96 } |
113 | 97 |
98 void MostVisitedSites::SetHomePageClient( | |
99 std::unique_ptr<HomePageClient> client) { | |
100 DCHECK(client); | |
101 home_page_client_ = std::move(client); | |
102 } | |
103 | |
114 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, | 104 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, |
115 size_t num_sites) { | 105 size_t num_sites) { |
116 DCHECK(observer); | 106 DCHECK(observer); |
117 observer_ = observer; | 107 observer_ = observer; |
118 num_sites_ = num_sites; | 108 num_sites_ = num_sites; |
119 | 109 |
120 // The order for this condition is important, ShouldShowPopularSites() should | 110 // The order for this condition is important, ShouldShowPopularSites() should |
121 // always be called last to keep metrics as relevant as possible. | 111 // always be called last to keep metrics as relevant as possible. |
122 if (popular_sites_ && NeedPopularSites(prefs_, num_sites_) && | 112 if (popular_sites_ && NeedPopularSites(prefs_, num_sites_) && |
123 ShouldShowPopularSites()) { | 113 ShouldShowPopularSites()) { |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 // The displayed tiles are invalidated. | 477 // The displayed tiles are invalidated. |
488 InitiateTopSitesQuery(); | 478 InitiateTopSitesQuery(); |
489 } | 479 } |
490 } | 480 } |
491 | 481 |
492 bool MostVisitedSites::ShouldAddHomeTile() const { | 482 bool MostVisitedSites::ShouldAddHomeTile() const { |
493 return base::FeatureList::IsEnabled(kPinHomePageAsTileFeature) && | 483 return base::FeatureList::IsEnabled(kPinHomePageAsTileFeature) && |
494 num_sites_ > 0u && home_page_client_ && | 484 num_sites_ > 0u && home_page_client_ && |
495 home_page_client_->IsHomePageEnabled() && | 485 home_page_client_->IsHomePageEnabled() && |
496 !home_page_client_->IsNewTabPageUsedAsHomePage() && | 486 !home_page_client_->IsNewTabPageUsedAsHomePage() && |
487 !home_page_client_->GetHomepageUrl().is_empty() && | |
mastiz
2017/06/01 08:18:44
For safety, check for home_page_client_ being nonn
fhorschig
2017/06/01 10:12:56
Added a comment to make this existing check more v
mastiz
2017/06/01 10:59:24
Oop, sorry, I totally missed the fact that it pred
| |
497 !(top_sites_ && | 488 !(top_sites_ && |
498 top_sites_->IsBlacklisted(home_page_client_->GetHomepageUrl())); | 489 top_sites_->IsBlacklisted(home_page_client_->GetHomepageUrl())); |
499 } | 490 } |
500 | 491 |
501 void MostVisitedSites::AddToHostsAndTotalCount(const NTPTilesVector& new_tiles, | 492 void MostVisitedSites::AddToHostsAndTotalCount(const NTPTilesVector& new_tiles, |
502 std::set<std::string>* hosts, | 493 std::set<std::string>* hosts, |
503 size_t* total_tile_count) const { | 494 size_t* total_tile_count) const { |
504 for (const auto& tile : new_tiles) { | 495 for (const auto& tile : new_tiles) { |
505 hosts->insert(tile.url.host()); | 496 hosts->insert(tile.url.host()); |
506 } | 497 } |
507 *total_tile_count += new_tiles.size(); | 498 *total_tile_count += new_tiles.size(); |
508 DCHECK_LE(*total_tile_count, num_sites_); | 499 DCHECK_LE(*total_tile_count, num_sites_); |
509 } | 500 } |
510 | 501 |
511 } // namespace ntp_tiles | 502 } // namespace ntp_tiles |
OLD | NEW |