Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: chrome/browser/profiles/profiles_state.cc

Issue 1242793005: Refactor most c/b/profiles calls to ProfileInfoCache. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Windows unit test and ChromeOS build Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/profiles/profiles_state.h" 5 #include "chrome/browser/profiles/profiles_state.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/prefs/pref_registry_simple.h" 8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browsing_data/browsing_data_helper.h" 12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_remover.h" 13 #include "chrome/browser/browsing_data/browsing_data_remover.h"
14 #include "chrome/browser/profiles/gaia_info_update_service.h" 14 #include "chrome/browser/profiles/gaia_info_update_service.h"
15 #include "chrome/browser/profiles/gaia_info_update_service_factory.h" 15 #include "chrome/browser/profiles/gaia_info_update_service_factory.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h" 17 #include "chrome/browser/profiles/profile_attributes_entry.h"
18 #include "chrome/browser/profiles/profile_attributes_storage.h"
18 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
20 #include "chrome/browser/signin/signin_error_controller_factory.h" 21 #include "chrome/browser/signin/signin_error_controller_factory.h"
21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/chrome_constants.h" 23 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
24 #include "chrome/grit/generated_resources.h" 25 #include "chrome/grit/generated_resources.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h" 26 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "components/signin/core/common/profile_management_switches.h" 27 #include "components/signin/core/common/profile_management_switches.h"
27 #include "content/public/browser/resource_dispatcher_host.h" 28 #include "content/public/browser/resource_dispatcher_host.h"
(...skipping 29 matching lines...) Expand all
57 registry->RegisterBooleanPref( 58 registry->RegisterBooleanPref(
58 prefs::kProfileAvatarRightClickTutorialDismissed, false); 59 prefs::kProfileAvatarRightClickTutorialDismissed, false);
59 } 60 }
60 61
61 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) { 62 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
62 base::string16 display_name; 63 base::string16 display_name;
63 64
64 if (profile_path == ProfileManager::GetGuestProfilePath()) { 65 if (profile_path == ProfileManager::GetGuestProfilePath()) {
65 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 66 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
66 } else { 67 } else {
67 const ProfileInfoCache& cache = 68 ProfileAttributesStorage& storage =
68 g_browser_process->profile_manager()->GetProfileInfoCache(); 69 g_browser_process->profile_manager()->GetProfileAttributesStorage();
69 size_t index = cache.GetIndexOfProfileWithPath(profile_path); 70 ProfileAttributesEntry* entry;
70 71
71 if (index == std::string::npos) 72 if (!storage.GetProfileAttributesWithPath(profile_path, &entry))
72 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 73 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
73 74
74 // Using the --new-avatar-menu flag, there's a couple of rules about what 75 // 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 // 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 // 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 // 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 // a default name, use the profiles's email address. Otherwise, it
79 // will return the actual name of the profile. 80 // will return the actual name of the profile.
80 const base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); 81 const base::string16 profile_name = entry->GetName();
81 const base::string16 email = cache.GetUserNameOfProfileAtIndex(index); 82 const base::string16 email = entry->GetUserName();
82 bool is_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index) && 83 bool is_default_name = entry->IsUsingDefaultName() &&
83 cache.IsDefaultProfileName(profile_name); 84 storage.IsDefaultProfileName(profile_name);
84 85
85 if (cache.GetNumberOfProfiles() == 1 && is_default_name) 86 if (storage.GetNumberOfProfiles() == 1 && is_default_name)
86 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 87 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
87 else 88 else
88 display_name = (is_default_name && !email.empty()) ? email : profile_name; 89 display_name = (is_default_name && !email.empty()) ? email : profile_name;
89 } 90 }
90 return display_name; 91 return display_name;
91 } 92 }
92 93
93 base::string16 GetAvatarButtonTextForProfile(Profile* profile) { 94 base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
94 const int kMaxCharactersToDisplay = 15; 95 const int kMaxCharactersToDisplay = 15;
95 base::string16 name = GetAvatarNameForProfile(profile->GetPath()); 96 base::string16 name = GetAvatarNameForProfile(profile->GetPath());
(...skipping 12 matching lines...) Expand all
108 return l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL, 109 return l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL,
109 item.name); 110 item.name);
110 } 111 }
111 if (item.child_account) 112 if (item.child_account)
112 return l10n_util::GetStringFUTF16(IDS_CHILD_AVATAR_LABEL, item.name); 113 return l10n_util::GetStringFUTF16(IDS_CHILD_AVATAR_LABEL, item.name);
113 return item.name; 114 return item.name;
114 } 115 }
115 116
116 void UpdateProfileName(Profile* profile, 117 void UpdateProfileName(Profile* profile,
117 const base::string16& new_profile_name) { 118 const base::string16& new_profile_name) {
118 const ProfileInfoCache& cache = 119 ProfileAttributesStorage& storage =
119 g_browser_process->profile_manager()->GetProfileInfoCache(); 120 g_browser_process->profile_manager()->GetProfileAttributesStorage();
120 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 121 ProfileAttributesEntry* entry;
121 if (profile_index == std::string::npos) 122 if (!storage.GetProfileAttributesWithPath(profile->GetPath(), &entry))
122 return; 123 return;
123 124
124 if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index)) 125 if (new_profile_name == entry->GetName())
125 return; 126 return;
126 127
127 // This is only called when updating the profile name through the UI, 128 // 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 // so we can assume the user has done this on purpose.
129 PrefService* pref_service = profile->GetPrefs(); 130 PrefService* pref_service = profile->GetPrefs();
130 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false); 131 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);
131 132
132 // Updating the profile preference will cause the cache to be updated for 133 // Updating the profile preference will cause the cache to be updated for
133 // this preference. 134 // this preference.
134 pref_service->SetString(prefs::kProfileName, 135 pref_service->SetString(prefs::kProfileName,
(...skipping 16 matching lines...) Expand all
151 152
152 return accounts; 153 return accounts;
153 } 154 }
154 155
155 bool IsRegularOrGuestSession(Browser* browser) { 156 bool IsRegularOrGuestSession(Browser* browser) {
156 Profile* profile = browser->profile(); 157 Profile* profile = browser->profile();
157 return profile->IsGuestSession() || !profile->IsOffTheRecord(); 158 return profile->IsGuestSession() || !profile->IsOffTheRecord();
158 } 159 }
159 160
160 bool IsProfileLocked(const base::FilePath& path) { 161 bool IsProfileLocked(const base::FilePath& path) {
161 const ProfileInfoCache& cache = 162 ProfileAttributesStorage& storage =
162 g_browser_process->profile_manager()->GetProfileInfoCache(); 163 g_browser_process->profile_manager()->GetProfileAttributesStorage();
163 size_t profile_index = cache.GetIndexOfProfileWithPath(path); 164 ProfileAttributesEntry* entry;
164 165
165 if (profile_index == std::string::npos) 166 if (!storage.GetProfileAttributesWithPath(path, &entry))
166 return false; 167 return false;
167 168
168 return cache.ProfileIsSigninRequiredAtIndex(profile_index); 169 return entry->IsSigninRequired();
169 } 170 }
170 171
171 void UpdateIsProfileLockEnabledIfNeeded(Profile* profile) { 172 void UpdateIsProfileLockEnabledIfNeeded(Profile* profile) {
172 DCHECK(switches::IsNewProfileManagement()); 173 DCHECK(switches::IsNewProfileManagement());
173 174
174 if (!profile->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain). 175 if (!profile->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain).
175 empty()) 176 empty())
176 return; 177 return;
177 178
178 UpdateGaiaProfileInfoIfNeeded(profile); 179 UpdateGaiaProfileInfoIfNeeded(profile);
(...skipping 21 matching lines...) Expand all
200 201
201 bool SetActiveProfileToGuestIfLocked() { 202 bool SetActiveProfileToGuestIfLocked() {
202 ProfileManager* profile_manager = g_browser_process->profile_manager(); 203 ProfileManager* profile_manager = g_browser_process->profile_manager();
203 204
204 const base::FilePath& active_profile_path = 205 const base::FilePath& active_profile_path =
205 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()); 206 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir());
206 const base::FilePath& guest_path = ProfileManager::GetGuestProfilePath(); 207 const base::FilePath& guest_path = ProfileManager::GetGuestProfilePath();
207 if (active_profile_path == guest_path) 208 if (active_profile_path == guest_path)
208 return true; 209 return true;
209 210
210 const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 211 ProfileAttributesStorage& storage =
211 size_t index = cache.GetIndexOfProfileWithPath(active_profile_path); 212 profile_manager->GetProfileAttributesStorage();
212 if (!cache.ProfileIsSigninRequiredAtIndex(index)) 213 ProfileAttributesEntry* entry;
214 if (storage.GetProfileAttributesWithPath(active_profile_path, &entry) &&
215 !entry->IsSigninRequired()) {
213 return false; 216 return false;
217 }
214 218
215 SetLastUsedProfile(guest_path.BaseName().MaybeAsASCII()); 219 SetLastUsedProfile(guest_path.BaseName().MaybeAsASCII());
216 220
217 return true; 221 return true;
218 } 222 }
219 223
220 void RemoveBrowsingDataForProfile(const base::FilePath& profile_path) { 224 void RemoveBrowsingDataForProfile(const base::FilePath& profile_path) {
221 // The BrowsingDataRemover relies on the ResourceDispatcherHost, which is 225 // The BrowsingDataRemover relies on the ResourceDispatcherHost, which is
222 // null in unit tests. 226 // null in unit tests.
223 if (!content::ResourceDispatcherHost::Get()) 227 if (!content::ResourceDispatcherHost::Get())
(...skipping 18 matching lines...) Expand all
242 // shouldn't have a browser. 246 // shouldn't have a browser.
243 if (profile_dir == base::FilePath(chrome::kSystemProfileDir).AsUTF8Unsafe()) 247 if (profile_dir == base::FilePath(chrome::kSystemProfileDir).AsUTF8Unsafe())
244 return; 248 return;
245 249
246 PrefService* local_state = g_browser_process->local_state(); 250 PrefService* local_state = g_browser_process->local_state();
247 DCHECK(local_state); 251 DCHECK(local_state);
248 local_state->SetString(prefs::kProfileLastUsed, profile_dir); 252 local_state->SetString(prefs::kProfileLastUsed, profile_dir);
249 } 253 }
250 254
251 } // namespace profiles 255 } // namespace profiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698