1 // Copyright 2014 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.
5 #include "components/signin/core/browser/signin_manager_base.h"
10 #include "base/command_line.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "components/signin/core/browser/signin_client.h"
17 #include "components/signin/core/common/signin_pref_names.h"
18 #include "components/signin/core/common/signin_switches.h"
19 #include "google_apis/gaia/gaia_auth_util.h"
20 #include "google_apis/gaia/gaia_constants.h"
21 #include "google_apis/gaia/gaia_urls.h"
23 using namespace signin_internals_util
;
25 SigninManagerBase::SigninManagerBase(SigninClient
* client
)
26 : client_(client
), initialized_(false), weak_pointer_factory_(this) {}
28 SigninManagerBase::~SigninManagerBase() {}
30 void SigninManagerBase::Initialize(PrefService
* local_state
) {
31 // Should never call Initialize() twice.
32 DCHECK(!IsInitialized());
35 // If the user is clearing the token service from the command line, then
36 // clear their login info also (not valid to be logged in without any
38 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
39 if (cmd_line
->HasSwitch(switches::kClearTokenService
))
40 client_
->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername
);
43 client_
->GetPrefs()->GetString(prefs::kGoogleServicesUsername
);
45 SetAuthenticatedUsername(user
);
48 bool SigninManagerBase::IsInitialized() const { return initialized_
; }
50 bool SigninManagerBase::IsSigninAllowed() const {
51 return client_
->GetPrefs()->GetBoolean(prefs::kSigninAllowed
);
54 const std::string
& SigninManagerBase::GetAuthenticatedUsername() const {
55 return authenticated_username_
;
58 const std::string
& SigninManagerBase::GetAuthenticatedAccountId() const {
59 return GetAuthenticatedUsername();
62 void SigninManagerBase::SetAuthenticatedUsername(const std::string
& username
) {
63 if (!authenticated_username_
.empty()) {
64 DLOG_IF(ERROR
, !gaia::AreEmailsSame(username
, authenticated_username_
))
65 << "Tried to change the authenticated username to something different: "
66 << "Current: " << authenticated_username_
<< ", New: " << username
;
69 // Prior to M26, chrome on iOS did not normalize the email before setting
70 // it in SigninManager. If the emails are the same as given by
71 // gaia::AreEmailsSame() but not the same as given by std::string::op==(),
72 // make sure to set the authenticated name below.
73 if (!gaia::AreEmailsSame(username
, authenticated_username_
) ||
74 username
== authenticated_username_
) {
81 std::string pref_username
=
82 client_
->GetPrefs()->GetString(prefs::kGoogleServicesUsername
);
83 DCHECK(pref_username
.empty() || gaia::AreEmailsSame(username
, pref_username
))
84 << "username: " << username
<< "; pref_username: " << pref_username
;
85 authenticated_username_
= username
;
86 client_
->GetPrefs()->SetString(prefs::kGoogleServicesUsername
, username
);
87 NotifyDiagnosticsObservers(USERNAME
, username
);
89 // Go ahead and update the last signed in username here as well. Once a
90 // user is signed in the two preferences should match. Doing it here as
91 // opposed to on signin allows us to catch the upgrade scenario.
92 client_
->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername
, username
);
95 void SigninManagerBase::clear_authenticated_username() {
96 authenticated_username_
.clear();
99 bool SigninManagerBase::IsAuthenticated() const {
100 return !GetAuthenticatedAccountId().empty();
103 bool SigninManagerBase::AuthInProgress() const {
104 // SigninManagerBase never kicks off auth processes itself.
108 void SigninManagerBase::Shutdown() {}
110 void SigninManagerBase::AddObserver(Observer
* observer
) {
111 observer_list_
.AddObserver(observer
);
114 void SigninManagerBase::RemoveObserver(Observer
* observer
) {
115 observer_list_
.RemoveObserver(observer
);
118 void SigninManagerBase::AddSigninDiagnosticsObserver(
119 SigninDiagnosticsObserver
* observer
) {
120 signin_diagnostics_observers_
.AddObserver(observer
);
123 void SigninManagerBase::RemoveSigninDiagnosticsObserver(
124 SigninDiagnosticsObserver
* observer
) {
125 signin_diagnostics_observers_
.RemoveObserver(observer
);
128 void SigninManagerBase::NotifyDiagnosticsObservers(
129 const UntimedSigninStatusField
& field
,
130 const std::string
& value
) {
131 FOR_EACH_OBSERVER(SigninDiagnosticsObserver
,
132 signin_diagnostics_observers_
,
133 NotifySigninValueChanged(field
, value
));
136 void SigninManagerBase::NotifyDiagnosticsObservers(
137 const TimedSigninStatusField
& field
,
138 const std::string
& value
) {
139 FOR_EACH_OBSERVER(SigninDiagnosticsObserver
,
140 signin_diagnostics_observers_
,
141 NotifySigninValueChanged(field
, value
));