| 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/remote/ntp_snippets_status_service.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_status_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "components/ntp_snippets/features.h" | 9 #include "components/ntp_snippets/features.h" |
| 10 #include "components/ntp_snippets/pref_names.h" | 10 #include "components/ntp_snippets/pref_names.h" |
| 11 #include "components/prefs/pref_registry_simple.h" | 11 #include "components/prefs/pref_registry_simple.h" |
| 12 #include "components/prefs/pref_service.h" | 12 #include "components/prefs/pref_service.h" |
| 13 #include "components/signin/core/browser/signin_manager.h" |
| 13 #include "components/variations/variations_associated_data.h" | 14 #include "components/variations/variations_associated_data.h" |
| 14 | 15 |
| 15 namespace ntp_snippets { | 16 namespace ntp_snippets { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kFetchingRequiresSignin[] = "fetching_requires_signin"; | 20 const char kFetchingRequiresSignin[] = "fetching_requires_signin"; |
| 20 const char kFetchingRequiresSigninEnabled[] = "true"; | 21 const char kFetchingRequiresSigninEnabled[] = "true"; |
| 21 const char kFetchingRequiresSigninDisabled[] = "false"; | 22 const char kFetchingRequiresSigninDisabled[] = "false"; |
| 22 | 23 |
| 23 } // namespace | 24 } // namespace |
| 24 | 25 |
| 25 NTPSnippetsStatusService::NTPSnippetsStatusService( | 26 NTPSnippetsStatusService::NTPSnippetsStatusService( |
| 26 SigninManagerBase* signin_manager, | 27 SigninManagerBase* signin_manager, |
| 27 PrefService* pref_service) | 28 PrefService* pref_service) |
| 28 : snippets_status_(SnippetsStatus::EXPLICITLY_DISABLED), | 29 : snippets_status_(SnippetsStatus::EXPLICITLY_DISABLED), |
| 29 require_signin_(false), | 30 require_signin_(false), |
| 30 signin_manager_(signin_manager), | 31 signin_manager_(signin_manager), |
| 31 pref_service_(pref_service), | 32 pref_service_(pref_service) { |
| 32 signin_observer_(this) { | |
| 33 std::string param_value_str = variations::GetVariationParamValueByFeature( | 33 std::string param_value_str = variations::GetVariationParamValueByFeature( |
| 34 kArticleSuggestionsFeature, kFetchingRequiresSignin); | 34 kArticleSuggestionsFeature, kFetchingRequiresSignin); |
| 35 if (param_value_str == kFetchingRequiresSigninEnabled) { | 35 if (param_value_str == kFetchingRequiresSigninEnabled) { |
| 36 require_signin_ = true; | 36 require_signin_ = true; |
| 37 } else if (!param_value_str.empty() && | 37 } else if (!param_value_str.empty() && |
| 38 param_value_str != kFetchingRequiresSigninDisabled) { | 38 param_value_str != kFetchingRequiresSigninDisabled) { |
| 39 DLOG(WARNING) << "Unknow value for the variations parameter " | 39 DLOG(WARNING) << "Unknow value for the variations parameter " |
| 40 << kFetchingRequiresSignin << ": " << param_value_str; | 40 << kFetchingRequiresSignin << ": " << param_value_str; |
| 41 } | 41 } |
| 42 } | 42 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 DCHECK(snippets_status_change_callback_.is_null()); | 54 DCHECK(snippets_status_change_callback_.is_null()); |
| 55 | 55 |
| 56 snippets_status_change_callback_ = callback; | 56 snippets_status_change_callback_ = callback; |
| 57 | 57 |
| 58 // Notify about the current state before registering the observer, to make | 58 // Notify about the current state before registering the observer, to make |
| 59 // sure we don't get a double notification due to an undefined start state. | 59 // sure we don't get a double notification due to an undefined start state. |
| 60 SnippetsStatus old_snippets_status = snippets_status_; | 60 SnippetsStatus old_snippets_status = snippets_status_; |
| 61 snippets_status_ = GetSnippetsStatusFromDeps(); | 61 snippets_status_ = GetSnippetsStatusFromDeps(); |
| 62 snippets_status_change_callback_.Run(old_snippets_status, snippets_status_); | 62 snippets_status_change_callback_.Run(old_snippets_status, snippets_status_); |
| 63 | 63 |
| 64 signin_observer_.Add(signin_manager_); | |
| 65 | |
| 66 pref_change_registrar_.Init(pref_service_); | 64 pref_change_registrar_.Init(pref_service_); |
| 67 pref_change_registrar_.Add( | 65 pref_change_registrar_.Add( |
| 68 prefs::kEnableSnippets, | 66 prefs::kEnableSnippets, |
| 69 base::Bind(&NTPSnippetsStatusService::OnSnippetsEnabledChanged, | 67 base::Bind(&NTPSnippetsStatusService::OnSnippetsEnabledChanged, |
| 70 base::Unretained(this))); | 68 base::Unretained(this))); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void NTPSnippetsStatusService::OnSnippetsEnabledChanged() { | 71 void NTPSnippetsStatusService::OnSnippetsEnabledChanged() { |
| 74 OnStateChanged(GetSnippetsStatusFromDeps()); | 72 OnStateChanged(GetSnippetsStatusFromDeps()); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void NTPSnippetsStatusService::OnStateChanged( | 75 void NTPSnippetsStatusService::OnStateChanged( |
| 78 SnippetsStatus new_snippets_status) { | 76 SnippetsStatus new_snippets_status) { |
| 79 if (new_snippets_status == snippets_status_) { | 77 if (new_snippets_status == snippets_status_) { |
| 80 return; | 78 return; |
| 81 } | 79 } |
| 82 | 80 |
| 83 snippets_status_change_callback_.Run(snippets_status_, new_snippets_status); | 81 snippets_status_change_callback_.Run(snippets_status_, new_snippets_status); |
| 84 snippets_status_ = new_snippets_status; | 82 snippets_status_ = new_snippets_status; |
| 85 } | 83 } |
| 86 | 84 |
| 87 bool NTPSnippetsStatusService::IsSignedIn() const { | 85 bool NTPSnippetsStatusService::IsSignedIn() const { |
| 86 // TODO(dgn): remove the SigninManager dependency. It should be possible to |
| 87 // replace it by passing the new state via OnSignInStateChanged(). |
| 88 return signin_manager_ && signin_manager_->IsAuthenticated(); | 88 return signin_manager_ && signin_manager_->IsAuthenticated(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void NTPSnippetsStatusService::GoogleSigninSucceeded( | 91 void NTPSnippetsStatusService::OnSignInStateChanged() { |
| 92 const std::string& account_id, | |
| 93 const std::string& username, | |
| 94 const std::string& password) { | |
| 95 OnStateChanged(GetSnippetsStatusFromDeps()); | 92 OnStateChanged(GetSnippetsStatusFromDeps()); |
| 96 } | 93 } |
| 97 | 94 |
| 98 void NTPSnippetsStatusService::GoogleSignedOut(const std::string& account_id, | |
| 99 const std::string& username) { | |
| 100 OnStateChanged(GetSnippetsStatusFromDeps()); | |
| 101 } | |
| 102 | |
| 103 SnippetsStatus NTPSnippetsStatusService::GetSnippetsStatusFromDeps() const { | 95 SnippetsStatus NTPSnippetsStatusService::GetSnippetsStatusFromDeps() const { |
| 104 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) { | 96 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) { |
| 105 DVLOG(1) << "[GetNewSnippetsStatus] Disabled via pref"; | 97 DVLOG(1) << "[GetNewSnippetsStatus] Disabled via pref"; |
| 106 return SnippetsStatus::EXPLICITLY_DISABLED; | 98 return SnippetsStatus::EXPLICITLY_DISABLED; |
| 107 } | 99 } |
| 108 | 100 |
| 109 if (require_signin_ && !IsSignedIn()) { | 101 if (require_signin_ && !IsSignedIn()) { |
| 110 DVLOG(1) << "[GetNewSnippetsStatus] Signed out and disabled due to this."; | 102 DVLOG(1) << "[GetNewSnippetsStatus] Signed out and disabled due to this."; |
| 111 return SnippetsStatus::SIGNED_OUT_AND_DISABLED; | 103 return SnippetsStatus::SIGNED_OUT_AND_DISABLED; |
| 112 } | 104 } |
| 113 | 105 |
| 114 DVLOG(1) << "[GetNewSnippetsStatus] Enabled, signed " | 106 DVLOG(1) << "[GetNewSnippetsStatus] Enabled, signed " |
| 115 << (IsSignedIn() ? "in" : "out"); | 107 << (IsSignedIn() ? "in" : "out"); |
| 116 return IsSignedIn() ? SnippetsStatus::ENABLED_AND_SIGNED_IN | 108 return IsSignedIn() ? SnippetsStatus::ENABLED_AND_SIGNED_IN |
| 117 : SnippetsStatus::ENABLED_AND_SIGNED_OUT; | 109 : SnippetsStatus::ENABLED_AND_SIGNED_OUT; |
| 118 } | 110 } |
| 119 | 111 |
| 120 } // namespace ntp_snippets | 112 } // namespace ntp_snippets |
| OLD | NEW |