OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/profile_info_cache.h" | 5 #include "chrome/browser/profiles/profile_info_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); | 162 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); |
163 } | 163 } |
164 | 164 |
165 // For profiles that don't have the "using default avatar" state set yet, | 165 // For profiles that don't have the "using default avatar" state set yet, |
166 // assume it's the same as the "using default name" state. | 166 // assume it's the same as the "using default name" state. |
167 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { | 167 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { |
168 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); | 168 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 // If needed, start downloading the high-res avatars. | 172 // If needed, start downloading the high-res avatars and migrate any legacy |
173 if (switches::IsNewAvatarMenu()) { | 173 // profile names. |
174 for (size_t i = 0; i < GetNumberOfProfiles(); i++) { | 174 if (switches::IsNewAvatarMenu()) |
175 DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i), | 175 MigrateLegacyProfileNamesAndDownloadAvatars(); |
176 GetPathOfProfileAtIndex(i)); | |
177 } | |
178 } | |
179 } | 176 } |
180 | 177 |
181 ProfileInfoCache::~ProfileInfoCache() { | 178 ProfileInfoCache::~ProfileInfoCache() { |
182 STLDeleteContainerPairSecondPointers( | 179 STLDeleteContainerPairSecondPointers( |
183 cached_avatar_images_.begin(), cached_avatar_images_.end()); | 180 cached_avatar_images_.begin(), cached_avatar_images_.end()); |
184 STLDeleteContainerPairSecondPointers( | 181 STLDeleteContainerPairSecondPointers( |
185 avatar_images_downloads_in_progress_.begin(), | 182 avatar_images_downloads_in_progress_.begin(), |
186 avatar_images_downloads_in_progress_.end()); | 183 avatar_images_downloads_in_progress_.end()); |
187 } | 184 } |
188 | 185 |
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 OnProfileAvatarChanged(profile_path)); | 1057 OnProfileAvatarChanged(profile_path)); |
1061 | 1058 |
1062 // Remove the file from the list of downloads in progress. Note that this list | 1059 // Remove the file from the list of downloads in progress. Note that this list |
1063 // only contains the high resolution avatars, and not the Gaia profile images. | 1060 // only contains the high resolution avatars, and not the Gaia profile images. |
1064 if (!avatar_images_downloads_in_progress_[file_name]) | 1061 if (!avatar_images_downloads_in_progress_[file_name]) |
1065 return; | 1062 return; |
1066 | 1063 |
1067 delete avatar_images_downloads_in_progress_[file_name]; | 1064 delete avatar_images_downloads_in_progress_[file_name]; |
1068 avatar_images_downloads_in_progress_[file_name] = NULL; | 1065 avatar_images_downloads_in_progress_[file_name] = NULL; |
1069 } | 1066 } |
| 1067 |
| 1068 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() { |
| 1069 DCHECK(switches::IsNewAvatarMenu()); |
| 1070 |
| 1071 // Only do this on desktop platforms. |
| 1072 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) |
| 1073 // Migrate any legacy profile names ("First user", "Default Profile") to |
| 1074 // new style default names ("Person 1"). The problem here is that every |
| 1075 // time you rename a profile, the ProfileInfoCache sorts itself, so |
| 1076 // whatever you were iterating through is no longer valid. We need to |
| 1077 // save a list of the profile paths (which thankfully do not change) that |
| 1078 // need to be renamed. We also can't pre-compute the new names, as they |
| 1079 // depend on the names of all the other profiles in the info cache, so they |
| 1080 // need to be re-computed after each rename. |
| 1081 std::vector<base::FilePath> profiles_to_rename; |
| 1082 |
| 1083 const base::string16 default_profile_name = base::i18n::ToLower( |
| 1084 l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME)); |
| 1085 const base::string16 default_legacy_profile_name = base::i18n::ToLower( |
| 1086 l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME)); |
| 1087 |
| 1088 for (size_t i = 0; i < GetNumberOfProfiles(); i++) { |
| 1089 // If needed, start downloading the high-res avatar for this profile. |
| 1090 DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i), |
| 1091 GetPathOfProfileAtIndex(i)); |
| 1092 |
| 1093 base::string16 name = base::i18n::ToLower(GetNameOfProfileAtIndex(i)); |
| 1094 if (name == default_profile_name || name == default_legacy_profile_name) |
| 1095 profiles_to_rename.push_back(GetPathOfProfileAtIndex(i)); |
| 1096 } |
| 1097 |
| 1098 // Rename the necessary profiles. |
| 1099 std::vector<base::FilePath>::const_iterator it; |
| 1100 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { |
| 1101 size_t profile_index = GetIndexOfProfileWithPath(*it); |
| 1102 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); |
| 1103 // This will assign a new "Person %d" type name and re-sort the cache. |
| 1104 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( |
| 1105 GetAvatarIconIndexOfProfileAtIndex(profile_index))); |
| 1106 } |
| 1107 #endif |
| 1108 } |
OLD | NEW |