Revert of ui: Clean up damaged rects and clear them after painting. (patchset #2...
[chromium-blink-merge.git] / components / user_manager / user_image / default_user_images.cc
blob2d9936b9f79231e6753a10190db8d366e71807e3
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.
5 #include "components/user_manager/user_image/default_user_images.h"
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 "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h"
17 #include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
18 #include "ui/gfx/image/image_skia.h"
20 namespace user_manager {
22 namespace {
24 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER_";
25 const char kZeroDefaultUrl[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER";
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 IDS_LOGIN_DEFAULT_USER_DESC_33,
65 // Returns a string consisting of the prefix specified and the index of the
66 // image if its valid.
67 std::string GetDefaultImageString(int index, const std::string& prefix) {
68 if (index < 0 || index >= kDefaultImagesCount) {
69 DCHECK(!base::SysInfo::IsRunningOnChromeOS());
70 return std::string();
72 return base::StringPrintf("%s%d", prefix.c_str(), index);
75 // Returns true if the string specified consists of the prefix and one of
76 // the default images indices. Returns the index of the image in |image_id|
77 // variable.
78 bool IsDefaultImageString(const std::string& s,
79 const std::string& prefix,
80 int* image_id) {
81 DCHECK(image_id);
82 if (!StartsWithASCII(s, prefix, true))
83 return false;
85 int image_index = -1;
86 if (base::StringToInt(base::StringPiece(s.begin() + prefix.length(), s.end()),
87 &image_index)) {
88 if (image_index < 0 || image_index >= kDefaultImagesCount)
89 return false;
90 *image_id = image_index;
91 return true;
94 return false;
96 } // namespace
98 std::string GetDefaultImageUrl(int index) {
99 if (index == 0)
100 return kZeroDefaultUrl;
101 return GetDefaultImageString(index, kDefaultUrlPrefix);
104 bool IsDefaultImageUrl(const std::string& url, int* image_id) {
105 if (url == kZeroDefaultUrl) {
106 *image_id = 0;
107 return true;
109 return IsDefaultImageString(url, kDefaultUrlPrefix, image_id);
112 const gfx::ImageSkia& GetDefaultImage(int index) {
113 DCHECK(index >= 0 && index < kDefaultImagesCount);
114 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
115 kDefaultImageResourceIDs[index]);
118 base::string16 GetDefaultImageDescription(int index) {
119 DCHECK(index >= 0 && index < kDefaultImagesCount);
120 int string_id = kDefaultImageDescriptions[index];
121 if (string_id)
122 return l10n_util::GetStringUTF16(string_id);
123 else
124 return base::string16();
127 // Resource IDs of default user images.
128 const int kDefaultImageResourceIDs[] = {
129 IDR_LOGIN_DEFAULT_USER,
130 IDR_LOGIN_DEFAULT_USER_1,
131 IDR_LOGIN_DEFAULT_USER_2,
132 IDR_LOGIN_DEFAULT_USER_3,
133 IDR_LOGIN_DEFAULT_USER_4,
134 IDR_LOGIN_DEFAULT_USER_5,
135 IDR_LOGIN_DEFAULT_USER_6,
136 IDR_LOGIN_DEFAULT_USER_7,
137 IDR_LOGIN_DEFAULT_USER_8,
138 IDR_LOGIN_DEFAULT_USER_9,
139 IDR_LOGIN_DEFAULT_USER_10,
140 IDR_LOGIN_DEFAULT_USER_11,
141 IDR_LOGIN_DEFAULT_USER_12,
142 IDR_LOGIN_DEFAULT_USER_13,
143 IDR_LOGIN_DEFAULT_USER_14,
144 IDR_LOGIN_DEFAULT_USER_15,
145 IDR_LOGIN_DEFAULT_USER_16,
146 IDR_LOGIN_DEFAULT_USER_17,
147 IDR_LOGIN_DEFAULT_USER_18,
148 IDR_LOGIN_DEFAULT_USER_19,
149 IDR_LOGIN_DEFAULT_USER_20,
150 IDR_LOGIN_DEFAULT_USER_21,
151 IDR_LOGIN_DEFAULT_USER_22,
152 IDR_LOGIN_DEFAULT_USER_23,
153 IDR_LOGIN_DEFAULT_USER_24,
154 IDR_LOGIN_DEFAULT_USER_25,
155 IDR_LOGIN_DEFAULT_USER_26,
156 IDR_LOGIN_DEFAULT_USER_27,
157 IDR_LOGIN_DEFAULT_USER_28,
158 IDR_LOGIN_DEFAULT_USER_29,
159 IDR_LOGIN_DEFAULT_USER_30,
160 IDR_LOGIN_DEFAULT_USER_31,
161 IDR_LOGIN_DEFAULT_USER_32,
162 IDR_LOGIN_DEFAULT_USER_33,
165 const int kDefaultImageAuthorIDs[] = {
166 IDS_LOGIN_DEFAULT_USER_AUTHOR,
167 IDS_LOGIN_DEFAULT_USER_AUTHOR_1,
168 IDS_LOGIN_DEFAULT_USER_AUTHOR_2,
169 IDS_LOGIN_DEFAULT_USER_AUTHOR_3,
170 IDS_LOGIN_DEFAULT_USER_AUTHOR_4,
171 IDS_LOGIN_DEFAULT_USER_AUTHOR_5,
172 IDS_LOGIN_DEFAULT_USER_AUTHOR_6,
173 IDS_LOGIN_DEFAULT_USER_AUTHOR_7,
174 IDS_LOGIN_DEFAULT_USER_AUTHOR_8,
175 IDS_LOGIN_DEFAULT_USER_AUTHOR_9,
176 IDS_LOGIN_DEFAULT_USER_AUTHOR_10,
177 IDS_LOGIN_DEFAULT_USER_AUTHOR_11,
178 IDS_LOGIN_DEFAULT_USER_AUTHOR_12,
179 IDS_LOGIN_DEFAULT_USER_AUTHOR_13,
180 IDS_LOGIN_DEFAULT_USER_AUTHOR_14,
181 IDS_LOGIN_DEFAULT_USER_AUTHOR_15,
182 IDS_LOGIN_DEFAULT_USER_AUTHOR_16,
183 IDS_LOGIN_DEFAULT_USER_AUTHOR_17,
184 IDS_LOGIN_DEFAULT_USER_AUTHOR_18,
185 IDS_LOGIN_DEFAULT_USER_AUTHOR_19,
186 IDS_LOGIN_DEFAULT_USER_AUTHOR_20,
187 IDS_LOGIN_DEFAULT_USER_AUTHOR_21,
188 IDS_LOGIN_DEFAULT_USER_AUTHOR_22,
189 IDS_LOGIN_DEFAULT_USER_AUTHOR_23,
190 IDS_LOGIN_DEFAULT_USER_AUTHOR_24,
191 IDS_LOGIN_DEFAULT_USER_AUTHOR_25,
192 IDS_LOGIN_DEFAULT_USER_AUTHOR_26,
193 IDS_LOGIN_DEFAULT_USER_AUTHOR_27,
194 IDS_LOGIN_DEFAULT_USER_AUTHOR_28,
195 IDS_LOGIN_DEFAULT_USER_AUTHOR_29,
196 IDS_LOGIN_DEFAULT_USER_AUTHOR_30,
197 IDS_LOGIN_DEFAULT_USER_AUTHOR_31,
198 IDS_LOGIN_DEFAULT_USER_AUTHOR_32,
199 IDS_LOGIN_DEFAULT_USER_AUTHOR_33,
202 const int kDefaultImageWebsiteIDs[] = {
203 IDS_LOGIN_DEFAULT_USER_WEBSITE,
204 IDS_LOGIN_DEFAULT_USER_WEBSITE_1,
205 IDS_LOGIN_DEFAULT_USER_WEBSITE_2,
206 IDS_LOGIN_DEFAULT_USER_WEBSITE_3,
207 IDS_LOGIN_DEFAULT_USER_WEBSITE_4,
208 IDS_LOGIN_DEFAULT_USER_WEBSITE_5,
209 IDS_LOGIN_DEFAULT_USER_WEBSITE_6,
210 IDS_LOGIN_DEFAULT_USER_WEBSITE_7,
211 IDS_LOGIN_DEFAULT_USER_WEBSITE_8,
212 IDS_LOGIN_DEFAULT_USER_WEBSITE_9,
213 IDS_LOGIN_DEFAULT_USER_WEBSITE_10,
214 IDS_LOGIN_DEFAULT_USER_WEBSITE_11,
215 IDS_LOGIN_DEFAULT_USER_WEBSITE_12,
216 IDS_LOGIN_DEFAULT_USER_WEBSITE_13,
217 IDS_LOGIN_DEFAULT_USER_WEBSITE_14,
218 IDS_LOGIN_DEFAULT_USER_WEBSITE_15,
219 IDS_LOGIN_DEFAULT_USER_WEBSITE_16,
220 IDS_LOGIN_DEFAULT_USER_WEBSITE_17,
221 IDS_LOGIN_DEFAULT_USER_WEBSITE_18,
222 IDS_LOGIN_DEFAULT_USER_WEBSITE_19,
223 IDS_LOGIN_DEFAULT_USER_WEBSITE_20,
224 IDS_LOGIN_DEFAULT_USER_WEBSITE_21,
225 IDS_LOGIN_DEFAULT_USER_WEBSITE_22,
226 IDS_LOGIN_DEFAULT_USER_WEBSITE_23,
227 IDS_LOGIN_DEFAULT_USER_WEBSITE_24,
228 IDS_LOGIN_DEFAULT_USER_WEBSITE_25,
229 IDS_LOGIN_DEFAULT_USER_WEBSITE_26,
230 IDS_LOGIN_DEFAULT_USER_WEBSITE_27,
231 IDS_LOGIN_DEFAULT_USER_WEBSITE_28,
232 IDS_LOGIN_DEFAULT_USER_WEBSITE_29,
233 IDS_LOGIN_DEFAULT_USER_WEBSITE_30,
234 IDS_LOGIN_DEFAULT_USER_WEBSITE_31,
235 IDS_LOGIN_DEFAULT_USER_WEBSITE_32,
236 IDS_LOGIN_DEFAULT_USER_WEBSITE_33,
239 const int kDefaultImagesCount = arraysize(kDefaultImageResourceIDs);
241 const int kFirstDefaultImageIndex = 19;
243 // The order and the values of these constants are important for histograms
244 // of different Chrome OS versions to be merged smoothly.
245 const int kHistogramImageFromCamera = 19;
246 const int kHistogramImageFromFile = 20;
247 const int kHistogramImageOld = 21;
248 const int kHistogramImageFromProfile = 22;
249 const int kHistogramVideoFromCamera = 23;
250 const int kHistogramVideoFromFile = 24;
251 const int kHistogramImagesCount = kDefaultImagesCount + 6;
253 int GetDefaultImageHistogramValue(int index) {
254 DCHECK(index >= 0 && index < kDefaultImagesCount);
255 // Create a gap in histogram values for
256 // [kHistogramImageFromCamera..kHistogramImageFromProfile] block to fit.
257 if (index < kHistogramImageFromCamera)
258 return index;
259 return index + 6;
262 } // namespace user_manager