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

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: address review comments 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
« no previous file with comments | « chrome/browser/profiles/profile_info_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 generic.
64 bool IsAvatarIconGeneric(int icon_id) { 64 const size_t kGenericIconCount = 8;
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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 if (GetNameOfProfileAtIndex(i) == name) { 212 if (GetNameOfProfileAtIndex(i) == name) {
222 name_found = true; 213 name_found = true;
223 break; 214 break;
224 } 215 }
225 } 216 }
226 if (!name_found) 217 if (!name_found)
227 return name; 218 return name;
228 } 219 }
229 } 220 }
230 221
231 int ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() { 222 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const {
232 // Start with a random icon to introduce variety. 223 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
233 size_t rand_start_index = base::RandInt(0, GetDefaultAvatarIconCount() - 1); 224 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index)
234 for (size_t icon_index = 0; icon_index < GetDefaultAvatarIconCount(); 225 return false;
235 ++icon_index) { 226 }
236 size_t rand_icon_index = 227 return true;
237 (icon_index + rand_start_index) % GetDefaultAvatarIconCount(); 228 }
238 if (IsAvatarIconGeneric(GetDefaultAvatarIconResourceIDAtIndex(
239 rand_icon_index)))
240 continue;
241 229
242 bool icon_found = false; 230 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile(
243 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { 231 bool allow_generic_icon,
244 if (GetAvatarIconIndexOfProfileAtIndex(i) == rand_icon_index) { 232 bool must_be_unique,
245 icon_found = true; 233 size_t* out_icon_index) const {
246 break; 234 size_t start = allow_generic_icon ? 0 : kGenericIconCount;
247 } 235 size_t end = GetDefaultAvatarIconCount();
236 size_t count = end - start;
237
238 int rand = base::RandInt(0, count);
239 for (size_t i = 0; i < count; ++i) {
240 size_t icon_index = start + (rand + i) % count;
241 if (!must_be_unique || IconIndexIsUnique(icon_index)) {
242 *out_icon_index = icon_index;
243 return true;
248 } 244 }
249 if (!icon_found)
250 return rand_icon_index;
251 } 245 }
252 246
253 // If there's no unique icon then just use the random one. 247 return false;
254 return rand_start_index; 248 }
249
250 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const {
251 size_t icon_index = 0;
252 // Try to find a unique, non-generic icon.
253 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index))
254 return icon_index;
255 // Try to find any unique icon.
256 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index))
257 return icon_index;
258 // Settle for any random icon, even if it's not unique.
259 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index))
260 return icon_index;
261
262 NOTREACHED();
263 return 0;
255 } 264 }
256 265
257 const FilePath& ProfileInfoCache::GetUserDataDir() const { 266 const FilePath& ProfileInfoCache::GetUserDataDir() const {
258 return user_data_dir_; 267 return user_data_dir_;
259 } 268 }
260 269
261 // static 270 // static
262 size_t ProfileInfoCache::GetDefaultAvatarIconCount() { 271 size_t ProfileInfoCache::GetDefaultAvatarIconCount() {
263 return kDefaultAvatarIconsCount; 272 return kDefaultAvatarIconsCount;
264 } 273 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 if (key_compare < 0) 346 if (key_compare < 0)
338 return sorted_keys_.begin() + i; 347 return sorted_keys_.begin() + i;
339 } 348 }
340 } 349 }
341 return sorted_keys_.end(); 350 return sorted_keys_.end();
342 } 351 }
343 352
344 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { 353 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) {
345 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); 354 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache);
346 } 355 }
OLDNEW
« no previous file with comments | « 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