Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4207c159ff3e0a308b39e51bbcd4c189179d5a58 |
| --- /dev/null |
| +++ b/chrome/browser/profiles/profile_info_cache.cc |
| @@ -0,0 +1,208 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/profiles/profile_info_cache.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/prefs/scoped_user_pref_update.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "grit/theme_resources.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +namespace { |
| + |
| +const char* kNameKey = "name"; |
|
willchan no longer on Chromium
2011/06/21 08:26:27
Prefer `static const char kNameKey[] = "name";` ov
sail
2011/06/21 16:28:13
Done.
|
| +const char* kAvatarIconKey = "avatar_icon"; |
| +const char* kDefaultUrlPrefix = "chrome://theme/IDR_PROFILE_AVATAR_"; |
| + |
| +const int kDefaultAvatarIconResources[] = { |
| + IDR_PROFILE_AVATAR_0, |
| + IDR_PROFILE_AVATAR_1, |
| + IDR_PROFILE_AVATAR_2, |
| + IDR_PROFILE_AVATAR_3, |
| +}; |
| + |
| +const int kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); |
| + |
| +// Checks if the given URL points to one of the default avatar icons. if it is, |
| +// returns true and its index through |icon_index|. If not, returns false. |
| +bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { |
| + DCHECK(icon_index); |
| + if (url.find(kDefaultUrlPrefix) != 0) |
| + return false; |
| + |
| + int int_value = -1; |
| + if (base::StringToInt(url.begin() + url.size(), |
| + url.end(), |
| + &int_value)) { |
| + if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) |
| + return false; |
| + *icon_index = int_value; |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +// Returns a URL for the default avatar icon with specified index. |
| +std::string GetDefaultAvatarIconUrl(int icon_index) { |
| + DCHECK_LT(icon_index, kDefaultAvatarIconsCount); |
| + return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); |
| +} |
| + |
| +} // namespace |
| + |
| +ProfileInfoCache::ProfileInfoCache(const FilePath& user_data_dir) |
| + : user_data_dir_(user_data_dir) { |
| + // Populate the cache |
| + const DictionaryValue* cache = |
| + g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache); |
|
willchan no longer on Chromium
2011/06/21 08:26:27
This is why your test is crashing. g_browser_proce
sail
2011/06/21 16:28:13
Done.
Awesome, thanks!
|
| + for (DictionaryValue::key_iterator it = cache->begin_keys(); |
| + it != cache->end_keys(); ++it) { |
| + std::string key = *it; |
| + DictionaryValue* info = NULL; |
| + cache->GetDictionary(key, &info); |
| + string16 name; |
| + info->GetString(kNameKey, &name); |
| + sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| + } |
| +} |
| + |
| +void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path, |
| + const string16& name, |
| + size_t icon_index) { |
| + std::string key = CacheKeyFromProfilePath(profile_path); |
| + DictionaryPrefUpdate update(g_browser_process->local_state(), |
| + prefs::kProfileInfoCache); |
| + DictionaryValue* cache = update.Get(); |
| + |
| + scoped_ptr<DictionaryValue> info(new DictionaryValue); |
| + info->SetString(kNameKey, name); |
| + info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| + cache->Set(key, info.release()); |
| + |
| + sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| +} |
| + |
| +void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
| + DictionaryPrefUpdate update(g_browser_process->local_state(), |
| + prefs::kProfileInfoCache); |
| + DictionaryValue* cache = update.Get(); |
| + |
| + std::string key = CacheKeyFromProfilePath(profile_path); |
| + cache->Remove(key, NULL); |
| + sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
| +} |
| + |
| +size_t ProfileInfoCache::GetNumberOfProfiles() const { |
| + return sorted_keys_.size(); |
| +} |
| + |
| +string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { |
| + string16 name; |
| + GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
| + return name; |
| +} |
| + |
| +FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { |
| + FilePath::StringType base_name; |
| +#if defined(OS_POSIX) |
| + base_name = sorted_keys_[index]; |
| +#elif defined(OS_WIN) |
| + base_name = ASCIIToWide(sorted_keys_[index]); |
| +#endif |
| + return user_data_dir_.Append(base_name); |
| +} |
| + |
| +const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
| + size_t index) const { |
| + std::string icon_url; |
| + GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); |
| + size_t icon_index = 0; |
| + if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) { |
| + int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index); |
| + return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); |
| + } |
| + |
| + DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
| + return ResourceBundle::GetSharedInstance().GetImageNamed( |
| + GetDefaultAvatarIconResourceIDAtIndex(0)); |
| +} |
| + |
| +void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
| + const string16& name) { |
| + scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| + info->SetString(kNameKey, name); |
| + // 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()); |
| + info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| + // This takes ownership of |info|. |
| + SetInfoForProfileAtIndex(index, info.release()); |
| +} |
| + |
| +size_t ProfileInfoCache::GetDefaultAvatarIconCount() { |
| + return kDefaultAvatarIconsCount; |
| +} |
| + |
| +int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
| + DCHECK_LT(index, GetDefaultAvatarIconCount()); |
| + return kDefaultAvatarIconResources[index]; |
| +} |
| + |
| +const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
| + size_t index) const { |
| + DCHECK_LT(index, GetNumberOfProfiles()); |
| + const DictionaryValue* cache = |
| + g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache); |
| + DictionaryValue* info = NULL; |
| + cache->GetDictionary(sorted_keys_[index], &info); |
| + return info; |
| +} |
| + |
| +void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, |
| + DictionaryValue* info) { |
| + DictionaryPrefUpdate update(g_browser_process->local_state(), |
| + prefs::kProfileInfoCache); |
| + DictionaryValue* cache = update.Get(); |
| + cache->Set(sorted_keys_[index], info); |
| +} |
| + |
| +std::string ProfileInfoCache::CacheKeyFromProfilePath( |
| + const FilePath& profile_path) const { |
| + DCHECK(user_data_dir_ == profile_path.DirName()); |
| + FilePath base_name = profile_path.BaseName(); |
| + return base_name.MaybeAsASCII(); |
| +} |
| + |
| +std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( |
| + std::string search_key, |
| + const string16& search_name) { |
| + for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| + int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); |
| + if (name_compare < 0) |
| + return sorted_keys_.begin() + i; |
| + if (name_compare == 0) { |
| + int key_compare = search_key.compare(sorted_keys_[i]); |
| + if (key_compare < 0) |
| + return sorted_keys_.begin() + i; |
| + } |
| + } |
| + return sorted_keys_.end(); |
| +} |
| + |
| +void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
| + prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| +} |