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 |
| 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 previous->set_form(form); |
| 51 previous->set_generator(generator); |
| 52 previous->set_password_manager(password_manager); |
49 return previous; | 53 return previous; |
50 } | 54 } |
51 | 55 |
52 if (previous.get()) | 56 if (previous.get()) |
53 previous->Hide(); | 57 previous->Hide(); |
54 | 58 |
55 PasswordGenerationPopupControllerImpl* controller = | 59 PasswordGenerationPopupControllerImpl* controller = |
56 new PasswordGenerationPopupControllerImpl( | 60 new PasswordGenerationPopupControllerImpl( |
57 bounds, | 61 bounds, |
58 form, | 62 form, |
59 generator, | 63 generator, |
60 password_manager, | 64 password_manager, |
61 observer, | 65 observer, |
62 web_contents, | 66 web_contents, |
63 container_view); | 67 container_view); |
64 return controller->GetWeakPtr(); | 68 return controller->GetWeakPtr(); |
65 } | 69 } |
66 | 70 |
67 PasswordGenerationPopupControllerImpl::PasswordGenerationPopupControllerImpl( | 71 PasswordGenerationPopupControllerImpl::PasswordGenerationPopupControllerImpl( |
68 const gfx::RectF& bounds, | 72 const gfx::RectF& bounds, |
69 const PasswordForm& form, | 73 const PasswordForm* form, |
70 PasswordGenerator* generator, | 74 PasswordGenerator* generator, |
71 PasswordManager* password_manager, | 75 PasswordManager* password_manager, |
72 PasswordGenerationPopupObserver* observer, | 76 PasswordGenerationPopupObserver* observer, |
73 content::WebContents* web_contents, | 77 content::WebContents* web_contents, |
74 gfx::NativeView container_view) | 78 gfx::NativeView container_view) |
75 : form_(form), | 79 : form_(form), |
76 generator_(generator), | 80 generator_(generator), |
77 password_manager_(password_manager), | 81 password_manager_(password_manager), |
78 observer_(observer), | 82 observer_(observer), |
79 controller_common_(bounds, container_view, web_contents), | 83 controller_common_(bounds, container_view, web_contents), |
80 view_(NULL), | 84 view_(NULL), |
81 current_password_(base::ASCIIToUTF16(generator->Generate())), | 85 font_list_(ResourceBundle::GetSharedInstance().GetFontList( |
| 86 ResourceBundle::SmallFont)), |
82 password_selected_(false), | 87 password_selected_(false), |
| 88 display_password_(false), |
83 weak_ptr_factory_(this) { | 89 weak_ptr_factory_(this) { |
84 controller_common_.SetKeyPressCallback( | 90 controller_common_.SetKeyPressCallback( |
85 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, | 91 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, |
86 base::Unretained(this))); | 92 base::Unretained(this))); |
87 } | 93 } |
88 | 94 |
89 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl() | 95 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl() |
90 {} | 96 {} |
91 | 97 |
92 base::WeakPtr<PasswordGenerationPopupControllerImpl> | 98 base::WeakPtr<PasswordGenerationPopupControllerImpl> |
(...skipping 24 matching lines...) Expand all Loading... |
117 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() { | 123 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() { |
118 if (password_selected_) { | 124 if (password_selected_) { |
119 PasswordAccepted(); // This will delete |this|. | 125 PasswordAccepted(); // This will delete |this|. |
120 return true; | 126 return true; |
121 } | 127 } |
122 | 128 |
123 return false; | 129 return false; |
124 } | 130 } |
125 | 131 |
126 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) { | 132 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) { |
| 133 if (!display_password_) |
| 134 return; |
| 135 |
127 password_selected_ = selected; | 136 password_selected_ = selected; |
| 137 view_->PasswordSelectionUpdated(); |
128 view_->UpdateBoundsAndRedrawPopup(); | 138 view_->UpdateBoundsAndRedrawPopup(); |
129 } | 139 } |
130 | 140 |
131 void PasswordGenerationPopupControllerImpl::PasswordAccepted() { | 141 void PasswordGenerationPopupControllerImpl::PasswordAccepted() { |
| 142 if (!display_password_) |
| 143 return; |
| 144 |
132 web_contents()->GetRenderViewHost()->Send( | 145 web_contents()->GetRenderViewHost()->Send( |
133 new AutofillMsg_GeneratedPasswordAccepted( | 146 new AutofillMsg_GeneratedPasswordAccepted( |
134 web_contents()->GetRenderViewHost()->GetRoutingID(), | 147 web_contents()->GetRenderViewHost()->GetRoutingID(), |
135 current_password_)); | 148 current_password_)); |
136 password_manager_->SetFormHasGeneratedPassword(form_); | 149 password_manager_->SetFormHasGeneratedPassword(*form_); |
137 Hide(); | 150 Hide(); |
138 } | 151 } |
139 | 152 |
140 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { | 153 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { |
141 // Minimum width we want to display the password. | |
142 int minimum_length_for_text = | |
143 2 * kHorizontalPadding + | |
144 font_list_.GetExpectedTextWidth(kMinimumWidth) + | |
145 2 * kPopupBorderThickness; | |
146 | |
147 // 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. |
148 return std::max(minimum_length_for_text, | 155 return std::max(kMinimumWidth, |
149 controller_common_.RoundedElementBounds().width()); | 156 controller_common_.RoundedElementBounds().width()); |
150 } | 157 } |
151 | 158 |
152 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { | 159 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { |
153 // 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 |
154 // 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 |
155 // 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 |
156 // 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. |
157 int total_length = gfx::GetStringWidth(HelpText() + LearnMoreLink(), | 164 int total_length = |
158 font_list_); | 165 gfx::GetStringWidth(HelpText() + SavedPasswordsLink(), font_list_); |
159 int usable_width = width - 2 * kHorizontalPadding; | 166 int usable_width = width - 2 * kHorizontalPadding; |
160 int text_height = | 167 int text_height = |
161 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * | 168 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * |
162 font_list_.GetHeight(); | 169 font_list_.GetFontSize(); |
163 int help_section_height = text_height + 2 * kHelpVerticalPadding; | 170 int help_section_height = text_height + 2 * kHelpVerticalPadding; |
164 | 171 |
165 int password_section_height = | 172 int password_section_height = 0; |
166 font_list_.GetHeight() + 2 * kPasswordVerticalPadding; | 173 if (display_password_) { |
| 174 password_section_height = |
| 175 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding; |
| 176 } |
167 | 177 |
168 return (2 * kPopupBorderThickness + | 178 return (2 * kPopupBorderThickness + |
169 help_section_height + | 179 help_section_height + |
170 password_section_height); | 180 password_section_height); |
171 } | 181 } |
172 | 182 |
173 void PasswordGenerationPopupControllerImpl::CalculateBounds() { | 183 void PasswordGenerationPopupControllerImpl::CalculateBounds() { |
174 int popup_width = GetDesiredWidth(); | 184 int popup_width = GetDesiredWidth(); |
175 int popup_height = GetDesiredHeight(popup_width); | 185 int popup_height = GetDesiredHeight(popup_width); |
176 | 186 |
177 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; |
178 | 189 |
179 // 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 |
180 // the popup. | 191 // the popup. |
181 password_bounds_ = gfx::Rect( | 192 if (display_password_) { |
182 kPopupBorderThickness, | 193 password_bounds_ = gfx::Rect( |
183 kPopupBorderThickness, | 194 kPopupBorderThickness, |
184 popup_bounds_.width() - 2 * kPopupBorderThickness, | 195 kPopupBorderThickness, |
185 font_list_.GetHeight() + 2 * kPasswordVerticalPadding); | 196 sub_view_width, |
| 197 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding); |
186 | 198 |
187 divider_bounds_ = gfx::Rect(kPopupBorderThickness, | 199 divider_bounds_ = gfx::Rect(kPopupBorderThickness, |
188 password_bounds_.bottom(), | 200 password_bounds_.bottom(), |
189 password_bounds_.width(), | 201 sub_view_width, |
190 kDividerHeight); | 202 kDividerHeight); |
| 203 } else { |
| 204 password_bounds_ = gfx::Rect(); |
| 205 divider_bounds_ = gfx::Rect(); |
| 206 } |
191 | 207 |
| 208 int help_y = std::max(kPopupBorderThickness, divider_bounds_.bottom()); |
192 int help_height = | 209 int help_height = |
193 popup_bounds_.height() - divider_bounds_.bottom() - kPopupBorderThickness; | 210 popup_bounds_.height() - help_y - kPopupBorderThickness; |
194 help_bounds_ = gfx::Rect( | 211 help_bounds_ = gfx::Rect( |
195 kPopupBorderThickness, | 212 kPopupBorderThickness, |
196 divider_bounds_.bottom(), | 213 help_y, |
197 password_bounds_.width(), | 214 sub_view_width, |
198 help_height); | 215 help_height); |
199 } | 216 } |
200 | 217 |
201 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 |
202 CalculateBounds(); | 223 CalculateBounds(); |
203 | 224 |
204 if (!view_) { | 225 if (!view_) { |
205 view_ = PasswordGenerationPopupView::Create(this); | 226 view_ = PasswordGenerationPopupView::Create(this); |
206 view_->Show(); | 227 view_->Show(); |
| 228 } else { |
| 229 view_->UpdateBoundsAndRedrawPopup(); |
207 } | 230 } |
208 | 231 |
209 controller_common_.RegisterKeyPressCallback(); | 232 controller_common_.RegisterKeyPressCallback(); |
210 | 233 |
211 if (observer_) | 234 if (observer_) |
212 observer_->OnPopupShown(); | 235 observer_->OnPopupShown(display_password_); |
213 } | 236 } |
214 | 237 |
215 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { | 238 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { |
216 Hide(); | 239 Hide(); |
217 } | 240 } |
218 | 241 |
219 void PasswordGenerationPopupControllerImpl::Hide() { | 242 void PasswordGenerationPopupControllerImpl::Hide() { |
220 controller_common_.RemoveKeyPressCallback(); | 243 controller_common_.RemoveKeyPressCallback(); |
221 | 244 |
222 if (view_) | 245 if (view_) |
223 view_->Hide(); | 246 view_->Hide(); |
224 | 247 |
225 if (observer_) | 248 if (observer_) |
226 observer_->OnPopupHidden(); | 249 observer_->OnPopupHidden(); |
227 | 250 |
228 delete this; | 251 delete this; |
229 } | 252 } |
230 | 253 |
231 void PasswordGenerationPopupControllerImpl::ViewDestroyed() { | 254 void PasswordGenerationPopupControllerImpl::ViewDestroyed() { |
232 view_ = NULL; | 255 view_ = NULL; |
233 | 256 |
234 Hide(); | 257 Hide(); |
235 } | 258 } |
236 | 259 |
237 void PasswordGenerationPopupControllerImpl::OnHelpLinkClicked() { | 260 void PasswordGenerationPopupControllerImpl::OnSavedPasswordsLinkClicked() { |
| 261 // TODO(gcasto): Change this to navigate to account central once passwords |
| 262 // are visible there. |
238 Browser* browser = | 263 Browser* browser = |
239 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); | 264 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); |
240 content::OpenURLParams params( | 265 content::OpenURLParams params( |
241 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), | 266 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), |
242 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); | 267 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); |
243 browser->OpenURL(params); | 268 browser->OpenURL(params); |
244 } | 269 } |
245 | 270 |
246 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( | 271 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( |
247 const gfx::Point& point) { | 272 const gfx::Point& point) { |
(...skipping 18 matching lines...) Expand all Loading... |
266 | 291 |
267 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const { | 292 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const { |
268 // Will be hidden when focus changes anyway. | 293 // Will be hidden when focus changes anyway. |
269 return false; | 294 return false; |
270 } | 295 } |
271 | 296 |
272 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { | 297 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { |
273 return controller_common_.container_view(); | 298 return controller_common_.container_view(); |
274 } | 299 } |
275 | 300 |
| 301 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const { |
| 302 return font_list_; |
| 303 } |
| 304 |
276 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { | 305 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { |
277 return popup_bounds_; | 306 return popup_bounds_; |
278 } | 307 } |
279 | 308 |
280 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() | 309 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() |
281 const { | 310 const { |
282 return password_bounds_; | 311 return password_bounds_; |
283 } | 312 } |
284 | 313 |
285 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() | 314 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() |
286 const { | 315 const { |
287 return divider_bounds_; | 316 return divider_bounds_; |
288 } | 317 } |
289 | 318 |
290 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { | 319 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { |
291 return help_bounds_; | 320 return help_bounds_; |
292 } | 321 } |
293 | 322 |
| 323 bool PasswordGenerationPopupControllerImpl::display_password() const { |
| 324 return display_password_; |
| 325 } |
| 326 |
294 bool PasswordGenerationPopupControllerImpl::password_selected() const { | 327 bool PasswordGenerationPopupControllerImpl::password_selected() const { |
295 return password_selected_; | 328 return password_selected_; |
296 } | 329 } |
297 | 330 |
298 base::string16 PasswordGenerationPopupControllerImpl::password() const { | 331 base::string16 PasswordGenerationPopupControllerImpl::password() const { |
299 return current_password_; | 332 return current_password_; |
300 } | 333 } |
301 | 334 |
| 335 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { |
| 336 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); |
| 337 } |
| 338 |
302 base::string16 PasswordGenerationPopupControllerImpl::HelpText() { | 339 base::string16 PasswordGenerationPopupControllerImpl::HelpText() { |
303 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT); | 340 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT); |
304 } | 341 } |
305 | 342 |
306 base::string16 PasswordGenerationPopupControllerImpl::LearnMoreLink() { | 343 base::string16 PasswordGenerationPopupControllerImpl::SavedPasswordsLink() { |
307 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_LEARN_MORE_LINK); | 344 return l10n_util::GetStringUTF16( |
| 345 IDS_PASSWORD_GENERATION_SAVED_PASSWORDS_LINK); |
308 } | 346 } |
309 | 347 |
310 } // namespace autofill | 348 } // namespace autofill |
OLD | NEW |