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.
4
5 #include "components/signin/core/browser/signin_manager_base.h"
6
7 #include <string>
8 #include <vector>
9
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"
22
23 using namespace signin_internals_util;
24
SigninManagerBase(SigninClient * client)25 SigninManagerBase::SigninManagerBase(SigninClient* client)
26 : client_(client), initialized_(false), weak_pointer_factory_(this) {}
27
~SigninManagerBase()28 SigninManagerBase::~SigninManagerBase() {}
29
Initialize(PrefService * local_state)30 void SigninManagerBase::Initialize(PrefService* local_state) {
31 // Should never call Initialize() twice.
32 DCHECK(!IsInitialized());
33 initialized_ = true;
34
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
37 // tokens).
38 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
39 if (cmd_line->HasSwitch(switches::kClearTokenService))
40 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
41
42 std::string user =
43 client_->GetPrefs()->GetString(prefs::kGoogleServicesUsername);
44 if (!user.empty())
45 SetAuthenticatedUsername(user);
46 }
47
IsInitialized() const48 bool SigninManagerBase::IsInitialized() const { return initialized_; }
49
IsSigninAllowed() const50 bool SigninManagerBase::IsSigninAllowed() const {
51 return client_->GetPrefs()->GetBoolean(prefs::kSigninAllowed);
52 }
53
GetAuthenticatedUsername() const54 const std::string& SigninManagerBase::GetAuthenticatedUsername() const {
55 return authenticated_username_;
56 }
57
GetAuthenticatedAccountId() const58 const std::string& SigninManagerBase::GetAuthenticatedAccountId() const {
59 return GetAuthenticatedUsername();
60 }
61
SetAuthenticatedUsername(const std::string & username)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;
67
68 #if defined(OS_IOS)
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_) {
75 return;
76 }
77 #else
78 return;
79 #endif
80 }
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);
88
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);
93 }
94
clear_authenticated_username()95 void SigninManagerBase::clear_authenticated_username() {
96 authenticated_username_.clear();
97 }
98
AuthInProgress() const99 bool SigninManagerBase::AuthInProgress() const {
100 // SigninManagerBase never kicks off auth processes itself.
101 return false;
102 }
103
Shutdown()104 void SigninManagerBase::Shutdown() {}
105
AddObserver(Observer * observer)106 void SigninManagerBase::AddObserver(Observer* observer) {
107 observer_list_.AddObserver(observer);
108 }
109
RemoveObserver(Observer * observer)110 void SigninManagerBase::RemoveObserver(Observer* observer) {
111 observer_list_.RemoveObserver(observer);
112 }
113
AddSigninDiagnosticsObserver(SigninDiagnosticsObserver * observer)114 void SigninManagerBase::AddSigninDiagnosticsObserver(
115 SigninDiagnosticsObserver* observer) {
116 signin_diagnostics_observers_.AddObserver(observer);
117 }
118
RemoveSigninDiagnosticsObserver(SigninDiagnosticsObserver * observer)119 void SigninManagerBase::RemoveSigninDiagnosticsObserver(
120 SigninDiagnosticsObserver* observer) {
121 signin_diagnostics_observers_.RemoveObserver(observer);
122 }
123
NotifyDiagnosticsObservers(const UntimedSigninStatusField & field,const std::string & value)124 void SigninManagerBase::NotifyDiagnosticsObservers(
125 const UntimedSigninStatusField& field,
126 const std::string& value) {
127 FOR_EACH_OBSERVER(SigninDiagnosticsObserver,
128 signin_diagnostics_observers_,
129 NotifySigninValueChanged(field, value));
130 }
131
NotifyDiagnosticsObservers(const TimedSigninStatusField & field,const std::string & value)132 void SigninManagerBase::NotifyDiagnosticsObservers(
133 const TimedSigninStatusField& field,
134 const std::string& value) {
135 FOR_EACH_OBSERVER(SigninDiagnosticsObserver,
136 signin_diagnostics_observers_,
137 NotifySigninValueChanged(field, value));
138 }
139