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 988f0ffd85fb3b4ceee8147bf6316ae1fdfa278e..099cb29243819d2899d9975c2c12e8a19a0d0e4f 100644 |
--- a/chrome/browser/profiles/profile_info_cache.cc |
+++ b/chrome/browser/profiles/profile_info_cache.cc |
@@ -4,12 +4,14 @@ |
#include "chrome/browser/profiles/profile_info_cache.h" |
+#include "base/file_util.h" |
#include "base/format_macros.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/rand_util.h" |
#include "base/string_number_conversions.h" |
#include "base/stringprintf.h" |
+#include "base/threading/thread_restrictions.h" |
#include "base/utf_string_conversions.h" |
#include "base/values.h" |
#include "chrome/browser/prefs/pref_service.h" |
@@ -21,14 +23,20 @@ |
#include "grit/theme_resources.h" |
#include "ui/base/l10n/l10n_util.h" |
#include "ui/base/resource/resource_bundle.h" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "ui/gfx/image/image.h" |
namespace { |
const char kNameKey[] = "name"; |
+const char kGAIANameKey[] = "test_gaia_name"; |
const char kUserNameKey[] = "user_name"; |
const char kAvatarIconKey[] = "avatar_icon"; |
+const char kAvatarIconFileNameKey[] = "test_avatar_icon_file_name"; |
const char kBackgroundAppsKey[] = "background_apps"; |
const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; |
+const char kIsUsingCustomAvatarIcon[] = "test_is_using_custom_avatar_icon"; |
+const char kIsUsingGAIAName[] = "test_is_using_gaia_name"; |
const int kDefaultAvatarIconResources[] = { |
IDR_PROFILE_AVATAR_0, |
@@ -88,6 +96,72 @@ const int kDefaultNames[] = { |
} // namespace |
+FilePath ProfileInfoCache::GetImagePath(std::string key, |
+ string16 file_name) { |
+ FilePath::StringType f1; |
+#if defined(OS_POSIX) |
+ f1 = key; |
+#elif defined(OS_WIN) |
+ f1 = ASCIIToWide(key); |
+#endif |
+ |
+ FilePath::StringType f2; |
+#if defined(OS_POSIX) |
+ f2 = UTF16ToUTF8(file_name); |
+#elif defined(OS_WIN) |
+ f2 = file_name; |
+#endif |
+ |
+ FilePath p = user_data_dir_.Append(f1); |
+ return p.Append(f2); |
+} |
+ |
+void ProfileInfoCache::ReadBitmap(std::string key, |
+ string16 file_name, |
+ SkBitmap* bmp) { |
+ if (file_name.empty()) |
+ return; |
+ |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ FilePath image_path = GetImagePath(key, file_name); |
+ |
+ std::string image_data; |
+ if (!file_util::ReadFileToString(image_path, &image_data)) { |
+ LOG(ERROR) << "Failed to read PNG file from disk."; |
+ return; |
+ } |
+ |
+ const unsigned char* data = |
+ reinterpret_cast<const unsigned char*>(image_data.data()); |
+ SkBitmap image; |
+ if (!gfx::PNGCodec::Decode(data, image_data.length(), &image)) { |
+ LOG(ERROR) << "Failed to decode PNG file."; |
+ return; |
+ } |
+ |
+ *bmp = image; |
+} |
+ |
+void ProfileInfoCache::SaveBitmap(std::string key, |
+ string16 file_name, |
+ const SkBitmap& image) { |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ FilePath image_path = GetImagePath(key, file_name); |
+ |
+ std::vector<unsigned char> encoded_image; |
+ if (!gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &encoded_image)) { |
+ LOG(ERROR) << "Failed to PNG encode the image."; |
+ return; |
+ } |
+ |
+ if (file_util::WriteFile(image_path, |
+ reinterpret_cast<char*>(&encoded_image[0]), |
+ encoded_image.size()) == -1) { |
+ LOG(ERROR) << "Failed to save image to file."; |
+ return; |
+ } |
+} |
+ |
ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
const FilePath& user_data_dir) |
: prefs_(prefs), |
@@ -103,6 +177,15 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
string16 name; |
info->GetString(kNameKey, &name); |
sorted_keys_.insert(FindPositionForProfile(key, name), key); |
+ |
+ string16 file_name; |
+ info->GetString(kAvatarIconFileNameKey, &file_name); |
+ SkBitmap bmp; |
+ ReadBitmap(key, UTF8ToUTF16("gaia_picture.png"), &bmp); |
+ if (!bmp.isNull()) { |
+ bitmaps_[key] = new SkBitmap(bmp); |
+ } else { |
+ } |
} |
} |
@@ -164,8 +247,18 @@ size_t ProfileInfoCache::GetIndexOfProfileWithPath( |
} |
string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { |
+ if (IsUsinGAIANameForProfileAtIndex(index)) { |
+ return GetGAIANameOfProfileAtIndex(index); |
+ } else { |
+ string16 name; |
+ GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
+ return name; |
+ } |
+} |
+ |
+string16 ProfileInfoCache::GetGAIANameOfProfileAtIndex(size_t index) const { |
string16 name; |
- GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
+ GetInfoForProfileAtIndex(index)->GetString(kGAIANameKey, &name); |
return name; |
} |
@@ -185,13 +278,29 @@ string16 ProfileInfoCache::GetUserNameOfProfileAtIndex(size_t index) const { |
return user_name; |
} |
-const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
+gfx::Image ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
size_t index) const { |
+ if (IsUsingCustomAvatarIconForProfileAtIndex(index)) { |
+ SkBitmap bmp = GetGAIAPictureOfProfileAtIndex(index); |
+ if (!bmp.isNull()) { |
+ return gfx::Image(new SkBitmap(bmp)); |
+ } |
+ } |
+ |
int resource_id = GetDefaultAvatarIconResourceIDAtIndex( |
GetAvatarIconIndexOfProfileAtIndex(index)); |
return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); |
} |
+SkBitmap ProfileInfoCache::GetGAIAPictureOfProfileAtIndex( |
+ size_t index) const { |
+ std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
+ const SkBitmap* bitmap = bitmaps_[key]; |
+ if (bitmap) |
+ return *bitmap; |
+ return SkBitmap(); |
+} |
+ |
bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex( |
size_t index) const { |
bool background_app_status; |
@@ -233,6 +342,14 @@ void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
content::NotificationService::NoDetails()); |
} |
+void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index, |
+ const string16& name) { |
+ scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
+ info->SetString(kGAIANameKey, name); |
+ // This takes ownership of |info|. |
+ SetInfoForProfileAtIndex(index, info.release()); |
+} |
+ |
void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
const string16& user_name) { |
scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
@@ -241,6 +358,47 @@ void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
SetInfoForProfileAtIndex(index, info.release()); |
} |
+bool ProfileInfoCache::IsUsingCustomAvatarIconForProfileAtIndex( |
+ size_t index) const { |
+ if (GetUserNameOfProfileAtIndex(index).empty()) { |
+ return false; |
+ } |
+ |
+ bool is_custom = false; |
+ GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingCustomAvatarIcon, |
+ &is_custom); |
+ return is_custom; |
+} |
+ |
+void ProfileInfoCache::SetIsUsingCustomAvatarIconForProfileAtIndex( |
+ size_t index, bool value) { |
+ scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
+ info->SetBoolean(kIsUsingCustomAvatarIcon, value); |
+ // This takes ownership of |info|. |
+ SetInfoForProfileAtIndex(index, info.release()); |
+} |
+ |
+bool ProfileInfoCache::IsUsinGAIANameForProfileAtIndex( |
+ size_t index) const { |
+ if (GetUserNameOfProfileAtIndex(index).empty()) { |
+ return false; |
+ } |
+ |
+ bool is_custom = false; |
+ GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingGAIAName, &is_custom); |
+ return is_custom; |
+} |
+ |
+void ProfileInfoCache::SetIsUsingGAIANameForProfileAtIndex( |
+ size_t index, bool value) { |
+ scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
+ info->SetBoolean(kIsUsingGAIAName, value); |
+ // This takes ownership of |info|. |
+ SetInfoForProfileAtIndex(index, info.release()); |
+} |
+ |
+ |
+ |
void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
size_t icon_index) { |
scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
@@ -249,6 +407,18 @@ void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
SetInfoForProfileAtIndex(index, info.release()); |
} |
+void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex( |
+ size_t index, const SkBitmap& bitmap) { |
+ std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
+ bitmaps_[key] = new SkBitmap(bitmap); |
+ SaveBitmap(key, UTF8ToUTF16("gaia_picture.png"), bitmap); |
+ |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
+ content::NotificationService::AllSources(), |
+ content::NotificationService::NoDetails()); |
+} |
+ |
void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( |
size_t index, |
bool running_background_apps) { |