1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/password_generation_bubble_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/password_manager/password_manager.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_list.h"
11 #include "chrome/common/url_constants.h"
12 #include "components/autofill/content/common/autofill_messages.h"
13 #include "components/autofill/core/browser/password_generator.h"
14 #include "components/autofill/core/common/password_generation_util.h"
15 #include "content/public/browser/page_navigator.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h"
19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/base/theme_provider.h"
23 #include "ui/gfx/canvas.h"
24 #include "ui/views/border.h"
25 #include "ui/views/controls/button/image_button.h"
26 #include "ui/views/controls/button/label_button.h"
27 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/link.h"
29 #include "ui/views/controls/textfield/textfield.h"
30 #include "ui/views/layout/layout_constants.h"
34 // Constants for PasswordGenerationBubbleView.
35 const int kBubbleMargin
= 9;
36 const int kButtonHorizontalSpacing
= 4;
37 const int kButtonWidth
= 65;
38 const int kDefaultTextFieldChars
= 18;
39 const int kTitleLabelVerticalOffset
= -1;
40 const int kVerticalPadding
= 8;
42 // Constants for Text fieldWrapper.
43 const int kTextfieldHorizontalPadding
= 2;
44 const int kTextfieldVerticalPadding
= 3;
45 const int kWrapperBorderSize
= 1;
47 // This class handles layout so that it looks like a Textfield and ImageButton
48 // are part of one logical textfield with the button on the right side of the
49 // field. It also assumes that the textfield is already sized appropriately
50 // and will alter the image size to fit.
51 class TextfieldWrapper
: public views::View
{
53 TextfieldWrapper(views::Textfield
* textfield
,
54 views::ImageButton
* image_button
);
55 virtual ~TextfieldWrapper();
57 virtual void Layout() OVERRIDE
;
58 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
61 gfx::Size
GetImageSize() const;
63 views::Textfield
* textfield_
;
64 views::ImageButton
* image_button_
;
67 TextfieldWrapper::TextfieldWrapper(views::Textfield
* textfield
,
68 views::ImageButton
* image_button
)
69 : textfield_(textfield
),
70 image_button_(image_button
) {
71 set_border(views::Border::CreateSolidBorder(kWrapperBorderSize
,
74 AddChildView(textfield_
);
75 AddChildView(image_button
);
78 TextfieldWrapper::~TextfieldWrapper() {}
80 void TextfieldWrapper::Layout() {
81 // Add some spacing between the textfield and the border.
82 textfield_
->SetPosition(gfx::Point(kTextfieldHorizontalPadding
,
83 kTextfieldVerticalPadding
));
84 textfield_
->SizeToPreferredSize();
86 // Button should be offset one pixel from the end of the textfield so that
87 // there is no overlap. It is also displaced down by the size of the border
88 // so it doesn't overlap with it either.
89 int button_x
= (textfield_
->GetPreferredSize().width() +
90 kTextfieldHorizontalPadding
+ 1);
91 image_button_
->SetPosition(gfx::Point(button_x
,
94 // Make sure that the image is centered after cropping.
95 image_button_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
96 views::ImageButton::ALIGN_MIDDLE
);
98 image_button_
->SetSize(GetImageSize());
101 gfx::Size
TextfieldWrapper::GetPreferredSize() {
102 int width
= (textfield_
->GetPreferredSize().width() +
103 GetImageSize().width() +
104 kTextfieldHorizontalPadding
* 3);
105 int height
= (textfield_
->GetPreferredSize().height() +
106 kTextfieldVerticalPadding
* 2);
108 return gfx::Size(width
, height
);
111 gfx::Size
TextfieldWrapper::GetImageSize() const {
112 // The image is sized so that it fills the space between the borders
114 int size
= (textfield_
->GetPreferredSize().height() +
115 (kTextfieldVerticalPadding
- kWrapperBorderSize
) * 2);
116 return gfx::Size(size
, size
);
120 PasswordGenerationBubbleView::PasswordGenerationBubbleView(
121 const autofill::PasswordForm
& form
,
122 const gfx::Rect
& anchor_rect
,
123 views::View
* anchor_view
,
124 content::RenderViewHost
* render_view_host
,
125 PasswordManager
* password_manager
,
126 autofill::PasswordGenerator
* password_generator
,
127 content::PageNavigator
* navigator
,
128 ui::ThemeProvider
* theme_provider
)
129 : BubbleDelegateView(anchor_view
, views::BubbleBorder::TOP_LEFT
),
131 accept_button_(NULL
),
133 regenerate_button_(NULL
),
134 textfield_wrapper_(NULL
),
136 anchor_rect_(anchor_rect
),
137 render_view_host_(render_view_host
),
138 password_manager_(password_manager
),
139 password_generator_(password_generator
),
140 navigator_(navigator
),
141 theme_provider_(theme_provider
) {}
143 PasswordGenerationBubbleView::~PasswordGenerationBubbleView() {}
145 void PasswordGenerationBubbleView::Init() {
146 set_margins(gfx::Insets(kBubbleMargin
, kBubbleMargin
,
147 kBubbleMargin
, kBubbleMargin
));
149 // TODO(gcasto): Localize text after we have finalized the UI.
151 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
152 title_label_
= new views::Label(
153 l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_BUBBLE_TITLE
),
154 rb
.GetFontList(ui::ResourceBundle::MediumFont
));
155 AddChildView(title_label_
);
157 regenerate_button_
= new views::ImageButton(this);
158 regenerate_button_
->SetImage(
159 views::CustomButton::STATE_NORMAL
,
160 theme_provider_
->GetImageSkiaNamed(IDR_RELOAD_DIMMED
));
161 regenerate_button_
->SetImage(
162 views::CustomButton::STATE_HOVERED
,
163 theme_provider_
->GetImageSkiaNamed(IDR_RELOAD
));
164 regenerate_button_
->SetImage(
165 views::CustomButton::STATE_PRESSED
,
166 theme_provider_
->GetImageSkiaNamed(IDR_RELOAD
));
168 textfield_
= new views::Textfield();
169 textfield_
->set_default_width_in_chars(kDefaultTextFieldChars
);
170 textfield_
->SetText(base::ASCIIToUTF16(password_generator_
->Generate()));
171 textfield_
->set_controller(this);
173 textfield_wrapper_
= new TextfieldWrapper(textfield_
,
175 AddChildView(textfield_wrapper_
);
177 accept_button_
= new views::LabelButton(
179 l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_BUTTON_TEXT
));
180 accept_button_
->SetStyle(views::Button::STYLE_BUTTON
);
181 AddChildView(accept_button_
);
184 void PasswordGenerationBubbleView::Layout() {
185 // We have the title label shifted up to make the borders look more uniform.
186 title_label_
->SetPosition(gfx::Point(0, kTitleLabelVerticalOffset
));
187 title_label_
->SizeToPreferredSize();
189 int y
= title_label_
->GetPreferredSize().height() + kVerticalPadding
;
191 textfield_wrapper_
->SetPosition(gfx::Point(0, y
));
192 textfield_wrapper_
->SizeToPreferredSize();
194 int button_x
= (textfield_wrapper_
->GetPreferredSize().width() +
195 kButtonHorizontalSpacing
);
196 accept_button_
->SetBounds(
198 y
- kWrapperBorderSize
,
200 textfield_wrapper_
->GetPreferredSize().height() + kWrapperBorderSize
* 2);
203 gfx::Size
PasswordGenerationBubbleView::GetPreferredSize() {
204 int width
= (textfield_wrapper_
->GetPreferredSize().width() +
205 kButtonHorizontalSpacing
+
207 int height
= (title_label_
->GetPreferredSize().height() +
208 textfield_wrapper_
->GetPreferredSize().height() +
210 return gfx::Size(width
, height
);
213 gfx::Rect
PasswordGenerationBubbleView::GetAnchorRect() {
217 void PasswordGenerationBubbleView::ButtonPressed(views::Button
* sender
,
218 const ui::Event
& event
) {
219 if (sender
== accept_button_
) {
220 render_view_host_
->Send(new AutofillMsg_GeneratedPasswordAccepted(
221 render_view_host_
->GetRoutingID(), textfield_
->text()));
222 password_manager_
->SetFormHasGeneratedPassword(form_
);
223 actions_
.password_accepted
= true;
226 if (sender
== regenerate_button_
) {
228 base::ASCIIToUTF16(password_generator_
->Generate()));
229 actions_
.password_regenerated
= true;
233 void PasswordGenerationBubbleView::ContentsChanged(
234 views::Textfield
* sender
,
235 const base::string16
& contents
) {
236 actions_
.password_edited
= true;
239 bool PasswordGenerationBubbleView::HandleKeyEvent(
240 views::Textfield
* sender
,
241 const ui::KeyEvent
& key_event
) {
245 views::View
* PasswordGenerationBubbleView::GetInitiallyFocusedView() {
249 void PasswordGenerationBubbleView::WindowClosing() {
250 autofill::password_generation::LogUserActions(actions_
);