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" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 void DeleteBitmap(const base::FilePath& image_path) { | 145 void DeleteBitmap(const base::FilePath& image_path) { |
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
147 base::DeleteFile(image_path, false); | 147 base::DeleteFile(image_path, false); |
148 } | 148 } |
149 | 149 |
150 } // namespace | 150 } // namespace |
151 | 151 |
152 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 152 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
153 const base::FilePath& user_data_dir) | 153 const base::FilePath& user_data_dir) |
154 : prefs_(prefs), | 154 : prefs_(prefs), |
155 user_data_dir_(user_data_dir) { | 155 user_data_dir_(user_data_dir), |
| 156 disable_avatar_download_for_testing_(false) { |
156 // Populate the cache | 157 // Populate the cache |
157 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 158 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
158 base::DictionaryValue* cache = update.Get(); | 159 base::DictionaryValue* cache = update.Get(); |
159 for (base::DictionaryValue::Iterator it(*cache); | 160 for (base::DictionaryValue::Iterator it(*cache); |
160 !it.IsAtEnd(); it.Advance()) { | 161 !it.IsAtEnd(); it.Advance()) { |
161 base::DictionaryValue* info = NULL; | 162 base::DictionaryValue* info = NULL; |
162 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); | 163 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); |
163 base::string16 name; | 164 base::string16 name; |
164 info->GetString(kNameKey, &name); | 165 info->GetString(kNameKey, &name); |
165 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); | 166 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); |
166 | 167 |
167 bool using_default_name; | 168 bool using_default_name; |
168 if (!info->GetBoolean(kIsUsingDefaultNameKey, &using_default_name)) { | 169 if (!info->GetBoolean(kIsUsingDefaultNameKey, &using_default_name)) { |
169 // If the preference hasn't been set, and the name is default, assume | 170 // If the preference hasn't been set, and the name is default, assume |
170 // that the user hasn't done this on purpose. | 171 // that the user hasn't done this on purpose. |
171 using_default_name = IsDefaultProfileName(name); | 172 using_default_name = IsDefaultProfileName(name); |
172 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); | 173 info->SetBoolean(kIsUsingDefaultNameKey, using_default_name); |
173 } | 174 } |
174 | 175 |
175 // For profiles that don't have the "using default avatar" state set yet, | 176 // For profiles that don't have the "using default avatar" state set yet, |
176 // assume it's the same as the "using default name" state. | 177 // assume it's the same as the "using default name" state. |
177 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { | 178 if (!info->HasKey(kIsUsingDefaultAvatarKey)) { |
178 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); | 179 info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name); |
179 } | 180 } |
180 } | 181 } |
181 | 182 |
182 // If needed, start downloading the high-res avatars and migrate any legacy | 183 // If needed, start downloading the high-res avatars and migrate any legacy |
183 // profile names. | 184 // profile names. |
184 if (switches::IsNewAvatarMenu()) | 185 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_) |
185 MigrateLegacyProfileNamesAndDownloadAvatars(); | 186 MigrateLegacyProfileNamesAndDownloadAvatars(); |
186 } | 187 } |
187 | 188 |
188 ProfileInfoCache::~ProfileInfoCache() { | 189 ProfileInfoCache::~ProfileInfoCache() { |
189 STLDeleteContainerPairSecondPointers( | 190 STLDeleteContainerPairSecondPointers( |
190 cached_avatar_images_.begin(), cached_avatar_images_.end()); | 191 cached_avatar_images_.begin(), cached_avatar_images_.end()); |
191 STLDeleteContainerPairSecondPointers( | 192 STLDeleteContainerPairSecondPointers( |
192 avatar_images_downloads_in_progress_.begin(), | 193 avatar_images_downloads_in_progress_.begin(), |
193 avatar_images_downloads_in_progress_.end()); | 194 avatar_images_downloads_in_progress_.end()); |
194 } | 195 } |
(...skipping 18 matching lines...) Expand all Loading... |
213 info->SetString(kSupervisedUserId, supervised_user_id); | 214 info->SetString(kSupervisedUserId, supervised_user_id); |
214 info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty()); | 215 info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty()); |
215 info->SetBoolean(kProfileIsEphemeral, false); | 216 info->SetBoolean(kProfileIsEphemeral, false); |
216 info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultProfileName(name)); | 217 info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultProfileName(name)); |
217 // Assume newly created profiles use a default avatar. | 218 // Assume newly created profiles use a default avatar. |
218 info->SetBoolean(kIsUsingDefaultAvatarKey, true); | 219 info->SetBoolean(kIsUsingDefaultAvatarKey, true); |
219 cache->SetWithoutPathExpansion(key, info.release()); | 220 cache->SetWithoutPathExpansion(key, info.release()); |
220 | 221 |
221 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 222 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
222 | 223 |
223 if (switches::IsNewAvatarMenu()) | 224 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_) |
224 DownloadHighResAvatarIfNeeded(icon_index, profile_path); | 225 DownloadHighResAvatarIfNeeded(icon_index, profile_path); |
225 | 226 |
226 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 227 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
227 observer_list_, | 228 observer_list_, |
228 OnProfileAdded(profile_path)); | 229 OnProfileAdded(profile_path)); |
229 } | 230 } |
230 | 231 |
231 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | 232 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
232 observer_list_.AddObserver(obs); | 233 observer_list_.AddObserver(obs); |
233 } | 234 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 } | 312 } |
312 | 313 |
313 base::string16 ProfileInfoCache::GetUserNameOfProfileAtIndex( | 314 base::string16 ProfileInfoCache::GetUserNameOfProfileAtIndex( |
314 size_t index) const { | 315 size_t index) const { |
315 base::string16 user_name; | 316 base::string16 user_name; |
316 GetInfoForProfileAtIndex(index)->GetString(kUserNameKey, &user_name); | 317 GetInfoForProfileAtIndex(index)->GetString(kUserNameKey, &user_name); |
317 return user_name; | 318 return user_name; |
318 } | 319 } |
319 | 320 |
320 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 321 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
321 size_t index) const { | 322 size_t index) { |
322 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { | 323 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { |
323 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); | 324 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); |
324 if (image) | 325 if (image) |
325 return *image; | 326 return *image; |
326 } | 327 } |
327 | 328 |
328 // Use the high resolution version of the avatar if it exists. | 329 // Use the high resolution version of the avatar if it exists. |
329 if (switches::IsNewAvatarMenu()) { | 330 if (switches::IsNewAvatarMenu()) { |
330 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); | 331 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); |
331 if (image) | 332 if (image) |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 size_t icon_index) { | 538 size_t icon_index) { |
538 scoped_ptr<base::DictionaryValue> info( | 539 scoped_ptr<base::DictionaryValue> info( |
539 GetInfoForProfileAtIndex(index)->DeepCopy()); | 540 GetInfoForProfileAtIndex(index)->DeepCopy()); |
540 info->SetString(kAvatarIconKey, | 541 info->SetString(kAvatarIconKey, |
541 profiles::GetDefaultAvatarIconUrl(icon_index)); | 542 profiles::GetDefaultAvatarIconUrl(icon_index)); |
542 // This takes ownership of |info|. | 543 // This takes ownership of |info|. |
543 SetInfoForProfileAtIndex(index, info.release()); | 544 SetInfoForProfileAtIndex(index, info.release()); |
544 | 545 |
545 base::FilePath profile_path = GetPathOfProfileAtIndex(index); | 546 base::FilePath profile_path = GetPathOfProfileAtIndex(index); |
546 | 547 |
547 if (switches::IsNewAvatarMenu()) | 548 if (switches::IsNewAvatarMenu() && !disable_avatar_download_for_testing_) |
548 DownloadHighResAvatarIfNeeded(icon_index, profile_path); | 549 DownloadHighResAvatarIfNeeded(icon_index, profile_path); |
549 | 550 |
550 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 551 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
551 observer_list_, | 552 observer_list_, |
552 OnProfileAvatarChanged(profile_path)); | 553 OnProfileAvatarChanged(profile_path)); |
553 } | 554 } |
554 | 555 |
555 void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index, | 556 void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index, |
556 bool is_omitted) { | 557 bool is_omitted) { |
557 if (IsOmittedProfileAtIndex(index) == is_omitted) | 558 if (IsOmittedProfileAtIndex(index) == is_omitted) |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | 860 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); |
860 } | 861 } |
861 | 862 |
862 void ProfileInfoCache::DownloadHighResAvatarIfNeeded( | 863 void ProfileInfoCache::DownloadHighResAvatarIfNeeded( |
863 size_t icon_index, | 864 size_t icon_index, |
864 const base::FilePath& profile_path) { | 865 const base::FilePath& profile_path) { |
865 // Downloading is only supported on desktop. | 866 // Downloading is only supported on desktop. |
866 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) | 867 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) |
867 return; | 868 return; |
868 #endif | 869 #endif |
| 870 DCHECK(!disable_avatar_download_for_testing_); |
869 | 871 |
870 const base::FilePath& file_path = | 872 const base::FilePath& file_path = |
871 profiles::GetPathOfHighResAvatarAtIndex(icon_index); | 873 profiles::GetPathOfHighResAvatarAtIndex(icon_index); |
872 base::Closure callback = | 874 base::Closure callback = |
873 base::Bind(&ProfileInfoCache::DownloadHighResAvatar, | 875 base::Bind(&ProfileInfoCache::DownloadHighResAvatar, |
874 AsWeakPtr(), | 876 AsWeakPtr(), |
875 icon_index, | 877 icon_index, |
876 profile_path); | 878 profile_path); |
877 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 879 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
878 base::Bind(&RunCallbackIfFileMissing, file_path, callback)); | 880 base::Bind(&RunCallbackIfFileMissing, file_path, callback)); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 const base::FilePath& profile_path, | 1060 const base::FilePath& profile_path, |
1059 const std::string& key, | 1061 const std::string& key, |
1060 const base::FilePath& image_path) const { | 1062 const base::FilePath& image_path) const { |
1061 // If the picture is already loaded then use it. | 1063 // If the picture is already loaded then use it. |
1062 if (cached_avatar_images_.count(key)) { | 1064 if (cached_avatar_images_.count(key)) { |
1063 if (cached_avatar_images_[key]->IsEmpty()) | 1065 if (cached_avatar_images_[key]->IsEmpty()) |
1064 return NULL; | 1066 return NULL; |
1065 return cached_avatar_images_[key]; | 1067 return cached_avatar_images_[key]; |
1066 } | 1068 } |
1067 | 1069 |
| 1070 // Don't download the image if downloading is disabled for tests. |
| 1071 if (disable_avatar_download_for_testing_) |
| 1072 return NULL; |
| 1073 |
1068 // If the picture is already being loaded then don't try loading it again. | 1074 // If the picture is already being loaded then don't try loading it again. |
1069 if (cached_avatar_images_loading_[key]) | 1075 if (cached_avatar_images_loading_[key]) |
1070 return NULL; | 1076 return NULL; |
1071 cached_avatar_images_loading_[key] = true; | 1077 cached_avatar_images_loading_[key] = true; |
1072 | 1078 |
1073 gfx::Image** image = new gfx::Image*; | 1079 gfx::Image** image = new gfx::Image*; |
1074 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 1080 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
1075 base::Bind(&ReadBitmap, image_path, image), | 1081 base::Bind(&ReadBitmap, image_path, image), |
1076 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, | 1082 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, |
1077 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), | 1083 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1163 std::vector<base::FilePath>::const_iterator it; | 1169 std::vector<base::FilePath>::const_iterator it; |
1164 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { | 1170 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { |
1165 size_t profile_index = GetIndexOfProfileWithPath(*it); | 1171 size_t profile_index = GetIndexOfProfileWithPath(*it); |
1166 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); | 1172 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); |
1167 // This will assign a new "Person %d" type name and re-sort the cache. | 1173 // This will assign a new "Person %d" type name and re-sort the cache. |
1168 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( | 1174 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( |
1169 GetAvatarIconIndexOfProfileAtIndex(profile_index))); | 1175 GetAvatarIconIndexOfProfileAtIndex(profile_index))); |
1170 } | 1176 } |
1171 #endif | 1177 #endif |
1172 } | 1178 } |
OLD | NEW |