OLD | NEW |
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/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/profiles/profile_info_cache.h" | 16 #include "chrome/browser/profiles/profile_info_cache.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
19 #include "skia/ext/image_operations.h" | 20 #include "skia/ext/image_operations.h" |
20 #include "third_party/skia/include/core/SkPaint.h" | 21 #include "third_party/skia/include/core/SkPaint.h" |
21 #include "third_party/skia/include/core/SkPath.h" | 22 #include "third_party/skia/include/core/SkPath.h" |
22 #include "third_party/skia/include/core/SkScalar.h" | 23 #include "third_party/skia/include/core/SkScalar.h" |
23 #include "third_party/skia/include/core/SkXfermode.h" | 24 #include "third_party/skia/include/core/SkXfermode.h" |
24 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
25 #include "ui/gfx/canvas.h" | 26 #include "ui/gfx/canvas.h" |
26 #include "ui/gfx/geometry/rect.h" | 27 #include "ui/gfx/geometry/rect.h" |
27 #include "ui/gfx/image/canvas_image_source.h" | 28 #include "ui/gfx/image/canvas_image_source.h" |
28 #include "ui/gfx/image/image.h" | 29 #include "ui/gfx/image/image.h" |
29 #include "ui/gfx/image/image_skia_operations.h" | 30 #include "ui/gfx/image/image_skia_operations.h" |
30 #include "ui/gfx/skia_util.h" | 31 #include "ui/gfx/skia_util.h" |
| 32 #include "url/gurl.h" |
| 33 #include "url/url_canon.h" |
31 | 34 |
32 // Helper methods for transforming and drawing avatar icons. | 35 // Helper methods for transforming and drawing avatar icons. |
33 namespace { | 36 namespace { |
34 | 37 |
| 38 // Path format for specifying thumbnail's size. |
| 39 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 40 // Default thumbnail size. |
| 41 const int kDefaultThumbnailSize = 64; |
| 42 // Separator of URL path components. |
| 43 const char kURLPathSeparator = '/'; |
| 44 |
35 // Determine what the scaled height of the avatar icon should be for a | 45 // Determine what the scaled height of the avatar icon should be for a |
36 // specified width, to preserve the aspect ratio. | 46 // specified width, to preserve the aspect ratio. |
37 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { | 47 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { |
38 // Multiply the width by the inverted aspect ratio (height over | 48 // Multiply the width by the inverted aspect ratio (height over |
39 // width), and then add 0.5 to ensure the int truncation rounds nicely. | 49 // width), and then add 0.5 to ensure the int truncation rounds nicely. |
40 int scaled_height = width * | 50 int scaled_height = width * |
41 ((float) avatar.height() / (float) avatar.width()) + 0.5f; | 51 ((float) avatar.height() / (float) avatar.width()) + 0.5f; |
42 return scaled_height; | 52 return scaled_height; |
43 } | 53 } |
44 | 54 |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 if (int_value < 0 || | 391 if (int_value < 0 || |
382 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) | 392 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
383 return false; | 393 return false; |
384 *icon_index = int_value; | 394 *icon_index = int_value; |
385 return true; | 395 return true; |
386 } | 396 } |
387 | 397 |
388 return false; | 398 return false; |
389 } | 399 } |
390 | 400 |
| 401 bool GetImageURLWithThumbnailSize( |
| 402 const GURL& old_url, int size, GURL* new_url) { |
| 403 DCHECK(new_url); |
| 404 std::vector<std::string> components = base::SplitString( |
| 405 old_url.path(), std::string(1, kURLPathSeparator), |
| 406 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 407 if (components.empty()) |
| 408 return false; |
| 409 |
| 410 const std::string& old_path = old_url.path(); |
| 411 std::string default_size_component( |
| 412 base::StringPrintf(kThumbnailSizeFormat, kDefaultThumbnailSize)); |
| 413 std::string new_size_component( |
| 414 base::StringPrintf(kThumbnailSizeFormat, size)); |
| 415 |
| 416 size_t pos = old_path.find(default_size_component); |
| 417 size_t end = std::string::npos; |
| 418 if (pos != std::string::npos) { |
| 419 // The default size is already specified in the URL so it needs to be |
| 420 // replaced with the new size. |
| 421 end = pos + default_size_component.size(); |
| 422 } else { |
| 423 // The default size is not in the URL so try to insert it before the last |
| 424 // component. |
| 425 const std::string& file_name = old_url.ExtractFileName(); |
| 426 if (!file_name.empty()) { |
| 427 pos = old_path.find(file_name); |
| 428 end = pos - 1; |
| 429 } |
| 430 } |
| 431 |
| 432 if (pos != std::string::npos) { |
| 433 std::string new_path = old_path.substr(0, pos) + new_size_component + |
| 434 old_path.substr(end); |
| 435 GURL::Replacements replacement; |
| 436 replacement.SetPathStr(new_path.c_str()); |
| 437 *new_url = old_url.ReplaceComponents(replacement); |
| 438 return new_url->is_valid(); |
| 439 } |
| 440 |
| 441 // We can't set the image size, just use the default size. |
| 442 *new_url = old_url; |
| 443 return true; |
| 444 } |
| 445 |
391 } // namespace profiles | 446 } // namespace profiles |
OLD | NEW |