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/files/file_util.h" | 8 #include "base/files/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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/prefs/scoped_user_pref_update.h" | 14 #include "base/prefs/scoped_user_pref_update.h" |
15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/chrome_notification_types.h" | |
23 #include "chrome/browser/profiles/profile_avatar_downloader.h" | 22 #include "chrome/browser/profiles/profile_avatar_downloader.h" |
24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 23 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
25 #include "chrome/browser/profiles/profiles_state.h" | 24 #include "chrome/browser/profiles/profiles_state.h" |
26 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
27 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
28 #include "components/signin/core/common/profile_management_switches.h" | 27 #include "components/signin/core/common/profile_management_switches.h" |
29 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/notification_service.h" | |
31 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
32 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
33 #include "ui/gfx/image/image.h" | 31 #include "ui/gfx/image/image.h" |
34 #include "ui/gfx/image/image_util.h" | 32 #include "ui/gfx/image/image_util.h" |
35 | 33 |
36 #if defined(ENABLE_SUPERVISED_USERS) | 34 #if defined(ENABLE_SUPERVISED_USERS) |
37 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 35 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
38 #endif | 36 #endif |
39 | 37 |
40 using content::BrowserThread; | 38 using content::BrowserThread; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 cache->SetWithoutPathExpansion(key, info.release()); | 218 cache->SetWithoutPathExpansion(key, info.release()); |
221 | 219 |
222 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 220 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
223 | 221 |
224 if (switches::IsNewAvatarMenu()) | 222 if (switches::IsNewAvatarMenu()) |
225 DownloadHighResAvatarIfNeeded(icon_index, profile_path); | 223 DownloadHighResAvatarIfNeeded(icon_index, profile_path); |
226 | 224 |
227 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 225 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
228 observer_list_, | 226 observer_list_, |
229 OnProfileAdded(profile_path)); | 227 OnProfileAdded(profile_path)); |
230 | |
231 content::NotificationService::current()->Notify( | |
232 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
233 content::NotificationService::AllSources(), | |
234 content::NotificationService::NoDetails()); | |
235 } | 228 } |
236 | 229 |
237 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | 230 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
238 observer_list_.AddObserver(obs); | 231 observer_list_.AddObserver(obs); |
239 } | 232 } |
240 | 233 |
241 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { | 234 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { |
242 observer_list_.RemoveObserver(obs); | 235 observer_list_.RemoveObserver(obs); |
243 } | 236 } |
244 | 237 |
(...skipping 12 matching lines...) Expand all Loading... |
257 | 250 |
258 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 251 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
259 base::DictionaryValue* cache = update.Get(); | 252 base::DictionaryValue* cache = update.Get(); |
260 std::string key = CacheKeyFromProfilePath(profile_path); | 253 std::string key = CacheKeyFromProfilePath(profile_path); |
261 cache->Remove(key, NULL); | 254 cache->Remove(key, NULL); |
262 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 255 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
263 | 256 |
264 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 257 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
265 observer_list_, | 258 observer_list_, |
266 OnProfileWasRemoved(profile_path, name)); | 259 OnProfileWasRemoved(profile_path, name)); |
267 | |
268 content::NotificationService::current()->Notify( | |
269 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
270 content::NotificationService::AllSources(), | |
271 content::NotificationService::NoDetails()); | |
272 } | 260 } |
273 | 261 |
274 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 262 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
275 return sorted_keys_.size(); | 263 return sorted_keys_.size(); |
276 } | 264 } |
277 | 265 |
278 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | 266 size_t ProfileInfoCache::GetIndexOfProfileWithPath( |
279 const base::FilePath& profile_path) const { | 267 const base::FilePath& profile_path) const { |
280 if (profile_path.DirName() != user_data_dir_) | 268 if (profile_path.DirName() != user_data_dir_) |
281 return std::string::npos; | 269 return std::string::npos; |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 466 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
479 | 467 |
480 return icon_index; | 468 return icon_index; |
481 } | 469 } |
482 | 470 |
483 void ProfileInfoCache::SetProfileActiveTimeAtIndex(size_t index) { | 471 void ProfileInfoCache::SetProfileActiveTimeAtIndex(size_t index) { |
484 scoped_ptr<base::DictionaryValue> info( | 472 scoped_ptr<base::DictionaryValue> info( |
485 GetInfoForProfileAtIndex(index)->DeepCopy()); | 473 GetInfoForProfileAtIndex(index)->DeepCopy()); |
486 info->SetDouble(kActiveTimeKey, base::Time::Now().ToDoubleT()); | 474 info->SetDouble(kActiveTimeKey, base::Time::Now().ToDoubleT()); |
487 // This takes ownership of |info|. | 475 // This takes ownership of |info|. |
488 SetInfoQuietlyForProfileAtIndex(index, info.release()); | 476 SetInfoForProfileAtIndex(index, info.release()); |
489 } | 477 } |
490 | 478 |
491 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 479 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
492 const base::string16& name) { | 480 const base::string16& name) { |
493 scoped_ptr<base::DictionaryValue> info( | 481 scoped_ptr<base::DictionaryValue> info( |
494 GetInfoForProfileAtIndex(index)->DeepCopy()); | 482 GetInfoForProfileAtIndex(index)->DeepCopy()); |
495 base::string16 current_name; | 483 base::string16 current_name; |
496 info->GetString(kNameKey, ¤t_name); | 484 info->GetString(kNameKey, ¤t_name); |
497 if (name == current_name) | 485 if (name == current_name) |
498 return; | 486 return; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 size_t index, | 518 size_t index, |
531 const base::string16& user_name) { | 519 const base::string16& user_name) { |
532 if (user_name == GetUserNameOfProfileAtIndex(index)) | 520 if (user_name == GetUserNameOfProfileAtIndex(index)) |
533 return; | 521 return; |
534 | 522 |
535 scoped_ptr<base::DictionaryValue> info( | 523 scoped_ptr<base::DictionaryValue> info( |
536 GetInfoForProfileAtIndex(index)->DeepCopy()); | 524 GetInfoForProfileAtIndex(index)->DeepCopy()); |
537 info->SetString(kUserNameKey, user_name); | 525 info->SetString(kUserNameKey, user_name); |
538 // This takes ownership of |info|. | 526 // This takes ownership of |info|. |
539 SetInfoForProfileAtIndex(index, info.release()); | 527 SetInfoForProfileAtIndex(index, info.release()); |
| 528 |
| 529 base::FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 530 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 531 observer_list_, |
| 532 OnProfileUserNameChanged(profile_path)); |
540 } | 533 } |
541 | 534 |
542 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 535 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
543 size_t icon_index) { | 536 size_t icon_index) { |
544 scoped_ptr<base::DictionaryValue> info( | 537 scoped_ptr<base::DictionaryValue> info( |
545 GetInfoForProfileAtIndex(index)->DeepCopy()); | 538 GetInfoForProfileAtIndex(index)->DeepCopy()); |
546 info->SetString(kAvatarIconKey, | 539 info->SetString(kAvatarIconKey, |
547 profiles::GetDefaultAvatarIconUrl(icon_index)); | 540 profiles::GetDefaultAvatarIconUrl(icon_index)); |
548 // This takes ownership of |info|. | 541 // This takes ownership of |info|. |
549 SetInfoForProfileAtIndex(index, info.release()); | 542 SetInfoForProfileAtIndex(index, info.release()); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 901 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
909 size_t index) const { | 902 size_t index) const { |
910 DCHECK_LT(index, GetNumberOfProfiles()); | 903 DCHECK_LT(index, GetNumberOfProfiles()); |
911 const base::DictionaryValue* cache = | 904 const base::DictionaryValue* cache = |
912 prefs_->GetDictionary(prefs::kProfileInfoCache); | 905 prefs_->GetDictionary(prefs::kProfileInfoCache); |
913 const base::DictionaryValue* info = NULL; | 906 const base::DictionaryValue* info = NULL; |
914 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 907 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
915 return info; | 908 return info; |
916 } | 909 } |
917 | 910 |
918 void ProfileInfoCache::SetInfoQuietlyForProfileAtIndex( | 911 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, |
919 size_t index, base::DictionaryValue* info) { | 912 base::DictionaryValue* info) { |
920 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 913 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
921 base::DictionaryValue* cache = update.Get(); | 914 base::DictionaryValue* cache = update.Get(); |
922 cache->SetWithoutPathExpansion(sorted_keys_[index], info); | 915 cache->SetWithoutPathExpansion(sorted_keys_[index], info); |
923 } | 916 } |
924 | 917 |
925 // TODO(noms): Switch to newer notification system. | |
926 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | |
927 base::DictionaryValue* info) { | |
928 SetInfoQuietlyForProfileAtIndex(index, info); | |
929 | |
930 content::NotificationService::current()->Notify( | |
931 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
932 content::NotificationService::AllSources(), | |
933 content::NotificationService::NoDetails()); | |
934 } | |
935 | |
936 std::string ProfileInfoCache::CacheKeyFromProfilePath( | 918 std::string ProfileInfoCache::CacheKeyFromProfilePath( |
937 const base::FilePath& profile_path) const { | 919 const base::FilePath& profile_path) const { |
938 DCHECK(user_data_dir_ == profile_path.DirName()); | 920 DCHECK(user_data_dir_ == profile_path.DirName()); |
939 base::FilePath base_name = profile_path.BaseName(); | 921 base::FilePath base_name = profile_path.BaseName(); |
940 return base_name.MaybeAsASCII(); | 922 return base_name.MaybeAsASCII(); |
941 } | 923 } |
942 | 924 |
943 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | 925 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( |
944 const std::string& search_key, | 926 const std::string& search_key, |
945 const base::string16& search_name) { | 927 const base::string16& search_name) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { | 975 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { |
994 base::string16 name = GetNameOfProfileAtIndex(index); | 976 base::string16 name = GetNameOfProfileAtIndex(index); |
995 | 977 |
996 // Remove and reinsert key in |sorted_keys_| to alphasort. | 978 // Remove and reinsert key in |sorted_keys_| to alphasort. |
997 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 979 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
998 std::vector<std::string>::iterator key_it = | 980 std::vector<std::string>::iterator key_it = |
999 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 981 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
1000 DCHECK(key_it != sorted_keys_.end()); | 982 DCHECK(key_it != sorted_keys_.end()); |
1001 sorted_keys_.erase(key_it); | 983 sorted_keys_.erase(key_it); |
1002 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 984 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
1003 | |
1004 content::NotificationService::current()->Notify( | |
1005 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
1006 content::NotificationService::AllSources(), | |
1007 content::NotificationService::NoDetails()); | |
1008 } | 985 } |
1009 | 986 |
1010 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( | 987 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( |
1011 size_t index) const { | 988 size_t index) const { |
1012 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); | 989 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); |
1013 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); | 990 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); |
1014 | 991 |
1015 // If this is the placeholder avatar, it is already included in the | 992 // If this is the placeholder avatar, it is already included in the |
1016 // resources, so it doesn't need to be downloaded. | 993 // resources, so it doesn't need to be downloaded. |
1017 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) { | 994 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 delete cached_avatar_images_[key]; | 1060 delete cached_avatar_images_[key]; |
1084 | 1061 |
1085 if (*image) { | 1062 if (*image) { |
1086 cached_avatar_images_[key] = *image; | 1063 cached_avatar_images_[key] = *image; |
1087 } else { | 1064 } else { |
1088 // Place an empty image in the cache to avoid reloading it again. | 1065 // Place an empty image in the cache to avoid reloading it again. |
1089 cached_avatar_images_[key] = new gfx::Image(); | 1066 cached_avatar_images_[key] = new gfx::Image(); |
1090 } | 1067 } |
1091 delete image; | 1068 delete image; |
1092 | 1069 |
1093 content::NotificationService::current()->Notify( | |
1094 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
1095 content::NotificationService::AllSources(), | |
1096 content::NotificationService::NoDetails()); | |
1097 | |
1098 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 1070 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
1099 observer_list_, | 1071 observer_list_, |
1100 OnProfileHighResAvatarLoaded(profile_path)); | 1072 OnProfileHighResAvatarLoaded(profile_path)); |
1101 } | 1073 } |
1102 | 1074 |
1103 void ProfileInfoCache::OnAvatarPictureSaved( | 1075 void ProfileInfoCache::OnAvatarPictureSaved( |
1104 const std::string& file_name, | 1076 const std::string& file_name, |
1105 const base::FilePath& profile_path) { | 1077 const base::FilePath& profile_path) { |
1106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1078 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1107 | 1079 |
1108 content::NotificationService::current()->Notify( | |
1109 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, | |
1110 content::NotificationService::AllSources(), | |
1111 content::NotificationService::NoDetails()); | |
1112 | |
1113 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 1080 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
1114 observer_list_, | 1081 observer_list_, |
1115 OnProfileHighResAvatarLoaded(profile_path)); | 1082 OnProfileHighResAvatarLoaded(profile_path)); |
1116 } | 1083 } |
1117 | 1084 |
1118 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() { | 1085 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() { |
1119 DCHECK(switches::IsNewAvatarMenu()); | 1086 DCHECK(switches::IsNewAvatarMenu()); |
1120 | 1087 |
1121 // Only do this on desktop platforms. | 1088 // Only do this on desktop platforms. |
1122 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) | 1089 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) |
(...skipping 25 matching lines...) Expand all Loading... |
1148 std::vector<base::FilePath>::const_iterator it; | 1115 std::vector<base::FilePath>::const_iterator it; |
1149 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { | 1116 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { |
1150 size_t profile_index = GetIndexOfProfileWithPath(*it); | 1117 size_t profile_index = GetIndexOfProfileWithPath(*it); |
1151 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); | 1118 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); |
1152 // This will assign a new "Person %d" type name and re-sort the cache. | 1119 // This will assign a new "Person %d" type name and re-sort the cache. |
1153 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( | 1120 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( |
1154 GetAvatarIconIndexOfProfileAtIndex(profile_index))); | 1121 GetAvatarIconIndexOfProfileAtIndex(profile_index))); |
1155 } | 1122 } |
1156 #endif | 1123 #endif |
1157 } | 1124 } |
OLD | NEW |