| 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" |
| 30 | 32 |
| 31 // Helper methods for transforming and drawing avatar icons. | 33 // Helper methods for transforming and drawing avatar icons. |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 36 // Path format for specifying thumbnail's size. |
| 37 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 38 // Default thumbnail size. |
| 39 const int kDefaultThumbnailSize = 64; |
| 40 // Separator of URL path components. |
| 41 const char kURLPathSeparator = '/'; |
| 42 |
| 34 // Determine what the scaled height of the avatar icon should be for a | 43 // Determine what the scaled height of the avatar icon should be for a |
| 35 // specified width, to preserve the aspect ratio. | 44 // specified width, to preserve the aspect ratio. |
| 36 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { | 45 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { |
| 37 // Multiply the width by the inverted aspect ratio (height over | 46 // Multiply the width by the inverted aspect ratio (height over |
| 38 // width), and then add 0.5 to ensure the int truncation rounds nicely. | 47 // width), and then add 0.5 to ensure the int truncation rounds nicely. |
| 39 int scaled_height = width * | 48 int scaled_height = width * |
| 40 ((float) avatar.height() / (float) avatar.width()) + 0.5f; | 49 ((float) avatar.height() / (float) avatar.width()) + 0.5f; |
| 41 return scaled_height; | 50 return scaled_height; |
| 42 } | 51 } |
| 43 | 52 |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 if (int_value < 0 || | 389 if (int_value < 0 || |
| 381 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) | 390 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
| 382 return false; | 391 return false; |
| 383 *icon_index = int_value; | 392 *icon_index = int_value; |
| 384 return true; | 393 return true; |
| 385 } | 394 } |
| 386 | 395 |
| 387 return false; | 396 return false; |
| 388 } | 397 } |
| 389 | 398 |
| 399 bool GetImageURLWithSize(const GURL& old_url, int size, GURL* new_url) { |
| 400 DCHECK(new_url); |
| 401 std::vector<std::string> components = base::SplitString( |
| 402 old_url.path(), std::string(1, kURLPathSeparator), |
| 403 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 404 if (components.size() == 0) |
| 405 return false; |
| 406 |
| 407 const std::string& old_spec = old_url.spec(); |
| 408 std::string default_size_component( |
| 409 base::StringPrintf(kThumbnailSizeFormat, kDefaultThumbnailSize)); |
| 410 std::string new_size_component( |
| 411 base::StringPrintf(kThumbnailSizeFormat, size)); |
| 412 |
| 413 size_t pos = old_spec.find(default_size_component); |
| 414 size_t end = std::string::npos; |
| 415 if (pos != std::string::npos) { |
| 416 // The default size is already specified in the URL so it needs to be |
| 417 // replaced with the new size. |
| 418 end = pos + default_size_component.size(); |
| 419 } else { |
| 420 // The default size is not in the URL so try to insert it before the last |
| 421 // component. |
| 422 const std::string& file_name = old_url.ExtractFileName(); |
| 423 if (!file_name.empty()) { |
| 424 pos = old_spec.find(file_name); |
| 425 end = pos - 1; |
| 426 } |
| 427 } |
| 428 |
| 429 if (pos != std::string::npos) { |
| 430 std::string new_spec = old_spec.substr(0, pos) + new_size_component + |
| 431 old_spec.substr(end); |
| 432 *new_url = GURL(new_spec); |
| 433 return new_url->is_valid(); |
| 434 } |
| 435 |
| 436 // We can't set the image size, just use the default size. |
| 437 *new_url = old_url; |
| 438 return true; |
| 439 } |
| 440 |
| 390 } // namespace profiles | 441 } // namespace profiles |
| OLD | NEW |