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