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

Unified Diff: chrome/browser/profiles/profile_info_cache.cc

Issue 861053004: [Profiles] Send out less profile avatar related notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mike nit Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/profiles/profile_info_cache.h ('k') | chrome/browser/profiles/profile_info_cache_observer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/profiles/profile_info_cache.cc
diff --git a/chrome/browser/profiles/profile_info_cache.cc b/chrome/browser/profiles/profile_info_cache.cc
index 4e6803e2ceb57727fbdd9386df1918ab01c384ac..afbda882e5de226aca50d3c25b04d508b6d3b766 100644
--- a/chrome/browser/profiles/profile_info_cache.cc
+++ b/chrome/browser/profiles/profile_info_cache.cc
@@ -135,6 +135,13 @@ void ReadBitmap(const base::FilePath& image_path,
*out_image = new gfx::Image(image);
}
+void RunCallbackIfFileMissing(const base::FilePath& file_path,
+ const base::Closure& callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ if (!base::PathExists(file_path))
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
+}
+
void DeleteBitmap(const base::FilePath& image_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::DeleteFile(image_path, false);
@@ -214,7 +221,7 @@ void ProfileInfoCache::AddProfileToCache(
sorted_keys_.insert(FindPositionForProfile(key, name), key);
if (switches::IsNewAvatarMenu())
- DownloadHighResAvatar(icon_index, profile_path);
+ DownloadHighResAvatarIfNeeded(icon_index, profile_path);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
@@ -536,9 +543,8 @@ void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
- // If needed, start downloading the high-res avatar.
if (switches::IsNewAvatarMenu())
- DownloadHighResAvatar(icon_index, profile_path);
+ DownloadHighResAvatarIfNeeded(icon_index, profile_path);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
@@ -693,7 +699,6 @@ void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index,
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
- // Retrieve some info to update observers who care about avatar changes.
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
@@ -848,7 +853,7 @@ void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kProfileInfoCache);
}
-void ProfileInfoCache::DownloadHighResAvatar(
+void ProfileInfoCache::DownloadHighResAvatarIfNeeded(
size_t icon_index,
const base::FilePath& profile_path) {
// Downloading is only supported on desktop.
@@ -856,25 +861,15 @@ void ProfileInfoCache::DownloadHighResAvatar(
return;
#endif
- // TODO(noms): We should check whether the file already exists on disk
- // before trying to re-download it. For now, since this is behind a flag and
- // the resources are still changing, re-download it every time the profile
- // avatar changes, to make sure we have the latest copy.
- std::string file_name = profiles::GetDefaultAvatarIconFileNameAtIndex(
- icon_index);
- // If the file is already being downloaded, don't start another download.
- if (avatar_images_downloads_in_progress_[file_name])
- return;
-
- // Start the download for this file. The cache takes ownership of the
- // |avatar_downloader|, which will be deleted when the download completes, or
- // if that never happens, when the ProfileInfoCache is destroyed.
- ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader(
- icon_index,
- profile_path,
- this);
- avatar_images_downloads_in_progress_[file_name] = avatar_downloader;
- avatar_downloader->Start();
+ const base::FilePath& file_path =
+ profiles::GetPathOfHighResAvatarAtIndex(icon_index);
+ base::Closure callback =
+ base::Bind(&ProfileInfoCache::DownloadHighResAvatar,
+ AsWeakPtr(),
+ icon_index,
+ profile_path);
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&RunCallbackIfFileMissing, file_path, callback));
}
void ProfileInfoCache::SaveAvatarImageAtPath(
@@ -888,12 +883,18 @@ void ProfileInfoCache::SaveAvatarImageAtPath(
scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes();
data->assign(png_data->front(), png_data->front() + png_data->size());
+ // 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.
+ if (avatar_images_downloads_in_progress_[key]) {
+ delete avatar_images_downloads_in_progress_[key];
+ avatar_images_downloads_in_progress_[key] = NULL;
+ }
+
if (!data->size()) {
LOG(ERROR) << "Failed to PNG encode the image.";
} else {
base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved,
AsWeakPtr(), key, profile_path);
-
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback));
}
@@ -1015,6 +1016,30 @@ const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex(
key, image_path);
}
+void ProfileInfoCache::DownloadHighResAvatar(
+ size_t icon_index,
+ const base::FilePath& profile_path) {
+ // Downloading is only supported on desktop.
+#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
+ return;
+#endif
+ const std::string file_name =
+ profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index);
+ // If the file is already being downloaded, don't start another download.
+ if (avatar_images_downloads_in_progress_[file_name])
+ return;
+
+ // Start the download for this file. The cache takes ownership of the
+ // |avatar_downloader|, which will be deleted when the download completes, or
+ // if that never happens, when the ProfileInfoCache is destroyed.
+ ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader(
+ icon_index,
+ profile_path,
+ this);
+ avatar_images_downloads_in_progress_[file_name] = avatar_downloader;
+ avatar_downloader->Start();
+}
+
const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath(
const base::FilePath& profile_path,
const std::string& key,
@@ -1063,7 +1088,7 @@ void ProfileInfoCache::OnAvatarPictureLoaded(const base::FilePath& profile_path,
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
- OnProfileAvatarChanged(profile_path));
+ OnProfileHighResAvatarLoaded(profile_path));
}
void ProfileInfoCache::OnAvatarPictureSaved(
@@ -1077,16 +1102,8 @@ void ProfileInfoCache::OnAvatarPictureSaved(
content::NotificationService::NoDetails());
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
- observer_list_,
- OnProfileAvatarChanged(profile_path));
-
- // 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.
- if (!avatar_images_downloads_in_progress_[file_name])
- return;
-
- delete avatar_images_downloads_in_progress_[file_name];
- avatar_images_downloads_in_progress_[file_name] = NULL;
+ observer_list_,
+ OnProfileHighResAvatarLoaded(profile_path));
}
void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() {
@@ -1110,9 +1127,8 @@ void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() {
l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME));
for (size_t i = 0; i < GetNumberOfProfiles(); i++) {
- // If needed, start downloading the high-res avatar for this profile.
- DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i),
- GetPathOfProfileAtIndex(i));
+ DownloadHighResAvatarIfNeeded(GetAvatarIconIndexOfProfileAtIndex(i),
+ GetPathOfProfileAtIndex(i));
base::string16 name = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
if (name == default_profile_name || name == default_legacy_profile_name)
« no previous file with comments | « chrome/browser/profiles/profile_info_cache.h ('k') | chrome/browser/profiles/profile_info_cache_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698