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 "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"
26
27 namespace profiles {
28
IsMultipleProfilesEnabled()29 bool IsMultipleProfilesEnabled() {
30 #if defined(OS_ANDROID)
31 return false;
32 #endif
33 return true;
34 }
35
GetDefaultProfileDir(const base::FilePath & user_data_dir)36 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
37 base::FilePath default_profile_dir(user_data_dir);
38 default_profile_dir =
39 default_profile_dir.AppendASCII(chrome::kInitialProfile);
40 return default_profile_dir;
41 }
42
RegisterPrefs(PrefRegistrySimple * registry)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);
48
49 // Preferences about the user manager.
50 registry->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled, true);
51 registry->RegisterBooleanPref(prefs::kBrowserAddPersonEnabled, true);
52 }
53
GetAvatarNameForProfile(const base::FilePath & profile_path)54 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
55 base::string16 display_name;
56
57 if (profile_path == ProfileManager::GetGuestProfilePath()) {
58 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
59 } else {
60 const ProfileInfoCache& cache =
61 g_browser_process->profile_manager()->GetProfileInfoCache();
62 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
63
64 if (index == std::string::npos)
65 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
66
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);
77
78 if (cache.GetNumberOfProfiles() == 1 && is_default_name)
79 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
80 else
81 display_name = (is_default_name && !email.empty()) ? email : profile_name;
82 }
83 return display_name;
84 }
85
GetAvatarButtonTextForProfile(Profile * profile)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,
94 name);
95 }
96 return name;
97 }
98
UpdateProfileName(Profile * profile,const base::string16 & new_profile_name)99 void UpdateProfileName(Profile* profile,
100 const base::string16& new_profile_name) {
101 const 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)
105 return;
106
107 if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index))
108 return;
109
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);
114
115 // Updating the profile preference will cause the cache to be updated for
116 // this preference.
117 pref_service->SetString(prefs::kProfileName,
118 base::UTF16ToUTF8(new_profile_name));
119 }
120
GetSecondaryAccountsForProfile(Profile * profile,const std::string & primary_account)121 std::vector<std::string> GetSecondaryAccountsForProfile(
122 Profile* profile,
123 const std::string& primary_account) {
124 std::vector<std::string> accounts =
125 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
126
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);
134
135 return accounts;
136 }
137
IsRegularOrGuestSession(Browser * browser)138 bool IsRegularOrGuestSession(Browser* browser) {
139 Profile* profile = browser->profile();
140 return profile->IsGuestSession() || !profile->IsOffTheRecord();
141 }
142
UpdateGaiaProfilePhotoIfNeeded(Profile * profile)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())
147 return;
148
149 DCHECK(profile);
150 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update();
151 }
152
GetSigninErrorController(Profile * profile)153 SigninErrorController* GetSigninErrorController(Profile* profile) {
154 ProfileOAuth2TokenService* token_service =
155 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
156 return token_service ? token_service->signin_error_controller() : NULL;
157 }
158
SetActiveProfileToGuestIfLocked()159 Profile* SetActiveProfileToGuestIfLocked() {
160 Profile* active_profile = ProfileManager::GetLastUsedProfile();
161 DCHECK(active_profile);
162
163 if (active_profile->IsGuestSession())
164 return active_profile;
165
166 ProfileManager* profile_manager = g_browser_process->profile_manager();
167 const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
168 size_t index = cache.GetIndexOfProfileWithPath(active_profile->GetPath());
169 if (!cache.ProfileIsSigninRequiredAtIndex(index))
170 return NULL;
171
172 // The guest profile must have been loaded already.
173 Profile* guest_profile = profile_manager->GetProfile(
174 ProfileManager::GetGuestProfilePath());
175 DCHECK(guest_profile);
176
177 PrefService* local_state = g_browser_process->local_state();
178 DCHECK(local_state);
179 local_state->SetString(prefs::kProfileLastUsed,
180 guest_profile->GetPath().BaseName().MaybeAsASCII());
181 return guest_profile;
182 }
183
184 } // namespace profiles
185