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

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

Issue 8227025: Multi-Profiles: Fix choosing random profile avatar (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 2 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
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/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/rand_util.h" 10 #include "base/rand_util.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 IDR_PROFILE_AVATAR_20, 53 IDR_PROFILE_AVATAR_20,
54 IDR_PROFILE_AVATAR_21, 54 IDR_PROFILE_AVATAR_21,
55 IDR_PROFILE_AVATAR_22, 55 IDR_PROFILE_AVATAR_22,
56 IDR_PROFILE_AVATAR_23, 56 IDR_PROFILE_AVATAR_23,
57 IDR_PROFILE_AVATAR_24, 57 IDR_PROFILE_AVATAR_24,
58 IDR_PROFILE_AVATAR_25, 58 IDR_PROFILE_AVATAR_25,
59 }; 59 };
60 60
61 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); 61 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
62 62
63 // Returns true if the resource ID belongs to a generic avatar icon. 63 // The first 8 icons are gendar netural.
Miranda Callahan 2011/10/11 19:17:51 I would actually stick with calling these "generic
sail 2011/10/11 21:44:54 Done.
64 bool IsAvatarIconGeneric(int icon_id) { 64 const size_t kGenderNeutralCount = 8;
Miranda Callahan 2011/10/11 19:17:51 same: kGenericIconCount
sail 2011/10/11 21:44:54 Done.
65 return icon_id == IDR_PROFILE_AVATAR_0 ||
66 icon_id == IDR_PROFILE_AVATAR_1 ||
67 icon_id == IDR_PROFILE_AVATAR_2 ||
68 icon_id == IDR_PROFILE_AVATAR_3 ||
69 icon_id == IDR_PROFILE_AVATAR_4 ||
70 icon_id == IDR_PROFILE_AVATAR_5 ||
71 icon_id == IDR_PROFILE_AVATAR_6 ||
72 icon_id == IDR_PROFILE_AVATAR_7;
73 }
74 65
75 } // namespace 66 } // namespace
76 67
77 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, 68 ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
78 const FilePath& user_data_dir) 69 const FilePath& user_data_dir)
79 : prefs_(prefs), 70 : prefs_(prefs),
80 user_data_dir_(user_data_dir) { 71 user_data_dir_(user_data_dir) {
81 // Populate the cache 72 // Populate the cache
82 const DictionaryValue* cache = 73 const DictionaryValue* cache =
83 prefs_->GetDictionary(prefs::kProfileInfoCache); 74 prefs_->GetDictionary(prefs::kProfileInfoCache);
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 if (GetNameOfProfileAtIndex(i) == name) { 210 if (GetNameOfProfileAtIndex(i) == name) {
220 name_found = true; 211 name_found = true;
221 break; 212 break;
222 } 213 }
223 } 214 }
224 if (!name_found) 215 if (!name_found)
225 return name; 216 return name;
226 } 217 }
227 } 218 }
228 219
229 int ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() { 220 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const {
230 // Start with a random icon to introduce variety. 221 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
231 size_t rand_start_index = base::RandInt(0, GetDefaultAvatarIconCount() - 1); 222 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index)
232 for (size_t icon_index = 0; icon_index < GetDefaultAvatarIconCount(); 223 return false;
233 ++icon_index) { 224 }
234 size_t rand_icon_index = 225 return true;
235 (icon_index + rand_start_index) % GetDefaultAvatarIconCount(); 226 }
236 if (IsAvatarIconGeneric(GetDefaultAvatarIconResourceIDAtIndex(
237 rand_icon_index)))
238 continue;
239 227
240 bool icon_found = false; 228 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile(
241 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { 229 bool allow_gender_neutral,
242 if (GetAvatarIconIndexOfProfileAtIndex(i) == rand_icon_index) { 230 bool must_be_unique,
243 icon_found = true; 231 size_t* out_icon_index) const {
244 break; 232 size_t start = allow_gender_neutral ? 0 : kGenderNeutralCount;
245 } 233 size_t end = GetDefaultAvatarIconCount();
234 size_t count = end - start;
235
236 int rand = base::RandInt(0, count);
237 for (size_t i = 0; i < count; ++i) {
238 size_t icon_index = start + (rand + i) % count;
239 if (!must_be_unique || IconIndexIsUnique(icon_index)) {
240 *out_icon_index = icon_index;
241 return true;
246 } 242 }
247 if (!icon_found)
248 return rand_icon_index;
249 } 243 }
250 244
251 // If there's no unique icon then just use the random one. 245 return false;
252 return rand_start_index; 246 }
247
248 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const {
249 size_t icon_index = 0;
250 // Try to find a unique, non-generic icon.
251 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index))
252 return icon_index;
253 // Try to find any unique icon.
254 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index))
255 return icon_index;
256 // Settle for any random icon, even if it's not unique.
257 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index))
258 return icon_index;
259
260 DCHECK(false);
Miranda Callahan 2011/10/11 19:17:51 Are we doing DCHECK(false) instead of NOTREACHED()
sail 2011/10/11 21:44:54 Done.
261 return 0;
253 } 262 }
254 263
255 const FilePath& ProfileInfoCache::GetUserDataDir() const { 264 const FilePath& ProfileInfoCache::GetUserDataDir() const {
256 return user_data_dir_; 265 return user_data_dir_;
257 } 266 }
258 267
259 // static 268 // static
260 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { 269 size_t ProfileInfoCache::GetDefaultAvatarIconCount() {
261 return kDefaultAvatarIconsCount; 270 return kDefaultAvatarIconsCount;
262 } 271 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 if (key_compare < 0) 344 if (key_compare < 0)
336 return sorted_keys_.begin() + i; 345 return sorted_keys_.begin() + i;
337 } 346 }
338 } 347 }
339 return sorted_keys_.end(); 348 return sorted_keys_.end();
340 } 349 }
341 350
342 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { 351 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) {
343 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); 352 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache);
344 } 353 }
OLDNEW
« chrome/browser/profiles/profile_info_cache.h ('K') | « chrome/browser/profiles/profile_info_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698