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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/ntp_snippets/pref_names.h" | 17 #include "components/ntp_snippets/pref_names.h" |
18 #include "components/prefs/pref_registry_simple.h" | 18 #include "components/prefs/pref_registry_simple.h" |
19 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
20 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
21 | 21 |
22 namespace ntp_snippets { | 22 namespace ntp_snippets { |
23 | 23 |
24 ContentSuggestionsService::ContentSuggestionsService( | 24 ContentSuggestionsService::ContentSuggestionsService( |
25 State state, | 25 State state, |
26 SigninManagerBase* signin_manager, | 26 SigninManagerBase* signin_manager, |
27 history::HistoryService* history_service, | 27 history::HistoryService* history_service, |
28 PrefService* pref_service) | 28 PrefService* pref_service, |
| 29 std::unique_ptr<CategoryRanker> category_ranker) |
29 : state_(state), | 30 : state_(state), |
30 signin_observer_(this), | 31 signin_observer_(this), |
31 history_service_observer_(this), | 32 history_service_observer_(this), |
32 ntp_snippets_service_(nullptr), | 33 ntp_snippets_service_(nullptr), |
33 pref_service_(pref_service), | 34 pref_service_(pref_service), |
34 user_classifier_(pref_service) { | 35 user_classifier_(pref_service), |
| 36 category_ranker_(std::move(category_ranker)) { |
35 // Can be null in tests. | 37 // Can be null in tests. |
36 if (signin_manager) { | 38 if (signin_manager) { |
37 signin_observer_.Add(signin_manager); | 39 signin_observer_.Add(signin_manager); |
38 } | 40 } |
39 | 41 |
40 if (history_service) { | 42 if (history_service) { |
41 history_service_observer_.Add(history_service); | 43 history_service_observer_.Add(history_service); |
42 } | 44 } |
43 | 45 |
44 RestoreDismissedCategoriesFromPrefs(); | 46 RestoreDismissedCategoriesFromPrefs(); |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 // Finally notify the observers so they refresh only after the backend is | 444 // Finally notify the observers so they refresh only after the backend is |
443 // ready. | 445 // ready. |
444 for (Observer& observer : observers_) { | 446 for (Observer& observer : observers_) { |
445 observer.OnFullRefreshRequired(); | 447 observer.OnFullRefreshRequired(); |
446 } | 448 } |
447 } | 449 } |
448 | 450 |
449 void ContentSuggestionsService::SortCategories() { | 451 void ContentSuggestionsService::SortCategories() { |
450 std::sort(categories_.begin(), categories_.end(), | 452 std::sort(categories_.begin(), categories_.end(), |
451 [this](const Category& left, const Category& right) { | 453 [this](const Category& left, const Category& right) { |
452 return category_factory_.CompareCategories(left, right); | 454 return category_ranker_->Compare(left, right); |
453 }); | 455 }); |
454 } | 456 } |
455 | 457 |
456 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { | 458 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
457 return base::ContainsKey(dismissed_providers_by_category_, category); | 459 return base::ContainsKey(dismissed_providers_by_category_, category); |
458 } | 460 } |
459 | 461 |
460 void ContentSuggestionsService::RestoreDismissedCategory(Category category) { | 462 void ContentSuggestionsService::RestoreDismissedCategory(Category category) { |
461 auto dismissed_it = dismissed_providers_by_category_.find(category); | 463 auto dismissed_it = dismissed_providers_by_category_.find(category); |
462 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category)); | 464 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category)); |
(...skipping 16 matching lines...) Expand all Loading... |
479 const base::ListValue* list = | 481 const base::ListValue* list = |
480 pref_service_->GetList(prefs::kDismissedCategories); | 482 pref_service_->GetList(prefs::kDismissedCategories); |
481 for (const std::unique_ptr<base::Value>& entry : *list) { | 483 for (const std::unique_ptr<base::Value>& entry : *list) { |
482 int id = 0; | 484 int id = 0; |
483 if (!entry->GetAsInteger(&id)) { | 485 if (!entry->GetAsInteger(&id)) { |
484 DLOG(WARNING) << "Invalid category pref value: " << *entry; | 486 DLOG(WARNING) << "Invalid category pref value: " << *entry; |
485 continue; | 487 continue; |
486 } | 488 } |
487 | 489 |
488 // When the provider is registered, it will be stored in this map. | 490 // When the provider is registered, it will be stored in this map. |
489 dismissed_providers_by_category_[category_factory()->FromIDValue(id)] = | 491 dismissed_providers_by_category_[Category::FromIDValue(id)] = nullptr; |
490 nullptr; | |
491 } | 492 } |
492 } | 493 } |
493 | 494 |
494 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 495 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
495 base::ListValue list; | 496 base::ListValue list; |
496 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 497 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
497 list.AppendInteger(category_provider_pair.first.id()); | 498 list.AppendInteger(category_provider_pair.first.id()); |
498 } | 499 } |
499 | 500 |
500 pref_service_->Set(prefs::kDismissedCategories, list); | 501 pref_service_->Set(prefs::kDismissedCategories, list); |
501 } | 502 } |
502 | 503 |
503 } // namespace ntp_snippets | 504 } // namespace ntp_snippets |
OLD | NEW |