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