1 // Copyright (c) 2012 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 "chrome/browser/signin/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 "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/signin/about_signin_internals.h"
18 #include "chrome/browser/signin/about_signin_internals_factory.h"
19 #include "chrome/browser/signin/signin_manager_cookie_helper.h"
20 #include "chrome/browser/signin/token_service.h"
21 #include "chrome/browser/signin/token_service_factory.h"
22 #include "chrome/browser/sync/sync_prefs.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/pref_names.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "google_apis/gaia/gaia_auth_util.h"
27 #include "google_apis/gaia/gaia_constants.h"
28 #include "google_apis/gaia/gaia_urls.h"
30 using namespace signin_internals_util
;
32 using content::BrowserThread
;
34 SigninManagerBase::SigninManagerBase()
36 weak_pointer_factory_(this) {
39 SigninManagerBase::~SigninManagerBase() {
42 void SigninManagerBase::Initialize(Profile
* profile
, PrefService
* local_state
) {
43 // Should never call Initialize() twice.
44 DCHECK(!IsInitialized());
47 // If the user is clearing the token service from the command line, then
48 // clear their login info also (not valid to be logged in without any
50 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
51 if (cmd_line
->HasSwitch(switches::kClearTokenService
))
52 profile
->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername
);
54 std::string user
= profile_
->GetPrefs()->GetString(
55 prefs::kGoogleServicesUsername
);
57 SetAuthenticatedUsername(user
);
60 bool SigninManagerBase::IsInitialized() const {
61 return profile_
!= NULL
;
64 bool SigninManagerBase::IsSigninAllowed() const {
65 return profile_
->GetPrefs()->GetBoolean(prefs::kSigninAllowed
);
68 const std::string
& SigninManagerBase::GetAuthenticatedUsername() const {
69 return authenticated_username_
;
72 void SigninManagerBase::SetAuthenticatedUsername(const std::string
& username
) {
73 if (!authenticated_username_
.empty()) {
74 DLOG_IF(ERROR
, !gaia::AreEmailsSame(username
, authenticated_username_
)) <<
75 "Tried to change the authenticated username to something different: " <<
76 "Current: " << authenticated_username_
<< ", New: " << username
;
79 // Prior to M26, chrome on iOS did not normalize the email before setting
80 // it in SigninManager. If the emails are the same as given by
81 // gaia::AreEmailsSame() but not the same as given by std::string::op==(),
82 // make sure to set the authenticated name below.
83 if (!gaia::AreEmailsSame(username
, authenticated_username_
) ||
84 username
== authenticated_username_
) {
91 authenticated_username_
= username
;
92 // TODO(tim): We could go further in ensuring kGoogleServicesUsername and
93 // authenticated_username_ are consistent once established (e.g. remove
94 // authenticated_username_ altogether). Bug 107160.
96 NotifyDiagnosticsObservers(USERNAME
, 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 profile_
->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername
, username
);
104 void SigninManagerBase::clear_authenticated_username() {
105 authenticated_username_
.clear();
108 bool SigninManagerBase::AuthInProgress() const {
109 // SigninManagerBase never kicks off auth processes itself.
113 void SigninManagerBase::Shutdown() {
116 void SigninManagerBase::AddSigninDiagnosticsObserver(
117 SigninDiagnosticsObserver
* observer
) {
118 signin_diagnostics_observers_
.AddObserver(observer
);
121 void SigninManagerBase::RemoveSigninDiagnosticsObserver(
122 SigninDiagnosticsObserver
* observer
) {
123 signin_diagnostics_observers_
.RemoveObserver(observer
);
126 void SigninManagerBase::NotifyDiagnosticsObservers(
127 const UntimedSigninStatusField
& field
,
128 const std::string
& value
) {
129 FOR_EACH_OBSERVER(SigninDiagnosticsObserver
,
130 signin_diagnostics_observers_
,
131 NotifySigninValueChanged(field
, value
));
134 void SigninManagerBase::NotifyDiagnosticsObservers(
135 const TimedSigninStatusField
& field
,
136 const std::string
& value
) {
137 FOR_EACH_OBSERVER(SigninDiagnosticsObserver
,
138 signin_diagnostics_observers_
,
139 NotifySigninValueChanged(field
, value
));