| 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 918 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
| 926 size_t index) const { | 919 size_t index) const { |
| 927 DCHECK_LT(index, GetNumberOfProfiles()); | 920 DCHECK_LT(index, GetNumberOfProfiles()); |
| 928 const base::DictionaryValue* cache = | 921 const base::DictionaryValue* cache = |
| 929 prefs_->GetDictionary(prefs::kProfileInfoCache); | 922 prefs_->GetDictionary(prefs::kProfileInfoCache); |
| 930 const base::DictionaryValue* info = NULL; | 923 const base::DictionaryValue* info = NULL; |
| 931 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 924 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
| 932 return info; | 925 return info; |
| 933 } | 926 } |
| 934 | 927 |
| 935 void ProfileInfoCache::SetInfoQuietlyForProfileAtIndex( | 928 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, |
| 936 size_t index, base::DictionaryValue* info) { | 929 base::DictionaryValue* info) { |
| 937 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 930 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 938 base::DictionaryValue* cache = update.Get(); | 931 base::DictionaryValue* cache = update.Get(); |
| 939 cache->SetWithoutPathExpansion(sorted_keys_[index], info); | 932 cache->SetWithoutPathExpansion(sorted_keys_[index], info); |
| 940 } | 933 } |
| 941 | 934 |
| 942 // TODO(noms): Switch to newer notification system. | |
| 943 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | |
| 944 base::DictionaryValue* info) { | |
| 945 SetInfoQuietlyForProfileAtIndex(index, info); | |
| 946 | |
| 947 content::NotificationService::current()->Notify( | |
| 948 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
| 949 content::NotificationService::AllSources(), | |
| 950 content::NotificationService::NoDetails()); | |
| 951 } | |
| 952 | |
| 953 std::string ProfileInfoCache::CacheKeyFromProfilePath( | 935 std::string ProfileInfoCache::CacheKeyFromProfilePath( |
| 954 const base::FilePath& profile_path) const { | 936 const base::FilePath& profile_path) const { |
| 955 DCHECK(user_data_dir_ == profile_path.DirName()); | 937 DCHECK(user_data_dir_ == profile_path.DirName()); |
| 956 base::FilePath base_name = profile_path.BaseName(); | 938 base::FilePath base_name = profile_path.BaseName(); |
| 957 return base_name.MaybeAsASCII(); | 939 return base_name.MaybeAsASCII(); |
| 958 } | 940 } |
| 959 | 941 |
| 960 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | 942 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( |
| 961 const std::string& search_key, | 943 const std::string& search_key, |
| 962 const base::string16& search_name) { | 944 const base::string16& search_name) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { | 992 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { |
| 1011 base::string16 name = GetNameOfProfileAtIndex(index); | 993 base::string16 name = GetNameOfProfileAtIndex(index); |
| 1012 | 994 |
| 1013 // Remove and reinsert key in |sorted_keys_| to alphasort. | 995 // Remove and reinsert key in |sorted_keys_| to alphasort. |
| 1014 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 996 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
| 1015 std::vector<std::string>::iterator key_it = | 997 std::vector<std::string>::iterator key_it = |
| 1016 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 998 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
| 1017 DCHECK(key_it != sorted_keys_.end()); | 999 DCHECK(key_it != sorted_keys_.end()); |
| 1018 sorted_keys_.erase(key_it); | 1000 sorted_keys_.erase(key_it); |
| 1019 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 1001 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 1020 | |
| 1021 content::NotificationService::current()->Notify( | |
| 1022 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
| 1023 content::NotificationService::AllSources(), | |
| 1024 content::NotificationService::NoDetails()); | |
| 1025 } | 1002 } |
| 1026 | 1003 |
| 1027 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( | 1004 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( |
| 1028 size_t index) const { | 1005 size_t index) const { |
| 1029 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); | 1006 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); |
| 1030 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); | 1007 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); |
| 1031 | 1008 |
| 1032 // If this is the placeholder avatar, it is already included in the | 1009 // If this is the placeholder avatar, it is already included in the |
| 1033 // resources, so it doesn't need to be downloaded. | 1010 // resources, so it doesn't need to be downloaded. |
| 1034 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) { | 1011 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) { |
| (...skipping 65 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 |