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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 } | 130 } |
131 | 131 |
132 *out_image = new gfx::Image(image); | 132 *out_image = new gfx::Image(image); |
133 } | 133 } |
134 | 134 |
135 void DeleteBitmap(const base::FilePath& image_path) { | 135 void DeleteBitmap(const base::FilePath& image_path) { |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
137 base::DeleteFile(image_path, false); | 137 base::DeleteFile(image_path, false); |
138 } | 138 } |
139 | 139 |
140 bool IsDefaultName(const base::string16& name) { | |
141 // Check if it's a "First user" old-style name. | |
142 if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME)) | |
143 return true; | |
144 | |
145 // Check if it's one of the old-style profile names. | |
146 for (size_t i = 0; i < arraysize(kDefaultNames); ++i) { | |
147 if (name == l10n_util::GetStringUTF16(kDefaultNames[i])) | |
148 return true; | |
149 } | |
150 | |
151 // Check whether it's one of the "Person %d" style names. | |
152 std::string default_name_format = l10n_util::GetStringFUTF8( | |
153 IDS_NEW_NUMBERED_PROFILE_NAME, base::string16()) + "%d"; | |
154 | |
155 int generic_profile_number; // Unused. Just a placeholder for sscanf. | |
156 int assignments = sscanf(base::UTF16ToUTF8(name).c_str(), | |
157 default_name_format.c_str(), | |
158 &generic_profile_number); | |
159 // Unless it matched the format, this is a custom name. | |
160 return assignments == 1; | |
161 } | |
162 | |
163 } // namespace | 140 } // namespace |
164 | 141 |
165 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 142 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
166 const base::FilePath& user_data_dir) | 143 const base::FilePath& user_data_dir) |
167 : prefs_(prefs), | 144 : prefs_(prefs), |
168 user_data_dir_(user_data_dir) { | 145 user_data_dir_(user_data_dir) { |
169 // Populate the cache | 146 // Populate the cache |
170 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 147 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
171 base::DictionaryValue* cache = update.Get(); | 148 base::DictionaryValue* cache = update.Get(); |
172 for (base::DictionaryValue::Iterator it(*cache); | 149 for (base::DictionaryValue::Iterator it(*cache); |
173 !it.IsAtEnd(); it.Advance()) { | 150 !it.IsAtEnd(); it.Advance()) { |
174 base::DictionaryValue* info = NULL; | 151 base::DictionaryValue* info = NULL; |
175 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); | 152 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); |
176 base::string16 name; | 153 base::string16 name; |
177 info->GetString(kNameKey, &name); | 154 info->GetString(kNameKey, &name); |
178 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); | 155 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); |
179 bool using_default_name = IsDefaultName(name); | 156 |
180 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); | 157 bool using_default_name; |
158 if (info->HasKey(kIsUsingDefaultNameKey)) { | |
159 info->GetBoolean(kIsUsingDefaultNameKey, &using_default_name); | |
Bernhard Bauer
2014/08/15 14:39:22
GetBoolean() returns a bool that says whether the
noms (inactive)
2014/08/15 16:22:47
Done.
| |
160 } else { | |
161 // If the preference hasn't been set, and the name is default, assume | |
162 // that the user hasn't done this on purpose. | |
163 using_default_name = IsDefaultProfileName(name); | |
164 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); | |
165 } | |
166 | |
181 // For profiles that don't have the "using default avatar" state set yet, | 167 // For profiles that don't have the "using default avatar" state set yet, |
182 // assume it's the same as the "using default name" state. | 168 // assume it's the same as the "using default name" state. |
183 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { | 169 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { |
184 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); | 170 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); |
185 } | 171 } |
186 } | 172 } |
187 | 173 |
188 // If needed, start downloading the high-res avatars. | 174 // If needed, start downloading the high-res avatars. |
189 if (switches::IsNewAvatarMenu()) { | 175 if (switches::IsNewAvatarMenu()) { |
190 for (size_t i = 0; i < GetNumberOfProfiles(); i++) { | 176 for (size_t i = 0; i < GetNumberOfProfiles(); i++) { |
(...skipping 24 matching lines...) Expand all Loading... | |
215 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue); | 201 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue); |
216 info->SetString(kNameKey, name); | 202 info->SetString(kNameKey, name); |
217 info->SetString(kUserNameKey, username); | 203 info->SetString(kUserNameKey, username); |
218 info->SetString(kAvatarIconKey, | 204 info->SetString(kAvatarIconKey, |
219 profiles::GetDefaultAvatarIconUrl(icon_index)); | 205 profiles::GetDefaultAvatarIconUrl(icon_index)); |
220 // Default value for whether background apps are running is false. | 206 // Default value for whether background apps are running is false. |
221 info->SetBoolean(kBackgroundAppsKey, false); | 207 info->SetBoolean(kBackgroundAppsKey, false); |
222 info->SetString(kSupervisedUserId, supervised_user_id); | 208 info->SetString(kSupervisedUserId, supervised_user_id); |
223 info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty()); | 209 info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty()); |
224 info->SetBoolean(kProfileIsEphemeral, false); | 210 info->SetBoolean(kProfileIsEphemeral, false); |
225 info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultName(name)); | 211 info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultProfileName(name)); |
226 // Assume newly created profiles use a default avatar. | 212 // Assume newly created profiles use a default avatar. |
227 info->SetBoolean(kIsUsingDefaultAvatarKey, true); | 213 info->SetBoolean(kIsUsingDefaultAvatarKey, true); |
228 cache->SetWithoutPathExpansion(key, info.release()); | 214 cache->SetWithoutPathExpansion(key, info.release()); |
229 | 215 |
230 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 216 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
231 | 217 |
232 if (switches::IsNewAvatarMenu()) | 218 if (switches::IsNewAvatarMenu()) |
233 DownloadHighResAvatar(icon_index, profile_path); | 219 DownloadHighResAvatar(icon_index, profile_path); |
234 | 220 |
235 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 221 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
481 const base::string16& name) { | 467 const base::string16& name) { |
482 scoped_ptr<base::DictionaryValue> info( | 468 scoped_ptr<base::DictionaryValue> info( |
483 GetInfoForProfileAtIndex(index)->DeepCopy()); | 469 GetInfoForProfileAtIndex(index)->DeepCopy()); |
484 base::string16 current_name; | 470 base::string16 current_name; |
485 info->GetString(kNameKey, ¤t_name); | 471 info->GetString(kNameKey, ¤t_name); |
486 if (name == current_name) | 472 if (name == current_name) |
487 return; | 473 return; |
488 | 474 |
489 base::string16 old_display_name = GetNameOfProfileAtIndex(index); | 475 base::string16 old_display_name = GetNameOfProfileAtIndex(index); |
490 info->SetString(kNameKey, name); | 476 info->SetString(kNameKey, name); |
491 info->SetBoolean(kIsUsingDefaultNameKey, false); | |
492 | 477 |
493 // This takes ownership of |info|. | 478 // This takes ownership of |info|. |
494 SetInfoForProfileAtIndex(index, info.release()); | 479 SetInfoForProfileAtIndex(index, info.release()); |
495 base::string16 new_display_name = GetNameOfProfileAtIndex(index); | 480 base::string16 new_display_name = GetNameOfProfileAtIndex(index); |
496 base::FilePath profile_path = GetPathOfProfileAtIndex(index); | 481 base::FilePath profile_path = GetPathOfProfileAtIndex(index); |
497 UpdateSortForProfileIndex(index); | 482 UpdateSortForProfileIndex(index); |
498 | 483 |
499 if (old_display_name != new_display_name) { | 484 if (old_display_name != new_display_name) { |
500 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 485 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
501 observer_list_, | 486 observer_list_, |
(...skipping 28 matching lines...) Expand all Loading... | |
530 | 515 |
531 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 516 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
532 size_t icon_index) { | 517 size_t icon_index) { |
533 scoped_ptr<base::DictionaryValue> info( | 518 scoped_ptr<base::DictionaryValue> info( |
534 GetInfoForProfileAtIndex(index)->DeepCopy()); | 519 GetInfoForProfileAtIndex(index)->DeepCopy()); |
535 info->SetString(kAvatarIconKey, | 520 info->SetString(kAvatarIconKey, |
536 profiles::GetDefaultAvatarIconUrl(icon_index)); | 521 profiles::GetDefaultAvatarIconUrl(icon_index)); |
537 // This takes ownership of |info|. | 522 // This takes ownership of |info|. |
538 SetInfoForProfileAtIndex(index, info.release()); | 523 SetInfoForProfileAtIndex(index, info.release()); |
539 | 524 |
540 SetProfileIsUsingDefaultAvatarAtIndex(index, false); | |
541 | |
542 base::FilePath profile_path = GetPathOfProfileAtIndex(index); | 525 base::FilePath profile_path = GetPathOfProfileAtIndex(index); |
543 | 526 |
544 // If needed, start downloading the high-res avatar. | 527 // If needed, start downloading the high-res avatar. |
545 if (switches::IsNewAvatarMenu()) | 528 if (switches::IsNewAvatarMenu()) |
546 DownloadHighResAvatar(icon_index, profile_path); | 529 DownloadHighResAvatar(icon_index, profile_path); |
547 | 530 |
548 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 531 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
549 observer_list_, | 532 observer_list_, |
550 OnProfileAvatarChanged(profile_path)); | 533 OnProfileAvatarChanged(profile_path)); |
551 } | 534 } |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
750 if (value == ProfileIsUsingDefaultAvatarAtIndex(index)) | 733 if (value == ProfileIsUsingDefaultAvatarAtIndex(index)) |
751 return; | 734 return; |
752 | 735 |
753 scoped_ptr<base::DictionaryValue> info( | 736 scoped_ptr<base::DictionaryValue> info( |
754 GetInfoForProfileAtIndex(index)->DeepCopy()); | 737 GetInfoForProfileAtIndex(index)->DeepCopy()); |
755 info->SetBoolean(kIsUsingDefaultAvatarKey, value); | 738 info->SetBoolean(kIsUsingDefaultAvatarKey, value); |
756 // This takes ownership of |info|. | 739 // This takes ownership of |info|. |
757 SetInfoForProfileAtIndex(index, info.release()); | 740 SetInfoForProfileAtIndex(index, info.release()); |
758 } | 741 } |
759 | 742 |
743 bool ProfileInfoCache::IsDefaultProfileName(const base::string16& name) { | |
744 // Check if it's a "First user" old-style name. | |
745 if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME)) | |
746 return true; | |
747 | |
748 // Check if it's one of the old-style profile names. | |
749 for (size_t i = 0; i < arraysize(kDefaultNames); ++i) { | |
750 if (name == l10n_util::GetStringUTF16(kDefaultNames[i])) | |
751 return true; | |
752 } | |
753 | |
754 // Check whether it's one of the "Person %d" style names. | |
755 std::string default_name_format = l10n_util::GetStringFUTF8( | |
756 IDS_NEW_NUMBERED_PROFILE_NAME, base::string16()) + "%d"; | |
Bernhard Bauer
2014/08/15 14:39:22
Use ASCIIToUTF16('%d') instead of base::string16()
Roger Tawa OOO till Jul 10th
2014/08/15 14:46:40
Why not:
std::string default_name_format =
noms (inactive)
2014/08/15 16:22:47
Acknowledged.
noms (inactive)
2014/08/15 16:22:47
I'll take Bernhard's version, since his compiles :
| |
757 | |
758 int generic_profile_number; // Unused. Just a placeholder for sscanf. | |
759 int assignments = sscanf(base::UTF16ToUTF8(name).c_str(), | |
760 default_name_format.c_str(), | |
761 &generic_profile_number); | |
762 // Unless it matched the format, this is a custom name. | |
763 return assignments == 1; | |
764 } | |
765 | |
760 base::string16 ProfileInfoCache::ChooseNameForNewProfile( | 766 base::string16 ProfileInfoCache::ChooseNameForNewProfile( |
761 size_t icon_index) const { | 767 size_t icon_index) const { |
762 base::string16 name; | 768 base::string16 name; |
763 for (int name_index = 1; ; ++name_index) { | 769 for (int name_index = 1; ; ++name_index) { |
764 if (switches::IsNewAvatarMenu()) { | 770 if (switches::IsNewAvatarMenu()) { |
765 name = l10n_util::GetStringFUTF16Int(IDS_NEW_NUMBERED_PROFILE_NAME, | 771 name = l10n_util::GetStringFUTF16Int(IDS_NEW_NUMBERED_PROFILE_NAME, |
766 name_index); | 772 name_index); |
767 } else if (icon_index < profiles::GetGenericAvatarIconCount()) { | 773 } else if (icon_index < profiles::GetGenericAvatarIconCount()) { |
768 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, | 774 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, |
769 name_index); | 775 name_index); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1049 OnProfileAvatarChanged(profile_path)); | 1055 OnProfileAvatarChanged(profile_path)); |
1050 | 1056 |
1051 // Remove the file from the list of downloads in progress. Note that this list | 1057 // Remove the file from the list of downloads in progress. Note that this list |
1052 // only contains the high resolution avatars, and not the Gaia profile images. | 1058 // only contains the high resolution avatars, and not the Gaia profile images. |
1053 if (!avatar_images_downloads_in_progress_[file_name]) | 1059 if (!avatar_images_downloads_in_progress_[file_name]) |
1054 return; | 1060 return; |
1055 | 1061 |
1056 delete avatar_images_downloads_in_progress_[file_name]; | 1062 delete avatar_images_downloads_in_progress_[file_name]; |
1057 avatar_images_downloads_in_progress_[file_name] = NULL; | 1063 avatar_images_downloads_in_progress_[file_name] = NULL; |
1058 } | 1064 } |
OLD | NEW |