Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / profiles / profiles_state.cc
blob930bc83adc7adf5f80cf012edda749ae5c964e99
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/browsing_data/browsing_data_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_remover.h"
14 #include "chrome/browser/profiles/gaia_info_update_service.h"
15 #include "chrome/browser/profiles/gaia_info_update_service_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
20 #include "chrome/browser/signin/signin_error_controller_factory.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "components/signin/core/common/profile_management_switches.h"
27 #include "content/public/browser/resource_dispatcher_host.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/text_elider.h"
31 namespace profiles {
33 bool IsMultipleProfilesEnabled() {
34 #if defined(OS_ANDROID)
35 return false;
36 #endif
37 return true;
40 base::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;
47 void RegisterPrefs(PrefRegistrySimple* registry) {
48 // Preferences about global profile information.
49 registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
50 registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
51 registry->RegisterListPref(prefs::kProfilesLastActive);
53 // Preferences about the user manager.
54 registry->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled, true);
55 registry->RegisterBooleanPref(prefs::kBrowserAddPersonEnabled, true);
57 registry->RegisterBooleanPref(
58 prefs::kProfileAvatarRightClickTutorialDismissed, false);
61 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
62 base::string16 display_name;
64 if (profile_path == ProfileManager::GetGuestProfilePath()) {
65 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
66 } else {
67 const ProfileInfoCache& cache =
68 g_browser_process->profile_manager()->GetProfileInfoCache();
69 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
71 if (index == std::string::npos)
72 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
74 // Using the --new-avatar-menu flag, there's a couple of rules about what
75 // the avatar button displays. If there's a single profile, with a default
76 // name (i.e. of the form Person %d) not manually set, it should display
77 // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using
78 // a default name, use the profiles's email address. Otherwise, it
79 // will return the actual name of the profile.
80 const base::string16 profile_name = cache.GetNameOfProfileAtIndex(index);
81 const base::string16 email = cache.GetUserNameOfProfileAtIndex(index);
82 bool is_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index) &&
83 cache.IsDefaultProfileName(profile_name);
85 if (cache.GetNumberOfProfiles() == 1 && is_default_name)
86 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
87 else
88 display_name = (is_default_name && !email.empty()) ? email : profile_name;
90 return display_name;
93 base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
94 const int kMaxCharactersToDisplay = 15;
95 base::string16 name = GetAvatarNameForProfile(profile->GetPath());
96 name = gfx::TruncateString(name,
97 kMaxCharactersToDisplay,
98 gfx::CHARACTER_BREAK);
99 if (profile->IsLegacySupervised()) {
100 name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL,
101 name);
103 return name;
106 base::string16 GetProfileSwitcherTextForItem(const AvatarMenu::Item& item) {
107 if (item.legacy_supervised) {
108 return l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL,
109 item.name);
111 if (item.child_account)
112 return l10n_util::GetStringFUTF16(IDS_CHILD_AVATAR_LABEL, item.name);
113 return item.name;
116 void UpdateProfileName(Profile* profile,
117 const base::string16& new_profile_name) {
118 const ProfileInfoCache& cache =
119 g_browser_process->profile_manager()->GetProfileInfoCache();
120 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
121 if (profile_index == std::string::npos)
122 return;
124 if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index))
125 return;
127 // This is only called when updating the profile name through the UI,
128 // so we can assume the user has done this on purpose.
129 PrefService* pref_service = profile->GetPrefs();
130 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);
132 // Updating the profile preference will cause the cache to be updated for
133 // this preference.
134 pref_service->SetString(prefs::kProfileName,
135 base::UTF16ToUTF8(new_profile_name));
138 std::vector<std::string> GetSecondaryAccountsForProfile(
139 Profile* profile,
140 const std::string& primary_account) {
141 std::vector<std::string> accounts =
142 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
144 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
145 // the primary account too, so we need to remove it from the list.
146 std::vector<std::string>::iterator primary_index =
147 std::find_if(accounts.begin(), accounts.end(),
148 std::bind1st(std::equal_to<std::string>(), primary_account));
149 DCHECK(primary_index != accounts.end());
150 accounts.erase(primary_index);
152 return accounts;
155 bool IsRegularOrGuestSession(Browser* browser) {
156 Profile* profile = browser->profile();
157 return profile->IsGuestSession() || !profile->IsOffTheRecord();
160 bool IsProfileLocked(const base::FilePath& path) {
161 const ProfileInfoCache& cache =
162 g_browser_process->profile_manager()->GetProfileInfoCache();
163 size_t profile_index = cache.GetIndexOfProfileWithPath(path);
165 if (profile_index == std::string::npos)
166 return false;
168 return cache.ProfileIsSigninRequiredAtIndex(profile_index);
171 void UpdateIsProfileLockEnabledIfNeeded(Profile* profile) {
172 DCHECK(switches::IsNewProfileManagement());
174 if (!profile->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain).
175 empty())
176 return;
178 UpdateGaiaProfileInfoIfNeeded(profile);
181 void UpdateGaiaProfileInfoIfNeeded(Profile* profile) {
182 // If the --google-profile-info flag isn't used, then the
183 // GAIAInfoUpdateService isn't initialized, and we can't download the profile
184 // info.
185 if (!switches::IsGoogleProfileInfo())
186 return;
188 DCHECK(profile);
190 GAIAInfoUpdateService* service =
191 GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile);
192 // The service may be null, for example during unit tests.
193 if (service)
194 service->Update();
197 SigninErrorController* GetSigninErrorController(Profile* profile) {
198 return SigninErrorControllerFactory::GetForProfile(profile);
201 bool SetActiveProfileToGuestIfLocked() {
202 ProfileManager* profile_manager = g_browser_process->profile_manager();
204 const base::FilePath& active_profile_path =
205 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir());
206 const base::FilePath& guest_path = ProfileManager::GetGuestProfilePath();
207 if (active_profile_path == guest_path)
208 return true;
210 const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
211 size_t index = cache.GetIndexOfProfileWithPath(active_profile_path);
212 if (!cache.ProfileIsSigninRequiredAtIndex(index))
213 return false;
215 SetLastUsedProfile(guest_path.BaseName().MaybeAsASCII());
217 return true;
220 void RemoveBrowsingDataForProfile(const base::FilePath& profile_path) {
221 // The BrowsingDataRemover relies on the ResourceDispatcherHost, which is
222 // null in unit tests.
223 if (!content::ResourceDispatcherHost::Get())
224 return;
226 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath(
227 profile_path);
228 if (!profile)
229 return;
231 // For guest the browsing data is in the OTR profile.
232 if (profile->IsGuestSession())
233 profile = profile->GetOffTheRecordProfile();
235 BrowsingDataRemover::CreateForUnboundedRange(profile)->Remove(
236 BrowsingDataRemover::REMOVE_WIPE_PROFILE, BrowsingDataHelper::ALL);
237 // BrowsingDataRemover deletes itself.
240 void SetLastUsedProfile(const std::string& profile_dir) {
241 // We should never be saving the System Profile as the last one used since it
242 // shouldn't have a browser.
243 if (profile_dir == base::FilePath(chrome::kSystemProfileDir).AsUTF8Unsafe())
244 return;
246 PrefService* local_state = g_browser_process->local_state();
247 DCHECK(local_state);
248 local_state->SetString(prefs::kProfileLastUsed, profile_dir);
251 } // namespace profiles