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 "chrome/grit/generated_resources.h"
22 #include "components/signin/core/browser/profile_oauth2_token_service.h"
23 #include "components/signin/core/common/profile_management_switches.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 // Preferences about global profile information.
45 registry
->RegisterStringPref(prefs::kProfileLastUsed
, std::string());
46 registry
->RegisterIntegerPref(prefs::kProfilesNumCreated
, 1);
47 registry
->RegisterListPref(prefs::kProfilesLastActive
);
49 // Preferences about the user manager.
50 registry
->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled
, true);
51 registry
->RegisterBooleanPref(prefs::kBrowserAddPersonEnabled
, true);
54 base::string16
GetAvatarNameForProfile(const base::FilePath
& profile_path
) {
55 base::string16 display_name
;
57 if (profile_path
== ProfileManager::GetGuestProfilePath()) {
58 display_name
= l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME
);
60 ProfileInfoCache
& cache
=
61 g_browser_process
->profile_manager()->GetProfileInfoCache();
62 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
64 if (index
== std::string::npos
)
65 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
67 // Using the --new-avatar-menu flag, there's a couple of rules about what
68 // the avatar button displays. If there's a single profile, with a default
69 // name (i.e. of the form Person %d) not manually set, it should display
70 // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using
71 // a default name, use the profiles's email address. Otherwise, it
72 // will return the actual name of the profile.
73 const base::string16 profile_name
= cache
.GetNameOfProfileAtIndex(index
);
74 const base::string16 email
= cache
.GetUserNameOfProfileAtIndex(index
);
75 bool is_default_name
= cache
.ProfileIsUsingDefaultNameAtIndex(index
) &&
76 cache
.IsDefaultProfileName(profile_name
);
78 if (cache
.GetNumberOfProfiles() == 1 && is_default_name
)
79 display_name
= l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME
);
81 display_name
= (is_default_name
&& !email
.empty()) ? email
: profile_name
;
86 base::string16
GetAvatarButtonTextForProfile(Profile
* profile
) {
87 const int kMaxCharactersToDisplay
= 15;
88 base::string16 name
= GetAvatarNameForProfile(profile
->GetPath());
89 name
= gfx::TruncateString(name
,
90 kMaxCharactersToDisplay
,
91 gfx::CHARACTER_BREAK
);
92 if (profile
->IsSupervised()) {
93 name
= l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL
,
99 void UpdateProfileName(Profile
* profile
,
100 const base::string16
& new_profile_name
) {
101 ProfileInfoCache
& cache
=
102 g_browser_process
->profile_manager()->GetProfileInfoCache();
103 size_t profile_index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
104 if (profile_index
== std::string::npos
)
107 if (new_profile_name
== cache
.GetNameOfProfileAtIndex(profile_index
))
110 // This is only called when updating the profile name through the UI,
111 // so we can assume the user has done this on purpose.
112 PrefService
* pref_service
= profile
->GetPrefs();
113 pref_service
->SetBoolean(prefs::kProfileUsingDefaultName
, false);
115 // Updating the profile preference will cause the cache to be updated for
117 pref_service
->SetString(prefs::kProfileName
,
118 base::UTF16ToUTF8(new_profile_name
));
121 std::vector
<std::string
> GetSecondaryAccountsForProfile(
123 const std::string
& primary_account
) {
124 std::vector
<std::string
> accounts
=
125 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
)->GetAccounts();
127 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
128 // the primary account too, so we need to remove it from the list.
129 std::vector
<std::string
>::iterator primary_index
=
130 std::find_if(accounts
.begin(), accounts
.end(),
131 std::bind1st(std::equal_to
<std::string
>(), primary_account
));
132 DCHECK(primary_index
!= accounts
.end());
133 accounts
.erase(primary_index
);
138 bool IsRegularOrGuestSession(Browser
* browser
) {
139 Profile
* profile
= browser
->profile();
140 return profile
->IsGuestSession() || !profile
->IsOffTheRecord();
143 void UpdateGaiaProfilePhotoIfNeeded(Profile
* profile
) {
144 // If the --google-profile-info flag isn't used, then the
145 // GAIAInfoUpdateService isn't initialized, and we can't download the picture.
146 if (!switches::IsGoogleProfileInfo())
150 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile
)->Update();
153 SigninErrorController
* GetSigninErrorController(Profile
* profile
) {
154 ProfileOAuth2TokenService
* token_service
=
155 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
156 return token_service
? token_service
->signin_error_controller() : NULL
;
159 } // namespace profiles