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