| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_snippets/content_suggestions_service.h" | 5 #include "components/ntp_snippets/content_suggestions_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 observer.ContentSuggestionsServiceShutdown(); | 59 observer.ContentSuggestionsServiceShutdown(); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 // static | 63 // static |
| 64 void ContentSuggestionsService::RegisterProfilePrefs( | 64 void ContentSuggestionsService::RegisterProfilePrefs( |
| 65 PrefRegistrySimple* registry) { | 65 PrefRegistrySimple* registry) { |
| 66 registry->RegisterListPref(prefs::kDismissedCategories); | 66 registry->RegisterListPref(prefs::kDismissedCategories); |
| 67 } | 67 } |
| 68 | 68 |
| 69 std::vector<Category> ContentSuggestionsService::GetCategories() const { |
| 70 std::vector<Category> sorted_categories = categories_; |
| 71 std::sort(sorted_categories.begin(), sorted_categories.end(), |
| 72 [this](const Category& left, const Category& right) { |
| 73 return category_ranker_->Compare(left, right); |
| 74 }); |
| 75 return sorted_categories; |
| 76 } |
| 77 |
| 69 CategoryStatus ContentSuggestionsService::GetCategoryStatus( | 78 CategoryStatus ContentSuggestionsService::GetCategoryStatus( |
| 70 Category category) const { | 79 Category category) const { |
| 71 if (state_ == State::DISABLED) { | 80 if (state_ == State::DISABLED) { |
| 72 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; | 81 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; |
| 73 } | 82 } |
| 74 | 83 |
| 75 auto iterator = providers_by_category_.find(category); | 84 auto iterator = providers_by_category_.find(category); |
| 76 if (iterator == providers_by_category_.end()) { | 85 if (iterator == providers_by_category_.end()) { |
| 77 return CategoryStatus::NOT_PROVIDED; | 86 return CategoryStatus::NOT_PROVIDED; |
| 78 } | 87 } |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 } | 391 } |
| 383 | 392 |
| 384 void ContentSuggestionsService::RegisterCategory( | 393 void ContentSuggestionsService::RegisterCategory( |
| 385 Category category, | 394 Category category, |
| 386 ContentSuggestionsProvider* provider) { | 395 ContentSuggestionsProvider* provider) { |
| 387 DCHECK(!base::ContainsKey(providers_by_category_, category)); | 396 DCHECK(!base::ContainsKey(providers_by_category_, category)); |
| 388 DCHECK(!IsCategoryDismissed(category)); | 397 DCHECK(!IsCategoryDismissed(category)); |
| 389 | 398 |
| 390 providers_by_category_[category] = provider; | 399 providers_by_category_[category] = provider; |
| 391 categories_.push_back(category); | 400 categories_.push_back(category); |
| 392 SortCategories(); | |
| 393 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 401 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| 394 suggestions_by_category_.insert( | 402 suggestions_by_category_.insert( |
| 395 std::make_pair(category, std::vector<ContentSuggestion>())); | 403 std::make_pair(category, std::vector<ContentSuggestion>())); |
| 396 } | 404 } |
| 397 } | 405 } |
| 398 | 406 |
| 399 void ContentSuggestionsService::UnregisterCategory( | 407 void ContentSuggestionsService::UnregisterCategory( |
| 400 Category category, | 408 Category category, |
| 401 ContentSuggestionsProvider* provider) { | 409 ContentSuggestionsProvider* provider) { |
| 402 auto providers_it = providers_by_category_.find(category); | 410 auto providers_it = providers_by_category_.find(category); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 provider->OnSignInStateChanged(); | 449 provider->OnSignInStateChanged(); |
| 442 } | 450 } |
| 443 | 451 |
| 444 // Finally notify the observers so they refresh only after the backend is | 452 // Finally notify the observers so they refresh only after the backend is |
| 445 // ready. | 453 // ready. |
| 446 for (Observer& observer : observers_) { | 454 for (Observer& observer : observers_) { |
| 447 observer.OnFullRefreshRequired(); | 455 observer.OnFullRefreshRequired(); |
| 448 } | 456 } |
| 449 } | 457 } |
| 450 | 458 |
| 451 void ContentSuggestionsService::SortCategories() { | |
| 452 std::sort(categories_.begin(), categories_.end(), | |
| 453 [this](const Category& left, const Category& right) { | |
| 454 return category_ranker_->Compare(left, right); | |
| 455 }); | |
| 456 } | |
| 457 | |
| 458 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { | 459 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
| 459 return base::ContainsKey(dismissed_providers_by_category_, category); | 460 return base::ContainsKey(dismissed_providers_by_category_, category); |
| 460 } | 461 } |
| 461 | 462 |
| 462 void ContentSuggestionsService::RestoreDismissedCategory(Category category) { | 463 void ContentSuggestionsService::RestoreDismissedCategory(Category category) { |
| 463 auto dismissed_it = dismissed_providers_by_category_.find(category); | 464 auto dismissed_it = dismissed_providers_by_category_.find(category); |
| 464 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category)); | 465 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category)); |
| 465 | 466 |
| 466 // Keep the reference to the provider and remove it from the dismissed ones, | 467 // Keep the reference to the provider and remove it from the dismissed ones, |
| 467 // because the category registration enforces that it's not dismissed. | 468 // because the category registration enforces that it's not dismissed. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 495 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 496 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 496 base::ListValue list; | 497 base::ListValue list; |
| 497 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 498 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 498 list.AppendInteger(category_provider_pair.first.id()); | 499 list.AppendInteger(category_provider_pair.first.id()); |
| 499 } | 500 } |
| 500 | 501 |
| 501 pref_service_->Set(prefs::kDismissedCategories, list); | 502 pref_service_->Set(prefs::kDismissedCategories, list); |
| 502 } | 503 } |
| 503 | 504 |
| 504 } // namespace ntp_snippets | 505 } // namespace ntp_snippets |
| OLD | NEW |