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