remote_suggestions_status_service_impl.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/ntp_snippets/remote/remote_suggestions_status_service_impl.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/feature_list.h"
  8. #include "base/logging.h"
  9. #include "components/feed/core/shared_prefs/pref_names.h"
  10. #include "components/ntp_snippets/content_suggestions_metrics.h"
  11. #include "components/ntp_snippets/features.h"
  12. #include "components/ntp_snippets/pref_names.h"
  13. #include "components/prefs/pref_registry_simple.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "components/variations/variations_associated_data.h"
  16. namespace ntp_snippets {
  17. RemoteSuggestionsStatusServiceImpl::RemoteSuggestionsStatusServiceImpl(
  18. bool is_signed_in,
  19. PrefService* pref_service,
  20. const std::vector<std::string>& additional_toggle_prefs)
  21. : status_(RemoteSuggestionsStatus::EXPLICITLY_DISABLED),
  22. additional_toggle_prefs_(additional_toggle_prefs),
  23. is_signed_in_(is_signed_in),
  24. list_visible_during_session_(true),
  25. pref_service_(pref_service) {
  26. ntp_snippets::metrics::RecordRemoteSuggestionsProviderState(
  27. !IsExplicitlyDisabled());
  28. }
  29. RemoteSuggestionsStatusServiceImpl::~RemoteSuggestionsStatusServiceImpl() =
  30. default;
  31. void RemoteSuggestionsStatusServiceImpl::Init(
  32. const StatusChangeCallback& callback) {
  33. DCHECK(status_change_callback_.is_null());
  34. status_change_callback_ = callback;
  35. list_visible_during_session_ =
  36. pref_service_->GetBoolean(feed::prefs::kArticlesListVisible);
  37. // Notify about the current state before registering the observer, to make
  38. // sure we don't get a double notification due to an undefined start state.
  39. RemoteSuggestionsStatus old_status = status_;
  40. status_ = GetStatusFromDeps();
  41. status_change_callback_.Run(old_status, status_);
  42. pref_change_registrar_.Init(pref_service_);
  43. pref_change_registrar_.Add(
  44. feed::prefs::kEnableSnippets,
  45. base::BindRepeating(
  46. &RemoteSuggestionsStatusServiceImpl::OnSnippetsEnabledChanged,
  47. base::Unretained(this)));
  48. pref_change_registrar_.Add(
  49. feed::prefs::kArticlesListVisible,
  50. base::BindRepeating(
  51. &RemoteSuggestionsStatusServiceImpl::OnListVisibilityChanged,
  52. base::Unretained(this)));
  53. for (const std::string& additional_toggle_pref : additional_toggle_prefs_) {
  54. pref_change_registrar_.Add(
  55. additional_toggle_pref,
  56. base::BindRepeating(
  57. &RemoteSuggestionsStatusServiceImpl::OnSnippetsEnabledChanged,
  58. base::Unretained(this)));
  59. }
  60. }
  61. void RemoteSuggestionsStatusServiceImpl::OnSnippetsEnabledChanged() {
  62. OnStateChanged(GetStatusFromDeps());
  63. }
  64. void RemoteSuggestionsStatusServiceImpl::OnStateChanged(
  65. RemoteSuggestionsStatus new_status) {
  66. if (new_status == status_) {
  67. return;
  68. }
  69. status_change_callback_.Run(status_, new_status);
  70. status_ = new_status;
  71. }
  72. bool RemoteSuggestionsStatusServiceImpl::IsSignedIn() const {
  73. return is_signed_in_;
  74. }
  75. void RemoteSuggestionsStatusServiceImpl::OnSignInStateChanged(
  76. bool has_signed_in) {
  77. is_signed_in_ = has_signed_in;
  78. OnStateChanged(GetStatusFromDeps());
  79. }
  80. void RemoteSuggestionsStatusServiceImpl::OnListVisibilityChanged() {
  81. if (pref_service_->GetBoolean(feed::prefs::kArticlesListVisible)) {
  82. list_visible_during_session_ = true;
  83. }
  84. OnStateChanged(GetStatusFromDeps());
  85. }
  86. bool RemoteSuggestionsStatusServiceImpl::IsExplicitlyDisabled() const {
  87. if (!pref_service_->GetBoolean(feed::prefs::kEnableSnippets)) {
  88. DVLOG(1) << "[GetStatusFromDeps] Disabled via pref.";
  89. return true;
  90. }
  91. if (!list_visible_during_session_) {
  92. DVLOG(1) << "[GetStatusFromDeps] Disabled because articles list hidden.";
  93. return true;
  94. }
  95. // |additional_toggle_prefs_| will always be empty on Android.
  96. for (const std::string& additional_toggle_pref : additional_toggle_prefs_) {
  97. if (!pref_service_->GetBoolean(additional_toggle_pref)) {
  98. DVLOG(1) << "[GetStatusFromDeps] Disabled via additional pref";
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. RemoteSuggestionsStatus RemoteSuggestionsStatusServiceImpl::GetStatusFromDeps()
  105. const {
  106. if (IsExplicitlyDisabled()) {
  107. return RemoteSuggestionsStatus::EXPLICITLY_DISABLED;
  108. }
  109. DVLOG(1) << "[GetStatusFromDeps] Enabled, signed "
  110. << (IsSignedIn() ? "in" : "out");
  111. return IsSignedIn() ? RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN
  112. : RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT;
  113. }
  114. } // namespace ntp_snippets