1 // Copyright 2014 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 "components/user_manager/user_image/default_user_images.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/sys_info.h"
14 #include "grit/ui_chromeos_resources.h"
15 #include "grit/ui_chromeos_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/image/image_skia.h"
19
20 namespace user_manager {
21
22 namespace {
23
24 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER_";
25 const char kZeroDefaultUrl[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER";
26
27 // IDs of default user image descriptions.
28 const int kDefaultImageDescriptions[] = {
29 0, // No description for deprecated user image 0.
30 0, // No description for deprecated user image 1.
31 0, // No description for deprecated user image 2.
32 0, // No description for deprecated user image 3.
33 0, // No description for deprecated user image 4.
34 0, // No description for deprecated user image 5.
35 0, // No description for deprecated user image 6.
36 0, // No description for deprecated user image 7.
37 0, // No description for deprecated user image 8.
38 0, // No description for deprecated user image 9.
39 0, // No description for deprecated user image 10.
40 0, // No description for deprecated user image 11.
41 0, // No description for deprecated user image 12.
42 0, // No description for deprecated user image 13.
43 0, // No description for deprecated user image 14.
44 0, // No description for deprecated user image 15.
45 0, // No description for deprecated user image 16.
46 0, // No description for deprecated user image 17.
47 0, // No description for deprecated user image 18.
48 IDS_LOGIN_DEFAULT_USER_DESC_19,
49 IDS_LOGIN_DEFAULT_USER_DESC_20,
50 IDS_LOGIN_DEFAULT_USER_DESC_21,
51 IDS_LOGIN_DEFAULT_USER_DESC_22,
52 IDS_LOGIN_DEFAULT_USER_DESC_23,
53 IDS_LOGIN_DEFAULT_USER_DESC_24,
54 IDS_LOGIN_DEFAULT_USER_DESC_25,
55 IDS_LOGIN_DEFAULT_USER_DESC_26,
56 IDS_LOGIN_DEFAULT_USER_DESC_27,
57 IDS_LOGIN_DEFAULT_USER_DESC_28,
58 IDS_LOGIN_DEFAULT_USER_DESC_29,
59 IDS_LOGIN_DEFAULT_USER_DESC_30,
60 IDS_LOGIN_DEFAULT_USER_DESC_31,
61 IDS_LOGIN_DEFAULT_USER_DESC_32,
62 };
63
64 // Returns a string consisting of the prefix specified and the index of the
65 // image if its valid.
GetDefaultImageString(int index,const std::string & prefix)66 std::string GetDefaultImageString(int index, const std::string& prefix) {
67 if (index < 0 || index >= kDefaultImagesCount) {
68 DCHECK(!base::SysInfo::IsRunningOnChromeOS());
69 return std::string();
70 }
71 return base::StringPrintf("%s%d", prefix.c_str(), index);
72 }
73
74 // Returns true if the string specified consists of the prefix and one of
75 // the default images indices. Returns the index of the image in |image_id|
76 // variable.
IsDefaultImageString(const std::string & s,const std::string & prefix,int * image_id)77 bool IsDefaultImageString(const std::string& s,
78 const std::string& prefix,
79 int* image_id) {
80 DCHECK(image_id);
81 if (!StartsWithASCII(s, prefix, true))
82 return false;
83
84 int image_index = -1;
85 if (base::StringToInt(base::StringPiece(s.begin() + prefix.length(), s.end()),
86 &image_index)) {
87 if (image_index < 0 || image_index >= kDefaultImagesCount)
88 return false;
89 *image_id = image_index;
90 return true;
91 }
92
93 return false;
94 }
95 } // namespace
96
GetDefaultImageUrl(int index)97 std::string GetDefaultImageUrl(int index) {
98 if (index == 0)
99 return kZeroDefaultUrl;
100 return GetDefaultImageString(index, kDefaultUrlPrefix);
101 }
102
IsDefaultImageUrl(const std::string & url,int * image_id)103 bool IsDefaultImageUrl(const std::string& url, int* image_id) {
104 if (url == kZeroDefaultUrl) {
105 *image_id = 0;
106 return true;
107 }
108 return IsDefaultImageString(url, kDefaultUrlPrefix, image_id);
109 }
110
GetDefaultImage(int index)111 const gfx::ImageSkia& GetDefaultImage(int index) {
112 DCHECK(index >= 0 && index < kDefaultImagesCount);
113 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
114 kDefaultImageResourceIDs[index]);
115 }
116
GetDefaultImageDescription(int index)117 base::string16 GetDefaultImageDescription(int index) {
118 DCHECK(index >= 0 && index < kDefaultImagesCount);
119 int string_id = kDefaultImageDescriptions[index];
120 if (string_id)
121 return l10n_util::GetStringUTF16(string_id);
122 else
123 return base::string16();
124 }
125
126 // Resource IDs of default user images.
127 const int kDefaultImageResourceIDs[] = {
128 IDR_LOGIN_DEFAULT_USER,
129 IDR_LOGIN_DEFAULT_USER_1,
130 IDR_LOGIN_DEFAULT_USER_2,
131 IDR_LOGIN_DEFAULT_USER_3,
132 IDR_LOGIN_DEFAULT_USER_4,
133 IDR_LOGIN_DEFAULT_USER_5,
134 IDR_LOGIN_DEFAULT_USER_6,
135 IDR_LOGIN_DEFAULT_USER_7,
136 IDR_LOGIN_DEFAULT_USER_8,
137 IDR_LOGIN_DEFAULT_USER_9,
138 IDR_LOGIN_DEFAULT_USER_10,
139 IDR_LOGIN_DEFAULT_USER_11,
140 IDR_LOGIN_DEFAULT_USER_12,
141 IDR_LOGIN_DEFAULT_USER_13,
142 IDR_LOGIN_DEFAULT_USER_14,
143 IDR_LOGIN_DEFAULT_USER_15,
144 IDR_LOGIN_DEFAULT_USER_16,
145 IDR_LOGIN_DEFAULT_USER_17,
146 IDR_LOGIN_DEFAULT_USER_18,
147 IDR_LOGIN_DEFAULT_USER_19,
148 IDR_LOGIN_DEFAULT_USER_20,
149 IDR_LOGIN_DEFAULT_USER_21,
150 IDR_LOGIN_DEFAULT_USER_22,
151 IDR_LOGIN_DEFAULT_USER_23,
152 IDR_LOGIN_DEFAULT_USER_24,
153 IDR_LOGIN_DEFAULT_USER_25,
154 IDR_LOGIN_DEFAULT_USER_26,
155 IDR_LOGIN_DEFAULT_USER_27,
156 IDR_LOGIN_DEFAULT_USER_28,
157 IDR_LOGIN_DEFAULT_USER_29,
158 IDR_LOGIN_DEFAULT_USER_30,
159 IDR_LOGIN_DEFAULT_USER_31,
160 IDR_LOGIN_DEFAULT_USER_32,
161 };
162
163 const int kDefaultImageAuthorIDs[] = {
164 IDS_LOGIN_DEFAULT_USER_AUTHOR,
165 IDS_LOGIN_DEFAULT_USER_AUTHOR_1,
166 IDS_LOGIN_DEFAULT_USER_AUTHOR_2,
167 IDS_LOGIN_DEFAULT_USER_AUTHOR_3,
168 IDS_LOGIN_DEFAULT_USER_AUTHOR_4,
169 IDS_LOGIN_DEFAULT_USER_AUTHOR_5,
170 IDS_LOGIN_DEFAULT_USER_AUTHOR_6,
171 IDS_LOGIN_DEFAULT_USER_AUTHOR_7,
172 IDS_LOGIN_DEFAULT_USER_AUTHOR_8,
173 IDS_LOGIN_DEFAULT_USER_AUTHOR_9,
174 IDS_LOGIN_DEFAULT_USER_AUTHOR_10,
175 IDS_LOGIN_DEFAULT_USER_AUTHOR_11,
176 IDS_LOGIN_DEFAULT_USER_AUTHOR_12,
177 IDS_LOGIN_DEFAULT_USER_AUTHOR_13,
178 IDS_LOGIN_DEFAULT_USER_AUTHOR_14,
179 IDS_LOGIN_DEFAULT_USER_AUTHOR_15,
180 IDS_LOGIN_DEFAULT_USER_AUTHOR_16,
181 IDS_LOGIN_DEFAULT_USER_AUTHOR_17,
182 IDS_LOGIN_DEFAULT_USER_AUTHOR_18,
183 IDS_LOGIN_DEFAULT_USER_AUTHOR_19,
184 IDS_LOGIN_DEFAULT_USER_AUTHOR_20,
185 IDS_LOGIN_DEFAULT_USER_AUTHOR_21,
186 IDS_LOGIN_DEFAULT_USER_AUTHOR_22,
187 IDS_LOGIN_DEFAULT_USER_AUTHOR_23,
188 IDS_LOGIN_DEFAULT_USER_AUTHOR_24,
189 IDS_LOGIN_DEFAULT_USER_AUTHOR_25,
190 IDS_LOGIN_DEFAULT_USER_AUTHOR_26,
191 IDS_LOGIN_DEFAULT_USER_AUTHOR_27,
192 IDS_LOGIN_DEFAULT_USER_AUTHOR_28,
193 IDS_LOGIN_DEFAULT_USER_AUTHOR_29,
194 IDS_LOGIN_DEFAULT_USER_AUTHOR_30,
195 IDS_LOGIN_DEFAULT_USER_AUTHOR_31,
196 IDS_LOGIN_DEFAULT_USER_AUTHOR_32,
197 };
198
199 const int kDefaultImageWebsiteIDs[] = {
200 IDS_LOGIN_DEFAULT_USER_WEBSITE,
201 IDS_LOGIN_DEFAULT_USER_WEBSITE_1,
202 IDS_LOGIN_DEFAULT_USER_WEBSITE_2,
203 IDS_LOGIN_DEFAULT_USER_WEBSITE_3,
204 IDS_LOGIN_DEFAULT_USER_WEBSITE_4,
205 IDS_LOGIN_DEFAULT_USER_WEBSITE_5,
206 IDS_LOGIN_DEFAULT_USER_WEBSITE_6,
207 IDS_LOGIN_DEFAULT_USER_WEBSITE_7,
208 IDS_LOGIN_DEFAULT_USER_WEBSITE_8,
209 IDS_LOGIN_DEFAULT_USER_WEBSITE_9,
210 IDS_LOGIN_DEFAULT_USER_WEBSITE_10,
211 IDS_LOGIN_DEFAULT_USER_WEBSITE_11,
212 IDS_LOGIN_DEFAULT_USER_WEBSITE_12,
213 IDS_LOGIN_DEFAULT_USER_WEBSITE_13,
214 IDS_LOGIN_DEFAULT_USER_WEBSITE_14,
215 IDS_LOGIN_DEFAULT_USER_WEBSITE_15,
216 IDS_LOGIN_DEFAULT_USER_WEBSITE_16,
217 IDS_LOGIN_DEFAULT_USER_WEBSITE_17,
218 IDS_LOGIN_DEFAULT_USER_WEBSITE_18,
219 IDS_LOGIN_DEFAULT_USER_WEBSITE_19,
220 IDS_LOGIN_DEFAULT_USER_WEBSITE_20,
221 IDS_LOGIN_DEFAULT_USER_WEBSITE_21,
222 IDS_LOGIN_DEFAULT_USER_WEBSITE_22,
223 IDS_LOGIN_DEFAULT_USER_WEBSITE_23,
224 IDS_LOGIN_DEFAULT_USER_WEBSITE_24,
225 IDS_LOGIN_DEFAULT_USER_WEBSITE_25,
226 IDS_LOGIN_DEFAULT_USER_WEBSITE_26,
227 IDS_LOGIN_DEFAULT_USER_WEBSITE_27,
228 IDS_LOGIN_DEFAULT_USER_WEBSITE_28,
229 IDS_LOGIN_DEFAULT_USER_WEBSITE_29,
230 IDS_LOGIN_DEFAULT_USER_WEBSITE_30,
231 IDS_LOGIN_DEFAULT_USER_WEBSITE_31,
232 IDS_LOGIN_DEFAULT_USER_WEBSITE_32,
233 };
234
235 const int kDefaultImagesCount = arraysize(kDefaultImageResourceIDs);
236
237 const int kFirstDefaultImageIndex = 19;
238
239 // The order and the values of these constants are important for histograms
240 // of different Chrome OS versions to be merged smoothly.
241 const int kHistogramImageFromCamera = 19;
242 const int kHistogramImageFromFile = 20;
243 const int kHistogramImageOld = 21;
244 const int kHistogramImageFromProfile = 22;
245 const int kHistogramVideoFromCamera = 23;
246 const int kHistogramVideoFromFile = 24;
247 const int kHistogramImagesCount = kDefaultImagesCount + 6;
248
GetDefaultImageHistogramValue(int index)249 int GetDefaultImageHistogramValue(int index) {
250 DCHECK(index >= 0 && index < kDefaultImagesCount);
251 // Create a gap in histogram values for
252 // [kHistogramImageFromCamera..kHistogramImageFromProfile] block to fit.
253 if (index < kHistogramImageFromCamera)
254 return index;
255 return index + 6;
256 }
257
258 } // namespace user_manager
259