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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 IDS_DEFAULT_AVATAR_NAME_26 | 82 IDS_DEFAULT_AVATAR_NAME_26 |
83 }; | 83 }; |
84 | 84 |
85 typedef std::vector<unsigned char> ImageData; | 85 typedef std::vector<unsigned char> ImageData; |
86 | 86 |
87 // Writes |data| to disk and takes ownership of the pointer. On successful | 87 // Writes |data| to disk and takes ownership of the pointer. On successful |
88 // completion, it runs |callback|. | 88 // completion, it runs |callback|. |
89 void SaveBitmap(scoped_ptr<ImageData> data, | 89 void SaveBitmap(scoped_ptr<ImageData> data, |
90 const base::FilePath& image_path, | 90 const base::FilePath& image_path, |
91 const base::Closure& callback) { | 91 const base::Closure& callback) { |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 92 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
93 | 93 |
94 // Make sure the destination directory exists. | 94 // Make sure the destination directory exists. |
95 base::FilePath dir = image_path.DirName(); | 95 base::FilePath dir = image_path.DirName(); |
96 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { | 96 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { |
97 LOG(ERROR) << "Failed to create parent directory."; | 97 LOG(ERROR) << "Failed to create parent directory."; |
98 return; | 98 return; |
99 } | 99 } |
100 | 100 |
101 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), | 101 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), |
102 data->size()) == -1) { | 102 data->size()) == -1) { |
103 LOG(ERROR) << "Failed to save image to file."; | 103 LOG(ERROR) << "Failed to save image to file."; |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | 107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
108 } | 108 } |
109 | 109 |
110 // Reads a PNG from disk and decodes it. If the bitmap was successfully read | 110 // Reads a PNG from disk and decodes it. If the bitmap was successfully read |
111 // from disk the then |out_image| will contain the bitmap image, otherwise it | 111 // from disk the then |out_image| will contain the bitmap image, otherwise it |
112 // will be NULL. | 112 // will be NULL. |
113 void ReadBitmap(const base::FilePath& image_path, | 113 void ReadBitmap(const base::FilePath& image_path, |
114 gfx::Image** out_image) { | 114 gfx::Image** out_image) { |
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 115 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
116 *out_image = NULL; | 116 *out_image = NULL; |
117 | 117 |
118 // If the path doesn't exist, don't even try reading it. | 118 // If the path doesn't exist, don't even try reading it. |
119 if (!base::PathExists(image_path)) | 119 if (!base::PathExists(image_path)) |
120 return; | 120 return; |
121 | 121 |
122 std::string image_data; | 122 std::string image_data; |
123 if (!base::ReadFileToString(image_path, &image_data)) { | 123 if (!base::ReadFileToString(image_path, &image_data)) { |
124 LOG(ERROR) << "Failed to read PNG file from disk."; | 124 LOG(ERROR) << "Failed to read PNG file from disk."; |
125 return; | 125 return; |
126 } | 126 } |
127 | 127 |
128 gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( | 128 gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( |
129 base::RefCountedString::TakeString(&image_data)); | 129 base::RefCountedString::TakeString(&image_data)); |
130 if (image.IsEmpty()) { | 130 if (image.IsEmpty()) { |
131 LOG(ERROR) << "Failed to decode PNG file."; | 131 LOG(ERROR) << "Failed to decode PNG file."; |
132 return; | 132 return; |
133 } | 133 } |
134 | 134 |
135 *out_image = new gfx::Image(image); | 135 *out_image = new gfx::Image(image); |
136 } | 136 } |
137 | 137 |
138 void RunCallbackIfFileMissing(const base::FilePath& file_path, | 138 void RunCallbackIfFileMissing(const base::FilePath& file_path, |
139 const base::Closure& callback) { | 139 const base::Closure& callback) { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
141 if (!base::PathExists(file_path)) | 141 if (!base::PathExists(file_path)) |
142 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | 142 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
143 } | 143 } |
144 | 144 |
145 void DeleteBitmap(const base::FilePath& image_path) { | 145 void DeleteBitmap(const base::FilePath& image_path) { |
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 146 DCHECK_CURRENTLY_ON(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 disable_avatar_download_for_testing_(false) { |
(...skipping 929 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 base::Bind(&ReadBitmap, image_path, image), | 1086 base::Bind(&ReadBitmap, image_path, image), |
1087 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, | 1087 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, |
1088 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), | 1088 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), |
1089 profile_path, key, image)); | 1089 profile_path, key, image)); |
1090 return NULL; | 1090 return NULL; |
1091 } | 1091 } |
1092 | 1092 |
1093 void ProfileInfoCache::OnAvatarPictureLoaded(const base::FilePath& profile_path, | 1093 void ProfileInfoCache::OnAvatarPictureLoaded(const base::FilePath& profile_path, |
1094 const std::string& key, | 1094 const std::string& key, |
1095 gfx::Image** image) const { | 1095 gfx::Image** image) const { |
1096 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1096 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1097 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175 | 1097 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/461175 |
1098 // is fixed. | 1098 // is fixed. |
1099 tracked_objects::ScopedTracker tracking_profile1( | 1099 tracked_objects::ScopedTracker tracking_profile1( |
1100 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1100 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
1101 "461175 ProfileInfoCache::OnAvatarPictureLoaded::Start")); | 1101 "461175 ProfileInfoCache::OnAvatarPictureLoaded::Start")); |
1102 | 1102 |
1103 cached_avatar_images_loading_[key] = false; | 1103 cached_avatar_images_loading_[key] = false; |
1104 delete cached_avatar_images_[key]; | 1104 delete cached_avatar_images_[key]; |
1105 | 1105 |
1106 if (*image) { | 1106 if (*image) { |
(...skipping 20 matching lines...) Expand all Loading... |
1127 delete image; | 1127 delete image; |
1128 | 1128 |
1129 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 1129 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
1130 observer_list_, | 1130 observer_list_, |
1131 OnProfileHighResAvatarLoaded(profile_path)); | 1131 OnProfileHighResAvatarLoaded(profile_path)); |
1132 } | 1132 } |
1133 | 1133 |
1134 void ProfileInfoCache::OnAvatarPictureSaved( | 1134 void ProfileInfoCache::OnAvatarPictureSaved( |
1135 const std::string& file_name, | 1135 const std::string& file_name, |
1136 const base::FilePath& profile_path) { | 1136 const base::FilePath& profile_path) { |
1137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1137 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1138 | 1138 |
1139 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 1139 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
1140 observer_list_, | 1140 observer_list_, |
1141 OnProfileHighResAvatarLoaded(profile_path)); | 1141 OnProfileHighResAvatarLoaded(profile_path)); |
1142 } | 1142 } |
1143 | 1143 |
1144 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() { | 1144 void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() { |
1145 DCHECK(switches::IsNewAvatarMenu()); | 1145 DCHECK(switches::IsNewAvatarMenu()); |
1146 | 1146 |
1147 // Only do this on desktop platforms. | 1147 // Only do this on desktop platforms. |
(...skipping 26 matching lines...) Expand all Loading... |
1174 std::vector<base::FilePath>::const_iterator it; | 1174 std::vector<base::FilePath>::const_iterator it; |
1175 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { | 1175 for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) { |
1176 size_t profile_index = GetIndexOfProfileWithPath(*it); | 1176 size_t profile_index = GetIndexOfProfileWithPath(*it); |
1177 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); | 1177 SetProfileIsUsingDefaultNameAtIndex(profile_index, true); |
1178 // This will assign a new "Person %d" type name and re-sort the cache. | 1178 // This will assign a new "Person %d" type name and re-sort the cache. |
1179 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( | 1179 SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile( |
1180 GetAvatarIconIndexOfProfileAtIndex(profile_index))); | 1180 GetAvatarIconIndexOfProfileAtIndex(profile_index))); |
1181 } | 1181 } |
1182 #endif | 1182 #endif |
1183 } | 1183 } |
OLD | NEW |