Chromium Code Reviews| Index: chrome/browser/ui/views/passwords/credentials_item_view.cc |
| diff --git a/chrome/browser/ui/views/passwords/credentials_item_view.cc b/chrome/browser/ui/views/passwords/credentials_item_view.cc |
| index 0c4bdbb2318388eccaa5b211754ab31db2f5270f..204a3272ddb8c18be3edef60a38d618c068bc17c 100644 |
| --- a/chrome/browser/ui/views/passwords/credentials_item_view.cc |
| +++ b/chrome/browser/ui/views/passwords/credentials_item_view.cc |
| @@ -4,14 +4,13 @@ |
| #include "chrome/browser/ui/views/passwords/credentials_item_view.h" |
| -#include "chrome/browser/browser_process.h" |
| -#include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| -#include "chrome/browser/profiles/profile_info_cache.h" |
| -#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| #include "grit/theme_resources.h" |
| +#include "net/base/load_flags.h" |
| #include "ui/base/resource/resource_bundle.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/image/image.h" |
| +#include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/path.h" |
| #include "ui/views/border.h" |
| #include "ui/views/controls/image_view.h" |
| @@ -31,6 +30,22 @@ gfx::Size GetTextLabelsSize(const views::Label* full_name_label, |
| full_name.height() + username.height()); |
| } |
| +// Crops and scales |image| to kIconSize |
| +gfx::ImageSkia ScaleImage(gfx::Image image) { |
| + gfx::ImageSkia skia_image = image.AsImageSkia(); |
|
Mike West
2015/01/21 16:51:52
Can we shortcut this by checking whether the image
vasilii
2015/01/21 19:41:23
I've rewritten the code so we use more gfx::ImageS
|
| + gfx::Size size = image.Size(); |
| + if (size.height() != size.width()) { |
| + gfx::Rect target(size); |
| + int side = std::min(size.height(), size.width()); |
| + target.ClampToCenteredSize(gfx::Size(side, side)); |
| + skia_image = gfx::ImageSkiaOperations::ExtractSubset(skia_image, target); |
| + } |
| + return gfx::ImageSkiaOperations::CreateResizedImage( |
| + skia_image, |
| + skia::ImageOperations::RESIZE_BEST, |
| + gfx::Size(kIconSize, kIconSize)); |
| +} |
| + |
| class CircularImageView : public views::ImageView { |
| public: |
| CircularImageView() = default; |
| @@ -56,23 +71,76 @@ void CircularImageView::OnPaint(gfx::Canvas* canvas) { |
| } // namespace |
| -CredentialsItemView::CredentialsItemView(views::ButtonListener* button_listener, |
| - const autofill::PasswordForm& form) |
| +// Helper class to download the avatar. It deletes itself once the request is |
| +// done. |
| +class CredentialsItemView::AvatarFetcher |
| + : public chrome::BitmapFetcherDelegate { |
| + public: |
| + AvatarFetcher(const GURL& url, |
| + const base::WeakPtr<CredentialsItemView>& delegate); |
| + |
| + void Start(net::URLRequestContextGetter* request_context); |
| + |
| + private: |
| + ~AvatarFetcher() override; |
| + |
| + // chrome::BitmapFetcherDelegate: |
| + void OnFetchComplete(const GURL url, const SkBitmap* bitmap) override; |
| + |
| + chrome::BitmapFetcher fetcher_; |
| + base::WeakPtr<CredentialsItemView> delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AvatarFetcher); |
| +}; |
| + |
| +CredentialsItemView::AvatarFetcher::AvatarFetcher( |
| + const GURL& url, |
| + const base::WeakPtr<CredentialsItemView>& delegate) |
| + : fetcher_(url, this), |
| + delegate_(delegate) { |
| +} |
| + |
| +CredentialsItemView::AvatarFetcher::~AvatarFetcher() = default; |
| + |
| +void CredentialsItemView::AvatarFetcher::Start( |
| + net::URLRequestContextGetter* request_context) { |
| + fetcher_.Start(request_context, std::string(), |
| + net::URLRequest::NEVER_CLEAR_REFERRER, |
| + net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES | |
| + net::LOAD_MAYBE_USER_GESTURE); |
| +} |
| + |
| +void CredentialsItemView::AvatarFetcher::OnFetchComplete( |
| + const GURL /*url*/, const SkBitmap* bitmap) { |
| + if (bitmap && delegate_) |
| + delegate_->UpdateAvatar(gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| + |
| + delete this; |
| +} |
| + |
| +CredentialsItemView::CredentialsItemView( |
| + views::ButtonListener* button_listener, |
| + const autofill::PasswordForm& form, |
| + net::URLRequestContextGetter* request_context) |
| : LabelButton(button_listener, base::string16()), |
| - form_(form) { |
| + form_(form), |
| + weak_ptr_factory_(this) { |
| set_notify_enter_exit_on_child(true); |
| // Create an image-view for the avatar. Make sure it ignores events so that |
| // the parent can receive the events instead. |
| image_view_ = new CircularImageView; |
| image_view_->set_interactive(false); |
| - |
| - // TODO(vasilii): temporary code below shows the built-in profile icon instead |
| - // of avatar. |
| - const ProfileInfoCache& cache = |
| - g_browser_process->profile_manager()->GetProfileInfoCache(); |
| - const gfx::Image& image = cache.GetAvatarIconOfProfileAtIndex(0); |
| - image_view_->SetImage(profiles::GetSizedAvatarIcon( |
| - image, true, kIconSize, kIconSize).ToImageSkia()); |
| + gfx::Image image = |
| + ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_26); |
| + image_view_->SetImageSize(gfx::Size(kIconSize, kIconSize)); |
| + image_view_->SetImage(image.ToImageSkia()); |
| + if (form_.avatar_url.is_valid()) { |
| + // Fetch the actual avatar. |
| + AvatarFetcher* fetcher = new AvatarFetcher(form_.avatar_url, |
| + weak_ptr_factory_.GetWeakPtr()); |
| + fetcher->Start(request_context); |
| + } |
| AddChildView(image_view_); |
| // Add a label to show the full name. |
| @@ -135,3 +203,7 @@ void CredentialsItemView::Layout() { |
| username_label_->SetBoundsRect(gfx::Rect(label_origin, username_size)); |
| } |
| } |
| + |
| +void CredentialsItemView::UpdateAvatar(const gfx::Image& image) { |
| + image_view_->SetImage(ScaleImage(image)); |
| +} |