Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_status_service.cc

Issue 2519053002: 📰 Let the backend trigger sign in related refreshes (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 snippets_status_change_callback_.Run(snippets_status_, new_snippets_status); 80 snippets_status_change_callback_.Run(snippets_status_, new_snippets_status);
83 snippets_status_ = new_snippets_status; 81 snippets_status_ = new_snippets_status;
84 } 82 }
85 83
86 bool NTPSnippetsStatusService::IsSignedIn() const { 84 bool NTPSnippetsStatusService::IsSignedIn() const {
87 return signin_manager_ && signin_manager_->IsAuthenticated(); 85 return signin_manager_ && signin_manager_->IsAuthenticated();
Marc Treib 2016/11/21 15:48:53 Hm... a bit weird that this still needs the Signin
dgn 2016/11/21 16:22:08 there are other cases where it need to pull the in
Marc Treib 2016/11/21 16:52:30 Okay, fair enough. Could you add a TODO though?
88 } 86 }
89 87
90 void NTPSnippetsStatusService::GoogleSigninSucceeded( 88 void NTPSnippetsStatusService::OnSignInStateChanged() {
91 const std::string& account_id,
92 const std::string& username,
93 const std::string& password) {
94 OnStateChanged(GetSnippetsStatusFromDeps()); 89 OnStateChanged(GetSnippetsStatusFromDeps());
95 } 90 }
96 91
97 void NTPSnippetsStatusService::GoogleSignedOut(const std::string& account_id,
98 const std::string& username) {
99 OnStateChanged(GetSnippetsStatusFromDeps());
100 }
101
102 SnippetsStatus NTPSnippetsStatusService::GetSnippetsStatusFromDeps() const { 92 SnippetsStatus NTPSnippetsStatusService::GetSnippetsStatusFromDeps() const {
103 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) { 93 if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) {
104 DVLOG(1) << "[GetNewSnippetsStatus] Disabled via pref"; 94 DVLOG(1) << "[GetNewSnippetsStatus] Disabled via pref";
105 return SnippetsStatus::EXPLICITLY_DISABLED; 95 return SnippetsStatus::EXPLICITLY_DISABLED;
106 } 96 }
107 97
108 if (require_signin_ && !IsSignedIn()) { 98 if (require_signin_ && !IsSignedIn()) {
109 DVLOG(1) << "[GetNewSnippetsStatus] Signed out and disabled due to this."; 99 DVLOG(1) << "[GetNewSnippetsStatus] Signed out and disabled due to this.";
110 return SnippetsStatus::SIGNED_OUT_AND_DISABLED; 100 return SnippetsStatus::SIGNED_OUT_AND_DISABLED;
111 } 101 }
112 102
113 DVLOG(1) << "[GetNewSnippetsStatus] Enabled, signed " 103 DVLOG(1) << "[GetNewSnippetsStatus] Enabled, signed "
114 << (IsSignedIn() ? "in" : "out"); 104 << (IsSignedIn() ? "in" : "out");
115 return IsSignedIn() ? SnippetsStatus::ENABLED_AND_SIGNED_IN 105 return IsSignedIn() ? SnippetsStatus::ENABLED_AND_SIGNED_IN
116 : SnippetsStatus::ENABLED_AND_SIGNED_OUT; 106 : SnippetsStatus::ENABLED_AND_SIGNED_OUT;
117 } 107 }
118 108
119 } // namespace ntp_snippets 109 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698