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. 4 5 #include "chrome/browser/signin/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 "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" 27 28 using namespace signin_internals_util; 29 30 using content::BrowserThread; 31 32 SigninManagerBase::SigninManagerBase() 33 : profile_(NULL), 34 weak_pointer_factory_(this) { 35 } 36 37 SigninManagerBase::~SigninManagerBase() { 38 } 39 40 void SigninManagerBase::Initialize(Profile* profile, PrefService* local_state) { 41 // Should never call Initialize() twice. 42 DCHECK(!IsInitialized()); 43 profile_ = profile; 44 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 47 // tokens). 48 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 49 if (cmd_line->HasSwitch(switches::kClearTokenService)) 50 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); 51 52 std::string user = profile_->GetPrefs()->GetString( 53 prefs::kGoogleServicesUsername); 54 if (!user.empty()) 55 SetAuthenticatedUsername(user); 56 } 57 58 bool SigninManagerBase::IsInitialized() const { 59 return profile_ != NULL; 60 } 61 62 bool SigninManagerBase::IsSigninAllowed() const { 63 return profile_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); 64 } 65 66 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { 67 return authenticated_username_; 68 } 69 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; 75 76 #if defined(OS_IOS) 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_) { 83 return; 84 } 85 #else 86 return; 87 #endif 88 } 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. 93 94 NotifyDiagnosticsObservers(USERNAME, username); 95 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); 100 } 101 102 void SigninManagerBase::clear_authenticated_username() { 103 authenticated_username_.clear(); 104 } 105 106 bool SigninManagerBase::AuthInProgress() const { 107 // SigninManagerBase never kicks off auth processes itself. 108 return false; 109 } 110 111 void SigninManagerBase::Shutdown() { 112 } 113 114 void SigninManagerBase::AddSigninDiagnosticsObserver( 115 SigninDiagnosticsObserver* observer) { 116 signin_diagnostics_observers_.AddObserver(observer); 117 } 118 119 void SigninManagerBase::RemoveSigninDiagnosticsObserver( 120 SigninDiagnosticsObserver* observer) { 121 signin_diagnostics_observers_.RemoveObserver(observer); 122 } 123 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 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