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/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_constants.h" 27 #include "google_apis/gaia/gaia_urls.h" 28 29 using namespace signin_internals_util; 30 31 using content::BrowserThread; 32 33 SigninManagerBase::SigninManagerBase() 34 : profile_(NULL), 35 weak_pointer_factory_(this) { 36 } 37 38 SigninManagerBase::~SigninManagerBase() { 39 } 40 41 void SigninManagerBase::Initialize(Profile* profile, PrefService* local_state) { 42 // Should never call Initialize() twice. 43 DCHECK(!IsInitialized()); 44 profile_ = profile; 45 46 // If the user is clearing the token service from the command line, then 47 // clear their login info also (not valid to be logged in without any 48 // tokens). 49 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 50 if (cmd_line->HasSwitch(switches::kClearTokenService)) 51 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); 52 53 std::string user = profile_->GetPrefs()->GetString( 54 prefs::kGoogleServicesUsername); 55 if (!user.empty()) 56 SetAuthenticatedUsername(user); 57 58 InitTokenService(); 59 } 60 61 void SigninManagerBase::InitTokenService() { 62 // TokenService can be null for unit tests. 63 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); 64 if (token_service) 65 token_service->Initialize(GaiaConstants::kChromeSource, profile_); 66 // Note: ChromeOS will kick off TokenService::LoadTokensFromDB from 67 // OAuthLoginManager once the rest of the Profile is fully initialized. 68 } 69 70 bool SigninManagerBase::IsInitialized() const { 71 return profile_ != NULL; 72 } 73 74 bool SigninManagerBase::IsSigninAllowed() const { 75 return profile_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); 76 } 77 78 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { 79 return authenticated_username_; 80 } 81 82 void SigninManagerBase::SetAuthenticatedUsername(const std::string& username) { 83 if (!authenticated_username_.empty()) { 84 DLOG_IF(ERROR, username != authenticated_username_) << 85 "Tried to change the authenticated username to something different: " << 86 "Current: " << authenticated_username_ << ", New: " << username; 87 return; 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