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/account_tracker_service.h"
17 #include "components/signin/core/browser/signin_client.h"
18 #include "components/signin/core/common/signin_pref_names.h"
19 #include "components/signin/core/common/signin_switches.h"
20 #include "google_apis/gaia/gaia_auth_util.h"
21 #include "google_apis/gaia/gaia_constants.h"
22 #include "google_apis/gaia/gaia_urls.h"
24 using namespace signin_internals_util
;
26 SigninManagerBase::SigninManagerBase(SigninClient
* client
)
27 : client_(client
), initialized_(false), weak_pointer_factory_(this) {}
29 SigninManagerBase::~SigninManagerBase() {}
31 void SigninManagerBase::Initialize(PrefService
* local_state
) {
32 // Should never call Initialize() twice.
33 DCHECK(!IsInitialized());
36 // If the user is clearing the token service from the command line, then
37 // clear their login info also (not valid to be logged in without any
39 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
40 if (cmd_line
->HasSwitch(switches::kClearTokenService
))
41 client_
->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername
);
44 client_
->GetPrefs()->GetString(prefs::kGoogleServicesUsername
);
46 SetAuthenticatedUsername(user
);
49 bool SigninManagerBase::IsInitialized() const { return initialized_
; }
51 bool SigninManagerBase::IsSigninAllowed() const {
52 return client_
->GetPrefs()->GetBoolean(prefs::kSigninAllowed
);
55 const std::string
& SigninManagerBase::GetAuthenticatedUsername() const {
56 return authenticated_username_
;
59 const std::string
& SigninManagerBase::GetAuthenticatedAccountId() const {
60 return authenticated_account_id_
;
63 void SigninManagerBase::SetAuthenticatedUsername(const std::string
& username
) {
64 DCHECK(!username
.empty());
65 if (!authenticated_username_
.empty()) {
66 DLOG_IF(ERROR
, !gaia::AreEmailsSame(username
, authenticated_username_
))
67 << "Tried to change the authenticated username to something different: "
68 << "Current: " << authenticated_username_
<< ", New: " << username
;
71 // Prior to M26, chrome on iOS did not normalize the email before setting
72 // it in SigninManager. If the emails are the same as given by
73 // gaia::AreEmailsSame() but not the same as given by std::string::op==(),
74 // make sure to set the authenticated name below.
75 if (!gaia::AreEmailsSame(username
, authenticated_username_
) ||
76 username
== authenticated_username_
) {
83 std::string pref_username
=
84 client_
->GetPrefs()->GetString(prefs::kGoogleServicesUsername
);
85 DCHECK(pref_username
.empty() || gaia::AreEmailsSame(username
, pref_username
))
86 << "username: " << username
<< "; pref_username: " << pref_username
;
87 authenticated_username_
= username
;
89 // TODO(rogerta): remove this DCHECK when migration work is started.
90 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED
,
91 AccountTrackerService::GetMigrationState(client_
->GetPrefs()));
92 authenticated_account_id_
=
93 AccountTrackerService::PickAccountIdForAccount(client_
->GetPrefs(),
96 client_
->GetPrefs()->SetString(prefs::kGoogleServicesUsername
, username
);
98 // Go ahead and update the last signed in username here as well. Once a
99 // user is signed in the two preferences should match. Doing it here as
100 // opposed to on signin allows us to catch the upgrade scenario.
101 client_
->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername
, username
);
104 void SigninManagerBase::ClearAuthenticatedUsername() {
105 authenticated_username_
.clear();
106 authenticated_account_id_
.clear();
109 bool SigninManagerBase::IsAuthenticated() const {
110 return !authenticated_account_id_
.empty();
113 bool SigninManagerBase::AuthInProgress() const {
114 // SigninManagerBase never kicks off auth processes itself.
118 void SigninManagerBase::Shutdown() {}
120 void SigninManagerBase::AddObserver(Observer
* observer
) {
121 observer_list_
.AddObserver(observer
);
124 void SigninManagerBase::RemoveObserver(Observer
* observer
) {
125 observer_list_
.RemoveObserver(observer
);
128 void SigninManagerBase::AddSigninDiagnosticsObserver(
129 SigninDiagnosticsObserver
* observer
) {
130 signin_diagnostics_observers_
.AddObserver(observer
);
133 void SigninManagerBase::RemoveSigninDiagnosticsObserver(
134 SigninDiagnosticsObserver
* observer
) {
135 signin_diagnostics_observers_
.RemoveObserver(observer
);
138 void SigninManagerBase::NotifyDiagnosticsObservers(
139 const TimedSigninStatusField
& field
,
140 const std::string
& value
) {
141 FOR_EACH_OBSERVER(SigninDiagnosticsObserver
,
142 signin_diagnostics_observers_
,
143 NotifySigninValueChanged(field
, value
));