Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/profiles/profile_info_cache.cc

Issue 890873004: ProfileInfoCache: Don't delete ProfileAvatarDownloader while we're being called by it (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // Used by SaveAvatarImageAtPath to post a task to delete the |downloader|
152 // "soon". We can't just delete it directly there because
153 // SaveAvatarImageAtPath is called from this very downloader.
154 void DeleteDownloader(ProfileAvatarDownloader* downloader) {
155 delete downloader;
156 }
157
151 } // namespace 158 } // namespace
152 159
153 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, 160 ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
154 const base::FilePath& user_data_dir) 161 const base::FilePath& user_data_dir)
155 : prefs_(prefs), 162 : prefs_(prefs),
156 user_data_dir_(user_data_dir) { 163 user_data_dir_(user_data_dir) {
157 // Populate the cache 164 // Populate the cache
158 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); 165 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
159 base::DictionaryValue* cache = update.Get(); 166 base::DictionaryValue* cache = update.Get();
160 for (base::DictionaryValue::Iterator it(*cache); 167 for (base::DictionaryValue::Iterator it(*cache);
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 const base::FilePath& image_path, 903 const base::FilePath& image_path,
897 const base::FilePath& profile_path) { 904 const base::FilePath& profile_path) {
898 cached_avatar_images_[key] = new gfx::Image(*image); 905 cached_avatar_images_[key] = new gfx::Image(*image);
899 906
900 scoped_ptr<ImageData> data(new ImageData); 907 scoped_ptr<ImageData> data(new ImageData);
901 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); 908 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes();
902 data->assign(png_data->front(), png_data->front() + png_data->size()); 909 data->assign(png_data->front(), png_data->front() + png_data->size());
903 910
904 // Remove the file from the list of downloads in progress. Note that this list 911 // 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. 912 // only contains the high resolution avatars, and not the Gaia profile images.
906 if (avatar_images_downloads_in_progress_[key]) { 913 auto downloader_iter = avatar_images_downloads_in_progress_.find(key);
907 delete avatar_images_downloads_in_progress_[key]; 914 if (downloader_iter != avatar_images_downloads_in_progress_.end()) {
908 avatar_images_downloads_in_progress_[key] = NULL; 915 // We mustn't delete the avatar downloader right here, since we're being
916 // called by it.
917 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
918 base::Bind(&DeleteDownloader,
919 downloader_iter->second));
gab 2015/02/03 18:20:16 FYI, BrowserThread::DeleteSoon(BrowserThread::UI,
920 avatar_images_downloads_in_progress_.erase(downloader_iter);
909 } 921 }
910 922
911 if (!data->size()) { 923 if (!data->size()) {
912 LOG(ERROR) << "Failed to PNG encode the image."; 924 LOG(ERROR) << "Failed to PNG encode the image.";
913 } else { 925 } else {
914 base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved, 926 base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved,
915 AsWeakPtr(), key, profile_path); 927 AsWeakPtr(), key, profile_path);
916 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 928 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
917 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback)); 929 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback));
918 } 930 }
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 void ProfileInfoCache::DownloadHighResAvatar( 1053 void ProfileInfoCache::DownloadHighResAvatar(
1042 size_t icon_index, 1054 size_t icon_index,
1043 const base::FilePath& profile_path) { 1055 const base::FilePath& profile_path) {
1044 // Downloading is only supported on desktop. 1056 // Downloading is only supported on desktop.
1045 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) 1057 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
1046 return; 1058 return;
1047 #endif 1059 #endif
1048 const std::string file_name = 1060 const std::string file_name =
1049 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index); 1061 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index);
1050 // If the file is already being downloaded, don't start another download. 1062 // If the file is already being downloaded, don't start another download.
1051 if (avatar_images_downloads_in_progress_[file_name]) 1063 if (avatar_images_downloads_in_progress_.count(file_name))
1052 return; 1064 return;
1053 1065
1054 // Start the download for this file. The cache takes ownership of the 1066 // Start the download for this file. The cache takes ownership of the
1055 // |avatar_downloader|, which will be deleted when the download completes, or 1067 // |avatar_downloader|, which will be deleted when the download completes, or
1056 // if that never happens, when the ProfileInfoCache is destroyed. 1068 // if that never happens, when the ProfileInfoCache is destroyed.
1057 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( 1069 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader(
1058 icon_index, 1070 icon_index,
1059 profile_path, 1071 profile_path,
1060 this); 1072 this);
1061 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; 1073 avatar_images_downloads_in_progress_[file_name] = avatar_downloader;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 std::vector<base::FilePath>::const_iterator it; 1173 std::vector<base::FilePath>::const_iterator it;
1162 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { 1174 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) {
1163 size_t profile_index = GetIndexOfProfileWithPath(*it); 1175 size_t profile_index = GetIndexOfProfileWithPath(*it);
1164 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); 1176 SetProfileIsUsingDefaultNameAtIndex(profile_index, true);
1165 // This will assign a new "Person %d" type name and re-sort the cache. 1177 // This will assign a new "Person %d" type name and re-sort the cache.
1166 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( 1178 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile(
1167 GetAvatarIconIndexOfProfileAtIndex(profile_index))); 1179 GetAvatarIconIndexOfProfileAtIndex(profile_index)));
1168 } 1180 }
1169 #endif 1181 #endif
1170 } 1182 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_info_cache.h ('k') | chrome/browser/profiles/profile_info_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698