Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: chrome/browser/profiles/profile_info_cache.cc

Issue 7256002: Multi-Profiles: New Profile Setup UI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: show product logo at bottom Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/prefs/pref_service.h"
14 #include "chrome/browser/prefs/scoped_user_pref_update.h"
15 #include "chrome/common/pref_names.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/resource/resource_bundle.h"
18
19 namespace {
20
21 const char kNameKey[] = "name";
22 const char kAvatarIconKey[] = "avatar_icon";
23 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_";
24
25 const int kDefaultAvatarIconResources[] = {
26 IDR_PROFILE_AVATAR_0,
27 IDR_PROFILE_AVATAR_1,
28 IDR_PROFILE_AVATAR_2,
29 IDR_PROFILE_AVATAR_3,
30 };
31
32 const int kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
33
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.
36 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) {
37 DCHECK(icon_index);
38 if (url.find(kDefaultUrlPrefix) != 0)
39 return false;
40
41 int int_value = -1;
42 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix),
43 url.end(),
44 &int_value)) {
45 if (int_value < 0 || int_value >= kDefaultAvatarIconsCount)
46 return false;
47 *icon_index = int_value;
48 return true;
49 }
50
51 return false;
52 }
53
54 } // namespace
55
56 ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
57 const FilePath& user_data_dir)
58 : prefs_(prefs),
59 user_data_dir_(user_data_dir) {
60 // Populate the cache
61 const DictionaryValue* cache =
62 prefs_->GetDictionary(prefs::kProfileInfoCache);
63 for (DictionaryValue::key_iterator it = cache->begin_keys();
64 it != cache->end_keys(); ++it) {
65 std::string key = *it;
66 DictionaryValue* info = NULL;
67 cache->GetDictionary(key, &info);
68 string16 name;
69 info->GetString(kNameKey, &name);
70 sorted_keys_.insert(FindPositionForProfile(key, name), key);
71 }
72 }
73
74 void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path,
75 const string16& name,
76 size_t icon_index) {
77 std::string key = CacheKeyFromProfilePath(profile_path);
78 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
79 DictionaryValue* cache = update.Get();
80
81 scoped_ptr<DictionaryValue> info(new DictionaryValue);
82 info->SetString(kNameKey, name);
83 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index));
84 cache->Set(key, info.release());
85
86 sorted_keys_.insert(FindPositionForProfile(key, name), key);
87 }
88
89 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) {
90 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
91 DictionaryValue* cache = update.Get();
92
93 std::string key = CacheKeyFromProfilePath(profile_path);
94 cache->Remove(key, NULL);
95 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key));
96 }
97
98 size_t ProfileInfoCache::GetNumberOfProfiles() const {
99 return sorted_keys_.size();
100 }
101
102 size_t ProfileInfoCache::GetIndexOfProfileWithPath(
103 const FilePath& profile_path) const {
104 if (profile_path.DirName() != user_data_dir_)
105 return std::string::npos;
106 std::string search_key = profile_path.BaseName().MaybeAsASCII();
107 for (size_t i = 0; i < sorted_keys_.size(); ++i) {
108 if (sorted_keys_[i] == search_key)
109 return i;
110 }
111 return std::string::npos;
112 }
113
114 string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const {
115 string16 name;
116 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name);
117 return name;
118 }
119
120 FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const {
121 FilePath::StringType base_name;
122 #if defined(OS_POSIX)
123 base_name = sorted_keys_[index];
124 #elif defined(OS_WIN)
125 base_name = ASCIIToWide(sorted_keys_[index]);
126 #endif
127 return user_data_dir_.Append(base_name);
128 }
129
130 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex(
131 size_t index) const {
132 int resource_id = GetDefaultAvatarIconResourceIDAtIndex(
133 GetAvatarIconIndexOfProfileAtIndex(index));
134 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
135 }
136
137 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index)
138 const {
139 std::string icon_url;
140 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url);
141 size_t icon_index = 0;
142 if (IsDefaultAvatarIconUrl(icon_url, &icon_index))
143 return icon_index;
144
145 DLOG(WARNING) << "Unknown avatar icon: " << icon_url;
146 return GetDefaultAvatarIconResourceIDAtIndex(0);
147 }
148
149 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index,
150 const string16& name) {
151 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
152 info->SetString(kNameKey, name);
153 // This takes ownership of |info|.
154 SetInfoForProfileAtIndex(index, info.release());
155 }
156
157 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
158 size_t icon_index) {
159 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
160 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index));
161 // This takes ownership of |info|.
162 SetInfoForProfileAtIndex(index, info.release());
163 }
164
165 size_t ProfileInfoCache::GetDefaultAvatarIconCount() {
166 return kDefaultAvatarIconsCount;
167 }
168
169 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
170 DCHECK_LT(index, GetDefaultAvatarIconCount());
171 return kDefaultAvatarIconResources[index];
172 }
173
174 std::string ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index) {
175 int icon_index = index;
176 DCHECK_LT(icon_index, kDefaultAvatarIconsCount);
177 return StringPrintf("%s%d", kDefaultUrlPrefix, icon_index);
178 }
179
180 const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex(
181 size_t index) const {
182 DCHECK_LT(index, GetNumberOfProfiles());
183 const DictionaryValue* cache =
184 prefs_->GetDictionary(prefs::kProfileInfoCache);
185 DictionaryValue* info = NULL;
186 cache->GetDictionary(sorted_keys_[index], &info);
187 return info;
188 }
189
190 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index,
191 DictionaryValue* info) {
192 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
193 DictionaryValue* cache = update.Get();
194 cache->Set(sorted_keys_[index], info);
195 }
196
197 std::string ProfileInfoCache::CacheKeyFromProfilePath(
198 const FilePath& profile_path) const {
199 DCHECK(user_data_dir_ == profile_path.DirName());
200 FilePath base_name = profile_path.BaseName();
201 return base_name.MaybeAsASCII();
202 }
203
204 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile(
205 std::string search_key,
206 const string16& search_name) {
207 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
208 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i));
209 if (name_compare < 0)
210 return sorted_keys_.begin() + i;
211 if (name_compare == 0) {
212 int key_compare = search_key.compare(sorted_keys_[i]);
213 if (key_compare < 0)
214 return sorted_keys_.begin() + i;
215 }
216 }
217 return sorted_keys_.end();
218 }
219
220 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) {
221 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache);
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698