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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 &int_value)) { | 44 &int_value)) { |
45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) | 45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount) |
46 return false; | 46 return false; |
47 *icon_index = int_value; | 47 *icon_index = int_value; |
48 return true; | 48 return true; |
49 } | 49 } |
50 | 50 |
51 return false; | 51 return false; |
52 } | 52 } |
53 | 53 |
| 54 // Returns a URL for the default avatar icon with specified index. |
| 55 std::string GetDefaultAvatarIconUrl(int icon_index) { |
| 56 DCHECK_LT(icon_index, kDefaultAvatarIconsCount); |
| 57 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); |
| 58 } |
| 59 |
54 } // namespace | 60 } // namespace |
55 | 61 |
56 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 62 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
57 const FilePath& user_data_dir) | 63 const FilePath& user_data_dir) |
58 : prefs_(prefs), | 64 : prefs_(prefs), |
59 user_data_dir_(user_data_dir) { | 65 user_data_dir_(user_data_dir) { |
60 // Populate the cache | 66 // Populate the cache |
61 const DictionaryValue* cache = | 67 const DictionaryValue* cache = |
62 prefs_->GetDictionary(prefs::kProfileInfoCache); | 68 prefs_->GetDictionary(prefs::kProfileInfoCache); |
63 for (DictionaryValue::key_iterator it = cache->begin_keys(); | 69 for (DictionaryValue::key_iterator it = cache->begin_keys(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 101 |
96 std::string key = CacheKeyFromProfilePath(profile_path); | 102 std::string key = CacheKeyFromProfilePath(profile_path); |
97 cache->Remove(key, NULL); | 103 cache->Remove(key, NULL); |
98 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 104 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
99 } | 105 } |
100 | 106 |
101 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 107 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
102 return sorted_keys_.size(); | 108 return sorted_keys_.size(); |
103 } | 109 } |
104 | 110 |
105 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | |
106 const FilePath& profile_path) const { | |
107 if (profile_path.DirName() != user_data_dir_) | |
108 return std::string::npos; | |
109 std::string search_key = profile_path.BaseName().MaybeAsASCII(); | |
110 for (size_t i = 0; i < sorted_keys_.size(); ++i) { | |
111 if (sorted_keys_[i] == search_key) | |
112 return i; | |
113 } | |
114 return std::string::npos; | |
115 } | |
116 | |
117 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { | 111 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { |
118 string16 name; | 112 string16 name; |
119 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); | 113 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
120 return name; | 114 return name; |
121 } | 115 } |
122 | 116 |
123 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { | 117 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const { |
124 FilePath::StringType base_name; | 118 FilePath::StringType base_name; |
125 #if defined(OS_POSIX) | 119 #if defined(OS_POSIX) |
126 base_name = sorted_keys_[index]; | 120 base_name = sorted_keys_[index]; |
127 #elif defined(OS_WIN) | 121 #elif defined(OS_WIN) |
128 base_name = ASCIIToWide(sorted_keys_[index]); | 122 base_name = ASCIIToWide(sorted_keys_[index]); |
129 #endif | 123 #endif |
130 return user_data_dir_.Append(base_name); | 124 return user_data_dir_.Append(base_name); |
131 } | 125 } |
132 | 126 |
133 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 127 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
134 size_t index) const { | 128 size_t index) const { |
135 int resource_id = GetDefaultAvatarIconResourceIDAtIndex( | |
136 GetAvatarIconIndexOfProfileAtIndex(index)); | |
137 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | |
138 } | |
139 | |
140 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) | |
141 const { | |
142 std::string icon_url; | 129 std::string icon_url; |
143 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | 130 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); |
144 size_t icon_index = 0; | 131 size_t icon_index = 0; |
145 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) | 132 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) { |
146 return icon_index; | 133 int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index); |
| 134 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); |
| 135 } |
147 | 136 |
148 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 137 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
149 return GetDefaultAvatarIconResourceIDAtIndex(0); | 138 return ResourceBundle::GetSharedInstance().GetImageNamed( |
| 139 GetDefaultAvatarIconResourceIDAtIndex(0)); |
150 } | 140 } |
151 | 141 |
152 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 142 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
153 const string16& name) { | 143 const string16& name) { |
154 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 144 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
155 info->SetString(kNameKey, name); | 145 info->SetString(kNameKey, name); |
156 // This takes ownership of |info|. | 146 // This takes ownership of |info|. |
157 SetInfoForProfileAtIndex(index, info.release()); | 147 SetInfoForProfileAtIndex(index, info.release()); |
158 } | 148 } |
159 | 149 |
160 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 150 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
161 size_t icon_index) { | 151 size_t icon_index) { |
162 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 152 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
163 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 153 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
164 // This takes ownership of |info|. | 154 // This takes ownership of |info|. |
165 SetInfoForProfileAtIndex(index, info.release()); | 155 SetInfoForProfileAtIndex(index, info.release()); |
166 } | 156 } |
167 | 157 |
168 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { | 158 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { |
169 return kDefaultAvatarIconsCount; | 159 return kDefaultAvatarIconsCount; |
170 } | 160 } |
171 | 161 |
172 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { | 162 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
173 DCHECK_LT(index, GetDefaultAvatarIconCount()); | 163 DCHECK_LT(index, GetDefaultAvatarIconCount()); |
174 return kDefaultAvatarIconResources[index]; | 164 return kDefaultAvatarIconResources[index]; |
175 } | 165 } |
176 | 166 |
177 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) { | |
178 int icon_index = index; | |
179 DCHECK_LT(icon_index, kDefaultAvatarIconsCount); | |
180 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index); | |
181 } | |
182 | |
183 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 167 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
184 size_t index) const { | 168 size_t index) const { |
185 DCHECK_LT(index, GetNumberOfProfiles()); | 169 DCHECK_LT(index, GetNumberOfProfiles()); |
186 const DictionaryValue* cache = | 170 const DictionaryValue* cache = |
187 prefs_->GetDictionary(prefs::kProfileInfoCache); | 171 prefs_->GetDictionary(prefs::kProfileInfoCache); |
188 DictionaryValue* info = NULL; | 172 DictionaryValue* info = NULL; |
189 cache->GetDictionary(sorted_keys_[index], &info); | 173 cache->GetDictionary(sorted_keys_[index], &info); |
190 return info; | 174 return info; |
191 } | 175 } |
192 | 176 |
(...skipping 23 matching lines...) Expand all Loading... |
216 if (key_compare < 0) | 200 if (key_compare < 0) |
217 return sorted_keys_.begin() + i; | 201 return sorted_keys_.begin() + i; |
218 } | 202 } |
219 } | 203 } |
220 return sorted_keys_.end(); | 204 return sorted_keys_.end(); |
221 } | 205 } |
222 | 206 |
223 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 207 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
224 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 208 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
225 } | 209 } |
OLD | NEW |