Chromium Code Reviews| Index: chrome/browser/profiles/profile_avatar_icon_util.cc |
| diff --git a/chrome/browser/profiles/profile_avatar_icon_util.cc b/chrome/browser/profiles/profile_avatar_icon_util.cc |
| index 7839fbc978393c90a59dc77e6faa955e10098cfa..9b5ee92c90a9e16ffca1b8e883a7e30cad1afbf2 100644 |
| --- a/chrome/browser/profiles/profile_avatar_icon_util.cc |
| +++ b/chrome/browser/profiles/profile_avatar_icon_util.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/path_service.h" |
| #include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| #include "base/strings/stringprintf.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/profiles/profile_info_cache.h" |
| @@ -27,10 +28,18 @@ |
| #include "ui/gfx/image/image.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/skia_util.h" |
| +#include "url/gurl.h" |
| // Helper methods for transforming and drawing avatar icons. |
| namespace { |
| +// Path format for specifying thumbnail's size. |
| +const char kThumbnailSizeFormat[] = "s%d-c"; |
| +// Default thumbnail size. |
| +const int kDefaultThumbnailSize = 64; |
| +// Separator of URL path components. |
| +const char kURLPathSeparator = '/'; |
| + |
| // Determine what the scaled height of the avatar icon should be for a |
| // specified width, to preserve the aspect ratio. |
| int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { |
| @@ -387,4 +396,46 @@ bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { |
| return false; |
| } |
| +bool GetImageURLWithSize(const GURL& old_url, int size, GURL* new_url) { |
|
sky
2015/12/04 00:29:20
I realize you're just moving code here, but I have
anthonyvd
2015/12/04 22:42:06
Sure, I've added some test coverage. Might as well
|
| + DCHECK(new_url); |
| + std::vector<std::string> components = base::SplitString( |
| + old_url.path(), std::string(1, kURLPathSeparator), |
| + base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + if (components.size() == 0) |
|
sky
2015/12/04 00:29:20
empty
anthonyvd
2015/12/04 22:42:06
Done.
|
| + return false; |
| + |
| + const std::string& old_spec = old_url.spec(); |
| + std::string default_size_component( |
| + base::StringPrintf(kThumbnailSizeFormat, kDefaultThumbnailSize)); |
| + std::string new_size_component( |
| + base::StringPrintf(kThumbnailSizeFormat, size)); |
| + |
| + size_t pos = old_spec.find(default_size_component); |
|
sky
2015/12/04 00:29:20
Seems like you only care about the path component.
anthonyvd
2015/12/04 22:42:06
Done.
|
| + size_t end = std::string::npos; |
| + if (pos != std::string::npos) { |
| + // The default size is already specified in the URL so it needs to be |
| + // replaced with the new size. |
| + end = pos + default_size_component.size(); |
| + } else { |
| + // The default size is not in the URL so try to insert it before the last |
| + // component. |
| + const std::string& file_name = old_url.ExtractFileName(); |
| + if (!file_name.empty()) { |
| + pos = old_spec.find(file_name); |
| + end = pos - 1; |
| + } |
| + } |
| + |
| + if (pos != std::string::npos) { |
| + std::string new_spec = old_spec.substr(0, pos) + new_size_component + |
| + old_spec.substr(end); |
| + *new_url = GURL(new_spec); |
| + return new_url->is_valid(); |
| + } |
| + |
| + // We can't set the image size, just use the default size. |
| + *new_url = old_url; |
| + return true; |
| +} |
| + |
| } // namespace profiles |