1 // Copyright 2013 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/profiles/profiles_state.h" 6 7 #include "base/files/file_path.h" 8 #include "base/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_service.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/profiles/gaia_info_update_service.h" 13 #include "chrome/browser/profiles/gaia_info_update_service_factory.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile_info_cache.h" 16 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 18 #include "chrome/browser/ui/browser.h" 19 #include "chrome/common/chrome_constants.h" 20 #include "chrome/common/pref_names.h" 21 #include "components/signin/core/browser/profile_oauth2_token_service.h" 22 #include "components/signin/core/common/profile_management_switches.h" 23 #include "grit/generated_resources.h" 24 #include "ui/base/l10n/l10n_util.h" 25 26 #if defined(OS_CHROMEOS) 27 #include "chrome/browser/chromeos/login/users/user_manager.h" 28 #endif 29 30 namespace profiles { 31 32 bool IsMultipleProfilesEnabled() { 33 #if defined(OS_ANDROID) 34 return false; 35 #endif 36 return true; 37 } 38 39 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) { 40 base::FilePath default_profile_dir(user_data_dir); 41 default_profile_dir = 42 default_profile_dir.AppendASCII(chrome::kInitialProfile); 43 return default_profile_dir; 44 } 45 46 void RegisterPrefs(PrefRegistrySimple* registry) { 47 registry->RegisterStringPref(prefs::kProfileLastUsed, std::string()); 48 registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1); 49 registry->RegisterListPref(prefs::kProfilesLastActive); 50 } 51 52 base::string16 GetAvatarNameForProfile(Profile* profile) { 53 base::string16 display_name; 54 55 if (profile->IsGuestSession()) { 56 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 57 } else { 58 ProfileInfoCache& cache = 59 g_browser_process->profile_manager()->GetProfileInfoCache(); 60 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 61 62 if (index == std::string::npos) 63 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 64 65 // Using the --new-profile-management flag, there's a couple of rules 66 // about what the avatar button displays. If there's a single, local 67 // profile, with a default name (i.e. of the form Person %d), it should 68 // display IDS_SINGLE_PROFILE_DISPLAY_NAME. If this is a signed in profile, 69 // or the user has edited the profile name, or there are multiple profiles, 70 // it will return the actual name of the profile. 71 base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); 72 bool has_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index); 73 74 if (cache.GetNumberOfProfiles() == 1 && has_default_name && 75 cache.GetUserNameOfProfileAtIndex(index).empty()) { 76 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 77 } else { 78 display_name = profile_name; 79 } 80 } 81 return display_name; 82 } 83 84 void UpdateProfileName(Profile* profile, 85 const base::string16& new_profile_name) { 86 PrefService* pref_service = profile->GetPrefs(); 87 // Updating the profile preference will cause the cache to be updated for 88 // this preference. 89 pref_service->SetString(prefs::kProfileName, 90 base::UTF16ToUTF8(new_profile_name)); 91 } 92 93 std::vector<std::string> GetSecondaryAccountsForProfile( 94 Profile* profile, 95 const std::string& primary_account) { 96 std::vector<std::string> accounts = 97 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 98 99 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 100 // the primary account too, so we need to remove it from the list. 101 std::vector<std::string>::iterator primary_index = 102 std::find_if(accounts.begin(), accounts.end(), 103 std::bind1st(std::equal_to<std::string>(), primary_account)); 104 DCHECK(primary_index != accounts.end()); 105 accounts.erase(primary_index); 106 107 return accounts; 108 } 109 110 bool IsRegularOrGuestSession(Browser* browser) { 111 Profile* profile = browser->profile(); 112 return profile->IsGuestSession() || !profile->IsOffTheRecord(); 113 } 114 115 void UpdateGaiaProfilePhotoIfNeeded(Profile* profile) { 116 // If the --google-profile-info flag isn't used, then the 117 // GAIAInfoUpdateService isn't initialized, and we can't download the picture. 118 if (!switches::IsGoogleProfileInfo()) 119 return; 120 121 DCHECK(profile); 122 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update(); 123 } 124 125 SigninErrorController* GetSigninErrorController(Profile* profile) { 126 ProfileOAuth2TokenService* token_service = 127 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 128 return token_service ? token_service->signin_error_controller() : NULL; 129 } 130 131 } // namespace profiles 132