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