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/sync/sync_prefs.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "google_apis/gaia/gaia_auth_util.h"
25 #include "google_apis/gaia/gaia_constants.h"
26 #include "google_apis/gaia/gaia_urls.h"
28 using namespace signin_internals_util
;
30 using content::BrowserThread
;
32 SigninManagerBase::SigninManagerBase()
34 weak_pointer_factory_(this) {
37 SigninManagerBase::~SigninManagerBase() {
40 void SigninManagerBase::Initialize(Profile
* profile
, PrefService
* local_state
) {
41 // Should never call Initialize() twice.
42 DCHECK(!IsInitialized());
45 // If the user is clearing the token service from the command line, then
46 // clear their login info also (not valid to be logged in without any
48 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
49 if (cmd_line
->HasSwitch(switches::kClearTokenService
))
50 profile
->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername
);
52 std::string user
= profile_
->GetPrefs()->GetString(
53 prefs::kGoogleServicesUsername
);
55 SetAuthenticatedUsername(user
);
58 bool SigninManagerBase::IsInitialized() const {
59 return profile_
!= NULL
;
62 bool SigninManagerBase::IsSigninAllowed() const {
63 return profile_
->GetPrefs()->GetBoolean(prefs::kSigninAllowed
);
66 const std::string
& SigninManagerBase::GetAuthenticatedUsername() const {
67 return authenticated_username_
;
70 void SigninManagerBase::SetAuthenticatedUsername(const std::string
& username
) {
71 if (!authenticated_username_
.empty()) {
72 DLOG_IF(ERROR
, !gaia::AreEmailsSame(username
, authenticated_username_
)) <<
73 "Tried to change the authenticated username to something different: " <<
74 "Current: " << authenticated_username_
<< ", New: " << username
;
77 // Prior to M26, chrome on iOS did not normalize the email before setting
78 // it in SigninManager. If the emails are the same as given by
79 // gaia::AreEmailsSame() but not the same as given by std::string::op==(),
80 // make sure to set the authenticated name below.
81 if (!gaia::AreEmailsSame(username
, authenticated_username_
) ||
82 username
== authenticated_username_
) {
89 authenticated_username_
= username
;
90 // TODO(tim): We could go further in ensuring kGoogleServicesUsername and
91 // authenticated_username_ are consistent once established (e.g. remove
92 // authenticated_username_ altogether). Bug 107160.
94 NotifyDiagnosticsObservers(USERNAME
, username
);
96 // Go ahead and update the last signed in username here as well. Once a
97 // user is signed in the two preferences should match. Doing it here as
98 // opposed to on signin allows us to catch the upgrade scenario.
99 profile_
->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername
, username
);
102 void SigninManagerBase::clear_authenticated_username() {
103 authenticated_username_
.clear();
106 bool SigninManagerBase::AuthInProgress() const {
107 // SigninManagerBase never kicks off auth processes itself.
111 void SigninManagerBase::Shutdown() {
114 void SigninManagerBase::AddSigninDiagnosticsObserver(
115 SigninDiagnosticsObserver
* observer
) {
116 signin_diagnostics_observers_
.AddObserver(observer
);
119 void SigninManagerBase::RemoveSigninDiagnosticsObserver(
120 SigninDiagnosticsObserver
* observer
) {
121 signin_diagnostics_observers_
.RemoveObserver(observer
);
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
));
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
));