[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
sfiera | 3ff01c0d | 2016-06-13 15:33:38 | [diff] [blame] | 5 | #include "components/ntp_tiles/most_visited_sites.h" |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 6 | |
sfiera | 3ff01c0d | 2016-06-13 15:33:38 | [diff] [blame] | 7 | #include <algorithm> |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 8 | #include <iterator> |
dcheng | bc6125d | 2015-12-30 01:54:07 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | d33adb40d | 2014-06-03 21:38:46 | [diff] [blame] | 12 | #include "base/callback.h" |
mastiz | f3468a8 | 2016-06-15 11:05:41 | [diff] [blame] | 13 | #include "base/feature_list.h" |
dgn | f6500c1 | 2017-05-09 17:05:31 | [diff] [blame] | 14 | #include "base/metrics/user_metrics.h" |
Weilun Shi | 354a4624 | 2018-09-25 06:51:03 | [diff] [blame] | 15 | #include "base/strings/string_split.h" |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
sdefresne | 0da3bc0 | 2015-01-29 18:26:35 | [diff] [blame] | 18 | #include "components/history/core/browser/top_sites.h" |
treib | db41639 | 2016-06-29 17:20:50 | [diff] [blame] | 19 | #include "components/ntp_tiles/constants.h" |
sfiera | ae1ae69 | 2016-10-24 18:19:15 | [diff] [blame] | 20 | #include "components/ntp_tiles/icon_cacher.h" |
sfiera | 0dc4da5 | 2016-04-29 09:23:05 | [diff] [blame] | 21 | #include "components/ntp_tiles/pref_names.h" |
| 22 | #include "components/ntp_tiles/switches.h" |
knn | cbbd754d | 2015-09-09 15:43:40 | [diff] [blame] | 23 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 25 | |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 26 | using history::TopSites; |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 27 | using suggestions::ChromeSuggestion; |
| 28 | using suggestions::SuggestionsProfile; |
sfiera | 7e952d4 | 2016-05-04 17:56:33 | [diff] [blame] | 29 | using suggestions::SuggestionsService; |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 30 | |
sfiera | 08009fe | 2016-06-15 17:07:26 | [diff] [blame] | 31 | namespace ntp_tiles { |
| 32 | |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 33 | namespace { |
| 34 | |
kristipark | c1680096 | 2018-07-31 20:05:29 | [diff] [blame] | 35 | // The maximum number of custom links that can be shown. This is independent of |
| 36 | // the maximum number of Most Visited sites that can be shown. |
| 37 | const size_t kMaxNumCustomLinks = 10; |
| 38 | |
mastiz | f3468a8 | 2016-06-15 11:05:41 | [diff] [blame] | 39 | const base::Feature kDisplaySuggestionsServiceTiles{ |
| 40 | "DisplaySuggestionsServiceTiles", base::FEATURE_ENABLED_BY_DEFAULT}; |
| 41 | |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 42 | // URL host prefixes. Hosts with these prefixes often redirect to each other, or |
| 43 | // have the same content. |
| 44 | // Popular sites are excluded if the user has visited a page whose host only |
| 45 | // differs by one of these prefixes. Even if the URL does not point to the exact |
| 46 | // same page, the user will have a personalized suggestion that is more likely |
| 47 | // to be of use for them. |
| 48 | // A cleaner way could be checking the history for redirects but this requires |
| 49 | // the page to be visited on the device. |
| 50 | const char* kKnownGenericPagePrefixes[] = { |
| 51 | "m.", "mobile.", // Common prefixes among popular sites. |
| 52 | "edition.", // Used among news papers (CNN, Independent, ...) |
| 53 | "www.", // Usually no-www domains redirect to www or vice-versa. |
| 54 | // The following entry MUST REMAIN LAST as it is prefix of every string! |
| 55 | ""}; // The no-www domain matches domains on same level . |
| 56 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 57 | // Determine whether we need any tiles from PopularSites to fill up a grid of |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 58 | // |num_tiles| tiles. If exploration sections are used, we need popular sites |
| 59 | // regardless of how many tiles we already have. |
treib | cd3d2ef | 2016-07-13 16:53:31 | [diff] [blame] | 60 | bool NeedPopularSites(const PrefService* prefs, int num_tiles) { |
Nicolas Dossou-gbete | 56f37dc | 2017-08-10 16:14:04 | [diff] [blame] | 61 | return base::FeatureList::IsEnabled(kSiteExplorationUiFeature) || |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 62 | prefs->GetInteger(prefs::kNumPersonalTiles) < num_tiles; |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 63 | } |
| 64 | |
atanasova | 9c0dda60 | 2016-03-22 17:41:13 | [diff] [blame] | 65 | bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { |
csharrison | 88b3b71 | 2016-11-14 23:12:35 | [diff] [blame] | 66 | return url1.host_piece() == url2.host_piece() && |
| 67 | url1.path_piece() == url2.path_piece(); |
atanasova | 9c0dda60 | 2016-03-22 17:41:13 | [diff] [blame] | 68 | } |
| 69 | |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 70 | bool HasHomeTile(const NTPTilesVector& tiles) { |
| 71 | for (const auto& tile : tiles) { |
| 72 | if (tile.source == TileSource::HOMEPAGE) |
| 73 | return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 78 | std::string StripFirstGenericPrefix(const std::string& host) { |
| 79 | for (const char* prefix : kKnownGenericPagePrefixes) { |
| 80 | if (base::StartsWith(host, prefix, base::CompareCase::INSENSITIVE_ASCII)) { |
| 81 | return std::string( |
| 82 | base::TrimString(host, prefix, base::TrimPositions::TRIM_LEADING)); |
| 83 | } |
| 84 | } |
| 85 | return host; |
| 86 | } |
| 87 | |
Friedrich Horschig | f738ab9 | 2018-02-14 10:10:10 | [diff] [blame] | 88 | bool ShouldShowPopularSites() { |
| 89 | return base::FeatureList::IsEnabled(kUsePopularSitesSuggestions); |
| 90 | } |
| 91 | |
Weilun Shi | 354a4624 | 2018-09-25 06:51:03 | [diff] [blame] | 92 | // Generate a short title for Most Visited items before they're converted to |
| 93 | // custom links. |
| 94 | base::string16 GenerateShortTitle(const base::string16& title) { |
| 95 | // Empty title only happened in the unittests. |
| 96 | if (title.empty()) |
| 97 | return base::string16(); |
| 98 | std::vector<base::string16> short_title_list = |
| 99 | SplitString(title, base::UTF8ToUTF16("-:|;"), base::TRIM_WHITESPACE, |
| 100 | base::SPLIT_WANT_NONEMPTY); |
| 101 | // Make sure it doesn't crash when the title only contains spaces. |
| 102 | if (short_title_list.empty()) |
| 103 | return base::string16(); |
| 104 | base::string16 short_title_front = short_title_list.front(); |
| 105 | base::string16 short_title_back = short_title_list.back(); |
| 106 | base::string16 short_title = short_title_front; |
| 107 | if (short_title_front != short_title_back) { |
| 108 | int words_in_front = |
| 109 | SplitString(short_title_front, base::kWhitespaceASCIIAs16, |
| 110 | base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY) |
| 111 | .size(); |
| 112 | int words_in_back = |
| 113 | SplitString(short_title_back, base::kWhitespaceASCIIAs16, |
| 114 | base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY) |
| 115 | .size(); |
| 116 | if (words_in_front >= 3 && words_in_back >= 1 && words_in_back <= 3) { |
| 117 | short_title = short_title_back; |
| 118 | } |
| 119 | } |
| 120 | return short_title; |
| 121 | } |
| 122 | |
sfiera | 8df7b38 | 2016-04-26 14:06:33 | [diff] [blame] | 123 | } // namespace |
| 124 | |
sfiera | c35d272 | 2016-12-14 09:41:45 | [diff] [blame] | 125 | MostVisitedSites::MostVisitedSites( |
| 126 | PrefService* prefs, |
| 127 | scoped_refptr<history::TopSites> top_sites, |
| 128 | SuggestionsService* suggestions, |
| 129 | std::unique_ptr<PopularSites> popular_sites, |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 130 | std::unique_ptr<CustomLinksManager> custom_links, |
sfiera | c35d272 | 2016-12-14 09:41:45 | [diff] [blame] | 131 | std::unique_ptr<IconCacher> icon_cacher, |
fhorschig | 3fc1f81 | 2017-06-01 12:28:28 | [diff] [blame] | 132 | std::unique_ptr<MostVisitedSitesSupervisor> supervisor) |
sfiera | 79e643b | 2016-05-13 16:52:49 | [diff] [blame] | 133 | : prefs_(prefs), |
sfiera | 685a987e | 2016-05-12 17:08:47 | [diff] [blame] | 134 | top_sites_(top_sites), |
| 135 | suggestions_service_(suggestions), |
sfiera | 96c0325 | 2016-09-14 18:58:16 | [diff] [blame] | 136 | popular_sites_(std::move(popular_sites)), |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 137 | custom_links_(std::move(custom_links)), |
sfiera | ae1ae69 | 2016-10-24 18:19:15 | [diff] [blame] | 138 | icon_cacher_(std::move(icon_cacher)), |
sfiera | c35d272 | 2016-12-14 09:41:45 | [diff] [blame] | 139 | supervisor_(std::move(supervisor)), |
sfiera | 685a987e | 2016-05-12 17:08:47 | [diff] [blame] | 140 | observer_(nullptr), |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 141 | max_num_sites_(0u), |
treib | baada0f | 2016-08-01 11:30:33 | [diff] [blame] | 142 | top_sites_observer_(this), |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 143 | mv_source_(TileSource::TOP_SITES), |
mastiz | d0da75e | 2017-01-09 11:36:48 | [diff] [blame] | 144 | top_sites_weak_ptr_factory_(this) { |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 145 | DCHECK(prefs_); |
sfiera | aa1b886 | 2016-10-04 16:09:23 | [diff] [blame] | 146 | // top_sites_ can be null in tests. |
| 147 | // TODO(sfiera): have iOS use a dummy TopSites in its tests. |
mastiz | c60d74e | 2016-06-14 14:02:49 | [diff] [blame] | 148 | DCHECK(suggestions_service_); |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 149 | if (supervisor_) |
| 150 | supervisor_->SetObserver(this); |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 151 | } |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 152 | |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 153 | MostVisitedSites::~MostVisitedSites() { |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 154 | if (supervisor_) |
| 155 | supervisor_->SetObserver(nullptr); |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 156 | } |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 157 | |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 158 | // static |
| 159 | bool MostVisitedSites::IsHostOrMobilePageKnown( |
| 160 | const std::set<std::string>& hosts_to_skip, |
| 161 | const std::string& host) { |
| 162 | std::string no_prefix_host = StripFirstGenericPrefix(host); |
| 163 | for (const char* prefix : kKnownGenericPagePrefixes) { |
| 164 | if (hosts_to_skip.count(prefix + no_prefix_host) || |
| 165 | hosts_to_skip.count(prefix + host)) { |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 172 | bool MostVisitedSites::DoesSourceExist(TileSource source) const { |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 173 | switch (source) { |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 174 | case TileSource::TOP_SITES: |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 175 | return top_sites_ != nullptr; |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 176 | case TileSource::SUGGESTIONS_SERVICE: |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 177 | return suggestions_service_ != nullptr; |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 178 | case TileSource::POPULAR_BAKED_IN: |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 179 | case TileSource::POPULAR: |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 180 | return popular_sites_ != nullptr; |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 181 | case TileSource::HOMEPAGE: |
| 182 | return homepage_client_ != nullptr; |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 183 | case TileSource::WHITELIST: |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 184 | return supervisor_ != nullptr; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 185 | case TileSource::CUSTOM_LINKS: |
| 186 | return custom_links_ != nullptr; |
sfiera | 65cefe9d | 2017-02-07 11:02:50 | [diff] [blame] | 187 | } |
| 188 | NOTREACHED(); |
| 189 | return false; |
| 190 | } |
| 191 | |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 192 | void MostVisitedSites::SetHomepageClient( |
| 193 | std::unique_ptr<HomepageClient> client) { |
| 194 | DCHECK(client); |
| 195 | homepage_client_ = std::move(client); |
| 196 | } |
| 197 | |
treib | 5a59f54 | 2016-05-18 12:17:29 | [diff] [blame] | 198 | void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 199 | size_t num_sites) { |
treib | c1bd1f1 | 2016-04-29 15:06:05 | [diff] [blame] | 200 | DCHECK(observer); |
sfiera | 929502f3 | 2016-04-21 12:52:48 | [diff] [blame] | 201 | observer_ = observer; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 202 | max_num_sites_ = num_sites; |
[email protected] | 852fc79 | 2013-10-04 04:15:12 | [diff] [blame] | 203 | |
treib | 99f6b99 | 2016-11-23 13:59:50 | [diff] [blame] | 204 | // The order for this condition is important, ShouldShowPopularSites() should |
Eric Noyau | 4793d93 | 2016-11-16 16:19:23 | [diff] [blame] | 205 | // always be called last to keep metrics as relevant as possible. |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 206 | if (popular_sites_ && NeedPopularSites(prefs_, max_num_sites_) && |
Eric Noyau | 4793d93 | 2016-11-16 16:19:23 | [diff] [blame] | 207 | ShouldShowPopularSites()) { |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 208 | popular_sites_->MaybeStartFetch( |
| 209 | false, base::Bind(&MostVisitedSites::OnPopularSitesDownloaded, |
sfiera | 00a58ab | 2016-07-28 10:20:49 | [diff] [blame] | 210 | base::Unretained(this))); |
treib | f79ed40d | 2015-09-22 12:38:06 | [diff] [blame] | 211 | } |
| 212 | |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 213 | if (top_sites_) { |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 214 | // Register as TopSitesObserver so that we can update ourselves when the |
| 215 | // TopSites changes. |
treib | baada0f | 2016-08-01 11:30:33 | [diff] [blame] | 216 | top_sites_observer_.Add(top_sites_.get()); |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 217 | } |
treib | cb9c93b | 2016-04-01 13:22:52 | [diff] [blame] | 218 | |
kristipark | e79bcc3 | 2018-08-29 19:02:31 | [diff] [blame] | 219 | if (custom_links_) { |
| 220 | custom_links_subscription_ = |
| 221 | custom_links_->RegisterCallbackForOnChanged(base::BindRepeating( |
| 222 | &MostVisitedSites::OnCustomLinksChanged, base::Unretained(this))); |
| 223 | } |
| 224 | |
mastiz | bad28af | 2017-01-30 16:04:37 | [diff] [blame] | 225 | suggestions_subscription_ = suggestions_service_->AddCallback(base::Bind( |
| 226 | &MostVisitedSites::OnSuggestionsProfileChanged, base::Unretained(this))); |
treib | cb9c93b | 2016-04-01 13:22:52 | [diff] [blame] | 227 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 228 | // Immediately build the current set of tiles, getting suggestions from the |
| 229 | // SuggestionsService's cache or, if that is empty, sites from TopSites. |
| 230 | BuildCurrentTiles(); |
treib | cb9c93b | 2016-04-01 13:22:52 | [diff] [blame] | 231 | // Also start a request for fresh suggestions. |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 232 | Refresh(); |
| 233 | } |
| 234 | |
| 235 | void MostVisitedSites::Refresh() { |
Mikel Astiz | 1d90c81 | 2017-07-21 13:52:51 | [diff] [blame] | 236 | if (top_sites_) { |
| 237 | // TopSites updates itself after a delay. To ensure up-to-date results, |
| 238 | // force an update now. |
| 239 | // TODO(mastiz): Is seems unnecessary to refresh TopSites if we will end up |
| 240 | // using server-side suggestions. |
| 241 | top_sites_->SyncWithHistory(); |
| 242 | } |
| 243 | |
sfiera | 728962f | 2016-04-29 11:04:02 | [diff] [blame] | 244 | suggestions_service_->FetchSuggestionsData(); |
[email protected] | c764b282 | 2013-07-03 04:16:48 | [diff] [blame] | 245 | } |
| 246 | |
Kristi Park | db8138f | 2018-10-22 17:06:37 | [diff] [blame] | 247 | void MostVisitedSites::RefreshTiles() { |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 248 | BuildCurrentTiles(); |
| 249 | } |
| 250 | |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 251 | void MostVisitedSites::InitializeCustomLinks() { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 252 | if (!custom_links_ || !current_tiles_.has_value() || !custom_links_enabled_) |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 253 | return; |
| 254 | |
Kristi Park | db8138f | 2018-10-22 17:06:37 | [diff] [blame] | 255 | if (custom_links_->Initialize(current_tiles_.value())) |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 256 | custom_links_action_count_ = 0; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void MostVisitedSites::UninitializeCustomLinks() { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 260 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 261 | return; |
| 262 | |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 263 | custom_links_action_count_ = -1; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 264 | custom_links_->Uninitialize(); |
| 265 | BuildCurrentTiles(); |
| 266 | Refresh(); |
| 267 | } |
| 268 | |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 269 | bool MostVisitedSites::IsCustomLinksInitialized() { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 270 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 271 | return false; |
| 272 | |
| 273 | return custom_links_->IsInitialized(); |
| 274 | } |
| 275 | |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 276 | void MostVisitedSites::EnableCustomLinks(bool enable) { |
| 277 | if (custom_links_enabled_ != enable) { |
| 278 | custom_links_enabled_ = enable; |
| 279 | BuildCurrentTiles(); |
| 280 | } |
| 281 | } |
| 282 | |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 283 | bool MostVisitedSites::AddCustomLink(const GURL& url, |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 284 | const base::string16& title) { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 285 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 286 | return false; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 287 | |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 288 | // Initialize custom links if they have not been initialized yet. |
| 289 | InitializeCustomLinks(); |
| 290 | |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 291 | bool success = custom_links_->AddLink(url, title); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 292 | if (success) { |
| 293 | if (custom_links_action_count_ != -1) |
| 294 | custom_links_action_count_++; |
kristipark | 0e19d3a | 2018-07-30 07:08:02 | [diff] [blame] | 295 | BuildCurrentTiles(); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 296 | } |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 297 | return success; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 298 | } |
| 299 | |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 300 | bool MostVisitedSites::UpdateCustomLink(const GURL& url, |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 301 | const GURL& new_url, |
Kristi Park | 6d2d998 | 2018-10-25 00:16:58 | [diff] [blame] | 302 | const base::string16& new_title) { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 303 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 304 | return false; |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 305 | |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 306 | // Initialize custom links if they have not been initialized yet. |
| 307 | InitializeCustomLinks(); |
| 308 | |
Kristi Park | 6d2d998 | 2018-10-25 00:16:58 | [diff] [blame] | 309 | bool success = custom_links_->UpdateLink(url, new_url, new_title); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 310 | if (success) { |
Kristi Park | 6d2d998 | 2018-10-25 00:16:58 | [diff] [blame] | 311 | if (custom_links_action_count_ != -1) |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 312 | custom_links_action_count_++; |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 313 | BuildCurrentTiles(); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 314 | } |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 315 | return success; |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 316 | } |
| 317 | |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 318 | bool MostVisitedSites::DeleteCustomLink(const GURL& url) { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 319 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 320 | return false; |
| 321 | |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 322 | // Initialize custom links if they have not been initialized yet. |
| 323 | InitializeCustomLinks(); |
| 324 | |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 325 | bool success = custom_links_->DeleteLink(url); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 326 | if (success) { |
| 327 | if (custom_links_action_count_ != -1) |
| 328 | custom_links_action_count_++; |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 329 | BuildCurrentTiles(); |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 330 | } |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 331 | return success; |
| 332 | } |
| 333 | |
| 334 | void MostVisitedSites::UndoCustomLinkAction() { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 335 | if (!custom_links_ || !custom_links_enabled_) |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 336 | return; |
| 337 | |
Kristi Park | c1fc354 | 2018-10-12 22:57:04 | [diff] [blame] | 338 | // If this is undoing the first action after initialization, uninitialize |
| 339 | // custom links. |
| 340 | if (custom_links_action_count_-- == 1) |
| 341 | UninitializeCustomLinks(); |
| 342 | else if (custom_links_->UndoAction()) |
kristipark | 0e19d3a | 2018-07-30 07:08:02 | [diff] [blame] | 343 | BuildCurrentTiles(); |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 344 | } |
| 345 | |
treib | 5a59f54 | 2016-05-18 12:17:29 | [diff] [blame] | 346 | void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, |
| 347 | bool add_url) { |
dgn | f6500c1 | 2017-05-09 17:05:31 | [diff] [blame] | 348 | if (add_url) { |
| 349 | base::RecordAction(base::UserMetricsAction("Suggestions.Site.Removed")); |
| 350 | } else { |
| 351 | base::RecordAction( |
| 352 | base::UserMetricsAction("Suggestions.Site.RemovalUndone")); |
| 353 | } |
| 354 | |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 355 | if (top_sites_) { |
| 356 | // Always blacklist in the local TopSites. |
| 357 | if (add_url) |
| 358 | top_sites_->AddBlacklistedURL(url); |
| 359 | else |
| 360 | top_sites_->RemoveBlacklistedURL(url); |
| 361 | } |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 362 | |
treib | fac7519 | 2015-08-17 13:21:24 | [diff] [blame] | 363 | // Only blacklist in the server-side suggestions service if it's active. |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 364 | if (mv_source_ == TileSource::SUGGESTIONS_SERVICE) { |
treib | 16070ce | 2016-03-11 11:57:40 | [diff] [blame] | 365 | if (add_url) |
sfiera | 728962f | 2016-04-29 11:04:02 | [diff] [blame] | 366 | suggestions_service_->BlacklistURL(url); |
treib | 16070ce | 2016-03-11 11:57:40 | [diff] [blame] | 367 | else |
sfiera | 728962f | 2016-04-29 11:04:02 | [diff] [blame] | 368 | suggestions_service_->UndoBlacklistURL(url); |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 369 | } |
[email protected] | a4c2f28 | 2013-07-20 05:26:05 | [diff] [blame] | 370 | } |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 371 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 372 | void MostVisitedSites::ClearBlacklistedUrls() { |
| 373 | if (top_sites_) { |
| 374 | // Always update the blacklist in the local TopSites. |
| 375 | top_sites_->ClearBlacklistedURLs(); |
| 376 | } |
| 377 | |
| 378 | // Only update the server-side blacklist if it's active. |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 379 | if (mv_source_ == TileSource::SUGGESTIONS_SERVICE) { |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 380 | suggestions_service_->ClearBlacklist(); |
| 381 | } |
| 382 | } |
| 383 | |
sfiera | 79e643b | 2016-05-13 16:52:49 | [diff] [blame] | 384 | void MostVisitedSites::OnBlockedSitesChanged() { |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 385 | BuildCurrentTiles(); |
atanasova | 9572aaf | 2016-02-26 18:08:26 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 388 | // static |
knn | cbbd754d | 2015-09-09 15:43:40 | [diff] [blame] | 389 | void MostVisitedSites::RegisterProfilePrefs( |
| 390 | user_prefs::PrefRegistrySyncable* registry) { |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 391 | registry->RegisterIntegerPref(prefs::kNumPersonalTiles, 0); |
knn | cbbd754d | 2015-09-09 15:43:40 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 394 | void MostVisitedSites::InitiateTopSitesQuery() { |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 395 | if (!top_sites_) |
| 396 | return; |
mastiz | d0da75e | 2017-01-09 11:36:48 | [diff] [blame] | 397 | if (top_sites_weak_ptr_factory_.HasWeakPtrs()) |
| 398 | return; // Ongoing query. |
sfiera | 728962f | 2016-04-29 11:04:02 | [diff] [blame] | 399 | top_sites_->GetMostVisitedURLs( |
knn | 6cf777fc | 2015-05-06 13:21:48 | [diff] [blame] | 400 | base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
mastiz | d0da75e | 2017-01-09 11:36:48 | [diff] [blame] | 401 | top_sites_weak_ptr_factory_.GetWeakPtr()), |
[email protected] | 7ceb4e3 | 2013-12-06 04:13:04 | [diff] [blame] | 402 | false); |
| 403 | } |
| 404 | |
atanasova | 9c0dda60 | 2016-03-22 17:41:13 | [diff] [blame] | 405 | base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 406 | if (supervisor_) { |
thestig | cff6d87 | 2017-06-08 18:30:35 | [diff] [blame] | 407 | for (const auto& whitelist : supervisor_->GetWhitelists()) { |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 408 | if (AreURLsEquivalent(whitelist.entry_point, url)) |
| 409 | return whitelist.large_icon_path; |
| 410 | } |
atanasova | 9c0dda60 | 2016-03-22 17:41:13 | [diff] [blame] | 411 | } |
| 412 | return base::FilePath(); |
| 413 | } |
| 414 | |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 415 | void MostVisitedSites::OnMostVisitedURLsAvailable( |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 416 | const history::MostVisitedURLList& visited_list) { |
kristipark | 90b244e | 2018-08-02 19:08:07 | [diff] [blame] | 417 | // Ignore the event if tiles are provided by the Suggestions Service or custom |
| 418 | // links, which take precedence. |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 419 | if (IsCustomLinksInitialized() || |
kristipark | 90b244e | 2018-08-02 19:08:07 | [diff] [blame] | 420 | mv_source_ == TileSource::SUGGESTIONS_SERVICE) { |
mastiz | 6484086 | 2017-01-05 12:42:31 | [diff] [blame] | 421 | return; |
| 422 | } |
| 423 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 424 | NTPTilesVector tiles; |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 425 | size_t num_tiles = std::min(visited_list.size(), max_num_sites_); |
knn | cbbd754d | 2015-09-09 15:43:40 | [diff] [blame] | 426 | for (size_t i = 0; i < num_tiles; ++i) { |
knn | 6cf777fc | 2015-05-06 13:21:48 | [diff] [blame] | 427 | const history::MostVisitedURL& visited = visited_list[i]; |
vitaliii | 875bfee | 2017-02-08 10:54:53 | [diff] [blame] | 428 | if (visited.url.is_empty()) |
knn | 6cf777fc | 2015-05-06 13:21:48 | [diff] [blame] | 429 | break; // This is the signal that there are no more real visited sites. |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 430 | if (supervisor_ && supervisor_->IsBlocked(visited.url)) |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 431 | continue; |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 432 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 433 | NTPTile tile; |
Weilun Shi | 354a4624 | 2018-09-25 06:51:03 | [diff] [blame] | 434 | tile.title = IsCustomLinksEnabled() ? GenerateShortTitle(visited.title) |
| 435 | : visited.title; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 436 | tile.url = visited.url; |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 437 | tile.source = TileSource::TOP_SITES; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 438 | tile.whitelist_icon_path = GetWhitelistLargeIconPath(visited.url); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 439 | // MostVisitedURL.title is either the title or the URL which is treated |
| 440 | // exactly as the title. Differentiating here is not worth the overhead. |
| 441 | tile.title_source = TileTitleSource::TITLE_TAG; |
Mikel Astiz | a0f98cc | 2017-10-12 19:48:48 | [diff] [blame] | 442 | // TODO(crbug.com/773278): Populate |data_generation_time| here in order to |
| 443 | // log UMA metrics of age. |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 444 | tiles.push_back(std::move(tile)); |
knn | 6cf777fc | 2015-05-06 13:21:48 | [diff] [blame] | 445 | } |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 446 | |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 447 | mv_source_ = TileSource::TOP_SITES; |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 448 | InitiateNotificationForNewTiles(std::move(tiles)); |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 449 | } |
| 450 | |
mastiz | bad28af | 2017-01-30 16:04:37 | [diff] [blame] | 451 | void MostVisitedSites::OnSuggestionsProfileChanged( |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 452 | const SuggestionsProfile& suggestions_profile) { |
kristipark | 90b244e | 2018-08-02 19:08:07 | [diff] [blame] | 453 | // Ignore the event if tiles are provided by custom links, which take |
| 454 | // precedence. |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 455 | if (IsCustomLinksInitialized() || |
kristipark | 90b244e | 2018-08-02 19:08:07 | [diff] [blame] | 456 | (suggestions_profile.suggestions_size() == 0 && |
| 457 | mv_source_ != TileSource::SUGGESTIONS_SERVICE)) { |
mastiz | bad28af | 2017-01-30 16:04:37 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
| 461 | BuildCurrentTilesGivenSuggestionsProfile(suggestions_profile); |
| 462 | } |
| 463 | |
| 464 | void MostVisitedSites::BuildCurrentTiles() { |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 465 | if (IsCustomLinksInitialized()) { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 466 | BuildCustomLinks(custom_links_->GetLinks()); |
| 467 | return; |
| 468 | } |
| 469 | |
mastiz | bad28af | 2017-01-30 16:04:37 | [diff] [blame] | 470 | BuildCurrentTilesGivenSuggestionsProfile( |
| 471 | suggestions_service_->GetSuggestionsDataFromCache().value_or( |
| 472 | SuggestionsProfile())); |
| 473 | } |
| 474 | |
| 475 | void MostVisitedSites::BuildCurrentTilesGivenSuggestionsProfile( |
| 476 | const suggestions::SuggestionsProfile& suggestions_profile) { |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 477 | size_t num_tiles = suggestions_profile.suggestions_size(); |
treib | c1bd1f1 | 2016-04-29 15:06:05 | [diff] [blame] | 478 | // With no server suggestions, fall back to local TopSites. |
mastiz | f3468a8 | 2016-06-15 11:05:41 | [diff] [blame] | 479 | if (num_tiles == 0 || |
| 480 | !base::FeatureList::IsEnabled(kDisplaySuggestionsServiceTiles)) { |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 481 | mv_source_ = TileSource::TOP_SITES; |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 482 | InitiateTopSitesQuery(); |
| 483 | return; |
| 484 | } |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 485 | if (max_num_sites_ < num_tiles) |
| 486 | num_tiles = max_num_sites_; |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 487 | |
Mikel Astiz | a0f98cc | 2017-10-12 19:48:48 | [diff] [blame] | 488 | const base::Time profile_timestamp = |
| 489 | base::Time::UnixEpoch() + |
| 490 | base::TimeDelta::FromMicroseconds(suggestions_profile.timestamp()); |
| 491 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 492 | NTPTilesVector tiles; |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 493 | for (size_t i = 0; i < num_tiles; ++i) { |
treib | d269822 | 2016-07-14 11:32:44 | [diff] [blame] | 494 | const ChromeSuggestion& suggestion_pb = suggestions_profile.suggestions(i); |
| 495 | GURL url(suggestion_pb.url()); |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 496 | if (supervisor_ && supervisor_->IsBlocked(url)) |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 497 | continue; |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 498 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 499 | NTPTile tile; |
Weilun Shi | 354a4624 | 2018-09-25 06:51:03 | [diff] [blame] | 500 | tile.title = |
| 501 | IsCustomLinksEnabled() |
| 502 | ? GenerateShortTitle(base::UTF8ToUTF16(suggestion_pb.title())) |
| 503 | : base::UTF8ToUTF16(suggestion_pb.title()); |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 504 | tile.url = url; |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 505 | tile.source = TileSource::SUGGESTIONS_SERVICE; |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 506 | // The title is an aggregation of multiple history entries of one site. |
| 507 | tile.title_source = TileTitleSource::INFERRED; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 508 | tile.whitelist_icon_path = GetWhitelistLargeIconPath(url); |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 509 | tile.thumbnail_url = GURL(suggestion_pb.thumbnail()); |
| 510 | tile.favicon_url = GURL(suggestion_pb.favicon_url()); |
Mikel Astiz | a0f98cc | 2017-10-12 19:48:48 | [diff] [blame] | 511 | tile.data_generation_time = profile_timestamp; |
| 512 | |
Jan Krcal | ace9332 | 2017-11-22 12:33:54 | [diff] [blame] | 513 | icon_cacher_->StartFetchMostLikely( |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 514 | url, base::BindRepeating(&MostVisitedSites::OnIconMadeAvailable, |
| 515 | base::Unretained(this), url)); |
atanasova | c3c25d1 | 2016-03-17 18:33:57 | [diff] [blame] | 516 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 517 | tiles.push_back(std::move(tile)); |
[email protected] | bc093e8 | 2014-03-04 21:49:11 | [diff] [blame] | 518 | } |
treib | 783a337 | 2015-08-25 16:24:39 | [diff] [blame] | 519 | |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 520 | mv_source_ = TileSource::SUGGESTIONS_SERVICE; |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 521 | InitiateNotificationForNewTiles(std::move(tiles)); |
knn | 6cf777fc | 2015-05-06 13:21:48 | [diff] [blame] | 522 | } |
[email protected] | d497524 | 2014-06-02 18:16:20 | [diff] [blame] | 523 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 524 | NTPTilesVector MostVisitedSites::CreateWhitelistEntryPointTiles( |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 525 | const std::set<std::string>& used_hosts, |
| 526 | size_t num_actual_tiles) { |
sfiera | 87db858 | 2016-08-05 10:46:42 | [diff] [blame] | 527 | if (!supervisor_) { |
| 528 | return NTPTilesVector(); |
| 529 | } |
| 530 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 531 | NTPTilesVector whitelist_tiles; |
thestig | cff6d87 | 2017-06-08 18:30:35 | [diff] [blame] | 532 | for (const auto& whitelist : supervisor_->GetWhitelists()) { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 533 | if (whitelist_tiles.size() + num_actual_tiles >= max_num_sites_) |
sfiera | e56d2a0 | 2017-02-07 09:40:09 | [diff] [blame] | 534 | break; |
| 535 | |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 536 | // Skip blacklisted sites. |
treib | f7d1151 | 2016-06-29 09:06:15 | [diff] [blame] | 537 | if (top_sites_ && top_sites_->IsBlacklisted(whitelist.entry_point)) |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 538 | continue; |
| 539 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 540 | // Skip tiles already present. |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 541 | if (used_hosts.find(whitelist.entry_point.host()) != used_hosts.end()) |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 542 | continue; |
| 543 | |
| 544 | // Skip whitelist entry points that are manually blocked. |
sfiera | 79e643b | 2016-05-13 16:52:49 | [diff] [blame] | 545 | if (supervisor_->IsBlocked(whitelist.entry_point)) |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 546 | continue; |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 547 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 548 | NTPTile tile; |
| 549 | tile.title = whitelist.title; |
| 550 | tile.url = whitelist.entry_point; |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 551 | tile.source = TileSource::WHITELIST; |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 552 | // User-set. Might be the title but we cannot be sure. |
| 553 | tile.title_source = TileTitleSource::UNKNOWN; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 554 | tile.whitelist_icon_path = whitelist.large_icon_path; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 555 | whitelist_tiles.push_back(std::move(tile)); |
atanasova | 9572aaf | 2016-02-26 18:08:26 | [diff] [blame] | 556 | } |
| 557 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 558 | return whitelist_tiles; |
atanasova | 9572aaf | 2016-02-26 18:08:26 | [diff] [blame] | 559 | } |
| 560 | |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 561 | std::map<SectionType, NTPTilesVector> |
| 562 | MostVisitedSites::CreatePopularSitesSections( |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 563 | const std::set<std::string>& used_hosts, |
| 564 | size_t num_actual_tiles) { |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 565 | std::map<SectionType, NTPTilesVector> sections = { |
| 566 | std::make_pair(SectionType::PERSONALIZED, NTPTilesVector())}; |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 567 | // For child accounts popular sites tiles will not be added. |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 568 | if (supervisor_ && supervisor_->IsChildProfile()) { |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 569 | return sections; |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 570 | } |
atanasova | 9514b30 | 2016-02-26 23:09:28 | [diff] [blame] | 571 | |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 572 | if (!popular_sites_ || !ShouldShowPopularSites()) { |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 573 | return sections; |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 574 | } |
atanasova | 9572aaf | 2016-02-26 18:08:26 | [diff] [blame] | 575 | |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 576 | const std::set<std::string> no_hosts; |
| 577 | for (const auto& section_type_and_sites : popular_sites()->sections()) { |
| 578 | SectionType type = section_type_and_sites.first; |
| 579 | const PopularSites::SitesVector& sites = section_type_and_sites.second; |
| 580 | if (type == SectionType::PERSONALIZED) { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 581 | size_t num_required_tiles = max_num_sites_ - num_actual_tiles; |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 582 | sections[type] = |
| 583 | CreatePopularSitesTiles(/*popular_sites=*/sites, |
| 584 | /*hosts_to_skip=*/used_hosts, |
| 585 | /*num_max_tiles=*/num_required_tiles); |
| 586 | } else { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 587 | sections[type] = |
| 588 | CreatePopularSitesTiles(/*popular_sites=*/sites, |
| 589 | /*hosts_to_skip=*/no_hosts, |
| 590 | /*num_max_tiles=*/max_num_sites_); |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | return sections; |
| 594 | } |
| 595 | |
| 596 | NTPTilesVector MostVisitedSites::CreatePopularSitesTiles( |
| 597 | const PopularSites::SitesVector& sites_vector, |
| 598 | const std::set<std::string>& hosts_to_skip, |
| 599 | size_t num_max_tiles) { |
knn | cbbd754d | 2015-09-09 15:43:40 | [diff] [blame] | 600 | // Collect non-blacklisted popular suggestions, skipping those already present |
| 601 | // in the personal suggestions. |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 602 | NTPTilesVector popular_sites_tiles; |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 603 | for (const PopularSites::Site& popular_site : sites_vector) { |
| 604 | if (popular_sites_tiles.size() >= num_max_tiles) { |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 605 | break; |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 606 | } |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 607 | |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 608 | // Skip blacklisted sites. |
| 609 | if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url)) |
| 610 | continue; |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 611 | |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 612 | const std::string& host = popular_site.url.host(); |
Friedrich Horschig | b173340 | 2017-08-28 17:06:11 | [diff] [blame] | 613 | if (IsHostOrMobilePageKnown(hosts_to_skip, host)) { |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 614 | continue; |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 615 | } |
atanasova | c3c25d1 | 2016-03-17 18:33:57 | [diff] [blame] | 616 | |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 617 | NTPTile tile; |
| 618 | tile.title = popular_site.title; |
| 619 | tile.url = GURL(popular_site.url); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 620 | tile.title_source = popular_site.title_source; |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 621 | tile.source = popular_site.baked_in ? TileSource::POPULAR_BAKED_IN |
| 622 | : TileSource::POPULAR; |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 623 | popular_sites_tiles.push_back(std::move(tile)); |
| 624 | base::Closure icon_available = |
| 625 | base::Bind(&MostVisitedSites::OnIconMadeAvailable, |
| 626 | base::Unretained(this), popular_site.url); |
| 627 | icon_cacher_->StartFetchPopularSites(popular_site, icon_available, |
| 628 | icon_available); |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 629 | } |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 630 | return popular_sites_tiles; |
atanasova | 9572aaf | 2016-02-26 18:08:26 | [diff] [blame] | 631 | } |
| 632 | |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 633 | void MostVisitedSites::OnHomepageTitleDetermined( |
| 634 | NTPTilesVector tiles, |
| 635 | const base::Optional<base::string16>& title) { |
| 636 | if (!title.has_value()) |
| 637 | return; // If there is no title, the most recent tile was already sent out. |
| 638 | |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 639 | MergeMostVisitedTiles(InsertHomeTile(std::move(tiles), title.value())); |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | NTPTilesVector MostVisitedSites::InsertHomeTile( |
| 643 | NTPTilesVector tiles, |
| 644 | const base::string16& title) const { |
| 645 | DCHECK(homepage_client_); |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 646 | DCHECK_GT(max_num_sites_, 0u); |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 647 | |
| 648 | const GURL& homepage_url = homepage_client_->GetHomepageUrl(); |
| 649 | NTPTilesVector new_tiles; |
| 650 | bool homepage_tile_added = false; |
| 651 | |
| 652 | for (auto& tile : tiles) { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 653 | if (new_tiles.size() >= max_num_sites_) { |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | |
| 657 | // If there's a tile has the same host name with homepage, insert the tile |
| 658 | // to the first position of the list. This is also a deduplication. |
| 659 | if (tile.url.host() == homepage_url.host() && !homepage_tile_added) { |
| 660 | tile.source = TileSource::HOMEPAGE; |
| 661 | homepage_tile_added = true; |
| 662 | new_tiles.insert(new_tiles.begin(), std::move(tile)); |
| 663 | continue; |
| 664 | } |
| 665 | new_tiles.push_back(std::move(tile)); |
| 666 | } |
| 667 | |
| 668 | if (!homepage_tile_added) { |
| 669 | // Make room for the homepage tile. |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 670 | if (new_tiles.size() >= max_num_sites_) { |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 671 | new_tiles.pop_back(); |
| 672 | } |
| 673 | NTPTile homepage_tile; |
| 674 | homepage_tile.url = homepage_url; |
| 675 | homepage_tile.title = title; |
| 676 | homepage_tile.source = TileSource::HOMEPAGE; |
| 677 | homepage_tile.title_source = TileTitleSource::TITLE_TAG; |
| 678 | |
| 679 | // Always insert |homepage_tile| to the front of |new_tiles| to ensure it's |
| 680 | // the first tile. |
| 681 | new_tiles.insert(new_tiles.begin(), std::move(homepage_tile)); |
| 682 | } |
| 683 | return new_tiles; |
| 684 | } |
| 685 | |
kristipark | e79bcc3 | 2018-08-29 19:02:31 | [diff] [blame] | 686 | void MostVisitedSites::OnCustomLinksChanged() { |
| 687 | DCHECK(custom_links_); |
| 688 | DCHECK(custom_links_->IsInitialized()); |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 689 | if (custom_links_enabled_) |
| 690 | BuildCustomLinks(custom_links_->GetLinks()); |
kristipark | e79bcc3 | 2018-08-29 19:02:31 | [diff] [blame] | 691 | } |
| 692 | |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 693 | void MostVisitedSites::BuildCustomLinks( |
| 694 | const std::vector<CustomLinksManager::Link>& links) { |
kristipark | 6d3d90d3 | 2018-07-19 03:28:01 | [diff] [blame] | 695 | DCHECK(IsCustomLinksEnabled()); |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 696 | |
| 697 | NTPTilesVector tiles; |
kristipark | c1680096 | 2018-07-31 20:05:29 | [diff] [blame] | 698 | size_t num_tiles = std::min(links.size(), kMaxNumCustomLinks); |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 699 | for (size_t i = 0; i < num_tiles; ++i) { |
| 700 | const CustomLinksManager::Link& link = links.at(i); |
| 701 | if (supervisor_ && supervisor_->IsBlocked(link.url)) |
| 702 | continue; |
| 703 | |
| 704 | NTPTile tile; |
| 705 | tile.title = link.title; |
| 706 | tile.url = link.url; |
| 707 | tile.source = TileSource::CUSTOM_LINKS; |
| 708 | // TODO(crbug.com/773278): Populate |data_generation_time| here in order to |
| 709 | // log UMA metrics of age. |
| 710 | tiles.push_back(std::move(tile)); |
| 711 | } |
| 712 | |
| 713 | mv_source_ = TileSource::CUSTOM_LINKS; |
| 714 | SaveTilesAndNotify(std::move(tiles), std::map<SectionType, NTPTilesVector>()); |
| 715 | } |
| 716 | |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 717 | void MostVisitedSites::InitiateNotificationForNewTiles( |
| 718 | NTPTilesVector new_tiles) { |
| 719 | if (ShouldAddHomeTile() && !HasHomeTile(new_tiles)) { |
| 720 | homepage_client_->QueryHomepageTitle( |
| 721 | base::BindOnce(&MostVisitedSites::OnHomepageTitleDetermined, |
| 722 | base::Unretained(this), new_tiles)); |
| 723 | GURL homepage_url = homepage_client_->GetHomepageUrl(); |
| 724 | icon_cacher_->StartFetchMostLikely( |
| 725 | homepage_url, |
| 726 | base::BindRepeating(&MostVisitedSites::OnIconMadeAvailable, |
| 727 | base::Unretained(this), homepage_url)); |
| 728 | |
| 729 | // Don't wait for the homepage title from history but immediately serve a |
| 730 | // copy of new tiles. |
| 731 | new_tiles = InsertHomeTile(std::move(new_tiles), base::string16()); |
| 732 | } |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 733 | MergeMostVisitedTiles(std::move(new_tiles)); |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 734 | } |
| 735 | |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 736 | void MostVisitedSites::MergeMostVisitedTiles(NTPTilesVector personal_tiles) { |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 737 | std::set<std::string> used_hosts; |
| 738 | size_t num_actual_tiles = 0u; |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 739 | AddToHostsAndTotalCount(personal_tiles, &used_hosts, &num_actual_tiles); |
| 740 | |
| 741 | NTPTilesVector whitelist_tiles = |
| 742 | CreateWhitelistEntryPointTiles(used_hosts, num_actual_tiles); |
| 743 | AddToHostsAndTotalCount(whitelist_tiles, &used_hosts, &num_actual_tiles); |
| 744 | |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 745 | std::map<SectionType, NTPTilesVector> sections = |
| 746 | CreatePopularSitesSections(used_hosts, num_actual_tiles); |
| 747 | AddToHostsAndTotalCount(sections[SectionType::PERSONALIZED], &used_hosts, |
| 748 | &num_actual_tiles); |
treib | 5a59f54 | 2016-05-18 12:17:29 | [diff] [blame] | 749 | |
sfiera | 3140e14 | 2017-03-07 16:00:48 | [diff] [blame] | 750 | NTPTilesVector new_tiles = |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 751 | MergeTiles(std::move(personal_tiles), std::move(whitelist_tiles), |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 752 | std::move(sections[SectionType::PERSONALIZED])); |
sfiera | 3140e14 | 2017-03-07 16:00:48 | [diff] [blame] | 753 | |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 754 | SaveTilesAndNotify(std::move(new_tiles), std::move(sections)); |
| 755 | } |
| 756 | |
| 757 | void MostVisitedSites::SaveTilesAndNotify( |
| 758 | NTPTilesVector new_tiles, |
| 759 | std::map<SectionType, NTPTilesVector> sections) { |
| 760 | if (current_tiles_.has_value() && (*current_tiles_ == new_tiles)) |
| 761 | return; |
sfiera | 3140e14 | 2017-03-07 16:00:48 | [diff] [blame] | 762 | current_tiles_.emplace(std::move(new_tiles)); |
treib | 5a59f54 | 2016-05-18 12:17:29 | [diff] [blame] | 763 | |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 764 | int num_personal_tiles = 0; |
sfiera | 3140e14 | 2017-03-07 16:00:48 | [diff] [blame] | 765 | for (const auto& tile : *current_tiles_) { |
Friedrich Horschig | 764f290 | 2018-01-22 14:28:48 | [diff] [blame] | 766 | if (tile.source != TileSource::POPULAR && |
| 767 | tile.source != TileSource::POPULAR_BAKED_IN) { |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 768 | num_personal_tiles++; |
Friedrich Horschig | 764f290 | 2018-01-22 14:28:48 | [diff] [blame] | 769 | } |
treib | cd3d2ef | 2016-07-13 16:53:31 | [diff] [blame] | 770 | } |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 771 | prefs_->SetInteger(prefs::kNumPersonalTiles, num_personal_tiles); |
sfiera | 3140e14 | 2017-03-07 16:00:48 | [diff] [blame] | 772 | if (!observer_) |
| 773 | return; |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 774 | sections[SectionType::PERSONALIZED] = *current_tiles_; |
| 775 | observer_->OnURLsAvailable(sections); |
treib | cf674e6 | 2015-08-17 15:43:01 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // static |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 779 | NTPTilesVector MostVisitedSites::MergeTiles(NTPTilesVector personal_tiles, |
| 780 | NTPTilesVector whitelist_tiles, |
| 781 | NTPTilesVector popular_tiles) { |
| 782 | NTPTilesVector merged_tiles; |
| 783 | std::move(personal_tiles.begin(), personal_tiles.end(), |
| 784 | std::back_inserter(merged_tiles)); |
| 785 | std::move(whitelist_tiles.begin(), whitelist_tiles.end(), |
| 786 | std::back_inserter(merged_tiles)); |
| 787 | std::move(popular_tiles.begin(), popular_tiles.end(), |
| 788 | std::back_inserter(merged_tiles)); |
| 789 | return merged_tiles; |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 790 | } |
| 791 | |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 792 | void MostVisitedSites::OnPopularSitesDownloaded(bool success) { |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 793 | if (!success) { |
| 794 | LOG(WARNING) << "Download of popular sites failed"; |
| 795 | return; |
| 796 | } |
bauerb | 21e371e | 2017-03-03 18:48:29 | [diff] [blame] | 797 | |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 798 | for (const auto& section : popular_sites_->sections()) { |
| 799 | for (const PopularSites::Site& site : section.second) { |
| 800 | // Ignore callback; these icons will be seen on the *next* NTP. |
| 801 | icon_cacher_->StartFetchPopularSites(site, base::Closure(), |
| 802 | base::Closure()); |
| 803 | } |
bauerb | 21e371e | 2017-03-03 18:48:29 | [diff] [blame] | 804 | } |
treib | cffa650 | 2015-08-06 09:12:27 | [diff] [blame] | 805 | } |
| 806 | |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 807 | void MostVisitedSites::OnIconMadeAvailable(const GURL& site_url) { |
| 808 | observer_->OnIconMadeAvailable(site_url); |
sfiera | ae1ae69 | 2016-10-24 18:19:15 | [diff] [blame] | 809 | } |
| 810 | |
treib | 16070ce | 2016-03-11 11:57:40 | [diff] [blame] | 811 | void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 812 | |
treib | 16070ce | 2016-03-11 11:57:40 | [diff] [blame] | 813 | void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
fserb | db57511 | 2015-06-29 21:31:59 | [diff] [blame] | 814 | ChangeReason change_reason) { |
treib | 231212e | 2017-04-04 17:09:22 | [diff] [blame] | 815 | if (mv_source_ == TileSource::TOP_SITES) { |
sfiera | 635d21d | 2016-08-04 07:56:09 | [diff] [blame] | 816 | // The displayed tiles are invalidated. |
newt | 2a47ce7 | 2015-10-01 20:09:49 | [diff] [blame] | 817 | InitiateTopSitesQuery(); |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 818 | } |
| 819 | } |
sfiera | 08009fe | 2016-06-15 17:07:26 | [diff] [blame] | 820 | |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 821 | bool MostVisitedSites::ShouldAddHomeTile() const { |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 822 | return max_num_sites_ > 0u && |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 823 | homepage_client_ && // No platform-specific implementation - no tile. |
Yafei Duan | ddf0db0 | 2018-07-18 17:21:47 | [diff] [blame] | 824 | homepage_client_->IsHomepageTileEnabled() && |
Yafei Duan | 3797cf4 | 2018-07-14 01:03:58 | [diff] [blame] | 825 | !homepage_client_->GetHomepageUrl().is_empty() && |
| 826 | !(top_sites_ && |
| 827 | top_sites_->IsBlacklisted(homepage_client_->GetHomepageUrl())); |
| 828 | } |
| 829 | |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 830 | void MostVisitedSites::AddToHostsAndTotalCount(const NTPTilesVector& new_tiles, |
| 831 | std::set<std::string>* hosts, |
| 832 | size_t* total_tile_count) const { |
| 833 | for (const auto& tile : new_tiles) { |
| 834 | hosts->insert(tile.url.host()); |
| 835 | } |
| 836 | *total_tile_count += new_tiles.size(); |
kristipark | c019977 | 2018-07-18 05:25:42 | [diff] [blame] | 837 | DCHECK_LE(*total_tile_count, max_num_sites_); |
fhorschig | d322245 | 2017-05-22 08:36:44 | [diff] [blame] | 838 | } |
| 839 | |
sfiera | 08009fe | 2016-06-15 17:07:26 | [diff] [blame] | 840 | } // namespace ntp_tiles |