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

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

Issue 1794353003: Refactor ProfileInfoCache in c/b/profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix a bug that causes unit tests without debug code to fail. Created 4 years, 8 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/browsing_data/browsing_data_helper.h" 13 #include "chrome/browser/browsing_data/browsing_data_helper.h"
14 #include "chrome/browser/browsing_data/browsing_data_remover.h" 14 #include "chrome/browser/browsing_data/browsing_data_remover.h"
15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
16 #include "chrome/browser/profiles/gaia_info_update_service.h" 16 #include "chrome/browser/profiles/gaia_info_update_service.h"
17 #include "chrome/browser/profiles/gaia_info_update_service_factory.h" 17 #include "chrome/browser/profiles/gaia_info_update_service_factory.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_info_cache.h" 19 #include "chrome/browser/profiles/profile_attributes_entry.h"
20 #include "chrome/browser/profiles/profile_attributes_storage.h"
20 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22 #include "chrome/browser/signin/signin_error_controller_factory.h" 23 #include "chrome/browser/signin/signin_error_controller_factory.h"
23 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
24 #include "chrome/common/chrome_constants.h" 25 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
26 #include "chrome/grit/generated_resources.h" 27 #include "chrome/grit/generated_resources.h"
27 #include "components/prefs/pref_registry_simple.h" 28 #include "components/prefs/pref_registry_simple.h"
28 #include "components/prefs/pref_service.h" 29 #include "components/prefs/pref_service.h"
29 #include "components/signin/core/browser/profile_oauth2_token_service.h" 30 #include "components/signin/core/browser/profile_oauth2_token_service.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 registry->RegisterBooleanPref( 63 registry->RegisterBooleanPref(
63 prefs::kProfileAvatarRightClickTutorialDismissed, false); 64 prefs::kProfileAvatarRightClickTutorialDismissed, false);
64 } 65 }
65 66
66 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) { 67 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
67 base::string16 display_name; 68 base::string16 display_name;
68 69
69 if (profile_path == ProfileManager::GetGuestProfilePath()) { 70 if (profile_path == ProfileManager::GetGuestProfilePath()) {
70 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 71 display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
71 } else { 72 } else {
72 const ProfileInfoCache& cache = 73 ProfileAttributesStorage& storage =
73 g_browser_process->profile_manager()->GetProfileInfoCache(); 74 g_browser_process->profile_manager()->GetProfileAttributesStorage();
74 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
75 75
76 if (index == std::string::npos) 76 ProfileAttributesEntry* entry;
77 if (!storage.GetProfileAttributesWithPath(profile_path, &entry))
77 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 78 return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
78 79
79 // Using the --new-avatar-menu flag, there's a couple of rules about what 80 // Using the --new-avatar-menu flag, there's a couple of rules about what
80 // the avatar button displays. If there's a single profile, with a default 81 // the avatar button displays. If there's a single profile, with a default
81 // name (i.e. of the form Person %d) not manually set, it should display 82 // name (i.e. of the form Person %d) not manually set, it should display
82 // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using 83 // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using
83 // a default name, use the profiles's email address. Otherwise, it 84 // a default name, use the profiles's email address. Otherwise, it
84 // will return the actual name of the profile. 85 // will return the actual name of the profile.
85 const base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); 86 const base::string16 profile_name = entry->GetName();
86 const base::string16 email = cache.GetUserNameOfProfileAtIndex(index); 87 const base::string16 email = entry->GetUserName();
87 bool is_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index) && 88 bool is_default_name = entry->IsUsingDefaultName() &&
88 cache.IsDefaultProfileName(profile_name); 89 storage.IsDefaultProfileName(profile_name);
89 90
90 if (cache.GetNumberOfProfiles() == 1 && is_default_name) 91 if (storage.GetNumberOfProfiles() == 1u && is_default_name)
91 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 92 display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
92 else 93 else
93 display_name = (is_default_name && !email.empty()) ? email : profile_name; 94 display_name = (is_default_name && !email.empty()) ? email : profile_name;
94 } 95 }
95 return display_name; 96 return display_name;
96 } 97 }
97 98
98 base::string16 GetAvatarButtonTextForProfile(Profile* profile) { 99 base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
99 const int kMaxCharactersToDisplay = 15; 100 const int kMaxCharactersToDisplay = 15;
100 base::string16 name = GetAvatarNameForProfile(profile->GetPath()); 101 base::string16 name = GetAvatarNameForProfile(profile->GetPath());
(...skipping 12 matching lines...) Expand all
113 return l10n_util::GetStringFUTF16( 114 return l10n_util::GetStringFUTF16(
114 IDS_LEGACY_SUPERVISED_USER_NEW_AVATAR_LABEL, item.name); 115 IDS_LEGACY_SUPERVISED_USER_NEW_AVATAR_LABEL, item.name);
115 } 116 }
116 if (item.child_account) 117 if (item.child_account)
117 return l10n_util::GetStringFUTF16(IDS_CHILD_AVATAR_LABEL, item.name); 118 return l10n_util::GetStringFUTF16(IDS_CHILD_AVATAR_LABEL, item.name);
118 return item.name; 119 return item.name;
119 } 120 }
120 121
121 void UpdateProfileName(Profile* profile, 122 void UpdateProfileName(Profile* profile,
122 const base::string16& new_profile_name) { 123 const base::string16& new_profile_name) {
123 const ProfileInfoCache& cache = 124 ProfileAttributesEntry* entry;
124 g_browser_process->profile_manager()->GetProfileInfoCache(); 125 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
125 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 126 GetProfileAttributesWithPath(profile->GetPath(), &entry)) {
126 if (profile_index == std::string::npos)
127 return; 127 return;
128 }
128 129
129 if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index)) 130 if (new_profile_name == entry->GetName())
130 return; 131 return;
131 132
132 // This is only called when updating the profile name through the UI, 133 // This is only called when updating the profile name through the UI,
133 // so we can assume the user has done this on purpose. 134 // so we can assume the user has done this on purpose.
134 PrefService* pref_service = profile->GetPrefs(); 135 PrefService* pref_service = profile->GetPrefs();
135 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false); 136 pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);
136 137
137 // Updating the profile preference will cause the cache to be updated for 138 // Updating the profile preference will cause the profile attributes storage
138 // this preference. 139 // to be updated for this preference.
139 pref_service->SetString(prefs::kProfileName, 140 pref_service->SetString(prefs::kProfileName,
140 base::UTF16ToUTF8(new_profile_name)); 141 base::UTF16ToUTF8(new_profile_name));
141 } 142 }
142 143
143 std::vector<std::string> GetSecondaryAccountsForProfile( 144 std::vector<std::string> GetSecondaryAccountsForProfile(
144 Profile* profile, 145 Profile* profile,
145 const std::string& primary_account) { 146 const std::string& primary_account) {
146 std::vector<std::string> accounts = 147 std::vector<std::string> accounts =
147 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 148 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
148 149
149 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 150 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
150 // the primary account too, so we need to remove it from the list. 151 // the primary account too, so we need to remove it from the list.
151 std::vector<std::string>::iterator primary_index = 152 std::vector<std::string>::iterator primary_index =
152 std::find(accounts.begin(), accounts.end(), primary_account); 153 std::find(accounts.begin(), accounts.end(), primary_account);
153 DCHECK(primary_index != accounts.end()); 154 DCHECK(primary_index != accounts.end());
154 accounts.erase(primary_index); 155 accounts.erase(primary_index);
155 156
156 return accounts; 157 return accounts;
157 } 158 }
158 159
159 bool IsRegularOrGuestSession(Browser* browser) { 160 bool IsRegularOrGuestSession(Browser* browser) {
160 Profile* profile = browser->profile(); 161 Profile* profile = browser->profile();
161 return profile->IsGuestSession() || !profile->IsOffTheRecord(); 162 return profile->IsGuestSession() || !profile->IsOffTheRecord();
162 } 163 }
163 164
164 bool IsProfileLocked(const base::FilePath& path) { 165 bool IsProfileLocked(const base::FilePath& profile_path) {
165 const ProfileInfoCache& cache = 166 ProfileAttributesEntry* entry;
166 g_browser_process->profile_manager()->GetProfileInfoCache(); 167 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
167 size_t profile_index = cache.GetIndexOfProfileWithPath(path); 168 GetProfileAttributesWithPath(profile_path, &entry)) {
169 return false;
170 }
168 171
169 if (profile_index == std::string::npos) 172 return entry->IsSigninRequired();
170 return false;
171
172 return cache.ProfileIsSigninRequiredAtIndex(profile_index);
173 } 173 }
174 174
175 void UpdateIsProfileLockEnabledIfNeeded(Profile* profile) { 175 void UpdateIsProfileLockEnabledIfNeeded(Profile* profile) {
176 DCHECK(switches::IsNewProfileManagement()); 176 DCHECK(switches::IsNewProfileManagement());
177 177
178 if (!profile->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain). 178 if (!profile->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain).
179 empty()) 179 empty())
180 return; 180 return;
181 181
182 UpdateGaiaProfileInfoIfNeeded(profile); 182 UpdateGaiaProfileInfoIfNeeded(profile);
(...skipping 21 matching lines...) Expand all
204 204
205 bool SetActiveProfileToGuestIfLocked() { 205 bool SetActiveProfileToGuestIfLocked() {
206 ProfileManager* profile_manager = g_browser_process->profile_manager(); 206 ProfileManager* profile_manager = g_browser_process->profile_manager();
207 207
208 const base::FilePath& active_profile_path = 208 const base::FilePath& active_profile_path =
209 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()); 209 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir());
210 const base::FilePath& guest_path = ProfileManager::GetGuestProfilePath(); 210 const base::FilePath& guest_path = ProfileManager::GetGuestProfilePath();
211 if (active_profile_path == guest_path) 211 if (active_profile_path == guest_path)
212 return true; 212 return true;
213 213
214 const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 214 ProfileAttributesEntry* entry;
215 size_t index = cache.GetIndexOfProfileWithPath(active_profile_path); 215 bool has_entry =
216 if (!cache.ProfileIsSigninRequiredAtIndex(index)) 216 g_browser_process->profile_manager()->GetProfileAttributesStorage().
217 GetProfileAttributesWithPath(active_profile_path, &entry);
218 DCHECK(has_entry);
219
220 if (!entry->IsSigninRequired())
217 return false; 221 return false;
218 222
219 SetLastUsedProfile(guest_path.BaseName().MaybeAsASCII()); 223 SetLastUsedProfile(guest_path.BaseName().MaybeAsASCII());
220 224
221 return true; 225 return true;
222 } 226 }
223 227
224 void RemoveBrowsingDataForProfile(const base::FilePath& profile_path) { 228 void RemoveBrowsingDataForProfile(const base::FilePath& profile_path) {
225 // The BrowsingDataRemover relies on the ResourceDispatcherHost, which is 229 // The BrowsingDataRemover relies on the ResourceDispatcherHost, which is
226 // null in unit tests. 230 // null in unit tests.
(...skipping 19 matching lines...) Expand all
246 // shouldn't have a browser. 250 // shouldn't have a browser.
247 if (profile_dir == base::FilePath(chrome::kSystemProfileDir).AsUTF8Unsafe()) 251 if (profile_dir == base::FilePath(chrome::kSystemProfileDir).AsUTF8Unsafe())
248 return; 252 return;
249 253
250 PrefService* local_state = g_browser_process->local_state(); 254 PrefService* local_state = g_browser_process->local_state();
251 DCHECK(local_state); 255 DCHECK(local_state);
252 local_state->SetString(prefs::kProfileLastUsed, profile_dir); 256 local_state->SetString(prefs::kProfileLastUsed, profile_dir);
253 } 257 }
254 258
255 } // namespace profiles 259 } // namespace profiles
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profiles_state.h ('k') | chrome/browser/ui/webui/signin/signin_create_profile_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698