| 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 <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 num_sites_(0), | 191 num_sites_(0), |
| 192 received_most_visited_sites_(false), | 192 received_most_visited_sites_(false), |
| 193 received_popular_sites_(false), | 193 received_popular_sites_(false), |
| 194 recorded_uma_(false), | 194 recorded_uma_(false), |
| 195 scoped_observer_(this), | 195 scoped_observer_(this), |
| 196 mv_source_(SUGGESTIONS_SERVICE), | 196 mv_source_(SUGGESTIONS_SERVICE), |
| 197 blocking_pool_(std::move(blocking_pool)), | 197 blocking_pool_(std::move(blocking_pool)), |
| 198 blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior( | 198 blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior( |
| 199 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 199 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 200 weak_ptr_factory_(this) { | 200 weak_ptr_factory_(this) { |
| 201 DCHECK(top_sites_); | |
| 202 DCHECK(suggestions_service_); | 201 DCHECK(suggestions_service_); |
| 203 supervisor_->SetObserver(this); | 202 supervisor_->SetObserver(this); |
| 204 } | 203 } |
| 205 | 204 |
| 206 MostVisitedSites::~MostVisitedSites() { | 205 MostVisitedSites::~MostVisitedSites() { |
| 207 supervisor_->SetObserver(nullptr); | 206 supervisor_->SetObserver(nullptr); |
| 208 } | 207 } |
| 209 | 208 |
| 210 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, | 209 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, |
| 211 int num_sites) { | 210 int num_sites) { |
| 212 DCHECK(observer); | 211 DCHECK(observer); |
| 213 observer_ = observer; | 212 observer_ = observer; |
| 214 num_sites_ = num_sites; | 213 num_sites_ = num_sites; |
| 215 | 214 |
| 216 if (ShouldShowPopularSites() && | 215 if (ShouldShowPopularSites() && |
| 217 NeedPopularSites(prefs_, num_sites_)) { | 216 NeedPopularSites(prefs_, num_sites_)) { |
| 218 popular_sites_.reset(new PopularSites( | 217 popular_sites_.reset(new PopularSites( |
| 219 blocking_pool_, prefs_, template_url_service_, variations_service_, | 218 blocking_pool_, prefs_, template_url_service_, variations_service_, |
| 220 download_context_, popular_sites_directory_, GetPopularSitesCountry(), | 219 download_context_, popular_sites_directory_, GetPopularSitesCountry(), |
| 221 GetPopularSitesVersion(), false, | 220 GetPopularSitesVersion(), false, |
| 222 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 221 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| 223 base::Unretained(this)))); | 222 base::Unretained(this)))); |
| 224 } else { | 223 } else { |
| 225 received_popular_sites_ = true; | 224 received_popular_sites_ = true; |
| 226 } | 225 } |
| 227 | 226 |
| 228 // TopSites updates itself after a delay. To ensure up-to-date results, | 227 if (top_sites_) { |
| 229 // force an update now. | 228 // TopSites updates itself after a delay. To ensure up-to-date results, |
| 230 top_sites_->SyncWithHistory(); | 229 // force an update now. |
| 230 top_sites_->SyncWithHistory(); |
| 231 | 231 |
| 232 // Register as TopSitesObserver so that we can update ourselves when the | 232 // Register as TopSitesObserver so that we can update ourselves when the |
| 233 // TopSites changes. | 233 // TopSites changes. |
| 234 scoped_observer_.Add(top_sites_.get()); | 234 scoped_observer_.Add(top_sites_.get()); |
| 235 } |
| 235 | 236 |
| 236 suggestions_subscription_ = suggestions_service_->AddCallback( | 237 suggestions_subscription_ = suggestions_service_->AddCallback( |
| 237 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, | 238 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, |
| 238 base::Unretained(this))); | 239 base::Unretained(this))); |
| 239 | 240 |
| 240 // Immediately build the current suggestions, getting personal suggestions | 241 // Immediately build the current suggestions, getting personal suggestions |
| 241 // from the SuggestionsService's cache or, if that is empty, from TopSites. | 242 // from the SuggestionsService's cache or, if that is empty, from TopSites. |
| 242 BuildCurrentSuggestions(); | 243 BuildCurrentSuggestions(); |
| 243 // Also start a request for fresh suggestions. | 244 // Also start a request for fresh suggestions. |
| 244 suggestions_service_->FetchSuggestionsData(); | 245 suggestions_service_->FetchSuggestionsData(); |
| 245 } | 246 } |
| 246 | 247 |
| 247 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, | 248 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, |
| 248 bool add_url) { | 249 bool add_url) { |
| 249 // Always blacklist in the local TopSites. | 250 if (top_sites_) { |
| 250 if (add_url) | 251 // Always blacklist in the local TopSites. |
| 251 top_sites_->AddBlacklistedURL(url); | 252 if (add_url) |
| 252 else | 253 top_sites_->AddBlacklistedURL(url); |
| 253 top_sites_->RemoveBlacklistedURL(url); | 254 else |
| 255 top_sites_->RemoveBlacklistedURL(url); |
| 256 } |
| 254 | 257 |
| 255 // Only blacklist in the server-side suggestions service if it's active. | 258 // Only blacklist in the server-side suggestions service if it's active. |
| 256 if (mv_source_ == SUGGESTIONS_SERVICE) { | 259 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 257 if (add_url) | 260 if (add_url) |
| 258 suggestions_service_->BlacklistURL(url); | 261 suggestions_service_->BlacklistURL(url); |
| 259 else | 262 else |
| 260 suggestions_service_->UndoBlacklistURL(url); | 263 suggestions_service_->UndoBlacklistURL(url); |
| 261 } | 264 } |
| 262 } | 265 } |
| 263 | 266 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } | 316 } |
| 314 | 317 |
| 315 void MostVisitedSites::BuildCurrentSuggestions() { | 318 void MostVisitedSites::BuildCurrentSuggestions() { |
| 316 // Get the current suggestions from cache. If the cache is empty, this will | 319 // Get the current suggestions from cache. If the cache is empty, this will |
| 317 // fall back to TopSites. | 320 // fall back to TopSites. |
| 318 OnSuggestionsProfileAvailable( | 321 OnSuggestionsProfileAvailable( |
| 319 suggestions_service_->GetSuggestionsDataFromCache()); | 322 suggestions_service_->GetSuggestionsDataFromCache()); |
| 320 } | 323 } |
| 321 | 324 |
| 322 void MostVisitedSites::InitiateTopSitesQuery() { | 325 void MostVisitedSites::InitiateTopSitesQuery() { |
| 326 if (!top_sites_) |
| 327 return; |
| 323 top_sites_->GetMostVisitedURLs( | 328 top_sites_->GetMostVisitedURLs( |
| 324 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, | 329 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
| 325 weak_ptr_factory_.GetWeakPtr()), | 330 weak_ptr_factory_.GetWeakPtr()), |
| 326 false); | 331 false); |
| 327 } | 332 } |
| 328 | 333 |
| 329 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { | 334 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { |
| 330 for (const auto& whitelist : supervisor_->whitelists()) { | 335 for (const auto& whitelist : supervisor_->whitelists()) { |
| 331 if (AreURLsEquivalent(whitelist.entry_point, url)) | 336 if (AreURLsEquivalent(whitelist.entry_point, url)) |
| 332 return whitelist.large_icon_path; | 337 return whitelist.large_icon_path; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 | 412 |
| 408 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 413 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
| 409 SuggestionsVector whitelist_suggestions; | 414 SuggestionsVector whitelist_suggestions; |
| 410 | 415 |
| 411 std::set<std::string> personal_hosts; | 416 std::set<std::string> personal_hosts; |
| 412 for (const auto& suggestion : personal_suggestions) | 417 for (const auto& suggestion : personal_suggestions) |
| 413 personal_hosts.insert(suggestion.url.host()); | 418 personal_hosts.insert(suggestion.url.host()); |
| 414 | 419 |
| 415 for (const auto& whitelist : supervisor_->whitelists()) { | 420 for (const auto& whitelist : supervisor_->whitelists()) { |
| 416 // Skip blacklisted sites. | 421 // Skip blacklisted sites. |
| 417 if (top_sites_->IsBlacklisted(whitelist.entry_point)) | 422 if (top_sites_ && top_sites_->IsBlacklisted(whitelist.entry_point)) |
| 418 continue; | 423 continue; |
| 419 | 424 |
| 420 // Skip suggestions already present. | 425 // Skip suggestions already present. |
| 421 if (personal_hosts.find(whitelist.entry_point.host()) != | 426 if (personal_hosts.find(whitelist.entry_point.host()) != |
| 422 personal_hosts.end()) | 427 personal_hosts.end()) |
| 423 continue; | 428 continue; |
| 424 | 429 |
| 425 // Skip whitelist entry points that are manually blocked. | 430 // Skip whitelist entry points that are manually blocked. |
| 426 if (supervisor_->IsBlocked(whitelist.entry_point)) | 431 if (supervisor_->IsBlocked(whitelist.entry_point)) |
| 427 continue; | 432 continue; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 458 SuggestionsVector popular_sites_suggestions; | 463 SuggestionsVector popular_sites_suggestions; |
| 459 | 464 |
| 460 if (num_popular_sites_suggestions > 0 && popular_sites_) { | 465 if (num_popular_sites_suggestions > 0 && popular_sites_) { |
| 461 std::set<std::string> hosts; | 466 std::set<std::string> hosts; |
| 462 for (const auto& suggestion : personal_suggestions) | 467 for (const auto& suggestion : personal_suggestions) |
| 463 hosts.insert(suggestion.url.host()); | 468 hosts.insert(suggestion.url.host()); |
| 464 for (const auto& suggestion : whitelist_suggestions) | 469 for (const auto& suggestion : whitelist_suggestions) |
| 465 hosts.insert(suggestion.url.host()); | 470 hosts.insert(suggestion.url.host()); |
| 466 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 471 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
| 467 // Skip blacklisted sites. | 472 // Skip blacklisted sites. |
| 468 if (top_sites_->IsBlacklisted(popular_site.url)) | 473 if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url)) |
| 469 continue; | 474 continue; |
| 470 std::string host = popular_site.url.host(); | 475 std::string host = popular_site.url.host(); |
| 471 // Skip suggestions already present in personal or whitelists. | 476 // Skip suggestions already present in personal or whitelists. |
| 472 if (hosts.find(host) != hosts.end()) | 477 if (hosts.find(host) != hosts.end()) |
| 473 continue; | 478 continue; |
| 474 | 479 |
| 475 Suggestion suggestion; | 480 Suggestion suggestion; |
| 476 suggestion.title = popular_site.title; | 481 suggestion.title = popular_site.title; |
| 477 suggestion.url = GURL(popular_site.url); | 482 suggestion.url = GURL(popular_site.url); |
| 478 suggestion.source = POPULAR; | 483 suggestion.source = POPULAR; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 | 579 |
| 575 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 580 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 576 ChangeReason change_reason) { | 581 ChangeReason change_reason) { |
| 577 if (mv_source_ == TOP_SITES) { | 582 if (mv_source_ == TOP_SITES) { |
| 578 // The displayed suggestions are invalidated. | 583 // The displayed suggestions are invalidated. |
| 579 InitiateTopSitesQuery(); | 584 InitiateTopSitesQuery(); |
| 580 } | 585 } |
| 581 } | 586 } |
| 582 | 587 |
| 583 } // namespace ntp_tiles | 588 } // namespace ntp_tiles |
| OLD | NEW |