Cleanup: new common GetProfileSwitcherTextForItem for use on both Win and Mac
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache.cc
blob583441d71cb1544d2c15491af406807a7ec1385c
1 // Copyright (c) 2012 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/profile_info_cache.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/i18n/case_conversion.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/profiler/scoped_tracker.h"
16 #include "base/rand_util.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/values.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/profiles/profile_avatar_downloader.h"
24 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
25 #include "chrome/browser/profiles/profiles_state.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "components/signin/core/common/profile_management_switches.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image.h"
33 #include "ui/gfx/image/image_util.h"
35 #if defined(ENABLE_SUPERVISED_USERS)
36 #include "chrome/browser/supervised_user/supervised_user_constants.h"
37 #endif
39 using content::BrowserThread;
41 namespace {
43 const char kNameKey[] = "name";
44 const char kShortcutNameKey[] = "shortcut_name";
45 const char kGAIANameKey[] = "gaia_name";
46 const char kGAIAGivenNameKey[] = "gaia_given_name";
47 const char kUserNameKey[] = "user_name";
48 const char kIsUsingDefaultNameKey[] = "is_using_default_name";
49 const char kIsUsingDefaultAvatarKey[] = "is_using_default_avatar";
50 const char kAvatarIconKey[] = "avatar_icon";
51 const char kAuthCredentialsKey[] = "local_auth_credentials";
52 const char kUseGAIAPictureKey[] = "use_gaia_picture";
53 const char kBackgroundAppsKey[] = "background_apps";
54 const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name";
55 const char kIsOmittedFromProfileListKey[] = "is_omitted_from_profile_list";
56 const char kSigninRequiredKey[] = "signin_required";
57 const char kSupervisedUserId[] = "managed_user_id";
58 const char kProfileIsEphemeral[] = "is_ephemeral";
59 const char kActiveTimeKey[] = "active_time";
60 const char kIsAuthErrorKey[] = "is_auth_error";
62 // First eight are generic icons, which use IDS_NUMBERED_PROFILE_NAME.
63 const int kDefaultNames[] = {
64 IDS_DEFAULT_AVATAR_NAME_8,
65 IDS_DEFAULT_AVATAR_NAME_9,
66 IDS_DEFAULT_AVATAR_NAME_10,
67 IDS_DEFAULT_AVATAR_NAME_11,
68 IDS_DEFAULT_AVATAR_NAME_12,
69 IDS_DEFAULT_AVATAR_NAME_13,
70 IDS_DEFAULT_AVATAR_NAME_14,
71 IDS_DEFAULT_AVATAR_NAME_15,
72 IDS_DEFAULT_AVATAR_NAME_16,
73 IDS_DEFAULT_AVATAR_NAME_17,
74 IDS_DEFAULT_AVATAR_NAME_18,
75 IDS_DEFAULT_AVATAR_NAME_19,
76 IDS_DEFAULT_AVATAR_NAME_20,
77 IDS_DEFAULT_AVATAR_NAME_21,
78 IDS_DEFAULT_AVATAR_NAME_22,
79 IDS_DEFAULT_AVATAR_NAME_23,
80 IDS_DEFAULT_AVATAR_NAME_24,
81 IDS_DEFAULT_AVATAR_NAME_25,
82 IDS_DEFAULT_AVATAR_NAME_26
85 typedef std::vector<unsigned char> ImageData;
87 // Writes |data| to disk and takes ownership of the pointer. On successful
88 // completion, it runs |callback|.
89 void SaveBitmap(scoped_ptr<ImageData> data,
90 const base::FilePath& image_path,
91 const base::Closure& callback) {
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
94 // Make sure the destination directory exists.
95 base::FilePath dir = image_path.DirName();
96 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) {
97 LOG(ERROR) << "Failed to create parent directory.";
98 return;
101 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]),
102 data->size()) == -1) {
103 LOG(ERROR) << "Failed to save image to file.";
104 return;
107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
110 // Reads a PNG from disk and decodes it. If the bitmap was successfully read
111 // from disk the then |out_image| will contain the bitmap image, otherwise it
112 // will be NULL.
113 void ReadBitmap(const base::FilePath& image_path,
114 gfx::Image** out_image) {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
116 *out_image = NULL;
118 // If the path doesn't exist, don't even try reading it.
119 if (!base::PathExists(image_path))
120 return;
122 std::string image_data;
123 if (!base::ReadFileToString(image_path, &image_data)) {
124 LOG(ERROR) << "Failed to read PNG file from disk.";
125 return;
128 gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
129 base::RefCountedString::TakeString(&image_data));
130 if (image.IsEmpty()) {
131 LOG(ERROR) << "Failed to decode PNG file.";
132 return;
135 *out_image = new gfx::Image(image);
138 void RunCallbackIfFileMissing(const base::FilePath& file_path,
139 const base::Closure& callback) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
141 if (!base::PathExists(file_path))
142 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
145 void DeleteBitmap(const base::FilePath& image_path) {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
147 base::DeleteFile(image_path, false);
150 } // namespace
152 ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
153 const base::FilePath& user_data_dir)
154 : prefs_(prefs),
155 user_data_dir_(user_data_dir),
156 disable_avatar_download_for_testing_(false) {
157 // Populate the cache
158 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
159 base::DictionaryValue* cache = update.Get();
160 for (base::DictionaryValue::Iterator it(*cache);
161 !it.IsAtEnd(); it.Advance()) {
162 base::DictionaryValue* info = NULL;
163 cache->GetDictionaryWithoutPathExpansion(it.key(), &info);
164 base::string16 name;
165 info->GetString(kNameKey, &name);
166 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key());
168 bool using_default_name;
169 if (!info->GetBoolean(kIsUsingDefaultNameKey, &using_default_name)) {
170 // If the preference hasn't been set, and the name is default, assume
171 // that the user hasn't done this on purpose.
172 using_default_name = IsDefaultProfileName(name);
173 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name);
176 // For profiles that don't have the "using default avatar" state set yet,
177 // assume it's the same as the "using default name" state.
178 if (!info->HasKey(kIsUsingDefaultAvatarKey)) {
179 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name);
183 // If needed, start downloading the high-res avatars and migrate any legacy
184 // profile names.
185 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_)
186 MigrateLegacyProfileNamesAndDownloadAvatars();
189 ProfileInfoCache::~ProfileInfoCache() {
190 STLDeleteContainerPairSecondPointers(
191 cached_avatar_images_.begin(), cached_avatar_images_.end());
192 STLDeleteContainerPairSecondPointers(
193 avatar_images_downloads_in_progress_.begin(),
194 avatar_images_downloads_in_progress_.end());
197 void ProfileInfoCache::AddProfileToCache(
198 const base::FilePath& profile_path,
199 const base::string16& name,
200 const base::string16& username,
201 size_t icon_index,
202 const std::string& supervised_user_id) {
203 std::string key = CacheKeyFromProfilePath(profile_path);
204 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
205 base::DictionaryValue* cache = update.Get();
207 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue);
208 info->SetString(kNameKey, name);
209 info->SetString(kUserNameKey, username);
210 info->SetString(kAvatarIconKey,
211 profiles::GetDefaultAvatarIconUrl(icon_index));
212 // Default value for whether background apps are running is false.
213 info->SetBoolean(kBackgroundAppsKey, false);
214 info->SetString(kSupervisedUserId, supervised_user_id);
215 info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty());
216 info->SetBoolean(kProfileIsEphemeral, false);
217 info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultProfileName(name));
218 // Assume newly created profiles use a default avatar.
219 info->SetBoolean(kIsUsingDefaultAvatarKey, true);
220 cache->SetWithoutPathExpansion(key, info.release());
222 sorted_keys_.insert(FindPositionForProfile(key, name), key);
224 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_)
225 DownloadHighResAvatarIfNeeded(icon_index, profile_path);
227 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
228 observer_list_,
229 OnProfileAdded(profile_path));
232 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) {
233 observer_list_.AddObserver(obs);
236 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) {
237 observer_list_.RemoveObserver(obs);
240 void ProfileInfoCache::DeleteProfileFromCache(
241 const base::FilePath& profile_path) {
242 size_t profile_index = GetIndexOfProfileWithPath(profile_path);
243 if (profile_index == std::string::npos) {
244 NOTREACHED();
245 return;
247 base::string16 name = GetNameOfProfileAtIndex(profile_index);
249 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
250 observer_list_,
251 OnProfileWillBeRemoved(profile_path));
253 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
254 base::DictionaryValue* cache = update.Get();
255 std::string key = CacheKeyFromProfilePath(profile_path);
256 cache->Remove(key, NULL);
257 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key));
259 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
260 observer_list_,
261 OnProfileWasRemoved(profile_path, name));
264 size_t ProfileInfoCache::GetNumberOfProfiles() const {
265 return sorted_keys_.size();
268 size_t ProfileInfoCache::GetIndexOfProfileWithPath(
269 const base::FilePath& profile_path) const {
270 if (profile_path.DirName() != user_data_dir_)
271 return std::string::npos;
272 std::string search_key = CacheKeyFromProfilePath(profile_path);
273 for (size_t i = 0; i < sorted_keys_.size(); ++i) {
274 if (sorted_keys_[i] == search_key)
275 return i;
277 return std::string::npos;
280 base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const {
281 base::string16 name;
282 // Unless the user has customized the profile name, we should use the
283 // profile's Gaia given name, if it's available.
284 if (ProfileIsUsingDefaultNameAtIndex(index)) {
285 base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index);
286 name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name;
288 if (name.empty())
289 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name);
290 return name;
293 base::string16 ProfileInfoCache::GetShortcutNameOfProfileAtIndex(size_t index)
294 const {
295 base::string16 shortcut_name;
296 GetInfoForProfileAtIndex(index)->GetString(
297 kShortcutNameKey, &shortcut_name);
298 return shortcut_name;
301 base::FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const {
302 return user_data_dir_.AppendASCII(sorted_keys_[index]);
305 base::Time ProfileInfoCache::GetProfileActiveTimeAtIndex(size_t index) const {
306 double dt;
307 if (GetInfoForProfileAtIndex(index)->GetDouble(kActiveTimeKey, &dt)) {
308 return base::Time::FromDoubleT(dt);
309 } else {
310 return base::Time();
314 base::string16 ProfileInfoCache::GetUserNameOfProfileAtIndex(
315 size_t index) const {
316 base::string16 user_name;
317 GetInfoForProfileAtIndex(index)->GetString(kUserNameKey, &user_name);
318 return user_name;
321 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex(
322 size_t index) {
323 if (IsUsingGAIAPictureOfProfileAtIndex(index)) {
324 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index);
325 if (image)
326 return *image;
329 // Use the high resolution version of the avatar if it exists.
330 if (switches::IsNewAvatarMenu()) {
331 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index);
332 if (image)
333 return *image;
336 int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex(
337 GetAvatarIconIndexOfProfileAtIndex(index));
338 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
341 std::string ProfileInfoCache::GetLocalAuthCredentialsOfProfileAtIndex(
342 size_t index) const {
343 std::string credentials;
344 GetInfoForProfileAtIndex(index)->GetString(kAuthCredentialsKey, &credentials);
345 return credentials;
348 bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex(
349 size_t index) const {
350 bool background_app_status;
351 if (!GetInfoForProfileAtIndex(index)->GetBoolean(kBackgroundAppsKey,
352 &background_app_status)) {
353 return false;
355 return background_app_status;
358 base::string16 ProfileInfoCache::GetGAIANameOfProfileAtIndex(
359 size_t index) const {
360 base::string16 name;
361 GetInfoForProfileAtIndex(index)->GetString(kGAIANameKey, &name);
362 return name;
365 base::string16 ProfileInfoCache::GetGAIAGivenNameOfProfileAtIndex(
366 size_t index) const {
367 base::string16 name;
368 GetInfoForProfileAtIndex(index)->GetString(kGAIAGivenNameKey, &name);
369 return name;
372 const gfx::Image* ProfileInfoCache::GetGAIAPictureOfProfileAtIndex(
373 size_t index) const {
374 base::FilePath path = GetPathOfProfileAtIndex(index);
375 std::string key = CacheKeyFromProfilePath(path);
377 std::string file_name;
378 GetInfoForProfileAtIndex(index)->GetString(
379 kGAIAPictureFileNameKey, &file_name);
381 // If the picture is not on disk then return NULL.
382 if (file_name.empty())
383 return NULL;
385 base::FilePath image_path = path.AppendASCII(file_name);
386 return LoadAvatarPictureFromPath(path, key, image_path);
389 bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex(size_t index) const {
390 bool value = false;
391 GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIAPictureKey, &value);
392 if (!value) {
393 // Prefer the GAIA avatar over a non-customized avatar.
394 value = ProfileIsUsingDefaultAvatarAtIndex(index) &&
395 GetGAIAPictureOfProfileAtIndex(index);
397 return value;
400 bool ProfileInfoCache::ProfileIsSupervisedAtIndex(size_t index) const {
401 return !GetSupervisedUserIdOfProfileAtIndex(index).empty();
404 bool ProfileInfoCache::ProfileIsChildAtIndex(size_t index) const {
405 #if defined(ENABLE_SUPERVISED_USERS)
406 return GetSupervisedUserIdOfProfileAtIndex(index) ==
407 supervised_users::kChildAccountSUID;
408 #else
409 return false;
410 #endif
413 bool ProfileInfoCache::ProfileIsLegacySupervisedAtIndex(size_t index) const {
414 return ProfileIsSupervisedAtIndex(index) && !ProfileIsChildAtIndex(index);
417 bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const {
418 bool value = false;
419 GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey,
420 &value);
421 return value;
424 bool ProfileInfoCache::ProfileIsSigninRequiredAtIndex(size_t index) const {
425 bool value = false;
426 GetInfoForProfileAtIndex(index)->GetBoolean(kSigninRequiredKey, &value);
427 return value;
430 std::string ProfileInfoCache::GetSupervisedUserIdOfProfileAtIndex(
431 size_t index) const {
432 std::string supervised_user_id;
433 GetInfoForProfileAtIndex(index)->GetString(kSupervisedUserId,
434 &supervised_user_id);
435 return supervised_user_id;
438 bool ProfileInfoCache::ProfileIsEphemeralAtIndex(size_t index) const {
439 bool value = false;
440 GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value);
441 return value;
444 bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const {
445 bool value = false;
446 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultNameKey, &value);
447 return value;
450 bool ProfileInfoCache::ProfileIsUsingDefaultAvatarAtIndex(size_t index) const {
451 bool value = false;
452 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultAvatarKey, &value);
453 return value;
456 bool ProfileInfoCache::ProfileIsAuthErrorAtIndex(size_t index) const {
457 bool value = false;
458 GetInfoForProfileAtIndex(index)->GetBoolean(kIsAuthErrorKey, &value);
459 return value;
462 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index)
463 const {
464 std::string icon_url;
465 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url);
466 size_t icon_index = 0;
467 if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index))
468 DLOG(WARNING) << "Unknown avatar icon: " << icon_url;
470 return icon_index;
473 void ProfileInfoCache::SetProfileActiveTimeAtIndex(size_t index) {
474 scoped_ptr<base::DictionaryValue> info(
475 GetInfoForProfileAtIndex(index)->DeepCopy());
476 info->SetDouble(kActiveTimeKey, base::Time::Now().ToDoubleT());
477 // This takes ownership of |info|.
478 SetInfoForProfileAtIndex(index, info.release());
481 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index,
482 const base::string16& name) {
483 scoped_ptr<base::DictionaryValue> info(
484 GetInfoForProfileAtIndex(index)->DeepCopy());
485 base::string16 current_name;
486 info->GetString(kNameKey, &current_name);
487 if (name == current_name)
488 return;
490 base::string16 old_display_name = GetNameOfProfileAtIndex(index);
491 info->SetString(kNameKey, name);
493 // This takes ownership of |info|.
494 SetInfoForProfileAtIndex(index, info.release());
496 base::string16 new_display_name = GetNameOfProfileAtIndex(index);
497 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
498 UpdateSortForProfileIndex(index);
500 if (old_display_name != new_display_name) {
501 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
502 observer_list_,
503 OnProfileNameChanged(profile_path, old_display_name));
507 void ProfileInfoCache::SetShortcutNameOfProfileAtIndex(
508 size_t index,
509 const base::string16& shortcut_name) {
510 if (shortcut_name == GetShortcutNameOfProfileAtIndex(index))
511 return;
512 scoped_ptr<base::DictionaryValue> info(
513 GetInfoForProfileAtIndex(index)->DeepCopy());
514 info->SetString(kShortcutNameKey, shortcut_name);
515 // This takes ownership of |info|.
516 SetInfoForProfileAtIndex(index, info.release());
519 void ProfileInfoCache::SetUserNameOfProfileAtIndex(
520 size_t index,
521 const base::string16& user_name) {
522 if (user_name == GetUserNameOfProfileAtIndex(index))
523 return;
525 scoped_ptr<base::DictionaryValue> info(
526 GetInfoForProfileAtIndex(index)->DeepCopy());
527 info->SetString(kUserNameKey, user_name);
528 // This takes ownership of |info|.
529 SetInfoForProfileAtIndex(index, info.release());
531 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
532 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
533 observer_list_,
534 OnProfileUserNameChanged(profile_path));
537 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
538 size_t icon_index) {
539 scoped_ptr<base::DictionaryValue> info(
540 GetInfoForProfileAtIndex(index)->DeepCopy());
541 info->SetString(kAvatarIconKey,
542 profiles::GetDefaultAvatarIconUrl(icon_index));
543 // This takes ownership of |info|.
544 SetInfoForProfileAtIndex(index, info.release());
546 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
548 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_)
549 DownloadHighResAvatarIfNeeded(icon_index, profile_path);
551 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
552 observer_list_,
553 OnProfileAvatarChanged(profile_path));
556 void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index,
557 bool is_omitted) {
558 if (IsOmittedProfileAtIndex(index) == is_omitted)
559 return;
560 scoped_ptr<base::DictionaryValue> info(
561 GetInfoForProfileAtIndex(index)->DeepCopy());
562 info->SetBoolean(kIsOmittedFromProfileListKey, is_omitted);
563 // This takes ownership of |info|.
564 SetInfoForProfileAtIndex(index, info.release());
566 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
567 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
568 observer_list_,
569 OnProfileIsOmittedChanged(profile_path));
572 void ProfileInfoCache::SetSupervisedUserIdOfProfileAtIndex(
573 size_t index,
574 const std::string& id) {
575 if (GetSupervisedUserIdOfProfileAtIndex(index) == id)
576 return;
577 scoped_ptr<base::DictionaryValue> info(
578 GetInfoForProfileAtIndex(index)->DeepCopy());
579 info->SetString(kSupervisedUserId, id);
580 // This takes ownership of |info|.
581 SetInfoForProfileAtIndex(index, info.release());
583 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
584 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
585 observer_list_,
586 OnProfileSupervisedUserIdChanged(profile_path));
589 void ProfileInfoCache::SetLocalAuthCredentialsOfProfileAtIndex(
590 size_t index,
591 const std::string& credentials) {
592 scoped_ptr<base::DictionaryValue> info(
593 GetInfoForProfileAtIndex(index)->DeepCopy());
594 info->SetString(kAuthCredentialsKey, credentials);
595 // This takes ownership of |info|.
596 SetInfoForProfileAtIndex(index, info.release());
599 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex(
600 size_t index,
601 bool running_background_apps) {
602 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps)
603 return;
604 scoped_ptr<base::DictionaryValue> info(
605 GetInfoForProfileAtIndex(index)->DeepCopy());
606 info->SetBoolean(kBackgroundAppsKey, running_background_apps);
607 // This takes ownership of |info|.
608 SetInfoForProfileAtIndex(index, info.release());
611 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index,
612 const base::string16& name) {
613 if (name == GetGAIANameOfProfileAtIndex(index))
614 return;
616 base::string16 old_display_name = GetNameOfProfileAtIndex(index);
617 scoped_ptr<base::DictionaryValue> info(
618 GetInfoForProfileAtIndex(index)->DeepCopy());
619 info->SetString(kGAIANameKey, name);
620 // This takes ownership of |info|.
621 SetInfoForProfileAtIndex(index, info.release());
622 base::string16 new_display_name = GetNameOfProfileAtIndex(index);
623 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
624 UpdateSortForProfileIndex(index);
626 if (old_display_name != new_display_name) {
627 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
628 observer_list_,
629 OnProfileNameChanged(profile_path, old_display_name));
633 void ProfileInfoCache::SetGAIAGivenNameOfProfileAtIndex(
634 size_t index,
635 const base::string16& name) {
636 if (name == GetGAIAGivenNameOfProfileAtIndex(index))
637 return;
639 base::string16 old_display_name = GetNameOfProfileAtIndex(index);
640 scoped_ptr<base::DictionaryValue> info(
641 GetInfoForProfileAtIndex(index)->DeepCopy());
642 info->SetString(kGAIAGivenNameKey, name);
643 // This takes ownership of |info|.
644 SetInfoForProfileAtIndex(index, info.release());
645 base::string16 new_display_name = GetNameOfProfileAtIndex(index);
646 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
647 UpdateSortForProfileIndex(index);
649 if (old_display_name != new_display_name) {
650 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
651 observer_list_,
652 OnProfileNameChanged(profile_path, old_display_name));
656 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index,
657 const gfx::Image* image) {
658 base::FilePath path = GetPathOfProfileAtIndex(index);
659 std::string key = CacheKeyFromProfilePath(path);
661 // Delete the old bitmap from cache.
662 std::map<std::string, gfx::Image*>::iterator it =
663 cached_avatar_images_.find(key);
664 if (it != cached_avatar_images_.end()) {
665 delete it->second;
666 cached_avatar_images_.erase(it);
669 std::string old_file_name;
670 GetInfoForProfileAtIndex(index)->GetString(
671 kGAIAPictureFileNameKey, &old_file_name);
672 std::string new_file_name;
674 if (!image) {
675 // Delete the old bitmap from disk.
676 if (!old_file_name.empty()) {
677 base::FilePath image_path = path.AppendASCII(old_file_name);
678 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
679 base::Bind(&DeleteBitmap, image_path));
681 } else {
682 // Save the new bitmap to disk.
683 new_file_name =
684 old_file_name.empty() ? profiles::kGAIAPictureFileName : old_file_name;
685 base::FilePath image_path = path.AppendASCII(new_file_name);
686 SaveAvatarImageAtPath(
687 image, key, image_path, GetPathOfProfileAtIndex(index));
690 scoped_ptr<base::DictionaryValue> info(
691 GetInfoForProfileAtIndex(index)->DeepCopy());
692 info->SetString(kGAIAPictureFileNameKey, new_file_name);
693 // This takes ownership of |info|.
694 SetInfoForProfileAtIndex(index, info.release());
696 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
697 observer_list_,
698 OnProfileAvatarChanged(path));
701 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index,
702 bool value) {
703 scoped_ptr<base::DictionaryValue> info(
704 GetInfoForProfileAtIndex(index)->DeepCopy());
705 info->SetBoolean(kUseGAIAPictureKey, value);
706 // This takes ownership of |info|.
707 SetInfoForProfileAtIndex(index, info.release());
709 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
710 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
711 observer_list_,
712 OnProfileAvatarChanged(profile_path));
715 void ProfileInfoCache::SetProfileSigninRequiredAtIndex(size_t index,
716 bool value) {
717 if (value == ProfileIsSigninRequiredAtIndex(index))
718 return;
720 scoped_ptr<base::DictionaryValue> info(
721 GetInfoForProfileAtIndex(index)->DeepCopy());
722 info->SetBoolean(kSigninRequiredKey, value);
723 // This takes ownership of |info|.
724 SetInfoForProfileAtIndex(index, info.release());
726 base::FilePath profile_path = GetPathOfProfileAtIndex(index);
727 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
728 observer_list_,
729 OnProfileSigninRequiredChanged(profile_path));
732 void ProfileInfoCache::SetProfileIsEphemeralAtIndex(size_t index, bool value) {
733 if (value == ProfileIsEphemeralAtIndex(index))
734 return;
736 scoped_ptr<base::DictionaryValue> info(
737 GetInfoForProfileAtIndex(index)->DeepCopy());
738 info->SetBoolean(kProfileIsEphemeral, value);
739 // This takes ownership of |info|.
740 SetInfoForProfileAtIndex(index, info.release());
743 void ProfileInfoCache::SetProfileIsUsingDefaultNameAtIndex(
744 size_t index, bool value) {
745 if (value == ProfileIsUsingDefaultNameAtIndex(index))
746 return;
748 base::string16 old_display_name = GetNameOfProfileAtIndex(index);
750 scoped_ptr<base::DictionaryValue> info(
751 GetInfoForProfileAtIndex(index)->DeepCopy());
752 info->SetBoolean(kIsUsingDefaultNameKey, value);
753 // This takes ownership of |info|.
754 SetInfoForProfileAtIndex(index, info.release());
756 base::string16 new_display_name = GetNameOfProfileAtIndex(index);
757 const base::FilePath profile_path = GetPathOfProfileAtIndex(index);
759 if (old_display_name != new_display_name) {
760 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
761 observer_list_,
762 OnProfileNameChanged(profile_path, old_display_name));
766 void ProfileInfoCache::SetProfileIsUsingDefaultAvatarAtIndex(
767 size_t index, bool value) {
768 if (value == ProfileIsUsingDefaultAvatarAtIndex(index))
769 return;
771 scoped_ptr<base::DictionaryValue> info(
772 GetInfoForProfileAtIndex(index)->DeepCopy());
773 info->SetBoolean(kIsUsingDefaultAvatarKey, value);
774 // This takes ownership of |info|.
775 SetInfoForProfileAtIndex(index, info.release());
778 void ProfileInfoCache::SetProfileIsAuthErrorAtIndex(size_t index, bool value) {
779 if (value == ProfileIsAuthErrorAtIndex(index))
780 return;
782 scoped_ptr<base::DictionaryValue> info(
783 GetInfoForProfileAtIndex(index)->DeepCopy());
784 info->SetBoolean(kIsAuthErrorKey, value);
785 // This takes ownership of |info|.
786 SetInfoForProfileAtIndex(index, info.release());
789 bool ProfileInfoCache::IsDefaultProfileName(const base::string16& name) const {
790 // Check if it's a "First user" old-style name.
791 if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME) ||
792 name == l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME))
793 return true;
795 // Check if it's one of the old-style profile names.
796 for (size_t i = 0; i < arraysize(kDefaultNames); ++i) {
797 if (name == l10n_util::GetStringUTF16(kDefaultNames[i]))
798 return true;
801 // Check whether it's one of the "Person %d" style names.
802 std::string default_name_format = l10n_util::GetStringFUTF8(
803 IDS_NEW_NUMBERED_PROFILE_NAME, base::ASCIIToUTF16("%d"));
805 int generic_profile_number; // Unused. Just a placeholder for sscanf.
806 int assignments = sscanf(base::UTF16ToUTF8(name).c_str(),
807 default_name_format.c_str(),
808 &generic_profile_number);
809 // Unless it matched the format, this is a custom name.
810 return assignments == 1;
813 base::string16 ProfileInfoCache::ChooseNameForNewProfile(
814 size_t icon_index) const {
815 base::string16 name;
816 for (int name_index = 1; ; ++name_index) {
817 if (switches::IsNewAvatarMenu()) {
818 name = l10n_util::GetStringFUTF16Int(IDS_NEW_NUMBERED_PROFILE_NAME,
819 name_index);
820 } else if (icon_index < profiles::GetGenericAvatarIconCount()) {
821 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME,
822 name_index);
823 } else {
824 name = l10n_util::GetStringUTF16(
825 kDefaultNames[icon_index - profiles::GetGenericAvatarIconCount()]);
826 if (name_index > 1)
827 name.append(base::UTF8ToUTF16(base::IntToString(name_index)));
830 // Loop through previously named profiles to ensure we're not duplicating.
831 bool name_found = false;
832 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
833 if (GetNameOfProfileAtIndex(i) == name) {
834 name_found = true;
835 break;
838 if (!name_found)
839 return name;
843 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const {
844 size_t icon_index = 0;
845 // Try to find a unique, non-generic icon.
846 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index))
847 return icon_index;
848 // Try to find any unique icon.
849 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index))
850 return icon_index;
851 // Settle for any random icon, even if it's not unique.
852 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index))
853 return icon_index;
855 NOTREACHED();
856 return 0;
859 const base::FilePath& ProfileInfoCache::GetUserDataDir() const {
860 return user_data_dir_;
863 // static
864 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) {
865 registry->RegisterDictionaryPref(prefs::kProfileInfoCache);
868 void ProfileInfoCache::DownloadHighResAvatarIfNeeded(
869 size_t icon_index,
870 const base::FilePath& profile_path) {
871 // Downloading is only supported on desktop.
872 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
873 return;
874 #endif
875 DCHECK(!disable_avatar_download_for_testing_);
877 const base::FilePath& file_path =
878 profiles::GetPathOfHighResAvatarAtIndex(icon_index);
879 base::Closure callback =
880 base::Bind(&ProfileInfoCache::DownloadHighResAvatar,
881 AsWeakPtr(),
882 icon_index,
883 profile_path);
884 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
885 base::Bind(&RunCallbackIfFileMissing, file_path, callback));
888 void ProfileInfoCache::SaveAvatarImageAtPath(
889 const gfx::Image* image,
890 const std::string& key,
891 const base::FilePath& image_path,
892 const base::FilePath& profile_path) {
893 cached_avatar_images_[key] = new gfx::Image(*image);
895 scoped_ptr<ImageData> data(new ImageData);
896 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes();
897 data->assign(png_data->front(), png_data->front() + png_data->size());
899 // Remove the file from the list of downloads in progress. Note that this list
900 // only contains the high resolution avatars, and not the Gaia profile images.
901 auto downloader_iter = avatar_images_downloads_in_progress_.find(key);
902 if (downloader_iter != avatar_images_downloads_in_progress_.end()) {
903 // We mustn't delete the avatar downloader right here, since we're being
904 // called by it.
905 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE,
906 downloader_iter->second);
907 avatar_images_downloads_in_progress_.erase(downloader_iter);
910 if (!data->size()) {
911 LOG(ERROR) << "Failed to PNG encode the image.";
912 } else {
913 base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved,
914 AsWeakPtr(), key, profile_path);
915 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
916 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback));
920 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex(
921 size_t index) const {
922 DCHECK_LT(index, GetNumberOfProfiles());
923 const base::DictionaryValue* cache =
924 prefs_->GetDictionary(prefs::kProfileInfoCache);
925 const base::DictionaryValue* info = NULL;
926 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info);
927 return info;
930 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index,
931 base::DictionaryValue* info) {
932 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
933 base::DictionaryValue* cache = update.Get();
934 cache->SetWithoutPathExpansion(sorted_keys_[index], info);
937 std::string ProfileInfoCache::CacheKeyFromProfilePath(
938 const base::FilePath& profile_path) const {
939 DCHECK(user_data_dir_ == profile_path.DirName());
940 base::FilePath base_name = profile_path.BaseName();
941 return base_name.MaybeAsASCII();
944 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile(
945 const std::string& search_key,
946 const base::string16& search_name) {
947 base::string16 search_name_l = base::i18n::ToLower(search_name);
948 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
949 base::string16 name_l = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
950 int name_compare = search_name_l.compare(name_l);
951 if (name_compare < 0)
952 return sorted_keys_.begin() + i;
953 if (name_compare == 0) {
954 int key_compare = search_key.compare(sorted_keys_[i]);
955 if (key_compare < 0)
956 return sorted_keys_.begin() + i;
959 return sorted_keys_.end();
962 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const {
963 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
964 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index)
965 return false;
967 return true;
970 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile(
971 bool allow_generic_icon,
972 bool must_be_unique,
973 size_t* out_icon_index) const {
974 // Always allow all icons for new profiles if using the
975 // --new-avatar-menu flag.
976 if (switches::IsNewAvatarMenu())
977 allow_generic_icon = true;
978 size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount();
979 size_t end = profiles::GetDefaultAvatarIconCount();
980 size_t count = end - start;
982 int rand = base::RandInt(0, count);
983 for (size_t i = 0; i < count; ++i) {
984 size_t icon_index = start + (rand + i) % count;
985 if (!must_be_unique || IconIndexIsUnique(icon_index)) {
986 *out_icon_index = icon_index;
987 return true;
991 return false;
994 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) {
995 base::string16 name = GetNameOfProfileAtIndex(index);
997 // Remove and reinsert key in |sorted_keys_| to alphasort.
998 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index));
999 std::vector<std::string>::iterator key_it =
1000 std::find(sorted_keys_.begin(), sorted_keys_.end(), key);
1001 DCHECK(key_it != sorted_keys_.end());
1002 sorted_keys_.erase(key_it);
1003 sorted_keys_.insert(FindPositionForProfile(key, name), key);
1006 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex(
1007 size_t index) const {
1008 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index);
1009 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index);
1011 // If this is the placeholder avatar, it is already included in the
1012 // resources, so it doesn't need to be downloaded.
1013 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) {
1014 return &ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1015 profiles::GetPlaceholderAvatarIconResourceID());
1018 base::FilePath image_path =
1019 profiles::GetPathOfHighResAvatarAtIndex(avatar_index);
1020 return LoadAvatarPictureFromPath(GetPathOfProfileAtIndex(index),
1021 key, image_path);
1024 void ProfileInfoCache::DownloadHighResAvatar(
1025 size_t icon_index,
1026 const base::FilePath& profile_path) {
1027 // Downloading is only supported on desktop.
1028 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
1029 return;
1030 #endif
1031 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1032 // is fixed.
1033 tracked_objects::ScopedTracker tracking_profile1(
1034 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1035 "461175 ProfileInfoCache::DownloadHighResAvatar::GetFileName"));
1036 const std::string file_name =
1037 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index);
1038 // If the file is already being downloaded, don't start another download.
1039 if (avatar_images_downloads_in_progress_.count(file_name))
1040 return;
1042 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1043 // is fixed.
1044 tracked_objects::ScopedTracker tracking_profile2(
1045 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1046 "461175 ProfileInfoCache::DownloadHighResAvatar::MakeDownloader"));
1047 // Start the download for this file. The cache takes ownership of the
1048 // |avatar_downloader|, which will be deleted when the download completes, or
1049 // if that never happens, when the ProfileInfoCache is destroyed.
1050 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader(
1051 icon_index,
1052 profile_path,
1053 this);
1054 avatar_images_downloads_in_progress_[file_name] = avatar_downloader;
1056 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1057 // is fixed.
1058 tracked_objects::ScopedTracker tracking_profile3(
1059 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1060 "461175 ProfileInfoCache::DownloadHighResAvatar::StartDownload"));
1061 avatar_downloader->Start();
1064 const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath(
1065 const base::FilePath& profile_path,
1066 const std::string& key,
1067 const base::FilePath& image_path) const {
1068 // If the picture is already loaded then use it.
1069 if (cached_avatar_images_.count(key)) {
1070 if (cached_avatar_images_[key]->IsEmpty())
1071 return NULL;
1072 return cached_avatar_images_[key];
1075 // Don't download the image if downloading is disabled for tests.
1076 if (disable_avatar_download_for_testing_)
1077 return NULL;
1079 // If the picture is already being loaded then don't try loading it again.
1080 if (cached_avatar_images_loading_[key])
1081 return NULL;
1082 cached_avatar_images_loading_[key] = true;
1084 gfx::Image** image = new gfx::Image*;
1085 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
1086 base::Bind(&ReadBitmap, image_path, image),
1087 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded,
1088 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(),
1089 profile_path, key, image));
1090 return NULL;
1093 void ProfileInfoCache::OnAvatarPictureLoaded(const base::FilePath& profile_path,
1094 const std::string& key,
1095 gfx::Image** image) const {
1096 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1097 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1098 // is fixed.
1099 tracked_objects::ScopedTracker tracking_profile1(
1100 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1101 "461175 ProfileInfoCache::OnAvatarPictureLoaded::Start"));
1103 cached_avatar_images_loading_[key] = false;
1104 delete cached_avatar_images_[key];
1106 if (*image) {
1107 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1108 // is fixed.
1109 tracked_objects::ScopedTracker tracking_profile2(
1110 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1111 "461175 ProfileInfoCache::OnAvatarPictureLoaded::SetImage"));
1112 cached_avatar_images_[key] = *image;
1113 } else {
1114 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1115 // is fixed.
1116 tracked_objects::ScopedTracker tracking_profile3(
1117 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1118 "461175 ProfileInfoCache::OnAvatarPictureLoaded::MakeEmptyImage"));
1119 // Place an empty image in the cache to avoid reloading it again.
1120 cached_avatar_images_[key] = new gfx::Image();
1122 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175
1123 // is fixed.
1124 tracked_objects::ScopedTracker tracking_profile4(
1125 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1126 "461175 ProfileInfoCache::OnAvatarPictureLoaded::DeleteImage"));
1127 delete image;
1129 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
1130 observer_list_,
1131 OnProfileHighResAvatarLoaded(profile_path));
1134 void ProfileInfoCache::OnAvatarPictureSaved(
1135 const std::string& file_name,
1136 const base::FilePath& profile_path) {
1137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1139 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
1140 observer_list_,
1141 OnProfileHighResAvatarLoaded(profile_path));
1144 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() {
1145 DCHECK(switches::IsNewAvatarMenu());
1147 // Only do this on desktop platforms.
1148 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
1149 // Migrate any legacy profile names ("First user", "Default Profile") to
1150 // new style default names ("Person 1"). The problem here is that every
1151 // time you rename a profile, the ProfileInfoCache sorts itself, so
1152 // whatever you were iterating through is no longer valid. We need to
1153 // save a list of the profile paths (which thankfully do not change) that
1154 // need to be renamed. We also can't pre-compute the new names, as they
1155 // depend on the names of all the other profiles in the info cache, so they
1156 // need to be re-computed after each rename.
1157 std::vector<base::FilePath> profiles_to_rename;
1159 const base::string16 default_profile_name = base::i18n::ToLower(
1160 l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME));
1161 const base::string16 default_legacy_profile_name = base::i18n::ToLower(
1162 l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME));
1164 for (size_t i = 0; i < GetNumberOfProfiles(); i++) {
1165 DownloadHighResAvatarIfNeeded(GetAvatarIconIndexOfProfileAtIndex(i),
1166 GetPathOfProfileAtIndex(i));
1168 base::string16 name = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
1169 if (name == default_profile_name || name == default_legacy_profile_name)
1170 profiles_to_rename.push_back(GetPathOfProfileAtIndex(i));
1173 // Rename the necessary profiles.
1174 std::vector<base::FilePath>::const_iterator it;
1175 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) {
1176 size_t profile_index = GetIndexOfProfileWithPath(*it);
1177 SetProfileIsUsingDefaultNameAtIndex(profile_index, true);
1178 // This will assign a new "Person %d" type name and re-sort the cache.
1179 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile(
1180 GetAvatarIconIndexOfProfileAtIndex(profile_index)));
1182 #endif