Commit bcfc1b3c authored by dconnelly's avatar dconnelly Committed by Commit bot

Extract some account chooser views things for cross-platform use.

- AvatarFetcher moves out as AccountAvatarFetcher
- Avatar icon size constant moves out as kAvatarImageSize
- Avatar icon scaling moves out as ScaleImageForAccountAvatar

BUG=448011

Review URL: https://codereview.chromium.org/901493003

Cr-Commit-Position: refs/heads/master@{#315316}
parent 3b657a82
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/passwords/account_avatar_fetcher.h"
#include "net/base/load_flags.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
AccountAvatarFetcher::AccountAvatarFetcher(
const GURL& url,
const base::WeakPtr<AccountAvatarFetcherDelegate>& delegate)
: fetcher_(url, this), delegate_(delegate) {
}
AccountAvatarFetcher::~AccountAvatarFetcher() = default;
void AccountAvatarFetcher::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 AccountAvatarFetcher::OnFetchComplete(const GURL /*url*/,
const SkBitmap* bitmap) {
if (bitmap && delegate_)
delegate_->UpdateAvatar(gfx::ImageSkia::CreateFrom1xBitmap(*bitmap));
delete this;
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_PASSWORDS_ACCOUNT_AVATAR_FETCHER_H_
#define CHROME_BROWSER_UI_PASSWORDS_ACCOUNT_AVATAR_FETCHER_H_
#include "base/memory/weak_ptr.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
#include "url/gurl.h"
namespace gfx {
class ImageSkia;
} // namespace gfx
namespace net {
class URLRequestContextGetter;
} // namespace net
class AccountAvatarFetcherDelegate {
public:
virtual void UpdateAvatar(const gfx::ImageSkia& image) = 0;
};
// Helper class to download an avatar. It deletes itself once the request is
// done.
class AccountAvatarFetcher : public chrome::BitmapFetcherDelegate {
public:
AccountAvatarFetcher(
const GURL& url,
const base::WeakPtr<AccountAvatarFetcherDelegate>& delegate);
void Start(net::URLRequestContextGetter* request_context);
private:
~AccountAvatarFetcher() override;
// chrome::BitmapFetcherDelegate:
void OnFetchComplete(const GURL url, const SkBitmap* bitmap) override;
chrome::BitmapFetcher fetcher_;
base::WeakPtr<AccountAvatarFetcherDelegate> delegate_;
DISALLOW_COPY_AND_ASSIGN(AccountAvatarFetcher);
};
#endif // CHROME_BROWSER_UI_PASSWORDS_ACCOUNT_AVATAR_FETCHER_H_
...@@ -8,8 +8,28 @@ ...@@ -8,8 +8,28 @@
#include "components/autofill/core/common/password_form.h" #include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/affiliation_utils.h" #include "components/password_manager/core/browser/affiliation_utils.h"
#include "net/base/net_util.h" #include "net/base/net_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "url/gurl.h" #include "url/gurl.h"
const int kAvatarImageSize = 50;
gfx::ImageSkia ScaleImageForAccountAvatar(gfx::ImageSkia skia_image) {
gfx::Size size = skia_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(kAvatarImageSize, kAvatarImageSize));
}
std::string GetHumanReadableOrigin(const autofill::PasswordForm& password_form, std::string GetHumanReadableOrigin(const autofill::PasswordForm& password_form,
const std::string& languages) { const std::string& languages) {
password_manager::FacetURI facet_uri = password_manager::FacetURI facet_uri =
......
...@@ -9,7 +9,17 @@ ...@@ -9,7 +9,17 @@
namespace autofill { namespace autofill {
struct PasswordForm; struct PasswordForm;
} } // namespace autofill
namespace gfx {
class ImageSkia;
} // namespace gfx
// The desired width and height in pixels for an account avatar.
extern const int kAvatarImageSize;
// Crops and scales |image_skia| to the desired size for an account avatar.
gfx::ImageSkia ScaleImageForAccountAvatar(gfx::ImageSkia image_skia);
// Returns the origin URI in a format which can be presented to a user based of // Returns the origin URI in a format which can be presented to a user based of
// |password_from| field values. For web URIs |languages| is using in order to // |password_from| field values. For web URIs |languages| is using in order to
......
...@@ -4,20 +4,17 @@ ...@@ -4,20 +4,17 @@
#include "chrome/browser/ui/views/passwords/credentials_item_view.h" #include "chrome/browser/ui/views/passwords/credentials_item_view.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "net/base/load_flags.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/path.h" #include "ui/gfx/path.h"
#include "ui/views/border.h" #include "ui/views/border.h"
#include "ui/views/controls/image_view.h" #include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
namespace { namespace {
const int kIconSize = 50;
// The default spacing between the icon and text. // The default spacing between the icon and text.
const int kSpacing = 5; const int kSpacing = 5;
...@@ -30,21 +27,6 @@ gfx::Size GetTextLabelsSize(const views::Label* full_name_label, ...@@ -30,21 +27,6 @@ gfx::Size GetTextLabelsSize(const views::Label* full_name_label,
full_name.height() + username.height()); full_name.height() + username.height());
} }
// Crops and scales |image| to kIconSize
gfx::ImageSkia ScaleImage(gfx::ImageSkia skia_image) {
gfx::Size size = skia_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 { class CircularImageView : public views::ImageView {
public: public:
CircularImageView() = default; CircularImageView() = default;
...@@ -70,54 +52,6 @@ void CircularImageView::OnPaint(gfx::Canvas* canvas) { ...@@ -70,54 +52,6 @@ void CircularImageView::OnPaint(gfx::Canvas* canvas) {
} // namespace } // namespace
// 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::ImageSkia::CreateFrom1xBitmap(*bitmap));
delete this;
}
CredentialsItemView::CredentialsItemView( CredentialsItemView::CredentialsItemView(
views::ButtonListener* button_listener, views::ButtonListener* button_listener,
const autofill::PasswordForm& form, const autofill::PasswordForm& form,
...@@ -134,12 +68,13 @@ CredentialsItemView::CredentialsItemView( ...@@ -134,12 +68,13 @@ CredentialsItemView::CredentialsItemView(
image_view_->set_interactive(false); image_view_->set_interactive(false);
gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed( gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE); IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE);
DCHECK(image.Width() >= kIconSize && image.Height() >= kIconSize); DCHECK(image.Width() >= kAvatarImageSize &&
image.Height() >= kAvatarImageSize);
UpdateAvatar(image.AsImageSkia()); UpdateAvatar(image.AsImageSkia());
if (form_.avatar_url.is_valid()) { if (form_.avatar_url.is_valid()) {
// Fetch the actual avatar. // Fetch the actual avatar.
AvatarFetcher* fetcher = new AvatarFetcher(form_.avatar_url, AccountAvatarFetcher* fetcher = new AccountAvatarFetcher(
weak_ptr_factory_.GetWeakPtr()); form_.avatar_url, weak_ptr_factory_.GetWeakPtr());
fetcher->Start(request_context); fetcher->Start(request_context);
} }
AddChildView(image_view_); AddChildView(image_view_);
...@@ -166,8 +101,8 @@ CredentialsItemView::~CredentialsItemView() = default; ...@@ -166,8 +101,8 @@ CredentialsItemView::~CredentialsItemView() = default;
gfx::Size CredentialsItemView::GetPreferredSize() const { gfx::Size CredentialsItemView::GetPreferredSize() const {
gfx::Size labels_size = GetTextLabelsSize(full_name_label_, username_label_); gfx::Size labels_size = GetTextLabelsSize(full_name_label_, username_label_);
gfx::Size size = gfx::Size(kIconSize + labels_size.width(), gfx::Size size = gfx::Size(kAvatarImageSize + labels_size.width(),
std::max(kIconSize, labels_size.height())); std::max(kAvatarImageSize, labels_size.height()));
const gfx::Insets insets(GetInsets()); const gfx::Insets insets(GetInsets());
size.Enlarge(insets.width(), insets.height()); size.Enlarge(insets.width(), insets.height());
size.Enlarge(kSpacing, 0); size.Enlarge(kSpacing, 0);
...@@ -206,5 +141,5 @@ void CredentialsItemView::Layout() { ...@@ -206,5 +141,5 @@ void CredentialsItemView::Layout() {
} }
void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) { void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) {
image_view_->SetImage(ScaleImage(image)); image_view_->SetImage(ScaleImageForAccountAvatar(image));
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/passwords/account_avatar_fetcher.h"
#include "components/autofill/core/common/password_form.h" #include "components/autofill/core/common/password_form.h"
#include "components/password_manager/content/common/credential_manager_types.h" #include "components/password_manager/content/common/credential_manager_types.h"
#include "ui/views/controls/button/label_button.h" #include "ui/views/controls/button/label_button.h"
...@@ -26,7 +27,8 @@ class Label; ...@@ -26,7 +27,8 @@ class Label;
// CredentialsItemView represents a credential view in the account chooser // CredentialsItemView represents a credential view in the account chooser
// bubble. // bubble.
class CredentialsItemView : public views::LabelButton { class CredentialsItemView : public AccountAvatarFetcherDelegate,
public views::LabelButton {
public: public:
CredentialsItemView(views::ButtonListener* button_listener, CredentialsItemView(views::ButtonListener* button_listener,
const autofill::PasswordForm& form, const autofill::PasswordForm& form,
...@@ -39,16 +41,15 @@ class CredentialsItemView : public views::LabelButton { ...@@ -39,16 +41,15 @@ class CredentialsItemView : public views::LabelButton {
return credential_type_; return credential_type_;
} }
private: // AccountAvatarFetcherDelegate:
class AvatarFetcher; void UpdateAvatar(const gfx::ImageSkia& image) override;
private:
// views::LabelButton: // views::LabelButton:
gfx::Size GetPreferredSize() const override; gfx::Size GetPreferredSize() const override;
int GetHeightForWidth(int w) const override; int GetHeightForWidth(int w) const override;
void Layout() override; void Layout() override;
void UpdateAvatar(const gfx::ImageSkia& image);
autofill::PasswordForm form_; autofill::PasswordForm form_;
password_manager::CredentialType credential_type_; password_manager::CredentialType credential_type_;
......
...@@ -790,6 +790,8 @@ ...@@ -790,6 +790,8 @@
'browser/ui/navigation_correction_tab_observer.h', 'browser/ui/navigation_correction_tab_observer.h',
'browser/ui/metro_pin_tab_helper_win.cc', 'browser/ui/metro_pin_tab_helper_win.cc',
'browser/ui/metro_pin_tab_helper_win.h', 'browser/ui/metro_pin_tab_helper_win.h',
'browser/ui/passwords/account_avatar_fetcher.cc',
'browser/ui/passwords/account_avatar_fetcher.h',
'browser/ui/passwords/manage_passwords_bubble.cc', 'browser/ui/passwords/manage_passwords_bubble.cc',
'browser/ui/passwords/manage_passwords_bubble.h', 'browser/ui/passwords/manage_passwords_bubble.h',
'browser/ui/passwords/manage_passwords_bubble_model.cc', 'browser/ui/passwords/manage_passwords_bubble_model.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment