| 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 "chrome/browser/android/ntp/most_visited_sites.h" | 5 #include "chrome/browser/android/ntp/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 ? base::StringPrintf(kHistogramServerFormat, | 156 ? base::StringPrintf(kHistogramServerFormat, |
| 157 suggestion.provider_index) | 157 suggestion.provider_index) |
| 158 : kHistogramServerName; | 158 : kHistogramServerName; |
| 159 } | 159 } |
| 160 NOTREACHED(); | 160 NOTREACHED(); |
| 161 return std::string(); | 161 return std::string(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace | 164 } // namespace |
| 165 | 165 |
| 166 SupervisedUserServiceSupervisor::SupervisedUserServiceSupervisor(Profile* profil
e) |
| 167 : profile_(profile) {} |
| 168 |
| 169 void SupervisedUserServiceSupervisor::AddObserver(Observer* observer) { |
| 170 DCHECK(observers_.find(observer) == observers_.end()); |
| 171 SupervisedUserService* supervised_user_service = |
| 172 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 173 std::unique_ptr<SUSObserver> ptr( |
| 174 new SUSObserver(supervised_user_service, observer)); |
| 175 observers_.emplace(observer, std::move(ptr)); |
| 176 } |
| 177 |
| 178 void SupervisedUserServiceSupervisor::RemoveObserver(Observer* observer) { |
| 179 auto it = observers_.find(observer); |
| 180 DCHECK(it != observers_.end()); |
| 181 observers_.erase(it); |
| 182 } |
| 183 |
| 184 SupervisedUserURLFilter* SupervisedUserServiceSupervisor::GetURLFilterForUIThrea
d() { |
| 185 SupervisedUserService* supervised_user_service = |
| 186 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 187 return supervised_user_service->GetURLFilterForUIThread(); |
| 188 } |
| 189 |
| 190 std::vector<Supervisor::Whitelist> |
| 191 SupervisedUserServiceSupervisor::whitelists() const { |
| 192 std::vector<Supervisor::Whitelist> results; |
| 193 SupervisedUserService* supervised_user_service = |
| 194 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 195 for (const auto& whitelist : supervised_user_service->whitelists()) { |
| 196 results.emplace_back(Whitelist{ |
| 197 whitelist->title(), |
| 198 whitelist->entry_point(), |
| 199 whitelist->large_icon_path(), |
| 200 }); |
| 201 } |
| 202 return results; |
| 203 } |
| 204 |
| 166 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} | 205 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} |
| 167 | 206 |
| 168 MostVisitedSites::Suggestion::~Suggestion() {} | 207 MostVisitedSites::Suggestion::~Suggestion() {} |
| 169 | 208 |
| 170 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; | 209 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; |
| 171 MostVisitedSites::Suggestion& | 210 MostVisitedSites::Suggestion& |
| 172 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; | 211 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; |
| 173 | 212 |
| 174 MostVisitedSites::MostVisitedSites( | 213 MostVisitedSites::MostVisitedSites( |
| 175 PrefService* prefs, | 214 PrefService* prefs, |
| 176 const TemplateURLService* template_url_service, | 215 const TemplateURLService* template_url_service, |
| 177 variations::VariationsService* variations_service, | 216 variations::VariationsService* variations_service, |
| 178 net::URLRequestContextGetter* download_context, | 217 net::URLRequestContextGetter* download_context, |
| 179 scoped_refptr<history::TopSites> top_sites, | 218 scoped_refptr<history::TopSites> top_sites, |
| 180 SuggestionsService* suggestions, | 219 SuggestionsService* suggestions, |
| 181 bool is_child_profile, | 220 bool is_child_profile, |
| 182 Profile* profile) | 221 Supervisor* supervisor) |
| 183 : profile_(profile), prefs_(prefs), | 222 : prefs_(prefs), template_url_service_(template_url_service), |
| 184 template_url_service_(template_url_service), | |
| 185 variations_service_(variations_service), | 223 variations_service_(variations_service), |
| 186 download_context_(download_context), top_sites_(top_sites), | 224 download_context_(download_context), top_sites_(top_sites), |
| 187 suggestions_service_(suggestions), is_child_profile_(is_child_profile), | 225 suggestions_service_(suggestions), supervisor_(supervisor), |
| 188 observer_(nullptr), num_sites_(0), received_most_visited_sites_(false), | 226 is_child_profile_(is_child_profile), observer_(nullptr), num_sites_(0), |
| 189 received_popular_sites_(false), recorded_uma_(false), | 227 received_most_visited_sites_(false), received_popular_sites_(false), |
| 190 scoped_observer_(this), mv_source_(SUGGESTIONS_SERVICE), | 228 recorded_uma_(false), scoped_observer_(this), |
| 191 weak_ptr_factory_(this) { | 229 mv_source_(SUGGESTIONS_SERVICE), weak_ptr_factory_(this) { |
| 192 SupervisedUserService* supervised_user_service = | 230 supervisor_->AddObserver(this); |
| 193 SupervisedUserServiceFactory::GetForProfile(profile_); | |
| 194 supervised_user_service->AddObserver(this); | |
| 195 } | 231 } |
| 196 | 232 |
| 197 MostVisitedSites::~MostVisitedSites() { | 233 MostVisitedSites::~MostVisitedSites() { |
| 198 SupervisedUserService* supervised_user_service = | 234 supervisor_->RemoveObserver(this); |
| 199 SupervisedUserServiceFactory::GetForProfile(profile_); | |
| 200 supervised_user_service->RemoveObserver(this); | |
| 201 } | 235 } |
| 202 | 236 |
| 203 void MostVisitedSites::SetMostVisitedURLsObserver( | 237 void MostVisitedSites::SetMostVisitedURLsObserver( |
| 204 MostVisitedSites::Observer* observer, int num_sites) { | 238 MostVisitedSites::Observer* observer, int num_sites) { |
| 205 DCHECK(observer); | 239 DCHECK(observer); |
| 206 observer_ = observer; | 240 observer_ = observer; |
| 207 num_sites_ = num_sites; | 241 num_sites_ = num_sites; |
| 208 | 242 |
| 209 if (ShouldShowPopularSites() && | 243 if (ShouldShowPopularSites() && |
| 210 NeedPopularSites(prefs_, num_sites_)) { | 244 NeedPopularSites(prefs_, num_sites_)) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 if (!top_sites_) | 407 if (!top_sites_) |
| 374 return; | 408 return; |
| 375 | 409 |
| 376 top_sites_->GetMostVisitedURLs( | 410 top_sites_->GetMostVisitedURLs( |
| 377 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, | 411 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
| 378 weak_ptr_factory_.GetWeakPtr()), | 412 weak_ptr_factory_.GetWeakPtr()), |
| 379 false); | 413 false); |
| 380 } | 414 } |
| 381 | 415 |
| 382 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { | 416 base::FilePath MostVisitedSites::GetWhitelistLargeIconPath(const GURL& url) { |
| 383 SupervisedUserService* supervised_user_service = | 417 for (const auto& whitelist : supervisor_->whitelists()) { |
| 384 SupervisedUserServiceFactory::GetForProfile(profile_); | 418 if (AreURLsEquivalent(whitelist.entry_point, url)) |
| 385 | 419 return whitelist.large_icon_path; |
| 386 for (const auto& whitelist : supervised_user_service->whitelists()) { | |
| 387 if (AreURLsEquivalent(whitelist->entry_point(), url)) | |
| 388 return whitelist->large_icon_path(); | |
| 389 } | 420 } |
| 390 return base::FilePath(); | 421 return base::FilePath(); |
| 391 } | 422 } |
| 392 | 423 |
| 393 void MostVisitedSites::OnMostVisitedURLsAvailable( | 424 void MostVisitedSites::OnMostVisitedURLsAvailable( |
| 394 const history::MostVisitedURLList& visited_list) { | 425 const history::MostVisitedURLList& visited_list) { |
| 395 SupervisedUserURLFilter* url_filter = | 426 SupervisedUserURLFilter* url_filter = supervisor_->GetURLFilterForUIThread(); |
| 396 SupervisedUserServiceFactory::GetForProfile(profile_) | |
| 397 ->GetURLFilterForUIThread(); | |
| 398 | 427 |
| 399 MostVisitedSites::SuggestionsPtrVector suggestions; | 428 MostVisitedSites::SuggestionsPtrVector suggestions; |
| 400 size_t num_tiles = | 429 size_t num_tiles = |
| 401 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); | 430 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); |
| 402 for (size_t i = 0; i < num_tiles; ++i) { | 431 for (size_t i = 0; i < num_tiles; ++i) { |
| 403 const history::MostVisitedURL& visited = visited_list[i]; | 432 const history::MostVisitedURL& visited = visited_list[i]; |
| 404 if (visited.url.is_empty()) { | 433 if (visited.url.is_empty()) { |
| 405 num_tiles = i; | 434 num_tiles = i; |
| 406 break; // This is the signal that there are no more real visited sites. | 435 break; // This is the signal that there are no more real visited sites. |
| 407 } | 436 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 429 const SuggestionsProfile& suggestions_profile) { | 458 const SuggestionsProfile& suggestions_profile) { |
| 430 int num_tiles = suggestions_profile.suggestions_size(); | 459 int num_tiles = suggestions_profile.suggestions_size(); |
| 431 // With no server suggestions, fall back to local TopSites. | 460 // With no server suggestions, fall back to local TopSites. |
| 432 if (num_tiles == 0) { | 461 if (num_tiles == 0) { |
| 433 InitiateTopSitesQuery(); | 462 InitiateTopSitesQuery(); |
| 434 return; | 463 return; |
| 435 } | 464 } |
| 436 if (num_sites_ < num_tiles) | 465 if (num_sites_ < num_tiles) |
| 437 num_tiles = num_sites_; | 466 num_tiles = num_sites_; |
| 438 | 467 |
| 439 SupervisedUserURLFilter* url_filter = | 468 SupervisedUserURLFilter* url_filter = supervisor_->GetURLFilterForUIThread(); |
| 440 SupervisedUserServiceFactory::GetForProfile(profile_) | |
| 441 ->GetURLFilterForUIThread(); | |
| 442 MostVisitedSites::SuggestionsPtrVector suggestions; | 469 MostVisitedSites::SuggestionsPtrVector suggestions; |
| 443 for (int i = 0; i < num_tiles; ++i) { | 470 for (int i = 0; i < num_tiles; ++i) { |
| 444 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 471 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 445 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == | 472 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == |
| 446 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 473 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 447 continue; | 474 continue; |
| 448 } | 475 } |
| 449 | 476 |
| 450 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); | 477 std::unique_ptr<Suggestion> generated_suggestion(new Suggestion()); |
| 451 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); | 478 generated_suggestion->title = base::UTF8ToUTF16(suggestion.title()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 467 | 494 |
| 468 MostVisitedSites::SuggestionsPtrVector | 495 MostVisitedSites::SuggestionsPtrVector |
| 469 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 496 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
| 470 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions) { | 497 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions) { |
| 471 size_t num_personal_suggestions = personal_suggestions.size(); | 498 size_t num_personal_suggestions = personal_suggestions.size(); |
| 472 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); | 499 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
| 473 | 500 |
| 474 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 501 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
| 475 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; | 502 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; |
| 476 | 503 |
| 477 SupervisedUserService* supervised_user_service = | |
| 478 SupervisedUserServiceFactory::GetForProfile(profile_); | |
| 479 SupervisedUserURLFilter* url_filter = | 504 SupervisedUserURLFilter* url_filter = |
| 480 supervised_user_service->GetURLFilterForUIThread(); | 505 supervisor_->GetURLFilterForUIThread(); |
| 481 | 506 |
| 482 std::set<std::string> personal_hosts; | 507 std::set<std::string> personal_hosts; |
| 483 for (const auto& suggestion : personal_suggestions) | 508 for (const auto& suggestion : personal_suggestions) |
| 484 personal_hosts.insert(suggestion->url.host()); | 509 personal_hosts.insert(suggestion->url.host()); |
| 485 | 510 |
| 486 for (const auto& whitelist : supervised_user_service->whitelists()) { | 511 for (const auto& whitelist : supervisor_->whitelists()) { |
| 487 // Skip blacklisted sites. | 512 // Skip blacklisted sites. |
| 488 if (top_sites_ && top_sites_->IsBlacklisted(whitelist->entry_point())) | 513 if (top_sites_ && top_sites_->IsBlacklisted(whitelist.entry_point)) |
| 489 continue; | 514 continue; |
| 490 | 515 |
| 491 // Skip suggestions already present. | 516 // Skip suggestions already present. |
| 492 if (personal_hosts.find(whitelist->entry_point().host()) != | 517 if (personal_hosts.find(whitelist.entry_point.host()) != |
| 493 personal_hosts.end()) | 518 personal_hosts.end()) |
| 494 continue; | 519 continue; |
| 495 | 520 |
| 496 // Skip whitelist entry points that are manually blocked. | 521 // Skip whitelist entry points that are manually blocked. |
| 497 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == | 522 if (url_filter->GetFilteringBehaviorForURL(whitelist.entry_point) == |
| 498 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 523 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 499 continue; | 524 continue; |
| 500 } | 525 } |
| 501 | 526 |
| 502 std::unique_ptr<Suggestion> suggestion(new Suggestion()); | 527 std::unique_ptr<Suggestion> suggestion(new Suggestion()); |
| 503 suggestion->title = whitelist->title(); | 528 suggestion->title = whitelist.title; |
| 504 suggestion->url = whitelist->entry_point(); | 529 suggestion->url = whitelist.entry_point; |
| 505 suggestion->source = WHITELIST; | 530 suggestion->source = WHITELIST; |
| 506 suggestion->whitelist_icon_path = whitelist->large_icon_path(); | 531 suggestion->whitelist_icon_path = whitelist.large_icon_path; |
| 507 | 532 |
| 508 whitelist_suggestions.push_back(std::move(suggestion)); | 533 whitelist_suggestions.push_back(std::move(suggestion)); |
| 509 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 534 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
| 510 break; | 535 break; |
| 511 } | 536 } |
| 512 | 537 |
| 513 return whitelist_suggestions; | 538 return whitelist_suggestions; |
| 514 } | 539 } |
| 515 | 540 |
| 516 MostVisitedSites::SuggestionsPtrVector | 541 MostVisitedSites::SuggestionsPtrVector |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 | 798 |
| 774 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 799 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 775 | 800 |
| 776 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 801 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 777 ChangeReason change_reason) { | 802 ChangeReason change_reason) { |
| 778 if (mv_source_ == TOP_SITES) { | 803 if (mv_source_ == TOP_SITES) { |
| 779 // The displayed suggestions are invalidated. | 804 // The displayed suggestions are invalidated. |
| 780 InitiateTopSitesQuery(); | 805 InitiateTopSitesQuery(); |
| 781 } | 806 } |
| 782 } | 807 } |
| OLD | NEW |