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