Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/ui/views/passwords/credentials_item_view.cc

Issue 854173003: Credential Management API: the account chooser dialog supports custom avatars. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ui/views/passwords/credentials_item_view.h" 5 #include "chrome/browser/ui/views/passwords/credentials_item_view.h"
6 6
7 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
8 #include "chrome/browser/profiles/profile_avatar_icon_util.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_info_cache.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "grit/theme_resources.h" 9 #include "grit/theme_resources.h"
10 #include "net/base/load_flags.h"
12 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/image/image.h" 13 #include "ui/gfx/image/image.h"
15 #include "ui/gfx/path.h" 14 #include "ui/gfx/path.h"
16 #include "ui/views/border.h" 15 #include "ui/views/border.h"
17 #include "ui/views/controls/image_view.h" 16 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h" 17 #include "ui/views/controls/label.h"
19 18
20 namespace { 19 namespace {
21 const int kIconSize = 50; 20 const int kIconSize = 50;
(...skipping 27 matching lines...) Expand all
49 circular_mask.addCircle( 48 circular_mask.addCircle(
50 SkIntToScalar(bounds.x() + bounds.right()) / 2, 49 SkIntToScalar(bounds.x() + bounds.right()) / 2,
51 SkIntToScalar(bounds.y() + bounds.bottom()) / 2, 50 SkIntToScalar(bounds.y() + bounds.bottom()) / 2,
52 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2); 51 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2);
53 canvas->ClipPath(circular_mask, true); 52 canvas->ClipPath(circular_mask, true);
54 ImageView::OnPaint(canvas); 53 ImageView::OnPaint(canvas);
55 } 54 }
56 55
57 } // namespace 56 } // namespace
58 57
58 // Helper class to download the avatar. It deletes itself once the request is
59 // done.
60 class CredentialsItemView::AvatarFetcher
61 : public chrome::BitmapFetcherDelegate {
62 public:
63 AvatarFetcher(const GURL& url,
64 const base::WeakPtr<CredentialsItemView>& delegate);
65
66 void Start(net::URLRequestContextGetter* request_context);
Mike West 2015/01/20 17:48:55 Nit: Newline after.
vasilii 2015/01/20 19:43:03 Done.
67 private:
68 ~AvatarFetcher() override;
69
70 // chrome::BitmapFetcherDelegate:
71 void OnFetchComplete(const GURL url, const SkBitmap* bitmap) override;
72
73 chrome::BitmapFetcher fetcher_;
74 base::WeakPtr<CredentialsItemView> delegate_;
75
76 DISALLOW_COPY_AND_ASSIGN(AvatarFetcher);
77 };
78
79 CredentialsItemView::AvatarFetcher::AvatarFetcher(
80 const GURL& url,
81 const base::WeakPtr<CredentialsItemView>& delegate)
82 : fetcher_(url, this),
83 delegate_(delegate) {
84 }
85
86 CredentialsItemView::AvatarFetcher::~AvatarFetcher() = default;
87
88 void CredentialsItemView::AvatarFetcher::Start(
89 net::URLRequestContextGetter* request_context) {
90 fetcher_.Start(request_context, std::string(),
91 net::URLRequest::NEVER_CLEAR_REFERRER,
92 net::LOAD_DO_NOT_SEND_COOKIES |
93 net::LOAD_DO_NOT_SAVE_COOKIES |
94 net::LOAD_MAYBE_USER_GESTURE);
95 }
96
97 void CredentialsItemView::AvatarFetcher::OnFetchComplete(
98 const GURL /*url*/, const SkBitmap* bitmap) {
99 if (bitmap && delegate_)
100 delegate_->UpdateAvatar(gfx::Image::CreateFrom1xBitmap(*bitmap));
Mike West 2015/01/20 17:48:55 We'll probably need to do some validation here (cr
vasilii 2015/01/20 19:43:03 Currently ImageView resizes it for us. Though the
101
102 delete this;
103 }
104
59 CredentialsItemView::CredentialsItemView(views::ButtonListener* button_listener, 105 CredentialsItemView::CredentialsItemView(views::ButtonListener* button_listener,
60 const autofill::PasswordForm& form) 106 const autofill::PasswordForm& form,
107 Profile* profile)
61 : LabelButton(button_listener, base::string16()), 108 : LabelButton(button_listener, base::string16()),
62 form_(form) { 109 form_(form),
110 weak_ptr_factory_(this) {
63 set_notify_enter_exit_on_child(true); 111 set_notify_enter_exit_on_child(true);
64 // Create an image-view for the avatar. Make sure it ignores events so that 112 // Create an image-view for the avatar. Make sure it ignores events so that
65 // the parent can receive the events instead. 113 // the parent can receive the events instead.
66 image_view_ = new CircularImageView; 114 image_view_ = new CircularImageView;
67 image_view_->set_interactive(false); 115 image_view_->set_interactive(false);
68 116 gfx::Image image =
69 // TODO(vasilii): temporary code below shows the built-in profile icon instead 117 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_26);
Mike West 2015/01/20 17:48:55 This isn't the same image you were displaying befo
vasilii 2015/01/20 19:43:03 It's profile_avatar_placeholder.png. Have a look a
70 // of avatar. 118 image_view_->SetImageSize(gfx::Size(kIconSize, kIconSize));
Mike West 2015/01/20 17:48:55 Why do you need to set the image size here, when y
vasilii 2015/01/20 19:43:03 With this new line ImageView resizes the image aut
71 const ProfileInfoCache& cache = 119 image_view_->SetImage(image.ToImageSkia());
72 g_browser_process->profile_manager()->GetProfileInfoCache(); 120 if (form_.avatar_url.is_valid()) {
73 const gfx::Image& image = cache.GetAvatarIconOfProfileAtIndex(0); 121 // Fetch the actual avatar.
74 image_view_->SetImage(profiles::GetSizedAvatarIcon( 122 AvatarFetcher* fetcher = new AvatarFetcher(form_.avatar_url,
75 image, true, kIconSize, kIconSize).ToImageSkia()); 123 weak_ptr_factory_.GetWeakPtr());
124 fetcher->Start(profile->GetRequestContext());
125 }
76 AddChildView(image_view_); 126 AddChildView(image_view_);
77 127
78 // Add a label to show the full name. 128 // Add a label to show the full name.
79 // TODO(vasilii): temporarily the label shows username. 129 // TODO(vasilii): temporarily the label shows username.
80 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); 130 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
81 full_name_label_ = new views::Label( 131 full_name_label_ = new views::Label(
82 form_.username_value, rb->GetFontList(ui::ResourceBundle::BoldFont)); 132 form_.username_value, rb->GetFontList(ui::ResourceBundle::BoldFont));
83 full_name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 133 full_name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
84 AddChildView(full_name_label_); 134 AddChildView(full_name_label_);
85 135
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 int y_offset = (child_area.height() - 178 int y_offset = (child_area.height() -
129 (full_name_size.height() + username_size.height())) / 2; 179 (full_name_size.height() + username_size.height())) / 2;
130 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing, 180 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing,
131 child_area.origin().y() + y_offset); 181 child_area.origin().y() + y_offset);
132 full_name_label_->SetBoundsRect(gfx::Rect(label_origin, full_name_size)); 182 full_name_label_->SetBoundsRect(gfx::Rect(label_origin, full_name_size));
133 if (username_label_) { 183 if (username_label_) {
134 label_origin.Offset(0, full_name_size.height()); 184 label_origin.Offset(0, full_name_size.height());
135 username_label_->SetBoundsRect(gfx::Rect(label_origin, username_size)); 185 username_label_->SetBoundsRect(gfx::Rect(label_origin, username_size));
136 } 186 }
137 } 187 }
188
189 void CredentialsItemView::UpdateAvatar(const gfx::Image& image) {
190 image_view_->SetImage(image.ToImageSkia());
Mike West 2015/01/20 17:48:55 This flow means that we'll be requesting the image
vasilii 2015/01/20 19:43:03 net::URLFetcher (used by chrome::BitmapFetcher) is
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698