Commit af25550b authored by treib's avatar treib Committed by Commit bot

ProfileInfoCache: Don't delete ProfileAvatarDownloader while we're being called by it

BUG=453795

Review URL: https://codereview.chromium.org/890873004

Cr-Commit-Position: refs/heads/master@{#313937}
parent f928d6bc
...@@ -148,6 +148,13 @@ void DeleteBitmap(const base::FilePath& image_path) { ...@@ -148,6 +148,13 @@ void DeleteBitmap(const base::FilePath& image_path) {
base::DeleteFile(image_path, false); base::DeleteFile(image_path, false);
} }
// Used by SaveAvatarImageAtPath to post a task to delete the |downloader|
// "soon". We can't just delete it directly there because
// SaveAvatarImageAtPath is called from this very downloader.
void DeleteDownloader(ProfileAvatarDownloader* downloader) {
delete downloader;
}
} // namespace } // namespace
ProfileInfoCache::ProfileInfoCache(PrefService* prefs, ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
...@@ -903,9 +910,14 @@ void ProfileInfoCache::SaveAvatarImageAtPath( ...@@ -903,9 +910,14 @@ void ProfileInfoCache::SaveAvatarImageAtPath(
// Remove the file from the list of downloads in progress. Note that this list // Remove the file from the list of downloads in progress. Note that this list
// only contains the high resolution avatars, and not the Gaia profile images. // only contains the high resolution avatars, and not the Gaia profile images.
if (avatar_images_downloads_in_progress_[key]) { auto downloader_iter = avatar_images_downloads_in_progress_.find(key);
delete avatar_images_downloads_in_progress_[key]; if (downloader_iter != avatar_images_downloads_in_progress_.end()) {
avatar_images_downloads_in_progress_[key] = NULL; // We mustn't delete the avatar downloader right here, since we're being
// called by it.
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&DeleteDownloader,
downloader_iter->second));
avatar_images_downloads_in_progress_.erase(downloader_iter);
} }
if (!data->size()) { if (!data->size()) {
...@@ -1048,7 +1060,7 @@ void ProfileInfoCache::DownloadHighResAvatar( ...@@ -1048,7 +1060,7 @@ void ProfileInfoCache::DownloadHighResAvatar(
const std::string file_name = const std::string file_name =
profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index); profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index);
// If the file is already being downloaded, don't start another download. // If the file is already being downloaded, don't start another download.
if (avatar_images_downloads_in_progress_[file_name]) if (avatar_images_downloads_in_progress_.count(file_name))
return; return;
// Start the download for this file. The cache takes ownership of the // Start the download for this file. The cache takes ownership of the
......
...@@ -231,7 +231,7 @@ class ProfileInfoCache : public ProfileInfoInterface, ...@@ -231,7 +231,7 @@ class ProfileInfoCache : public ProfileInfoInterface,
// This prevents a picture from being downloaded multiple times. The // This prevents a picture from being downloaded multiple times. The
// ProfileAvatarDownloader instances are deleted when the download completes // ProfileAvatarDownloader instances are deleted when the download completes
// or when the ProfileInfoCache is destroyed. // or when the ProfileInfoCache is destroyed.
mutable std::map<std::string, ProfileAvatarDownloader*> std::map<std::string, ProfileAvatarDownloader*>
avatar_images_downloads_in_progress_; avatar_images_downloads_in_progress_;
DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
......
...@@ -571,11 +571,13 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { ...@@ -571,11 +571,13 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
avatar_downloader.OnFetchComplete( avatar_downloader.OnFetchComplete(
GURL("http://www.google.com/avatar.png"), &bitmap); GURL("http://www.google.com/avatar.png"), &bitmap);
// Now the download should not be in progress anymore.
EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
std::string file_name = std::string file_name =
profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex); profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
// The file should have been cached and saved. // The file should have been cached and saved.
EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size()); EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
EXPECT_EQ(GetCache()->cached_avatar_images_[file_name], EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment