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

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

Issue 222313005: [Profiles] Download high-res avatars using the --new-profile-management flag (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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_avatar_icon_util.h" 5 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
6 6
7 #include "base/file_util.h"
7 #include "base/format_macros.h" 8 #include "base/format_macros.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
9 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "chrome/common/chrome_paths.h"
11 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
12 #include "grit/theme_resources.h" 15 #include "grit/theme_resources.h"
13 #include "third_party/skia/include/core/SkPaint.h" 16 #include "third_party/skia/include/core/SkPaint.h"
14 #include "third_party/skia/include/core/SkPath.h" 17 #include "third_party/skia/include/core/SkPath.h"
15 #include "third_party/skia/include/core/SkScalar.h" 18 #include "third_party/skia/include/core/SkScalar.h"
16 #include "third_party/skia/include/core/SkXfermode.h" 19 #include "third_party/skia/include/core/SkXfermode.h"
17 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/image/canvas_image_source.h" 22 #include "ui/gfx/image/canvas_image_source.h"
23 #include "ui/gfx/image/image.h"
20 #include "ui/gfx/image/image_skia_operations.h" 24 #include "ui/gfx/image/image_skia_operations.h"
21 25
22 // Helper methods for transforming and drawing avatar icons. 26 // Helper methods for transforming and drawing avatar icons.
23 namespace { 27 namespace {
24 28
29 const int kDefaultAvatarIconResources[] = {
30 IDR_PROFILE_AVATAR_0,
31 IDR_PROFILE_AVATAR_1,
32 IDR_PROFILE_AVATAR_2,
33 IDR_PROFILE_AVATAR_3,
34 IDR_PROFILE_AVATAR_4,
35 IDR_PROFILE_AVATAR_5,
36 IDR_PROFILE_AVATAR_6,
37 IDR_PROFILE_AVATAR_7,
38 IDR_PROFILE_AVATAR_8,
39 IDR_PROFILE_AVATAR_9,
40 IDR_PROFILE_AVATAR_10,
41 IDR_PROFILE_AVATAR_11,
42 IDR_PROFILE_AVATAR_12,
43 IDR_PROFILE_AVATAR_13,
44 IDR_PROFILE_AVATAR_14,
45 IDR_PROFILE_AVATAR_15,
46 IDR_PROFILE_AVATAR_16,
47 IDR_PROFILE_AVATAR_17,
48 IDR_PROFILE_AVATAR_18,
49 IDR_PROFILE_AVATAR_19,
50 IDR_PROFILE_AVATAR_20,
51 IDR_PROFILE_AVATAR_21,
52 IDR_PROFILE_AVATAR_22,
53 IDR_PROFILE_AVATAR_23,
54 IDR_PROFILE_AVATAR_24,
55 IDR_PROFILE_AVATAR_25,
56 };
57
58 // File names for the high-res avatar icon resources. In the same order as
59 // the avatars in kDefaultAvatarIconResources.
60 const char* kDefaultAvatarIconResourceFileNames[] = {
61 "avatar_generic.png",
62 "avatar_generic_aqua.png",
63 "avatar_generic_blue.png",
64 "avatar_generic_green.png",
65 "avatar_generic_orange.png",
66 "avatar_generic_purple.png",
67 "avatar_generic_red.png",
68 "avatar_generic_yellow.png",
69 "avatar_secret_agent.png",
70 "avatar_superhero.png",
71 "avatar_volley_ball.png",
72 "avatar_businessman.png",
73 "avatar_ninja.png",
74 "avatar_alien.png",
75 "avatar_smiley.png",
76 "avatar_flower.png",
77 "avatar_pizza.png",
78 "avatar_soccer.png",
79 "avatar_burger.png",
80 "avatar_cat.png",
81 "avatar_cupcake.png",
82 "avatar_dog.png",
83 "avatar_horse.png",
84 "avatar_margarita.png",
85 "avatar_note.png",
86 "avatar_sun_cloud.png",
87 };
88
89 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
90
91 // The first 8 icons are generic.
92 const size_t kGenericAvatarIconsCount = 8;
93
25 // Determine what the scaled height of the avatar icon should be for a 94 // Determine what the scaled height of the avatar icon should be for a
26 // specified width, to preserve the aspect ratio. 95 // specified width, to preserve the aspect ratio.
27 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { 96 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) {
28
29 // Multiply the width by the inverted aspect ratio (height over 97 // Multiply the width by the inverted aspect ratio (height over
30 // width), and then add 0.5 to ensure the int truncation rounds nicely. 98 // width), and then add 0.5 to ensure the int truncation rounds nicely.
31 int scaled_height = width * 99 int scaled_height = width *
32 ((float) avatar.height() / (float) avatar.width()) + 0.5f; 100 ((float) avatar.height() / (float) avatar.width()) + 0.5f;
33 return scaled_height; 101 return scaled_height;
34 } 102 }
35 103
36 // A CanvasImageSource that draws a sized and positioned avatar with an 104 // A CanvasImageSource that draws a sized and positioned avatar with an
37 // optional border independently of the scale factor. 105 // optional border independently of the scale factor.
38 class AvatarImageSource : public gfx::CanvasImageSource { 106 class AvatarImageSource : public gfx::CanvasImageSource {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 const int kAvatarIconWidth = 38; 247 const int kAvatarIconWidth = 38;
180 const int kAvatarIconHeight = 31; 248 const int kAvatarIconHeight = 31;
181 const int kAvatarIconPadding = 2; 249 const int kAvatarIconPadding = 2;
182 const SkColor kAvatarTutorialBackgroundColor = SkColorSetRGB(0x42, 0x85, 0xf4); 250 const SkColor kAvatarTutorialBackgroundColor = SkColorSetRGB(0x42, 0x85, 0xf4);
183 const SkColor kAvatarTutorialContentTextColor = SkColorSetRGB(0xc6, 0xda, 0xfc); 251 const SkColor kAvatarTutorialContentTextColor = SkColorSetRGB(0xc6, 0xda, 0xfc);
184 252
185 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; 253 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_";
186 const char kGAIAPictureFileName[] = "Google Profile Picture.png"; 254 const char kGAIAPictureFileName[] = "Google Profile Picture.png";
187 const char kHighResAvatarFolderName[] = "Avatars"; 255 const char kHighResAvatarFolderName[] = "Avatars";
188 256
189 const int kDefaultAvatarIconResources[] = {
190 IDR_PROFILE_AVATAR_0,
191 IDR_PROFILE_AVATAR_1,
192 IDR_PROFILE_AVATAR_2,
193 IDR_PROFILE_AVATAR_3,
194 IDR_PROFILE_AVATAR_4,
195 IDR_PROFILE_AVATAR_5,
196 IDR_PROFILE_AVATAR_6,
197 IDR_PROFILE_AVATAR_7,
198 IDR_PROFILE_AVATAR_8,
199 IDR_PROFILE_AVATAR_9,
200 IDR_PROFILE_AVATAR_10,
201 IDR_PROFILE_AVATAR_11,
202 IDR_PROFILE_AVATAR_12,
203 IDR_PROFILE_AVATAR_13,
204 IDR_PROFILE_AVATAR_14,
205 IDR_PROFILE_AVATAR_15,
206 IDR_PROFILE_AVATAR_16,
207 IDR_PROFILE_AVATAR_17,
208 IDR_PROFILE_AVATAR_18,
209 IDR_PROFILE_AVATAR_19,
210 IDR_PROFILE_AVATAR_20,
211 IDR_PROFILE_AVATAR_21,
212 IDR_PROFILE_AVATAR_22,
213 IDR_PROFILE_AVATAR_23,
214 IDR_PROFILE_AVATAR_24,
215 IDR_PROFILE_AVATAR_25,
216 };
217
218 // File names for the high-res avatar icon resources. In the same order as
219 // the avatars in kDefaultAvatarIconResources.
220 const char* kDefaultAvatarIconResourceFileNames[] = {
221 "avatar_generic.png",
222 "avatar_generic_aqua.png",
223 "avatar_generic_blue.png",
224 "avatar_generic_green.png",
225 "avatar_generic_orange.png",
226 "avatar_generic_purple.png",
227 "avatar_generic_red.png",
228 "avatar_generic_yellow.png",
229 "avatar_secret_agent.png",
230 "avatar_superhero.png",
231 "avatar_volley_ball.png",
232 "avatar_businessman.png",
233 "avatar_ninja.png",
234 "avatar_alien.png",
235 "avatar_smiley.png",
236 "avatar_flower.png",
237 "avatar_pizza.png",
238 "avatar_soccer.png",
239 "avatar_burger.png",
240 "avatar_cat.png",
241 "avatar_cupcake.png",
242 "avatar_dog.png",
243 "avatar_horse.png",
244 "avatar_margarita.png",
245 "avatar_note.png",
246 "avatar_sun_cloud.png",
247 };
248
249 const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
250
251 // The first 8 icons are generic.
252 const size_t kGenericAvatarIconsCount = 8;
253
254 gfx::Image GetSizedAvatarIconWithBorder(const gfx::Image& image, 257 gfx::Image GetSizedAvatarIconWithBorder(const gfx::Image& image,
255 bool is_rectangle, 258 bool is_rectangle,
256 int width, int height) { 259 int width, int height) {
257 if (!is_rectangle) 260 if (!is_rectangle)
258 return image; 261 return image;
259 262
260 gfx::Size size(width, height); 263 gfx::Size size(width, height);
261 264
262 // Source for a centered, sized icon with a border. 265 // Source for a centered, sized icon with a border.
263 scoped_ptr<gfx::ImageSkiaSource> source( 266 scoped_ptr<gfx::ImageSkiaSource> source(
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 334
332 int GetDefaultAvatarIconResourceIDAtIndex(size_t index) { 335 int GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
333 DCHECK(IsDefaultAvatarIconIndex(index)); 336 DCHECK(IsDefaultAvatarIconIndex(index));
334 return kDefaultAvatarIconResources[index]; 337 return kDefaultAvatarIconResources[index];
335 } 338 }
336 339
337 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) { 340 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) {
338 return kDefaultAvatarIconResourceFileNames[index]; 341 return kDefaultAvatarIconResourceFileNames[index];
339 } 342 }
340 343
344 base::FilePath GetPathOfHighResAvatarAtIndex(size_t index) {
345 std::string file_name = kDefaultAvatarIconResourceFileNames[index];
346 base::FilePath user_data_dir;
347 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
348 base::FilePath image_path = user_data_dir.AppendASCII(
349 kHighResAvatarFolderName).AppendASCII(file_name);
350 return image_path;
351 }
352
341 std::string GetDefaultAvatarIconUrl(size_t index) { 353 std::string GetDefaultAvatarIconUrl(size_t index) {
342 DCHECK(IsDefaultAvatarIconIndex(index)); 354 DCHECK(IsDefaultAvatarIconIndex(index));
343 return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); 355 return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index);
344 } 356 }
345 357
346 bool IsDefaultAvatarIconIndex(size_t index) { 358 bool IsDefaultAvatarIconIndex(size_t index) {
347 return index < kDefaultAvatarIconsCount; 359 return index < kDefaultAvatarIconsCount;
348 } 360 }
349 361
350 bool IsDefaultAvatarIconUrl(const std::string& url, 362 bool IsDefaultAvatarIconUrl(const std::string& url,
(...skipping 11 matching lines...) Expand all
362 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) 374 int_value >= static_cast<int>(kDefaultAvatarIconsCount))
363 return false; 375 return false;
364 *icon_index = int_value; 376 *icon_index = int_value;
365 return true; 377 return true;
366 } 378 }
367 379
368 return false; 380 return false;
369 } 381 }
370 382
371 } // namespace profiles 383 } // namespace profiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698