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/signin/signin_error_controller_factory.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "components/signin/core/browser/profile_oauth2_token_service.h"
24 #include "components/signin/core/common/profile_management_switches.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/gfx/text_elider.h"
30 bool IsMultipleProfilesEnabled() {
31 #if defined(OS_ANDROID)
37 base::FilePath
GetDefaultProfileDir(const base::FilePath
& user_data_dir
) {
38 base::FilePath
default_profile_dir(user_data_dir
);
40 default_profile_dir
.AppendASCII(chrome::kInitialProfile
);
41 return default_profile_dir
;
44 void RegisterPrefs(PrefRegistrySimple
* registry
) {
45 // Preferences about global profile information.
46 registry
->RegisterStringPref(prefs::kProfileLastUsed
, std::string());
47 registry
->RegisterIntegerPref(prefs::kProfilesNumCreated
, 1);
48 registry
->RegisterListPref(prefs::kProfilesLastActive
);
50 // Preferences about the user manager.
51 registry
->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled
, true);
52 registry
->RegisterBooleanPref(prefs::kBrowserAddPersonEnabled
, true);
55 base::string16
GetAvatarNameForProfile(const base::FilePath
& profile_path
) {
56 base::string16 display_name
;
58 if (profile_path
== ProfileManager::GetGuestProfilePath()) {
59 display_name
= l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME
);
61 const ProfileInfoCache
& cache
=
62 g_browser_process
->profile_manager()->GetProfileInfoCache();
63 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
65 if (index
== std::string::npos
)
66 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
68 // Using the --new-avatar-menu flag, there's a couple of rules about what
69 // the avatar button displays. If there's a single profile, with a default
70 // name (i.e. of the form Person %d) not manually set, it should display
71 // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using
72 // a default name, use the profiles's email address. Otherwise, it
73 // will return the actual name of the profile.
74 const base::string16 profile_name
= cache
.GetNameOfProfileAtIndex(index
);
75 const base::string16 email
= cache
.GetUserNameOfProfileAtIndex(index
);
76 bool is_default_name
= cache
.ProfileIsUsingDefaultNameAtIndex(index
) &&
77 cache
.IsDefaultProfileName(profile_name
);
79 if (cache
.GetNumberOfProfiles() == 1 && is_default_name
)
80 display_name
= l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
82 display_name
= (is_default_name
&& !email
.empty()) ? email
: profile_name
;
87 base::string16
GetAvatarButtonTextForProfile(Profile
* profile
) {
88 const int kMaxCharactersToDisplay
= 15;
89 base::string16 name
= GetAvatarNameForProfile(profile
->GetPath());
90 name
= gfx::TruncateString(name
,
91 kMaxCharactersToDisplay
,
92 gfx::CHARACTER_BREAK
);
93 if (profile
->IsLegacySupervised()) {
94 name
= l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL
,
100 void UpdateProfileName(Profile
* profile
,
101 const base::string16
& new_profile_name
) {
102 const ProfileInfoCache
& cache
=
103 g_browser_process
->profile_manager()->GetProfileInfoCache();
104 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
105 if (profile_index
== std::string::npos
)
108 if (new_profile_name
== cache
.GetNameOfProfileAtIndex(profile_index
))
111 // This is only called when updating the profile name through the UI,
112 // so we can assume the user has done this on purpose.
113 PrefService
* pref_service
= profile
->GetPrefs();
114 pref_service
->SetBoolean(prefs::kProfileUsingDefaultName
, false);
116 // Updating the profile preference will cause the cache to be updated for
118 pref_service
->SetString(prefs::kProfileName
,
119 base::UTF16ToUTF8(new_profile_name
));
122 std::vector
<std::string
> GetSecondaryAccountsForProfile(
124 const std::string
& primary_account
) {
125 std::vector
<std::string
> accounts
=
126 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
)->GetAccounts();
128 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
129 // the primary account too, so we need to remove it from the list.
130 std::vector
<std::string
>::iterator primary_index
=
131 std::find_if(accounts
.begin(), accounts
.end(),
132 std::bind1st(std::equal_to
<std::string
>(), primary_account
));
133 DCHECK(primary_index
!= accounts
.end());
134 accounts
.erase(primary_index
);
139 bool IsRegularOrGuestSession(Browser
* browser
) {
140 Profile
* profile
= browser
->profile();
141 return profile
->IsGuestSession() || !profile
->IsOffTheRecord();
144 bool IsProfileLocked(Profile
* profile
) {
145 const ProfileInfoCache
& cache
=
146 g_browser_process
->profile_manager()->GetProfileInfoCache();
147 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
149 if (profile_index
== std::string::npos
)
152 return cache
.ProfileIsSigninRequiredAtIndex(profile_index
);
155 void UpdateIsProfileLockEnabledIfNeeded(Profile
* profile
) {
156 DCHECK(switches::IsNewProfileManagement());
158 if (!profile
->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain
).
162 UpdateGaiaProfileInfoIfNeeded(profile
);
165 void UpdateGaiaProfileInfoIfNeeded(Profile
* profile
) {
166 // If the --google-profile-info flag isn't used, then the
167 // GAIAInfoUpdateService isn't initialized, and we can't download the profile
169 if (!switches::IsGoogleProfileInfo())
174 GAIAInfoUpdateService
* service
=
175 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile
);
176 // The service may be null, for example during unit tests.
181 SigninErrorController
* GetSigninErrorController(Profile
* profile
) {
182 return SigninErrorControllerFactory::GetForProfile(profile
);
185 bool SetActiveProfileToGuestIfLocked() {
186 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
188 const base::FilePath
& active_profile_path
=
189 profile_manager
->GetLastUsedProfileDir(profile_manager
->user_data_dir());
190 const base::FilePath
& guest_path
= ProfileManager::GetGuestProfilePath();
191 if (active_profile_path
== guest_path
)
194 const ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
195 size_t index
= cache
.GetIndexOfProfileWithPath(active_profile_path
);
196 if (!cache
.ProfileIsSigninRequiredAtIndex(index
))
199 PrefService
* local_state
= g_browser_process
->local_state();
201 local_state
->SetString(
202 prefs::kProfileLastUsed
,
203 guest_path
.BaseName().MaybeAsASCII());
207 } // namespace profiles