Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / profiles / profile_avatar_icon_util.cc
blob7839fbc978393c90a59dc77e6faa955e10098cfa
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 "chrome/browser/profiles/profile_avatar_icon_util.h"
7 #include "base/files/file_util.h"
8 #include "base/format_macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "grit/theme_resources.h"
18 #include "skia/ext/image_operations.h"
19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "third_party/skia/include/core/SkPath.h"
21 #include "third_party/skia/include/core/SkScalar.h"
22 #include "third_party/skia/include/core/SkXfermode.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/image/canvas_image_source.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/image/image_skia_operations.h"
29 #include "ui/gfx/skia_util.h"
31 // Helper methods for transforming and drawing avatar icons.
32 namespace {
34 // Determine what the scaled height of the avatar icon should be for a
35 // specified width, to preserve the aspect ratio.
36 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) {
37 // Multiply the width by the inverted aspect ratio (height over
38 // width), and then add 0.5 to ensure the int truncation rounds nicely.
39 int scaled_height = width *
40 ((float) avatar.height() / (float) avatar.width()) + 0.5f;
41 return scaled_height;
44 // A CanvasImageSource that draws a sized and positioned avatar with an
45 // optional border independently of the scale factor.
46 class AvatarImageSource : public gfx::CanvasImageSource {
47 public:
48 enum AvatarPosition {
49 POSITION_CENTER,
50 POSITION_BOTTOM_CENTER,
53 enum AvatarBorder {
54 BORDER_NONE,
55 BORDER_NORMAL,
56 BORDER_ETCHED,
59 AvatarImageSource(gfx::ImageSkia avatar,
60 const gfx::Size& canvas_size,
61 int width,
62 AvatarPosition position,
63 AvatarBorder border);
64 ~AvatarImageSource() override;
66 // CanvasImageSource override:
67 void Draw(gfx::Canvas* canvas) override;
69 private:
70 gfx::ImageSkia avatar_;
71 const gfx::Size canvas_size_;
72 const int width_;
73 const int height_;
74 const AvatarPosition position_;
75 const AvatarBorder border_;
77 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource);
80 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar,
81 const gfx::Size& canvas_size,
82 int width,
83 AvatarPosition position,
84 AvatarBorder border)
85 : gfx::CanvasImageSource(canvas_size, false),
86 canvas_size_(canvas_size),
87 width_(width),
88 height_(GetScaledAvatarHeightForWidth(width, avatar)),
89 position_(position),
90 border_(border) {
91 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage(
92 avatar, skia::ImageOperations::RESIZE_BEST,
93 gfx::Size(width_, height_));
96 AvatarImageSource::~AvatarImageSource() {
99 void AvatarImageSource::Draw(gfx::Canvas* canvas) {
100 // Center the avatar horizontally.
101 int x = (canvas_size_.width() - width_) / 2;
102 int y;
104 if (position_ == POSITION_CENTER) {
105 // Draw the avatar centered on the canvas.
106 y = (canvas_size_.height() - height_) / 2;
107 } else {
108 // Draw the avatar on the bottom center of the canvas, leaving 1px below.
109 y = canvas_size_.height() - height_ - 1;
112 canvas->DrawImageInt(avatar_, x, y);
114 // The border should be square.
115 int border_size = std::max(width_, height_);
116 // Reset the x and y for the square border.
117 x = (canvas_size_.width() - border_size) / 2;
118 y = (canvas_size_.height() - border_size) / 2;
120 if (border_ == BORDER_NORMAL) {
121 // Draw a gray border on the inside of the avatar.
122 SkColor border_color = SkColorSetARGB(83, 0, 0, 0);
124 // Offset the rectangle by a half pixel so the border is drawn within the
125 // appropriate pixels no matter the scale factor. Subtract 1 from the right
126 // and bottom sizes to specify the endpoints, yielding -0.5.
127 SkPath path;
128 path.addRect(SkFloatToScalar(x + 0.5f), // left
129 SkFloatToScalar(y + 0.5f), // top
130 SkFloatToScalar(x + border_size - 0.5f), // right
131 SkFloatToScalar(y + border_size - 0.5f)); // bottom
133 SkPaint paint;
134 paint.setColor(border_color);
135 paint.setStyle(SkPaint::kStroke_Style);
136 paint.setStrokeWidth(SkIntToScalar(1));
138 canvas->DrawPath(path, paint);
139 } else if (border_ == BORDER_ETCHED) {
140 // Give the avatar an etched look by drawing a highlight on the bottom and
141 // right edges.
142 SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0);
143 SkColor highlight_color = SkColorSetARGB(96, 255, 255, 255);
145 SkPaint paint;
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeWidth(SkIntToScalar(1));
149 SkPath path;
151 // Left and top shadows. To support higher scale factors than 1, position
152 // the orthogonal dimension of each line on the half-pixel to separate the
153 // pixel. For a vertical line, this means adding 0.5 to the x-value.
154 path.moveTo(SkFloatToScalar(x + 0.5f), SkIntToScalar(y + height_));
156 // Draw up to the top-left. Stop with the y-value at a half-pixel.
157 path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 0.5f));
159 // Draw right to the top-right, stopping within the last pixel.
160 path.rLineTo(SkFloatToScalar(width_ - 0.5f), SkIntToScalar(0));
162 paint.setColor(shadow_color);
163 canvas->DrawPath(path, paint);
165 path.reset();
167 // Bottom and right highlights. Note that the shadows own the shared corner
168 // pixels, so reduce the sizes accordingly.
169 path.moveTo(SkIntToScalar(x + 1), SkFloatToScalar(y + height_ - 0.5f));
171 // Draw right to the bottom-right.
172 path.rLineTo(SkFloatToScalar(width_ - 1.5f), SkIntToScalar(0));
174 // Draw up to the top-right.
175 path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 1.5f));
177 paint.setColor(highlight_color);
178 canvas->DrawPath(path, paint);
182 } // namespace
184 namespace profiles {
186 struct IconResourceInfo {
187 int resource_id;
188 const char* filename;
191 const int kAvatarIconWidth = 38;
192 const int kAvatarIconHeight = 31;
193 const SkColor kAvatarTutorialBackgroundColor = SkColorSetRGB(0x42, 0x85, 0xf4);
194 const SkColor kAvatarTutorialContentTextColor = SkColorSetRGB(0xc6, 0xda, 0xfc);
195 const SkColor kAvatarBubbleAccountsBackgroundColor =
196 SkColorSetRGB(0xf3, 0xf3, 0xf3);
197 const SkColor kAvatarBubbleGaiaBackgroundColor =
198 SkColorSetRGB(0xf5, 0xf5, 0xf5);
199 const SkColor kUserManagerBackgroundColor = SkColorSetRGB(0xee, 0xee, 0xee);
201 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_";
202 const char kGAIAPictureFileName[] = "Google Profile Picture.png";
203 const char kHighResAvatarFolderName[] = "Avatars";
205 // The size of the function-static kDefaultAvatarIconResources array below.
206 const size_t kDefaultAvatarIconsCount = 27;
208 // The first 8 icons are generic.
209 const size_t kGenericAvatarIconsCount = 8;
211 // The avatar used as a placeholder (grey silhouette).
212 const size_t kPlaceholderAvatarIcon = 26;
214 gfx::Image GetSizedAvatarIcon(const gfx::Image& image,
215 bool is_rectangle,
216 int width, int height) {
217 if (!is_rectangle && image.Height() <= height)
218 return image;
220 gfx::Size size(width, height);
222 // Source for a centered, sized icon. GAIA images get a border.
223 scoped_ptr<gfx::ImageSkiaSource> source(
224 new AvatarImageSource(
225 *image.ToImageSkia(),
226 size,
227 std::min(width, height),
228 AvatarImageSource::POSITION_CENTER,
229 AvatarImageSource::BORDER_NONE));
231 return gfx::Image(gfx::ImageSkia(source.release(), size));
234 gfx::Image GetAvatarIconForMenu(const gfx::Image& image,
235 bool is_rectangle) {
236 return GetSizedAvatarIcon(
237 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight);
240 gfx::Image GetAvatarIconForWebUI(const gfx::Image& image,
241 bool is_rectangle) {
242 return GetSizedAvatarIcon(image, is_rectangle,
243 kAvatarIconWidth, kAvatarIconHeight);
246 gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image,
247 bool is_gaia_image,
248 int dst_width,
249 int dst_height) {
250 // The image requires no border or resizing.
251 if (!is_gaia_image && image.Height() <= kAvatarIconHeight)
252 return image;
254 int size = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight),
255 std::min(dst_width, dst_height));
256 gfx::Size dst_size(dst_width, dst_height);
258 // Source for a sized icon drawn at the bottom center of the canvas,
259 // with an etched border (for GAIA images).
260 scoped_ptr<gfx::ImageSkiaSource> source(
261 new AvatarImageSource(
262 *image.ToImageSkia(),
263 dst_size,
264 size,
265 AvatarImageSource::POSITION_BOTTOM_CENTER,
266 is_gaia_image ? AvatarImageSource::BORDER_ETCHED :
267 AvatarImageSource::BORDER_NONE));
269 return gfx::Image(gfx::ImageSkia(source.release(), dst_size));
272 SkBitmap GetAvatarIconAsSquare(const SkBitmap& source_bitmap,
273 int scale_factor) {
274 SkBitmap square_bitmap;
275 if ((source_bitmap.width() == scale_factor * profiles::kAvatarIconWidth) &&
276 (source_bitmap.height() == scale_factor * profiles::kAvatarIconHeight)) {
277 // Shave a couple of columns so the |source_bitmap| is more square. So when
278 // resized to a square aspect ratio it looks pretty.
279 gfx::Rect frame(scale_factor * profiles::kAvatarIconWidth,
280 scale_factor * profiles::kAvatarIconHeight);
281 frame.Inset(scale_factor * 2, 0, scale_factor * 2, 0);
282 source_bitmap.extractSubset(&square_bitmap, gfx::RectToSkIRect(frame));
283 } else {
284 // If not the avatar icon's aspect ratio, the image should be square.
285 DCHECK(source_bitmap.width() == source_bitmap.height());
286 square_bitmap = source_bitmap;
288 return square_bitmap;
291 // Helper methods for accessing, transforming and drawing avatar icons.
292 size_t GetDefaultAvatarIconCount() {
293 return kDefaultAvatarIconsCount;
296 size_t GetGenericAvatarIconCount() {
297 return kGenericAvatarIconsCount;
300 size_t GetPlaceholderAvatarIndex() {
301 return kPlaceholderAvatarIcon;
304 int GetPlaceholderAvatarIconResourceID() {
305 return IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE;
308 const IconResourceInfo* GetDefaultAvatarIconResourceInfo(size_t index) {
309 DCHECK(index < kDefaultAvatarIconsCount);
310 static const IconResourceInfo resource_info[kDefaultAvatarIconsCount] = {
311 {IDR_PROFILE_AVATAR_0, "avatar_generic.png"},
312 {IDR_PROFILE_AVATAR_1, "avatar_generic_aqua.png"},
313 {IDR_PROFILE_AVATAR_2, "avatar_generic_blue.png"},
314 {IDR_PROFILE_AVATAR_3, "avatar_generic_green.png"},
315 {IDR_PROFILE_AVATAR_4, "avatar_generic_orange.png"},
316 {IDR_PROFILE_AVATAR_5, "avatar_generic_purple.png"},
317 {IDR_PROFILE_AVATAR_6, "avatar_generic_red.png"},
318 {IDR_PROFILE_AVATAR_7, "avatar_generic_yellow.png"},
319 {IDR_PROFILE_AVATAR_8, "avatar_secret_agent.png"},
320 {IDR_PROFILE_AVATAR_9, "avatar_superhero.png"},
321 {IDR_PROFILE_AVATAR_10, "avatar_volley_ball.png"},
322 {IDR_PROFILE_AVATAR_11, "avatar_businessman.png"},
323 {IDR_PROFILE_AVATAR_12, "avatar_ninja.png"},
324 {IDR_PROFILE_AVATAR_13, "avatar_alien.png"},
325 {IDR_PROFILE_AVATAR_14, "avatar_smiley.png"},
326 {IDR_PROFILE_AVATAR_15, "avatar_flower.png"},
327 {IDR_PROFILE_AVATAR_16, "avatar_pizza.png"},
328 {IDR_PROFILE_AVATAR_17, "avatar_soccer.png"},
329 {IDR_PROFILE_AVATAR_18, "avatar_burger.png"},
330 {IDR_PROFILE_AVATAR_19, "avatar_cat.png"},
331 {IDR_PROFILE_AVATAR_20, "avatar_cupcake.png"},
332 {IDR_PROFILE_AVATAR_21, "avatar_dog.png"},
333 {IDR_PROFILE_AVATAR_22, "avatar_horse.png"},
334 {IDR_PROFILE_AVATAR_23, "avatar_margarita.png"},
335 {IDR_PROFILE_AVATAR_24, "avatar_note.png"},
336 {IDR_PROFILE_AVATAR_25, "avatar_sun_cloud.png"},
337 {IDR_PROFILE_AVATAR_26, NULL},
339 return &resource_info[index];
342 int GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
343 DCHECK(IsDefaultAvatarIconIndex(index));
344 return GetDefaultAvatarIconResourceInfo(index)->resource_id;
347 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) {
348 DCHECK(index < kDefaultAvatarIconsCount);
349 DCHECK(index != kPlaceholderAvatarIcon);
350 return GetDefaultAvatarIconResourceInfo(index)->filename;
353 base::FilePath GetPathOfHighResAvatarAtIndex(size_t index) {
354 const char* file_name = GetDefaultAvatarIconFileNameAtIndex(index);
355 base::FilePath user_data_dir;
356 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
357 return user_data_dir.AppendASCII(
358 kHighResAvatarFolderName).AppendASCII(file_name);
361 std::string GetDefaultAvatarIconUrl(size_t index) {
362 DCHECK(IsDefaultAvatarIconIndex(index));
363 return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index);
366 bool IsDefaultAvatarIconIndex(size_t index) {
367 return index < kDefaultAvatarIconsCount;
370 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) {
371 DCHECK(icon_index);
372 if (url.find(kDefaultUrlPrefix) != 0)
373 return false;
375 int int_value = -1;
376 if (base::StringToInt(base::StringPiece(url.begin() +
377 strlen(kDefaultUrlPrefix),
378 url.end()),
379 &int_value)) {
380 if (int_value < 0 ||
381 int_value >= static_cast<int>(kDefaultAvatarIconsCount))
382 return false;
383 *icon_index = int_value;
384 return true;
387 return false;
390 } // namespace profiles