Commit be073e56 authored by noms's avatar noms Committed by Commit bot

[Profiles] Update legacy default profile names.

In the new profile management world, you can find yourself using a super old
profile named by default "Default Profile". These names should be migrated to
the new style default names ("Person 1").

The code is fantastically special because the ProfileInfoCache re-sorts itself after
each rename, so doing a batch rename is really not a thing it wants to do.

BUG=406409

Review URL: https://codereview.chromium.org/490363002

Cr-Commit-Position: refs/heads/master@{#291598}
parent 7b783055
...@@ -169,13 +169,10 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs, ...@@ -169,13 +169,10 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
} }
} }
// If needed, start downloading the high-res avatars. // If needed, start downloading the high-res avatars and migrate any legacy
if (switches::IsNewAvatarMenu()) { // profile names.
for (size_t i = 0; i < GetNumberOfProfiles(); i++) { if (switches::IsNewAvatarMenu())
DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i), MigrateLegacyProfileNamesAndDownloadAvatars();
GetPathOfProfileAtIndex(i));
}
}
} }
ProfileInfoCache::~ProfileInfoCache() { ProfileInfoCache::~ProfileInfoCache() {
...@@ -1067,3 +1064,45 @@ void ProfileInfoCache::OnAvatarPictureSaved( ...@@ -1067,3 +1064,45 @@ void ProfileInfoCache::OnAvatarPictureSaved(
delete avatar_images_downloads_in_progress_[file_name]; delete avatar_images_downloads_in_progress_[file_name];
avatar_images_downloads_in_progress_[file_name] = NULL; avatar_images_downloads_in_progress_[file_name] = NULL;
} }
void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() {
DCHECK(switches::IsNewAvatarMenu());
// Only do this on desktop platforms.
#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
// Migrate any legacy profile names ("First user", "Default Profile") to
// new style default names ("Person 1"). The problem here is that every
// time you rename a profile, the ProfileInfoCache sorts itself, so
// whatever you were iterating through is no longer valid. We need to
// save a list of the profile paths (which thankfully do not change) that
// need to be renamed. We also can't pre-compute the new names, as they
// depend on the names of all the other profiles in the info cache, so they
// need to be re-computed after each rename.
std::vector<base::FilePath> profiles_to_rename;
const base::string16 default_profile_name = base::i18n::ToLower(
l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME));
const base::string16 default_legacy_profile_name = base::i18n::ToLower(
l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME));
for (size_t i = 0; i < GetNumberOfProfiles(); i++) {
// If needed, start downloading the high-res avatar for this profile.
DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i),
GetPathOfProfileAtIndex(i));
base::string16 name = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
if (name == default_profile_name || name == default_legacy_profile_name)
profiles_to_rename.push_back(GetPathOfProfileAtIndex(i));
}
// Rename the necessary profiles.
std::vector<base::FilePath>::const_iterator it;
for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) {
size_t profile_index = GetIndexOfProfileWithPath(*it);
SetProfileIsUsingDefaultNameAtIndex(profile_index, true);
// This will assign a new "Person %d" type name and re-sort the cache.
SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile(
GetAvatarIconIndexOfProfileAtIndex(profile_index)));
}
#endif
}
...@@ -206,6 +206,11 @@ class ProfileInfoCache : public ProfileInfoInterface, ...@@ -206,6 +206,11 @@ class ProfileInfoCache : public ProfileInfoInterface,
void OnAvatarPictureSaved(const std::string& file_name, void OnAvatarPictureSaved(const std::string& file_name,
const base::FilePath& profile_path); const base::FilePath& profile_path);
// Migrate any legacy profile names ("First user", "Default Profile") to
// new style default names ("Person 1"), and download and high-res avatars
// used by the profiles.
void MigrateLegacyProfileNamesAndDownloadAvatars();
PrefService* prefs_; PrefService* prefs_;
std::vector<std::string> sorted_keys_; std::vector<std::string> sorted_keys_;
base::FilePath user_data_dir_; base::FilePath user_data_dir_;
......
...@@ -590,4 +590,85 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { ...@@ -590,4 +590,85 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
EXPECT_TRUE(base::DeleteFile(icon_path, true)); EXPECT_TRUE(base::DeleteFile(icon_path, true));
EXPECT_FALSE(base::PathExists(icon_path)); EXPECT_FALSE(base::PathExists(icon_path));
} }
TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
base::FilePath path_1 = GetProfilePath("path_1");
GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
base::string16(), 0, std::string());
base::FilePath path_2 = GetProfilePath("path_2");
GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
base::string16(), 1, std::string());
base::string16 name_3 = ASCIIToUTF16("Lemonade");
base::FilePath path_3 = GetProfilePath("path_3");
GetCache()->AddProfileToCache(path_3, name_3,
base::string16(), 2, std::string());
base::string16 name_4 = ASCIIToUTF16("Batman");
base::FilePath path_4 = GetProfilePath("path_4");
GetCache()->AddProfileToCache(path_4, name_4,
base::string16(), 3, std::string());
base::string16 name_5 = ASCIIToUTF16("Person 2");
base::FilePath path_5 = GetProfilePath("path_5");
GetCache()->AddProfileToCache(path_5, name_5,
base::string16(), 2, std::string());
EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
ResetCache();
// Legacy profile names like "Default Profile" and "First user" should be
// migrated to "Person %n" type names.
EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_1)));
EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_2)));
// Other profile names should not be migrated even if they're the old
// default cartoon profile names.
EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_3)));
EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_4)));
EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_5)));
}
#endif #endif
TEST_F(ProfileInfoCacheTest,
DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
base::string16 name_1 = ASCIIToUTF16("Default Profile");
base::FilePath path_1 = GetProfilePath("path_1");
GetCache()->AddProfileToCache(path_1, name_1,
base::string16(), 0, std::string());
base::string16 name_2 = ASCIIToUTF16("First user");
base::FilePath path_2 = GetProfilePath("path_2");
GetCache()->AddProfileToCache(path_2, name_2,
base::string16(), 1, std::string());
base::string16 name_3 = ASCIIToUTF16("Lemonade");
base::FilePath path_3 = GetProfilePath("path_3");
GetCache()->AddProfileToCache(path_3, name_3,
base::string16(), 2, std::string());
base::string16 name_4 = ASCIIToUTF16("Batman");
base::FilePath path_4 = GetProfilePath("path_4");
GetCache()->AddProfileToCache(path_4, name_4,
base::string16(), 3, std::string());
EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
ResetCache();
// Profile names should have been preserved.
EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_1)));
EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_2)));
EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_3)));
EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
GetCache()->GetIndexOfProfileWithPath(path_4)));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment