| 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 history::HistoryService* history_service, | 27 history::HistoryService* history_service, |
| 27 PrefService* pref_service) | 28 PrefService* pref_service) |
| 28 : state_(state), | 29 : state_(state), |
| 30 signin_observer_(this), |
| 29 history_service_observer_(this), | 31 history_service_observer_(this), |
| 30 ntp_snippets_service_(nullptr), | 32 ntp_snippets_service_(nullptr), |
| 31 pref_service_(pref_service), | 33 pref_service_(pref_service), |
| 32 user_classifier_(pref_service) { | 34 user_classifier_(pref_service) { |
| 33 // Can be null in tests. | 35 // Can be null in tests. |
| 36 if (signin_manager) { |
| 37 signin_observer_.Add(signin_manager); |
| 38 } |
| 39 |
| 34 if (history_service) { | 40 if (history_service) { |
| 35 history_service_observer_.Add(history_service); | 41 history_service_observer_.Add(history_service); |
| 36 } | 42 } |
| 37 | 43 |
| 38 RestoreDismissedCategoriesFromPrefs(); | 44 RestoreDismissedCategoriesFromPrefs(); |
| 39 } | 45 } |
| 40 | 46 |
| 41 ContentSuggestionsService::~ContentSuggestionsService() = default; | 47 ContentSuggestionsService::~ContentSuggestionsService() = default; |
| 42 | 48 |
| 43 void ContentSuggestionsService::Shutdown() { | 49 void ContentSuggestionsService::Shutdown() { |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 284 |
| 279 void ContentSuggestionsService::OnSuggestionInvalidated( | 285 void ContentSuggestionsService::OnSuggestionInvalidated( |
| 280 ContentSuggestionsProvider* provider, | 286 ContentSuggestionsProvider* provider, |
| 281 const ContentSuggestion::ID& suggestion_id) { | 287 const ContentSuggestion::ID& suggestion_id) { |
| 282 RemoveSuggestionByID(suggestion_id); | 288 RemoveSuggestionByID(suggestion_id); |
| 283 for (Observer& observer : observers_) { | 289 for (Observer& observer : observers_) { |
| 284 observer.OnSuggestionInvalidated(suggestion_id); | 290 observer.OnSuggestionInvalidated(suggestion_id); |
| 285 } | 291 } |
| 286 } | 292 } |
| 287 | 293 |
| 294 // SigninManagerBase::Observer implementation |
| 295 void ContentSuggestionsService::GoogleSigninSucceeded( |
| 296 const std::string& account_id, |
| 297 const std::string& username, |
| 298 const std::string& password) { |
| 299 OnSignInStateChanged(); |
| 300 } |
| 301 |
| 302 void ContentSuggestionsService::GoogleSignedOut(const std::string& account_id, |
| 303 const std::string& username) { |
| 304 OnSignInStateChanged(); |
| 305 } |
| 306 |
| 288 // history::HistoryServiceObserver implementation. | 307 // history::HistoryServiceObserver implementation. |
| 289 void ContentSuggestionsService::OnURLsDeleted( | 308 void ContentSuggestionsService::OnURLsDeleted( |
| 290 history::HistoryService* history_service, | 309 history::HistoryService* history_service, |
| 291 bool all_history, | 310 bool all_history, |
| 292 bool expired, | 311 bool expired, |
| 293 const history::URLRows& deleted_rows, | 312 const history::URLRows& deleted_rows, |
| 294 const std::set<GURL>& favicon_urls) { | 313 const std::set<GURL>& favicon_urls) { |
| 295 // We don't care about expired entries. | 314 // We don't care about expired entries. |
| 296 if (expired) { | 315 if (expired) { |
| 297 return; | 316 return; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 427 |
| 409 return true; | 428 return true; |
| 410 } | 429 } |
| 411 | 430 |
| 412 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 431 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
| 413 for (Observer& observer : observers_) { | 432 for (Observer& observer : observers_) { |
| 414 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category)); | 433 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category)); |
| 415 } | 434 } |
| 416 } | 435 } |
| 417 | 436 |
| 437 void ContentSuggestionsService::OnSignInStateChanged() { |
| 438 // First notify the providers, so they can make the required changes. |
| 439 for (const auto& provider : providers_) { |
| 440 provider->OnSignInStateChanged(); |
| 441 } |
| 442 |
| 443 // Finally notify the observers so they refresh only after the backend is |
| 444 // ready. |
| 445 for (Observer& observer : observers_) { |
| 446 observer.OnFullRefreshRequired(); |
| 447 } |
| 448 } |
| 449 |
| 418 void ContentSuggestionsService::SortCategories() { | 450 void ContentSuggestionsService::SortCategories() { |
| 419 std::sort(categories_.begin(), categories_.end(), | 451 std::sort(categories_.begin(), categories_.end(), |
| 420 [this](const Category& left, const Category& right) { | 452 [this](const Category& left, const Category& right) { |
| 421 return category_factory_.CompareCategories(left, right); | 453 return category_factory_.CompareCategories(left, right); |
| 422 }); | 454 }); |
| 423 } | 455 } |
| 424 | 456 |
| 425 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { | 457 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
| 426 return base::ContainsKey(dismissed_providers_by_category_, category); | 458 return base::ContainsKey(dismissed_providers_by_category_, category); |
| 427 } | 459 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 495 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 464 base::ListValue list; | 496 base::ListValue list; |
| 465 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 497 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 466 list.AppendInteger(category_provider_pair.first.id()); | 498 list.AppendInteger(category_provider_pair.first.id()); |
| 467 } | 499 } |
| 468 | 500 |
| 469 pref_service_->Set(prefs::kDismissedCategories, list); | 501 pref_service_->Set(prefs::kDismissedCategories, list); |
| 470 } | 502 } |
| 471 | 503 |
| 472 } // namespace ntp_snippets | 504 } // namespace ntp_snippets |
| OLD | NEW |