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/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 IDR_PROFILE_AVATAR_4, | 36 IDR_PROFILE_AVATAR_4, |
37 IDR_PROFILE_AVATAR_5, | 37 IDR_PROFILE_AVATAR_5, |
38 IDR_PROFILE_AVATAR_6, | 38 IDR_PROFILE_AVATAR_6, |
39 IDR_PROFILE_AVATAR_7, | 39 IDR_PROFILE_AVATAR_7, |
40 IDR_PROFILE_AVATAR_8, | 40 IDR_PROFILE_AVATAR_8, |
41 IDR_PROFILE_AVATAR_9, | 41 IDR_PROFILE_AVATAR_9, |
42 }; | 42 }; |
43 | 43 |
44 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); | 44 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); |
45 | 45 |
46 // Checks if the given URL points to one of the default avatar icons. if it is, | |
47 // returns true and its index through |icon_index|. If not, returns false. | |
48 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { | |
49 DCHECK(icon_index); | |
50 if (url.find(kDefaultUrlPrefix) != 0) | |
51 return false; | |
52 | |
53 int int_value = -1; | |
54 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), | |
55 url.end(), | |
56 &int_value)) { | |
57 if (int_value < 0 || | |
58 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) | |
59 return false; | |
60 *icon_index = int_value; | |
61 return true; | |
62 } | |
63 | |
64 return false; | |
65 } | |
66 | |
67 } // namespace | 46 } // namespace |
68 | 47 |
69 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 48 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
70 const FilePath& user_data_dir) | 49 const FilePath& user_data_dir) |
71 : prefs_(prefs), | 50 : prefs_(prefs), |
72 user_data_dir_(user_data_dir) { | 51 user_data_dir_(user_data_dir) { |
73 // Populate the cache | 52 // Populate the cache |
74 const DictionaryValue* cache = | 53 const DictionaryValue* cache = |
75 prefs_->GetDictionary(prefs::kProfileInfoCache); | 54 prefs_->GetDictionary(prefs::kProfileInfoCache); |
76 for (DictionaryValue::key_iterator it = cache->begin_keys(); | 55 for (DictionaryValue::key_iterator it = cache->begin_keys(); |
(...skipping 25 matching lines...) Expand all Loading... |
102 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 81 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
103 } | 82 } |
104 | 83 |
105 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 84 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
106 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 85 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
107 DictionaryValue* cache = update.Get(); | 86 DictionaryValue* cache = update.Get(); |
108 | 87 |
109 std::string key = CacheKeyFromProfilePath(profile_path); | 88 std::string key = CacheKeyFromProfilePath(profile_path); |
110 cache->Remove(key, NULL); | 89 cache->Remove(key, NULL); |
111 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 90 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
| 91 |
| 92 NotificationService::current()->Notify( |
| 93 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 94 NotificationService::AllSources(), |
| 95 NotificationService::NoDetails()); |
112 } | 96 } |
113 | 97 |
114 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 98 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
115 return sorted_keys_.size(); | 99 return sorted_keys_.size(); |
116 } | 100 } |
117 | 101 |
118 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | 102 size_t ProfileInfoCache::GetIndexOfProfileWithPath( |
119 const FilePath& profile_path) const { | 103 const FilePath& profile_path) const { |
120 if (profile_path.DirName() != user_data_dir_) | 104 if (profile_path.DirName() != user_data_dir_) |
121 return std::string::npos; | 105 return std::string::npos; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 197 } |
214 | 198 |
215 // If there's no unique icon then just use the random one. | 199 // If there's no unique icon then just use the random one. |
216 return rand_start_index; | 200 return rand_start_index; |
217 } | 201 } |
218 | 202 |
219 const FilePath& ProfileInfoCache::GetUserDataDir() const { | 203 const FilePath& ProfileInfoCache::GetUserDataDir() const { |
220 return user_data_dir_; | 204 return user_data_dir_; |
221 } | 205 } |
222 | 206 |
| 207 // static |
223 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | 208 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { |
224 return kDefaultAvatarIconsCount; | 209 return kDefaultAvatarIconsCount; |
225 } | 210 } |
226 | 211 |
| 212 // static |
227 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | 213 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
228 DCHECK_LT(index, GetDefaultAvatarIconCount()); | 214 DCHECK_LT(index, GetDefaultAvatarIconCount()); |
229 return kDefaultAvatarIconResources[index]; | 215 return kDefaultAvatarIconResources[index]; |
230 } | 216 } |
231 | 217 |
| 218 // static |
232 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) { | 219 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) { |
233 DCHECK_LT(index, kDefaultAvatarIconsCount); | 220 DCHECK_LT(index, kDefaultAvatarIconsCount); |
234 return StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); | 221 return StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); |
235 } | 222 } |
236 | 223 |
| 224 // static |
| 225 bool ProfileInfoCache::IsDefaultAvatarIconUrl(const std::string& url, |
| 226 size_t* icon_index) { |
| 227 DCHECK(icon_index); |
| 228 if (url.find(kDefaultUrlPrefix) != 0) |
| 229 return false; |
| 230 |
| 231 int int_value = -1; |
| 232 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), |
| 233 url.end(), |
| 234 &int_value)) { |
| 235 if (int_value < 0 || |
| 236 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
| 237 return false; |
| 238 *icon_index = int_value; |
| 239 return true; |
| 240 } |
| 241 |
| 242 return false; |
| 243 } |
| 244 |
237 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 245 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
238 size_t index) const { | 246 size_t index) const { |
239 DCHECK_LT(index, GetNumberOfProfiles()); | 247 DCHECK_LT(index, GetNumberOfProfiles()); |
240 const DictionaryValue* cache = | 248 const DictionaryValue* cache = |
241 prefs_->GetDictionary(prefs::kProfileInfoCache); | 249 prefs_->GetDictionary(prefs::kProfileInfoCache); |
242 DictionaryValue* info = NULL; | 250 DictionaryValue* info = NULL; |
243 cache->GetDictionary(sorted_keys_[index], &info); | 251 cache->GetDictionary(sorted_keys_[index], &info); |
244 return info; | 252 return info; |
245 } | 253 } |
246 | 254 |
(...skipping 28 matching lines...) Expand all Loading... |
275 if (key_compare < 0) | 283 if (key_compare < 0) |
276 return sorted_keys_.begin() + i; | 284 return sorted_keys_.begin() + i; |
277 } | 285 } |
278 } | 286 } |
279 return sorted_keys_.end(); | 287 return sorted_keys_.end(); |
280 } | 288 } |
281 | 289 |
282 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 290 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
283 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 291 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
284 } | 292 } |
OLD | NEW |