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 11 matching lines...) Expand all Loading... |
22 const char kAvatarIconKey[] = "avatar_icon"; | 22 const char kAvatarIconKey[] = "avatar_icon"; |
23 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; | 23 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; |
24 | 24 |
25 const int kDefaultAvatarIconResources[] = { | 25 const int kDefaultAvatarIconResources[] = { |
26 IDR_PROFILE_AVATAR_0, | 26 IDR_PROFILE_AVATAR_0, |
27 IDR_PROFILE_AVATAR_1, | 27 IDR_PROFILE_AVATAR_1, |
28 IDR_PROFILE_AVATAR_2, | 28 IDR_PROFILE_AVATAR_2, |
29 IDR_PROFILE_AVATAR_3, | 29 IDR_PROFILE_AVATAR_3, |
30 }; | 30 }; |
31 | 31 |
32 const int kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); | 32 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); |
33 | 33 |
34 // Checks if the given URL points to one of the default avatar icons. if it is, | 34 // Checks if the given URL points to one of the default avatar icons. if it is, |
35 // returns true and its index through |icon_index|. If not, returns false. | 35 // returns true and its index through |icon_index|. If not, returns false. |
36 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { | 36 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { |
37 DCHECK(icon_index); | 37 DCHECK(icon_index); |
38 if (url.find(kDefaultUrlPrefix) != 0) | 38 if (url.find(kDefaultUrlPrefix) != 0) |
39 return false; | 39 return false; |
40 | 40 |
41 int int_value = -1; | 41 int int_value = -1; |
42 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), | 42 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), |
43 url.end(), | 43 url.end(), |
44 &int_value)) { | 44 &int_value)) { |
45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) | 45 if (int_value < 0 || |
| 46 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
46 return false; | 47 return false; |
47 *icon_index = int_value; | 48 *icon_index = int_value; |
48 return true; | 49 return true; |
49 } | 50 } |
50 | 51 |
51 return false; | 52 return false; |
52 } | 53 } |
53 | 54 |
54 // Returns a URL for the default avatar icon with specified index. | 55 // Returns a URL for the default avatar icon with specified index. |
55 std::string GetDefaultAvatarIconUrl(int icon_index) { | 56 std::string GetDefaultAvatarIconUrl(int icon_index) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 158 |
158 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | 159 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { |
159 return kDefaultAvatarIconsCount; | 160 return kDefaultAvatarIconsCount; |
160 } | 161 } |
161 | 162 |
162 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | 163 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
163 DCHECK_LT(index, GetDefaultAvatarIconCount()); | 164 DCHECK_LT(index, GetDefaultAvatarIconCount()); |
164 return kDefaultAvatarIconResources[index]; | 165 return kDefaultAvatarIconResources[index]; |
165 } | 166 } |
166 | 167 |
| 168 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) { |
| 169 DCHECK_LT(index, kDefaultAvatarIconsCount); |
| 170 return StringPrintf("%s%zu", kDefaultUrlPrefix, index); |
| 171 } |
| 172 |
167 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 173 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
168 size_t index) const { | 174 size_t index) const { |
169 DCHECK_LT(index, GetNumberOfProfiles()); | 175 DCHECK_LT(index, GetNumberOfProfiles()); |
170 const DictionaryValue* cache = | 176 const DictionaryValue* cache = |
171 prefs_->GetDictionary(prefs::kProfileInfoCache); | 177 prefs_->GetDictionary(prefs::kProfileInfoCache); |
172 DictionaryValue* info = NULL; | 178 DictionaryValue* info = NULL; |
173 cache->GetDictionary(sorted_keys_[index], &info); | 179 cache->GetDictionary(sorted_keys_[index], &info); |
174 return info; | 180 return info; |
175 } | 181 } |
176 | 182 |
(...skipping 23 matching lines...) Expand all Loading... |
200 if (key_compare < 0) | 206 if (key_compare < 0) |
201 return sorted_keys_.begin() + i; | 207 return sorted_keys_.begin() + i; |
202 } | 208 } |
203 } | 209 } |
204 return sorted_keys_.end(); | 210 return sorted_keys_.end(); |
205 } | 211 } |
206 | 212 |
207 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 213 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
208 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 214 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
209 } | 215 } |
OLD | NEW |