Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 &int_value)) { | 44 &int_value)) { |
| 45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) | 45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) |
| 46 return false; | 46 return false; |
| 47 *icon_index = int_value; | 47 *icon_index = int_value; |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 | 50 |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Returns a URL for the default avatar icon with specified index. | |
| 55 std::string GetDefaultAvatarIconUrl(int icon_index) { | |
| 56 DCHECK_LT(icon_index, kDefaultAvatarIconsCount); | |
| 57 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); | |
| 58 } | |
| 59 | |
| 60 } // namespace | 54 } // namespace |
| 61 | 55 |
| 62 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 56 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
| 63 const FilePath& user_data_dir) | 57 const FilePath& user_data_dir) |
| 64 : prefs_(prefs), | 58 : prefs_(prefs), |
| 65 user_data_dir_(user_data_dir) { | 59 user_data_dir_(user_data_dir) { |
| 66 // Populate the cache | 60 // Populate the cache |
| 67 const DictionaryValue* cache = | 61 const DictionaryValue* cache = |
| 68 prefs_->GetDictionary(prefs::kProfileInfoCache); | 62 prefs_->GetDictionary(prefs::kProfileInfoCache); |
| 69 for (DictionaryValue::key_iterator it = cache->begin_keys(); | 63 for (DictionaryValue::key_iterator it = cache->begin_keys(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 95 |
| 102 std::string key = CacheKeyFromProfilePath(profile_path); | 96 std::string key = CacheKeyFromProfilePath(profile_path); |
| 103 cache->Remove(key, NULL); | 97 cache->Remove(key, NULL); |
| 104 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 98 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
| 105 } | 99 } |
| 106 | 100 |
| 107 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 101 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
| 108 return sorted_keys_.size(); | 102 return sorted_keys_.size(); |
| 109 } | 103 } |
| 110 | 104 |
| 105 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | |
| 106 const FilePath& profile_path) const { | |
| 107 if (profile_path.DirName() != user_data_dir_) | |
| 108 return std::string::npos; | |
| 109 std::string search_key = profile_path.BaseName().MaybeAsASCII(); | |
| 110 for (size_t i = 0; i < sorted_keys_.size(); ++i) { | |
| 111 if (sorted_keys_[i] == search_key) | |
| 112 return i; | |
| 113 } | |
| 114 return std::string::npos; | |
| 115 } | |
| 116 | |
| 111 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { | 117 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { |
| 112 string16 name; | 118 string16 name; |
| 113 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); | 119 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
| 114 return name; | 120 return name; |
| 115 } | 121 } |
| 116 | 122 |
| 117 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { | 123 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { |
| 118 FilePath::StringType base_name; | 124 FilePath::StringType base_name; |
| 119 #if defined(OS_POSIX) | 125 #if defined(OS_POSIX) |
| 120 base_name = sorted_keys_[index]; | 126 base_name = sorted_keys_[index]; |
| 121 #elif defined(OS_WIN) | 127 #elif defined(OS_WIN) |
| 122 base_name = ASCIIToWide(sorted_keys_[index]); | 128 base_name = ASCIIToWide(sorted_keys_[index]); |
| 123 #endif | 129 #endif |
| 124 return user_data_dir_.Append(base_name); | 130 return user_data_dir_.Append(base_name); |
| 125 } | 131 } |
| 126 | 132 |
| 127 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 133 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
| 128 size_t index) const { | 134 size_t index) const { |
| 135 int resource_id = GetDefaultAvatarIconResourceIDAtIndex( | |
| 136 GetAvatarIconIndexOfProfileAtIndex(index)); | |
| 137 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
| 138 } | |
| 139 | |
| 140 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) | |
| 141 const { | |
| 129 std::string icon_url; | 142 std::string icon_url; |
| 130 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | 143 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); |
| 131 size_t icon_index = 0; | 144 size_t icon_index = 0; |
| 132 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) { | 145 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) |
| 133 int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index); | 146 return icon_index; |
| 134 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
| 135 } | |
| 136 | 147 |
| 137 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 148 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
| 138 return ResourceBundle::GetSharedInstance().GetImageNamed( | 149 return GetDefaultAvatarIconResourceIDAtIndex(0); |
| 139 GetDefaultAvatarIconResourceIDAtIndex(0)); | |
| 140 } | 150 } |
| 141 | 151 |
| 142 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 152 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
| 143 const string16& name) { | 153 const string16& name) { |
| 144 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 154 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 145 info->SetString(kNameKey, name); | 155 info->SetString(kNameKey, name); |
| 146 // This takes ownership of |info|. | 156 // This takes ownership of |info|. |
| 147 SetInfoForProfileAtIndex(index, info.release()); | 157 SetInfoForProfileAtIndex(index, info.release()); |
| 148 } | 158 } |
| 149 | 159 |
| 150 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 160 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
| 151 size_t icon_index) { | 161 size_t icon_index) { |
| 152 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 162 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 153 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 163 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 154 // This takes ownership of |info|. | 164 // This takes ownership of |info|. |
| 155 SetInfoForProfileAtIndex(index, info.release()); | 165 SetInfoForProfileAtIndex(index, info.release()); |
| 156 } | 166 } |
| 157 | 167 |
| 158 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | 168 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { |
| 159 return kDefaultAvatarIconsCount; | 169 return kDefaultAvatarIconsCount; |
| 160 } | 170 } |
| 161 | 171 |
| 162 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | 172 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
| 163 DCHECK_LT(index, GetDefaultAvatarIconCount()); | 173 DCHECK_LT(index, GetDefaultAvatarIconCount()); |
| 164 return kDefaultAvatarIconResources[index]; | 174 return kDefaultAvatarIconResources[index]; |
| 165 } | 175 } |
| 166 | 176 |
| 177 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) { | |
| 178 int icon_index = index; | |
|
James Hawkins
2011/06/30 23:02:58
Why are you using a temp int?
sail
2011/07/01 01:16:18
Removed.
| |
| 179 DCHECK_LT(icon_index, kDefaultAvatarIconsCount); | |
| 180 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); | |
| 181 } | |
| 182 | |
| 167 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 183 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
| 168 size_t index) const { | 184 size_t index) const { |
| 169 DCHECK_LT(index, GetNumberOfProfiles()); | 185 DCHECK_LT(index, GetNumberOfProfiles()); |
| 170 const DictionaryValue* cache = | 186 const DictionaryValue* cache = |
| 171 prefs_->GetDictionary(prefs::kProfileInfoCache); | 187 prefs_->GetDictionary(prefs::kProfileInfoCache); |
| 172 DictionaryValue* info = NULL; | 188 DictionaryValue* info = NULL; |
| 173 cache->GetDictionary(sorted_keys_[index], &info); | 189 cache->GetDictionary(sorted_keys_[index], &info); |
| 174 return info; | 190 return info; |
| 175 } | 191 } |
| 176 | 192 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 200 if (key_compare < 0) | 216 if (key_compare < 0) |
| 201 return sorted_keys_.begin() + i; | 217 return sorted_keys_.begin() + i; |
| 202 } | 218 } |
| 203 } | 219 } |
| 204 return sorted_keys_.end(); | 220 return sorted_keys_.end(); |
| 205 } | 221 } |
| 206 | 222 |
| 207 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 223 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
| 208 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 224 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 209 } | 225 } |
| OLD | NEW |