Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/stringprintf.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "grit/theme_resources.h" | |
| 18 #include "ui/base/resource/resource_bundle.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 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.
| |
| 23 const char* kAvatarIconKey = "avatar_icon"; | |
| 24 const char* kDefaultUrlPrefix = "chrome://theme/IDR_PROFILE_AVATAR_"; | |
| 25 | |
| 26 const int kDefaultAvatarIconResources[] = { | |
| 27 IDR_PROFILE_AVATAR_0, | |
| 28 IDR_PROFILE_AVATAR_1, | |
| 29 IDR_PROFILE_AVATAR_2, | |
| 30 IDR_PROFILE_AVATAR_3, | |
| 31 }; | |
| 32 | |
| 33 const int kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); | |
| 34 | |
| 35 // Checks if the given URL points to one of the default avatar icons. if it is, | |
| 36 // returns true and its index through |icon_index|. If not, returns false. | |
| 37 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { | |
| 38 DCHECK(icon_index); | |
| 39 if (url.find(kDefaultUrlPrefix) != 0) | |
| 40 return false; | |
| 41 | |
| 42 int int_value = -1; | |
| 43 if (base::StringToInt(url.begin() + url.size(), | |
| 44 url.end(), | |
| 45 &int_value)) { | |
| 46 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) | |
| 47 return false; | |
| 48 *icon_index = int_value; | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 // Returns a URL for the default avatar icon with specified index. | |
| 56 std::string GetDefaultAvatarIconUrl(int icon_index) { | |
| 57 DCHECK_LT(icon_index, kDefaultAvatarIconsCount); | |
| 58 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 ProfileInfoCache::ProfileInfoCache(const FilePath& user_data_dir) | |
| 64 : user_data_dir_(user_data_dir) { | |
| 65 // Populate the cache | |
| 66 const DictionaryValue* cache = | |
| 67 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!
| |
| 68 for (DictionaryValue::key_iterator it = cache->begin_keys(); | |
| 69 it != cache->end_keys(); ++it) { | |
| 70 std::string key = *it; | |
| 71 DictionaryValue* info = NULL; | |
| 72 cache->GetDictionary(key, &info); | |
| 73 string16 name; | |
| 74 info->GetString(kNameKey, &name); | |
| 75 sorted_keys_.insert(FindPositionForProfile(key, name), key); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path, | |
| 80 const string16& name, | |
| 81 size_t icon_index) { | |
| 82 std::string key = CacheKeyFromProfilePath(profile_path); | |
| 83 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 84 prefs::kProfileInfoCache); | |
| 85 DictionaryValue* cache = update.Get(); | |
| 86 | |
| 87 scoped_ptr<DictionaryValue> info(new DictionaryValue); | |
| 88 info->SetString(kNameKey, name); | |
| 89 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | |
| 90 cache->Set(key, info.release()); | |
| 91 | |
| 92 sorted_keys_.insert(FindPositionForProfile(key, name), key); | |
| 93 } | |
| 94 | |
| 95 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | |
| 96 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 97 prefs::kProfileInfoCache); | |
| 98 DictionaryValue* cache = update.Get(); | |
| 99 | |
| 100 std::string key = CacheKeyFromProfilePath(profile_path); | |
| 101 cache->Remove(key, NULL); | |
| 102 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | |
| 103 } | |
| 104 | |
| 105 size_t ProfileInfoCache::GetNumberOfProfiles() const { | |
| 106 return sorted_keys_.size(); | |
| 107 } | |
| 108 | |
| 109 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { | |
| 110 string16 name; | |
| 111 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); | |
| 112 return name; | |
| 113 } | |
| 114 | |
| 115 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { | |
| 116 FilePath::StringType base_name; | |
| 117 #if defined(OS_POSIX) | |
| 118 base_name = sorted_keys_[index]; | |
| 119 #elif defined(OS_WIN) | |
| 120 base_name = ASCIIToWide(sorted_keys_[index]); | |
| 121 #endif | |
| 122 return user_data_dir_.Append(base_name); | |
| 123 } | |
| 124 | |
| 125 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | |
| 126 size_t index) const { | |
| 127 std::string icon_url; | |
| 128 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | |
| 129 size_t icon_index = 0; | |
| 130 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) { | |
| 131 int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index); | |
| 132 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
| 133 } | |
| 134 | |
| 135 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | |
| 136 return ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 137 GetDefaultAvatarIconResourceIDAtIndex(0)); | |
| 138 } | |
| 139 | |
| 140 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | |
| 141 const string16& name) { | |
| 142 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 143 info->SetString(kNameKey, name); | |
| 144 // This takes ownership of |info|. | |
| 145 SetInfoForProfileAtIndex(index, info.release()); | |
| 146 } | |
| 147 | |
| 148 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | |
| 149 size_t icon_index) { | |
| 150 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 151 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | |
| 152 // This takes ownership of |info|. | |
| 153 SetInfoForProfileAtIndex(index, info.release()); | |
| 154 } | |
| 155 | |
| 156 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | |
| 157 return kDefaultAvatarIconsCount; | |
| 158 } | |
| 159 | |
| 160 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | |
| 161 DCHECK_LT(index, GetDefaultAvatarIconCount()); | |
| 162 return kDefaultAvatarIconResources[index]; | |
| 163 } | |
| 164 | |
| 165 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | |
| 166 size_t index) const { | |
| 167 DCHECK_LT(index, GetNumberOfProfiles()); | |
| 168 const DictionaryValue* cache = | |
| 169 g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache); | |
| 170 DictionaryValue* info = NULL; | |
| 171 cache->GetDictionary(sorted_keys_[index], &info); | |
| 172 return info; | |
| 173 } | |
| 174 | |
| 175 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | |
| 176 DictionaryValue* info) { | |
| 177 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 178 prefs::kProfileInfoCache); | |
| 179 DictionaryValue* cache = update.Get(); | |
| 180 cache->Set(sorted_keys_[index], info); | |
| 181 } | |
| 182 | |
| 183 std::string ProfileInfoCache::CacheKeyFromProfilePath( | |
| 184 const FilePath& profile_path) const { | |
| 185 DCHECK(user_data_dir_ == profile_path.DirName()); | |
| 186 FilePath base_name = profile_path.BaseName(); | |
| 187 return base_name.MaybeAsASCII(); | |
| 188 } | |
| 189 | |
| 190 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | |
| 191 std::string search_key, | |
| 192 const string16& search_name) { | |
| 193 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | |
| 194 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); | |
| 195 if (name_compare < 0) | |
| 196 return sorted_keys_.begin() + i; | |
| 197 if (name_compare == 0) { | |
| 198 int key_compare = search_key.compare(sorted_keys_[i]); | |
| 199 if (key_compare < 0) | |
| 200 return sorted_keys_.begin() + i; | |
| 201 } | |
| 202 } | |
| 203 return sorted_keys_.end(); | |
| 204 } | |
| 205 | |
| 206 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | |
| 207 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | |
| 208 } | |
| OLD | NEW |