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

Side by Side Diff: chrome/browser/ui/autofill/password_generation_popup_controller_impl.cc

Issue 147533005: [Password Generation] Update UI to match final mocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@replace_password_generation_ui
Patch Set: Created 6 years, 10 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/autofill/password_generation_popup_controller_impl.h " 5 #include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h "
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/strings/utf_string_conversion_utils.h" 9 #include "base/strings/utf_string_conversion_utils.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/password_manager/password_manager.h" 11 #include "chrome/browser/password_manager/password_manager.h"
12 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" 12 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h"
13 #include "chrome/browser/ui/autofill/password_generation_popup_view.h" 13 #include "chrome/browser/ui/autofill/password_generation_popup_view.h"
14 #include "chrome/browser/ui/autofill/popup_constants.h" 14 #include "chrome/browser/ui/autofill/popup_constants.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h" 16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
18 #include "components/autofill/content/common/autofill_messages.h" 18 #include "components/autofill/content/common/autofill_messages.h"
19 #include "components/autofill/core/browser/password_generator.h" 19 #include "components/autofill/core/browser/password_generator.h"
20 #include "content/public/browser/native_web_keyboard_event.h" 20 #include "content/public/browser/native_web_keyboard_event.h"
21 #include "content/public/browser/render_view_host.h" 21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
23 #include "grit/generated_resources.h" 23 #include "grit/generated_resources.h"
24 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/events/keycodes/keyboard_codes.h" 26 #include "ui/events/keycodes/keyboard_codes.h"
26 #include "ui/gfx/rect_conversions.h" 27 #include "ui/gfx/rect_conversions.h"
28 #include "ui/gfx/text_utils.h"
27 29
28 namespace autofill { 30 namespace autofill {
29 31
30 const int kMinimumWidth = 60; 32 // All measurements in pixels
33 const int kMinimumWidth = 300;
31 const int kDividerHeight = 1; 34 const int kDividerHeight = 1;
32 35
33 base::WeakPtr<PasswordGenerationPopupControllerImpl> 36 base::WeakPtr<PasswordGenerationPopupControllerImpl>
34 PasswordGenerationPopupControllerImpl::GetOrCreate( 37 PasswordGenerationPopupControllerImpl::GetOrCreate(
35 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous, 38 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous,
36 const gfx::RectF& bounds, 39 const gfx::RectF& bounds,
37 const PasswordForm& form, 40 PasswordForm* form,
38 PasswordGenerator* generator, 41 PasswordGenerator* generator,
39 PasswordManager* password_manager, 42 PasswordManager* password_manager,
40 PasswordGenerationPopupObserver* observer, 43 PasswordGenerationPopupObserver* observer,
41 content::WebContents* web_contents, 44 content::WebContents* web_contents,
42 gfx::NativeView container_view) { 45 gfx::NativeView container_view) {
43 if (previous.get() && 46 if (previous.get() &&
44 previous->element_bounds() == bounds && 47 previous->element_bounds() == bounds &&
45 previous->web_contents() == web_contents && 48 previous->web_contents() == web_contents &&
46 previous->container_view() == container_view) { 49 previous->container_view() == container_view) {
47 // TODO(gcasto): Should we clear state here? 50 previous->set_form(form);
51 previous->set_generator(generator);
52 previous->set_password_manager(password_manager);
48 return previous; 53 return previous;
49 } 54 }
50 55
51 if (previous.get()) 56 if (previous.get())
52 previous->Hide(); 57 previous->Hide();
53 58
54 PasswordGenerationPopupControllerImpl* controller = 59 PasswordGenerationPopupControllerImpl* controller =
55 new PasswordGenerationPopupControllerImpl( 60 new PasswordGenerationPopupControllerImpl(
56 bounds, 61 bounds,
57 form, 62 form,
58 generator, 63 generator,
59 password_manager, 64 password_manager,
60 observer, 65 observer,
61 web_contents, 66 web_contents,
62 container_view); 67 container_view);
63 return controller->GetWeakPtr(); 68 return controller->GetWeakPtr();
64 } 69 }
65 70
66 PasswordGenerationPopupControllerImpl::PasswordGenerationPopupControllerImpl( 71 PasswordGenerationPopupControllerImpl::PasswordGenerationPopupControllerImpl(
67 const gfx::RectF& bounds, 72 const gfx::RectF& bounds,
68 const PasswordForm& form, 73 PasswordForm* form,
69 PasswordGenerator* generator, 74 PasswordGenerator* generator,
70 PasswordManager* password_manager, 75 PasswordManager* password_manager,
71 PasswordGenerationPopupObserver* observer, 76 PasswordGenerationPopupObserver* observer,
72 content::WebContents* web_contents, 77 content::WebContents* web_contents,
73 gfx::NativeView container_view) 78 gfx::NativeView container_view)
74 : form_(form), 79 : form_(form),
75 generator_(generator), 80 generator_(generator),
76 password_manager_(password_manager), 81 password_manager_(password_manager),
77 observer_(observer), 82 observer_(observer),
78 controller_common_(bounds, container_view, web_contents), 83 controller_common_(bounds, container_view, web_contents),
79 view_(NULL), 84 view_(NULL),
80 current_password_(base::ASCIIToUTF16(generator->Generate())), 85 font_list_(ResourceBundle::GetSharedInstance().GetFontList(
86 ResourceBundle::SmallFont)),
81 password_selected_(false), 87 password_selected_(false),
88 display_password_(false),
82 weak_ptr_factory_(this) { 89 weak_ptr_factory_(this) {
83 controller_common_.SetKeyPressCallback( 90 controller_common_.SetKeyPressCallback(
84 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, 91 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent,
85 base::Unretained(this))); 92 base::Unretained(this)));
86 } 93 }
87 94
88 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl() 95 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl()
89 {} 96 {}
90 97
91 base::WeakPtr<PasswordGenerationPopupControllerImpl> 98 base::WeakPtr<PasswordGenerationPopupControllerImpl>
(...skipping 24 matching lines...) Expand all
116 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() { 123 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() {
117 if (password_selected_) { 124 if (password_selected_) {
118 PasswordAccepted(); // This will delete |this|. 125 PasswordAccepted(); // This will delete |this|.
119 return true; 126 return true;
120 } 127 }
121 128
122 return false; 129 return false;
123 } 130 }
124 131
125 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) { 132 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) {
133 if (!display_password_)
134 return;
135
126 password_selected_ = selected; 136 password_selected_ = selected;
137 view_->PasswordSelectionUpdated();
127 view_->UpdateBoundsAndRedrawPopup(); 138 view_->UpdateBoundsAndRedrawPopup();
128 } 139 }
129 140
130 void PasswordGenerationPopupControllerImpl::PasswordAccepted() { 141 void PasswordGenerationPopupControllerImpl::PasswordAccepted() {
142 if (!display_password_)
143 return;
144
131 web_contents()->GetRenderViewHost()->Send( 145 web_contents()->GetRenderViewHost()->Send(
132 new AutofillMsg_GeneratedPasswordAccepted( 146 new AutofillMsg_GeneratedPasswordAccepted(
133 web_contents()->GetRenderViewHost()->GetRoutingID(), 147 web_contents()->GetRenderViewHost()->GetRoutingID(),
134 current_password_)); 148 current_password_));
135 password_manager_->SetFormHasGeneratedPassword(form_); 149 password_manager_->SetFormHasGeneratedPassword(*form_);
136 Hide(); 150 Hide();
137 } 151 }
138 152
139 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { 153 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() {
140 // Minimum width we want to display the password.
141 int minimum_length_for_text = 2 * kHorizontalPadding +
142 font_.GetExpectedTextWidth(kMinimumWidth) +
143 2 * kPopupBorderThickness;
144
145 // If the width of the field is longer than the minimum, use that instead. 154 // If the width of the field is longer than the minimum, use that instead.
146 return std::max(minimum_length_for_text, 155 return std::max(kMinimumWidth,
147 controller_common_.RoundedElementBounds().width()); 156 controller_common_.RoundedElementBounds().width());
148 } 157 }
149 158
150 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { 159 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) {
151 // Note that this wrapping isn't exactly what the popup will do. It shouldn't 160 // Note that this wrapping isn't exactly what the popup will do. It shouldn't
152 // line break in the middle of the link, but as long as the link isn't longer 161 // line break in the middle of the link, but as long as the link isn't longer
153 // than given width this shouldn't affect the height calculated here. The 162 // than given width this shouldn't affect the height calculated here. The
154 // default width should be wide enough to prevent this from being an issue. 163 // default width should be wide enough to prevent this from being an issue.
155 int total_length = font_.GetStringWidth(HelpText() + LearnMoreLink()); 164 int total_length =
165 gfx::GetStringWidth(HelpText() + SavedPasswordsLink(), font_list_);
156 int usable_width = width - 2 * kHorizontalPadding; 166 int usable_width = width - 2 * kHorizontalPadding;
157 int text_height = 167 int text_height =
158 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * 168 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) *
159 font_.GetHeight(); 169 font_list_.GetFontSize();
160 int help_section_height = text_height + 2 * kHelpVerticalPadding; 170 int help_section_height = text_height + 2 * kHelpVerticalPadding;
161 171
162 int password_section_height = 172 int password_section_height = 0;
163 font_.GetHeight() + 2 * kPasswordVerticalPadding; 173 if (display_password_) {
174 password_section_height =
175 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding;
176 }
164 177
165 return (2 * kPopupBorderThickness + 178 return (2 * kPopupBorderThickness +
166 help_section_height + 179 help_section_height +
167 password_section_height); 180 password_section_height);
168 } 181 }
169 182
170 void PasswordGenerationPopupControllerImpl::CalculateBounds() { 183 void PasswordGenerationPopupControllerImpl::CalculateBounds() {
171 int popup_width = GetDesiredWidth(); 184 int popup_width = GetDesiredWidth();
172 int popup_height = GetDesiredHeight(popup_width); 185 int popup_height = GetDesiredHeight(popup_width);
173 186
174 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); 187 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width);
188 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness;
175 189
176 // Calculate the bounds for the rest of the elements given the bounds of 190 // Calculate the bounds for the rest of the elements given the bounds of
177 // the popup. 191 // the popup.
178 password_bounds_ = gfx::Rect( 192 if (display_password_) {
179 kPopupBorderThickness, 193 password_bounds_ = gfx::Rect(
180 kPopupBorderThickness, 194 kPopupBorderThickness,
181 popup_bounds_.width() - 2 * kPopupBorderThickness, 195 kPopupBorderThickness,
182 font_.GetHeight() + 2 * kPasswordVerticalPadding); 196 sub_view_width,
197 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding);
183 198
184 divider_bounds_ = gfx::Rect(kPopupBorderThickness, 199 divider_bounds_ = gfx::Rect(kPopupBorderThickness,
185 password_bounds_.bottom(), 200 password_bounds_.bottom(),
186 password_bounds_.width(), 201 sub_view_width,
187 kDividerHeight); 202 kDividerHeight);
203 } else {
204 password_bounds_ = gfx::Rect();
205 divider_bounds_ = gfx::Rect();
206 }
188 207
208 int help_y = std::max(kPopupBorderThickness, divider_bounds_.bottom());
189 int help_height = 209 int help_height =
190 popup_bounds_.height() - divider_bounds_.bottom() - kPopupBorderThickness; 210 popup_bounds_.height() - help_y - kPopupBorderThickness;
191 help_bounds_ = gfx::Rect( 211 help_bounds_ = gfx::Rect(
192 kPopupBorderThickness, 212 kPopupBorderThickness,
193 divider_bounds_.bottom(), 213 help_y,
194 password_bounds_.width(), 214 sub_view_width,
195 help_height); 215 help_height);
196 } 216 }
197 217
198 void PasswordGenerationPopupControllerImpl::Show() { 218 void PasswordGenerationPopupControllerImpl::Show(bool display_password) {
219 display_password_ = display_password;
220 if (display_password_)
221 current_password_ = base::ASCIIToUTF16(generator_->Generate());
222
199 CalculateBounds(); 223 CalculateBounds();
200 224
201 if (!view_) { 225 if (!view_) {
202 view_ = PasswordGenerationPopupView::Create(this); 226 view_ = PasswordGenerationPopupView::Create(this);
203 view_->Show(); 227 view_->Show();
228 } else {
229 view_->UpdateBoundsAndRedrawPopup();
204 } 230 }
205 231
206 controller_common_.RegisterKeyPressCallback(); 232 controller_common_.RegisterKeyPressCallback();
207 233
208 if (observer_) 234 if (observer_)
209 observer_->OnPopupShown(); 235 observer_->OnPopupShown(display_password_);
210 } 236 }
211 237
212 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { 238 void PasswordGenerationPopupControllerImpl::HideAndDestroy() {
213 Hide(); 239 Hide();
214 } 240 }
215 241
216 void PasswordGenerationPopupControllerImpl::Hide() { 242 void PasswordGenerationPopupControllerImpl::Hide() {
217 controller_common_.RemoveKeyPressCallback(); 243 controller_common_.RemoveKeyPressCallback();
218 244
219 if (view_) 245 if (view_)
220 view_->Hide(); 246 view_->Hide();
221 247
222 if (observer_) 248 if (observer_)
223 observer_->OnPopupHidden(); 249 observer_->OnPopupHidden();
224 250
225 delete this; 251 delete this;
226 } 252 }
227 253
228 void PasswordGenerationPopupControllerImpl::ViewDestroyed() { 254 void PasswordGenerationPopupControllerImpl::ViewDestroyed() {
229 view_ = NULL; 255 view_ = NULL;
230 256
231 Hide(); 257 Hide();
232 } 258 }
233 259
234 void PasswordGenerationPopupControllerImpl::OnHelpLinkClicked() { 260 void PasswordGenerationPopupControllerImpl::OnSavedPasswordsLinkClicked() {
261 // TODO(gcasto): Change this to navigate to account central once passwords
262 // are visible there.
235 Browser* browser = 263 Browser* browser =
236 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); 264 chrome::FindBrowserWithWebContents(controller_common_.web_contents());
237 content::OpenURLParams params( 265 content::OpenURLParams params(
238 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), 266 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(),
239 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); 267 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false);
240 browser->OpenURL(params); 268 browser->OpenURL(params);
241 } 269 }
242 270
243 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( 271 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint(
244 const gfx::Point& point) { 272 const gfx::Point& point) {
(...skipping 18 matching lines...) Expand all
263 291
264 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const { 292 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const {
265 // Will be hidden when focus changes anyway. 293 // Will be hidden when focus changes anyway.
266 return false; 294 return false;
267 } 295 }
268 296
269 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { 297 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() {
270 return controller_common_.container_view(); 298 return controller_common_.container_view();
271 } 299 }
272 300
301 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const {
302 return font_list_;
303 }
304
273 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { 305 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const {
274 return popup_bounds_; 306 return popup_bounds_;
275 } 307 }
276 308
277 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() 309 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds()
278 const { 310 const {
279 return password_bounds_; 311 return password_bounds_;
280 } 312 }
281 313
282 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() 314 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds()
283 const { 315 const {
284 return divider_bounds_; 316 return divider_bounds_;
285 } 317 }
286 318
287 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { 319 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const {
288 return help_bounds_; 320 return help_bounds_;
289 } 321 }
290 322
323 bool PasswordGenerationPopupControllerImpl::display_password() const {
324 return display_password_;
325 }
326
291 bool PasswordGenerationPopupControllerImpl::password_selected() const { 327 bool PasswordGenerationPopupControllerImpl::password_selected() const {
292 return password_selected_; 328 return password_selected_;
293 } 329 }
294 330
295 base::string16 PasswordGenerationPopupControllerImpl::password() const { 331 base::string16 PasswordGenerationPopupControllerImpl::password() const {
296 return current_password_; 332 return current_password_;
297 } 333 }
298 334
335 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() {
336 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION);
337 }
338
299 base::string16 PasswordGenerationPopupControllerImpl::HelpText() { 339 base::string16 PasswordGenerationPopupControllerImpl::HelpText() {
300 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT); 340 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT);
301 } 341 }
302 342
303 base::string16 PasswordGenerationPopupControllerImpl::LearnMoreLink() { 343 base::string16 PasswordGenerationPopupControllerImpl::SavedPasswordsLink() {
304 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_LEARN_MORE_LINK); 344 return l10n_util::GetStringUTF16(
345 IDS_PASSWORD_GENERATION_SAVED_PASSWORDS_LINK);
305 } 346 }
306 347
307 } // namespace autofill 348 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698