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 std::string kNameKey = "name"; | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
No global non-PODs. Just use const char kNameKey[]
sail
2011/06/21 03:26:53
Done.
| |
| 23 std::string kAvatarIconKey = "avatar_icon"; | |
| 24 std::string 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. | |
|
Miranda Callahan
2011/06/15 15:16:08
Ah, I see, you have comments in the ".cc" file --
sail
2011/06/21 03:26:53
Done.
| |
| 37 bool IsDefaultAvatarIconUrl(const std::string url, size_t* icon_index) { | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
const std::string&
sail
2011/06/21 03:26:53
Done.
| |
| 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 URL to default avatar icon with specifided index. | |
|
Miranda Callahan
2011/06/15 15:16:08
I actually like the word "specifided," but others
sail
2011/06/21 03:26:53
Done.
| |
| 56 std::string GetDefaultAvatarIconUrl(int icon_index) { | |
| 57 DCHECK(icon_index < kDefaultAvatarIconsCount); | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
DCHECK_LT
sail
2011/06/21 03:26:53
Done.
| |
| 58 return StringPrintf("%s%d", kDefaultUrlPrefix.c_str(), icon_index); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 ProfileInfoCache::ProfileInfoCache(const FilePath& user_data_dir) | |
| 64 : user_data_dir_(user_data_dir) { | |
| 65 g_browser_process->local_state()->RegisterDictionaryPref( | |
| 66 prefs::kProfileInfoCache); | |
| 67 | |
| 68 // Popuplate the cache | |
|
Miranda Callahan
2011/06/15 15:16:08
same w/"Popuplate"
sail
2011/06/21 03:26:53
Done.
| |
| 69 const DictionaryValue* cache = | |
| 70 g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache); | |
| 71 for (DictionaryValue::key_iterator it = cache->begin_keys(); | |
| 72 it != cache->end_keys(); ++it) { | |
| 73 std::string key = *it; | |
| 74 DictionaryValue* info = NULL; | |
| 75 cache->GetDictionary(key, &info); | |
| 76 string16 name; | |
| 77 info->GetString(kNameKey, &name); | |
| 78 sorted_keys_.insert(FindPositionForProfile(key, name), key); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path, | |
| 83 string16 name, | |
| 84 size_t icon_index) { | |
| 85 std::string key = CacheKeyFromProfilePath(profile_path); | |
| 86 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 87 prefs::kProfileInfoCache); | |
| 88 DictionaryValue* cache = update.Get(); | |
| 89 | |
| 90 scoped_ptr<DictionaryValue> info(new DictionaryValue); | |
| 91 info->SetString(kNameKey, name); | |
| 92 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | |
| 93 cache->Set(key, info.release()); | |
| 94 | |
| 95 sorted_keys_.insert(FindPositionForProfile(key, name), key); | |
| 96 } | |
| 97 | |
| 98 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | |
| 99 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 100 prefs::kProfileInfoCache); | |
| 101 DictionaryValue* cache = update.Get(); | |
| 102 | |
| 103 std::string key = CacheKeyFromProfilePath(profile_path); | |
| 104 cache->Remove(key, NULL); | |
| 105 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | |
| 106 } | |
| 107 | |
| 108 size_t ProfileInfoCache::GetNumberOfProfiles() const { | |
| 109 return sorted_keys_.size(); | |
| 110 } | |
| 111 | |
| 112 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { | |
| 113 string16 name; | |
| 114 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); | |
| 115 return name; | |
| 116 } | |
| 117 | |
| 118 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { | |
| 119 FilePath::StringType base_name; | |
| 120 #if defined(OS_POSIX) | |
| 121 base_name = sorted_keys_[index]; | |
| 122 #elif defined(OS_WIN) | |
| 123 base_name = ASCIIToWide(sorted_keys_[index]); | |
| 124 #endif | |
| 125 return user_data_dir_.Append(base_name); | |
| 126 } | |
| 127 | |
| 128 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | |
| 129 size_t index) const { | |
| 130 std::string icon_url; | |
| 131 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | |
| 132 size_t icon_index = 0; | |
| 133 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) { | |
| 134 int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index); | |
| 135 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
| 136 } | |
| 137 | |
| 138 DLOG(WARNING) << "Unkown avatar icon: " << icon_url; | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
Unknown
sail
2011/06/21 03:26:53
Done.
| |
| 139 return ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 140 GetDefaultAvatarIconResourceIDAtIndex(0)); | |
| 141 } | |
| 142 | |
| 143 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, string16 name) { | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
const string16&
sail
2011/06/21 03:26:53
Done.
| |
| 144 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 145 info->SetString(kNameKey, name); | |
| 146 // This takes ownership of |info|. | |
| 147 SetInfoForProfileAtIndex(index, info.release()); | |
| 148 } | |
| 149 | |
| 150 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | |
| 151 size_t icon_index) { | |
| 152 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 153 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | |
| 154 // This takes ownership of |info|. | |
| 155 SetInfoForProfileAtIndex(index, info.release()); | |
| 156 } | |
| 157 | |
| 158 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | |
| 159 return kDefaultAvatarIconsCount; | |
| 160 } | |
| 161 | |
| 162 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | |
| 163 DCHECK(index < GetDefaultAvatarIconCount()); | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
DCHECK_LT
sail
2011/06/21 03:26:53
Done.
| |
| 164 return kDefaultAvatarIconResources[index]; | |
| 165 } | |
| 166 | |
| 167 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | |
| 168 size_t index) const { | |
| 169 DCHECK(index < GetNumberOfProfiles()); | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
DCHECK_LT
sail
2011/06/21 03:26:53
Done.
| |
| 170 const DictionaryValue* cache = | |
| 171 g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache); | |
| 172 DictionaryValue* info = NULL; | |
| 173 cache->GetDictionary(sorted_keys_[index], &info); | |
| 174 return info; | |
| 175 } | |
| 176 | |
| 177 // Saves the profile info to the cache and takes ownership of |info|. | |
| 178 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | |
| 179 DictionaryValue* info) { | |
| 180 DictionaryPrefUpdate update(g_browser_process->local_state(), | |
| 181 prefs::kProfileInfoCache); | |
| 182 DictionaryValue* cache = update.Get(); | |
| 183 cache->Set(sorted_keys_[index], info); | |
| 184 } | |
| 185 | |
| 186 std::string ProfileInfoCache::CacheKeyFromProfilePath( | |
| 187 const FilePath& profile_path) const { | |
| 188 DCHECK(user_data_dir_ == profile_path.DirName()); | |
|
willchan no longer on Chromium
2011/06/15 11:22:12
DCHECK_EQ
sail
2011/06/21 03:26:53
Using DCHECK_EQ didn't work because the macro need
| |
| 189 FilePath base_name = profile_path.BaseName(); | |
| 190 return base_name.MaybeAsASCII(); | |
| 191 } | |
| 192 | |
| 193 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | |
| 194 std::string search_key, | |
| 195 string16 search_name) { | |
| 196 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | |
| 197 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); | |
| 198 if (name_compare < 0) | |
| 199 return sorted_keys_.begin() + i; | |
| 200 if (name_compare == 0) { | |
| 201 int key_compare = search_key.compare(sorted_keys_[i]); | |
| 202 if (key_compare < 0) | |
| 203 return sorted_keys_.begin() + i; | |
| 204 } | |
| 205 } | |
| 206 return sorted_keys_.end(); | |
| 207 } | |
| OLD | NEW |