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