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.
5 #include "chrome/browser/profiles/profiles_state.h"
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 #include "ui/gfx/text_elider.h"
29 bool IsMultipleProfilesEnabled() {
30 #if defined(OS_ANDROID)
36 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
37 base::FilePath default_profile_dir(user_data_dir);
39 default_profile_dir.AppendASCII(chrome::kInitialProfile);
40 return default_profile_dir;
43 void RegisterPrefs(PrefRegistrySimple* registry) {
44 registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
45 registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
46 registry->RegisterListPref(prefs::kProfilesLastActive);
49 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
50 base::string16 display_name;
52 if (profile_path == ProfileManager::GetGuestProfilePath()) {
53 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
55 ProfileInfoCache& cache =
56 g_browser_process->profile_manager()->GetProfileInfoCache();
57 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
59 if (index == std::string::npos)
60 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
62 // Using the --new-avatar-menu flag, there's a couple of rules about what
63 // the avatar button displays. If there's a single profile, with a default
64 // name (i.e. of the form Person %d) not manually set, it should display
65 // IDS_SINGLE_PROFILE_DISPLAY_NAME. Otherwise, it will return the actual
66 // name of the profile.
67 base::string16 profile_name = cache.GetNameOfProfileAtIndex(index);
68 bool has_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index) &&
69 cache.IsDefaultProfileName(profile_name);
71 if (cache.GetNumberOfProfiles() == 1 && has_default_name)
72 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
74 display_name = profile_name;
79 base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
80 const int kMaxCharactersToDisplay = 15;
81 base::string16 name = GetAvatarNameForProfile(profile->GetPath());
82 name = gfx::TruncateString(name,
83 kMaxCharactersToDisplay,
84 gfx::CHARACTER_BREAK);
85 if (profile->IsSupervised()) {
86 name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL,
92 void UpdateProfileName(Profile* profile,
93 const base::string16& new_profile_name) {
94 ProfileInfoCache& cache =
95 g_browser_process->profile_manager()->GetProfileInfoCache();
96 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
97 if (profile_index == std::string::npos)
100 if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index))
103 // This is only called when updating the profile name through the UI,
104 // so we can assume the user has done this on purpose.
105 PrefService* pref_service = profile->GetPrefs();
106 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);
108 // Updating the profile preference will cause the cache to be updated for
110 pref_service->SetString(prefs::kProfileName,
111 base::UTF16ToUTF8(new_profile_name));
114 std::vector<std::string> GetSecondaryAccountsForProfile(
116 const std::string& primary_account) {
117 std::vector<std::string> accounts =
118 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
120 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
121 // the primary account too, so we need to remove it from the list.
122 std::vector<std::string>::iterator primary_index =
123 std::find_if(accounts.begin(), accounts.end(),
124 std::bind1st(std::equal_to<std::string>(), primary_account));
125 DCHECK(primary_index != accounts.end());
126 accounts.erase(primary_index);
131 bool IsRegularOrGuestSession(Browser* browser) {
132 Profile* profile = browser->profile();
133 return profile->IsGuestSession() || !profile->IsOffTheRecord();
136 void UpdateGaiaProfilePhotoIfNeeded(Profile* profile) {
137 // If the --google-profile-info flag isn't used, then the
138 // GAIAInfoUpdateService isn't initialized, and we can't download the picture.
139 if (!switches::IsGoogleProfileInfo())
143 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update();
146 SigninErrorController* GetSigninErrorController(Profile* profile) {
147 ProfileOAuth2TokenService* token_service =
148 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
149 return token_service ? token_service->signin_error_controller() : NULL;
152 } // namespace profiles