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

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

Issue 8539043: Refactor ProfileInfoCache (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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_util.h ('k') | chrome/browser/profiles/profile_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_util.h"
6
7 #include "base/format_macros.h"
8 #include "base/rand_util.h"
9 #include "base/string_number_conversions.h"
10 #include "base/stringprintf.h"
11 #include "base/utf_string_conversions.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15
16 namespace {
17
18 const int kDefaultAvatarIconResources[] = {
19 IDR_PROFILE_AVATAR_0,
20 IDR_PROFILE_AVATAR_1,
21 IDR_PROFILE_AVATAR_2,
22 IDR_PROFILE_AVATAR_3,
23 IDR_PROFILE_AVATAR_4,
24 IDR_PROFILE_AVATAR_5,
25 IDR_PROFILE_AVATAR_6,
26 IDR_PROFILE_AVATAR_7,
27 IDR_PROFILE_AVATAR_8,
28 IDR_PROFILE_AVATAR_9,
29 IDR_PROFILE_AVATAR_10,
30 IDR_PROFILE_AVATAR_11,
31 IDR_PROFILE_AVATAR_12,
32 IDR_PROFILE_AVATAR_13,
33 IDR_PROFILE_AVATAR_14,
34 IDR_PROFILE_AVATAR_15,
35 IDR_PROFILE_AVATAR_16,
36 IDR_PROFILE_AVATAR_17,
37 IDR_PROFILE_AVATAR_18,
38 IDR_PROFILE_AVATAR_19,
39 IDR_PROFILE_AVATAR_20,
40 IDR_PROFILE_AVATAR_21,
41 IDR_PROFILE_AVATAR_22,
42 IDR_PROFILE_AVATAR_23,
43 IDR_PROFILE_AVATAR_24,
44 IDR_PROFILE_AVATAR_25,
45 };
46
47 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
48
49 // The first 8 icons are generic.
50 const size_t kGenericIconCount = 8;
51
52 // First eight are generic icons, which use IDS_NUMBERED_PROFILE_NAME.
53 const int kDefaultNames[] = {
54 IDS_DEFAULT_AVATAR_NAME_8,
55 IDS_DEFAULT_AVATAR_NAME_9,
56 IDS_DEFAULT_AVATAR_NAME_10,
57 IDS_DEFAULT_AVATAR_NAME_11,
58 IDS_DEFAULT_AVATAR_NAME_12,
59 IDS_DEFAULT_AVATAR_NAME_13,
60 IDS_DEFAULT_AVATAR_NAME_14,
61 IDS_DEFAULT_AVATAR_NAME_15,
62 IDS_DEFAULT_AVATAR_NAME_16,
63 IDS_DEFAULT_AVATAR_NAME_17,
64 IDS_DEFAULT_AVATAR_NAME_18,
65 IDS_DEFAULT_AVATAR_NAME_19,
66 IDS_DEFAULT_AVATAR_NAME_20,
67 IDS_DEFAULT_AVATAR_NAME_21,
68 IDS_DEFAULT_AVATAR_NAME_22,
69 IDS_DEFAULT_AVATAR_NAME_23,
70 IDS_DEFAULT_AVATAR_NAME_24,
71 IDS_DEFAULT_AVATAR_NAME_25
72 };
73
74 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_";
75
76
77 bool IconIndexIsUnique(
78 const std::vector<ProfileInfoEntry>& entries,
79 size_t icon_index) {
80 for (std::vector<ProfileInfoEntry>::const_iterator it = entries.begin();
81 it != entries.end(); ++it) {
82 if (it->icon_index() == icon_index)
83 return false;
84 }
85 return true;
86 }
87
88 // Tries to find an icon index that satisfies all the given conditions.
89 // Returns true if an icon was found, false otherwise.
90 bool ChooseAvatarIconIndexForNewProfile(
91 const std::vector<ProfileInfoEntry>& entries,
92 bool allow_generic_icon,
93 bool must_be_unique,
94 size_t* out_icon_index) {
95 size_t start = allow_generic_icon ? 0 : kGenericIconCount;
96 size_t end = profiles::GetDefaultAvatarIconCount();
97 size_t count = end - start;
98
99 int rand = base::RandInt(0, count);
100 for (size_t i = 0; i < count; ++i) {
101 size_t icon_index = start + (rand + i) % count;
102 if (!must_be_unique || IconIndexIsUnique(entries, icon_index)) {
103 *out_icon_index = icon_index;
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 } // namespace
112
113 namespace profiles {
114
115 // Gets the number of default avatar icons that exist.
116 size_t GetDefaultAvatarIconCount() {
117 return kDefaultAvatarIconsCount;
118 }
119
120 int GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
121 DCHECK_LT(index, GetDefaultAvatarIconCount());
122 return kDefaultAvatarIconResources[index];
123 }
124
125 std::string GetDefaultAvatarIconUrl(size_t index) {
126 DCHECK_LT(index, kDefaultAvatarIconsCount);
127 return StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index);
128 }
129
130 bool IsDefaultAvatarIconUrl(const std::string& url,
131 size_t* icon_index) {
132 DCHECK(icon_index);
133 if (url.find(kDefaultUrlPrefix) != 0)
134 return false;
135
136 int int_value = -1;
137 if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix),
138 url.end(),
139 &int_value)) {
140 if (int_value < 0 ||
141 int_value >= static_cast<int>(kDefaultAvatarIconsCount))
142 return false;
143 *icon_index = int_value;
144 return true;
145 }
146
147 return false;
148 }
149
150 string16 ChooseNameForNewProfile(const std::vector<ProfileInfoEntry>& entries,
151 size_t icon_index) {
152 for (int name_index = 1; ; ++name_index) {
153 string16 name;
154 if (icon_index < kGenericIconCount) {
155 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME,
156 name_index);
157 } else {
158 name = l10n_util::GetStringUTF16(
159 kDefaultNames[icon_index - kGenericIconCount]);
160 if (name_index > 1)
161 name.append(UTF8ToUTF16(base::IntToString(name_index)));
162 }
163
164 // Loop through previously named profiles to ensure we're not duplicating.
165 bool name_found = false;
166 for (std::vector<ProfileInfoEntry>::const_iterator it = entries.begin();
167 it != entries.end(); ++it) {
168 if (it->name() == name) {
169 name_found = true;
170 break;
171 }
172 }
173 if (!name_found)
174 return name;
175 }
176 }
177
178 size_t ChooseAvatarIconIndexForNewProfile(
179 const std::vector<ProfileInfoEntry>& entries) {
180 size_t icon_index = 0;
181 // Try to find a unique, non-generic icon.
182 if (::ChooseAvatarIconIndexForNewProfile(entries, false, true, &icon_index))
183 return icon_index;
184 // Try to find any unique icon.
185 if (::ChooseAvatarIconIndexForNewProfile(entries, true, true, &icon_index))
186 return icon_index;
187 // Settle for any random icon, even if it's not unique.
188 if (::ChooseAvatarIconIndexForNewProfile(entries, true, false, &icon_index))
189 return icon_index;
190
191 NOTREACHED();
192 return 0;
193 }
194
195 } // namespace profile_icon_util
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_info_util.h ('k') | chrome/browser/profiles/profile_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698