Chromium Code Reviews| 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 RestoreDismissedCategoriesFromPrefs(); | 43 RestoreDismissedCategoriesFromPrefs(); |
| 38 } | 44 } |
| 39 | 45 |
| 40 ContentSuggestionsService::~ContentSuggestionsService() = default; | 46 ContentSuggestionsService::~ContentSuggestionsService() = default; |
| 41 | 47 |
| 42 void ContentSuggestionsService::Shutdown() { | 48 void ContentSuggestionsService::Shutdown() { |
| 43 ntp_snippets_service_ = nullptr; | 49 ntp_snippets_service_ = nullptr; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 } | 268 } |
| 263 | 269 |
| 264 void ContentSuggestionsService::OnSuggestionInvalidated( | 270 void ContentSuggestionsService::OnSuggestionInvalidated( |
| 265 ContentSuggestionsProvider* provider, | 271 ContentSuggestionsProvider* provider, |
| 266 const ContentSuggestion::ID& suggestion_id) { | 272 const ContentSuggestion::ID& suggestion_id) { |
| 267 RemoveSuggestionByID(suggestion_id); | 273 RemoveSuggestionByID(suggestion_id); |
| 268 for (Observer& observer : observers_) | 274 for (Observer& observer : observers_) |
| 269 observer.OnSuggestionInvalidated(suggestion_id); | 275 observer.OnSuggestionInvalidated(suggestion_id); |
| 270 } | 276 } |
| 271 | 277 |
| 278 // SigninManagerBase::Observer implementation | |
| 279 void ContentSuggestionsService::GoogleSigninSucceeded( | |
| 280 const std::string& account_id, | |
| 281 const std::string& username, | |
| 282 const std::string& password) { | |
| 283 OnSignInStateChanged(); | |
| 284 }; | |
|
Bernhard Bauer
2016/11/21 15:48:15
Nit: empty line between method bodies.
dgn
2016/11/21 16:22:08
Done.
| |
| 285 void ContentSuggestionsService::GoogleSignedOut(const std::string& account_id, | |
|
Marc Treib
2016/11/21 15:48:53
nit: empty line before
dgn
2016/11/21 16:22:08
Done.
| |
| 286 const std::string& username) { | |
| 287 OnSignInStateChanged(); | |
| 288 } | |
| 289 | |
| 272 // history::HistoryServiceObserver implementation. | 290 // history::HistoryServiceObserver implementation. |
| 273 void ContentSuggestionsService::OnURLsDeleted( | 291 void ContentSuggestionsService::OnURLsDeleted( |
| 274 history::HistoryService* history_service, | 292 history::HistoryService* history_service, |
| 275 bool all_history, | 293 bool all_history, |
| 276 bool expired, | 294 bool expired, |
| 277 const history::URLRows& deleted_rows, | 295 const history::URLRows& deleted_rows, |
| 278 const std::set<GURL>& favicon_urls) { | 296 const std::set<GURL>& favicon_urls) { |
| 279 // We don't care about expired entries. | 297 // We don't care about expired entries. |
| 280 if (expired) | 298 if (expired) |
| 281 return; | 299 return; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 suggestions->erase(position); | 404 suggestions->erase(position); |
| 387 | 405 |
| 388 return true; | 406 return true; |
| 389 } | 407 } |
| 390 | 408 |
| 391 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 409 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
| 392 for (Observer& observer : observers_) | 410 for (Observer& observer : observers_) |
| 393 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category)); | 411 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category)); |
| 394 } | 412 } |
| 395 | 413 |
| 414 void ContentSuggestionsService::OnSignInStateChanged() { | |
| 415 // First notify the providers, so they can make the required changes. | |
| 416 for (const auto& provider : providers_) { | |
| 417 provider->OnSignInStateChanged(); | |
| 418 } | |
| 419 | |
| 420 // Finally notify the observers so they refresh only after the backend is | |
| 421 // ready. | |
| 422 for (Observer& observer : observers_) { | |
| 423 observer.OnFullRefreshRequired(); | |
| 424 } | |
| 425 } | |
| 426 | |
| 396 void ContentSuggestionsService::SortCategories() { | 427 void ContentSuggestionsService::SortCategories() { |
| 397 std::sort(categories_.begin(), categories_.end(), | 428 std::sort(categories_.begin(), categories_.end(), |
| 398 [this](const Category& left, const Category& right) { | 429 [this](const Category& left, const Category& right) { |
| 399 return category_factory_.CompareCategories(left, right); | 430 return category_factory_.CompareCategories(left, right); |
| 400 }); | 431 }); |
| 401 } | 432 } |
| 402 | 433 |
| 403 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { | 434 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
| 404 return base::ContainsKey(dismissed_providers_by_category_, category); | 435 return base::ContainsKey(dismissed_providers_by_category_, category); |
| 405 } | 436 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 471 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 441 base::ListValue list; | 472 base::ListValue list; |
| 442 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 473 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 443 list.AppendInteger(category_provider_pair.first.id()); | 474 list.AppendInteger(category_provider_pair.first.id()); |
| 444 } | 475 } |
| 445 | 476 |
| 446 pref_service_->Set(prefs::kDismissedCategories, list); | 477 pref_service_->Set(prefs::kDismissedCategories, list); |
| 447 } | 478 } |
| 448 | 479 |
| 449 } // namespace ntp_snippets | 480 } // namespace ntp_snippets |
| OLD | NEW |