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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
142 if (!base::PathExists(file_path)) | 142 if (!base::PathExists(file_path)) |
143 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | 143 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
144 } | 144 } |
145 | 145 |
146 void DeleteBitmap(const base::FilePath& image_path) { | 146 void DeleteBitmap(const base::FilePath& image_path) { |
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
148 base::DeleteFile(image_path, false); | 148 base::DeleteFile(image_path, false); |
149 } | 149 } |
150 | 150 |
151 void DeleteDownloader(ProfileAvatarDownloader* downloader) { | |
noms (inactive)
2015/01/30 16:05:40
nit: add a comment as to why we would need to post
Marc Treib
2015/01/30 16:12:40
Done.
This would be much nicer as an inline lambda
| |
152 delete downloader; | |
153 } | |
154 | |
151 } // namespace | 155 } // namespace |
152 | 156 |
153 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 157 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
154 const base::FilePath& user_data_dir) | 158 const base::FilePath& user_data_dir) |
155 : prefs_(prefs), | 159 : prefs_(prefs), |
156 user_data_dir_(user_data_dir) { | 160 user_data_dir_(user_data_dir) { |
157 // Populate the cache | 161 // Populate the cache |
158 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 162 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
159 base::DictionaryValue* cache = update.Get(); | 163 base::DictionaryValue* cache = update.Get(); |
160 for (base::DictionaryValue::Iterator it(*cache); | 164 for (base::DictionaryValue::Iterator it(*cache); |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 const base::FilePath& image_path, | 900 const base::FilePath& image_path, |
897 const base::FilePath& profile_path) { | 901 const base::FilePath& profile_path) { |
898 cached_avatar_images_[key] = new gfx::Image(*image); | 902 cached_avatar_images_[key] = new gfx::Image(*image); |
899 | 903 |
900 scoped_ptr<ImageData> data(new ImageData); | 904 scoped_ptr<ImageData> data(new ImageData); |
901 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); | 905 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); |
902 data->assign(png_data->front(), png_data->front() + png_data->size()); | 906 data->assign(png_data->front(), png_data->front() + png_data->size()); |
903 | 907 |
904 // Remove the file from the list of downloads in progress. Note that this list | 908 // Remove the file from the list of downloads in progress. Note that this list |
905 // only contains the high resolution avatars, and not the Gaia profile images. | 909 // only contains the high resolution avatars, and not the Gaia profile images. |
906 if (avatar_images_downloads_in_progress_[key]) { | 910 auto downloader_iter = avatar_images_downloads_in_progress_.find(key); |
noms (inactive)
2015/01/30 16:05:40
YAY AUTO!!!
| |
907 delete avatar_images_downloads_in_progress_[key]; | 911 if (downloader_iter != avatar_images_downloads_in_progress_.end()) { |
908 avatar_images_downloads_in_progress_[key] = NULL; | 912 // We mustn't delete the avatar downloader right here, since we're being |
913 // called by it. | |
914 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
915 base::Bind(&DeleteDownloader, | |
916 downloader_iter->second)); | |
917 avatar_images_downloads_in_progress_.erase(downloader_iter); | |
909 } | 918 } |
910 | 919 |
911 if (!data->size()) { | 920 if (!data->size()) { |
912 LOG(ERROR) << "Failed to PNG encode the image."; | 921 LOG(ERROR) << "Failed to PNG encode the image."; |
913 } else { | 922 } else { |
914 base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved, | 923 base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved, |
915 AsWeakPtr(), key, profile_path); | 924 AsWeakPtr(), key, profile_path); |
916 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 925 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
917 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback)); | 926 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback)); |
918 } | 927 } |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1041 void ProfileInfoCache::DownloadHighResAvatar( | 1050 void ProfileInfoCache::DownloadHighResAvatar( |
1042 size_t icon_index, | 1051 size_t icon_index, |
1043 const base::FilePath& profile_path) { | 1052 const base::FilePath& profile_path) { |
1044 // Downloading is only supported on desktop. | 1053 // Downloading is only supported on desktop. |
1045 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) | 1054 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) |
1046 return; | 1055 return; |
1047 #endif | 1056 #endif |
1048 const std::string file_name = | 1057 const std::string file_name = |
1049 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index); | 1058 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index); |
1050 // If the file is already being downloaded, don't start another download. | 1059 // If the file is already being downloaded, don't start another download. |
1051 if (avatar_images_downloads_in_progress_[file_name]) | 1060 if (avatar_images_downloads_in_progress_.count(file_name)) |
1052 return; | 1061 return; |
1053 | 1062 |
1054 // Start the download for this file. The cache takes ownership of the | 1063 // Start the download for this file. The cache takes ownership of the |
1055 // |avatar_downloader|, which will be deleted when the download completes, or | 1064 // |avatar_downloader|, which will be deleted when the download completes, or |
1056 // if that never happens, when the ProfileInfoCache is destroyed. | 1065 // if that never happens, when the ProfileInfoCache is destroyed. |
1057 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( | 1066 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( |
1058 icon_index, | 1067 icon_index, |
1059 profile_path, | 1068 profile_path, |
1060 this); | 1069 this); |
1061 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; | 1070 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1161 std::vector<base::FilePath>::const_iterator it; | 1170 std::vector<base::FilePath>::const_iterator it; |
1162 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { | 1171 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { |
1163 size_t profile_index = GetIndexOfProfileWithPath(*it); | 1172 size_t profile_index = GetIndexOfProfileWithPath(*it); |
1164 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); | 1173 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); |
1165 // This will assign a new "Person %d" type name and re-sort the cache. | 1174 // This will assign a new "Person %d" type name and re-sort the cache. |
1166 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( | 1175 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( |
1167 GetAvatarIconIndexOfProfileAtIndex(profile_index))); | 1176 GetAvatarIconIndexOfProfileAtIndex(profile_index))); |
1168 } | 1177 } |
1169 #endif | 1178 #endif |
1170 } | 1179 } |
OLD | NEW |