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/string_split.h" | |
10 #include "base/strings/string_util.h" | |
9 #include "base/strings/utf_string_conversion_utils.h" | 11 #include "base/strings/utf_string_conversion_utils.h" |
10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/password_manager/password_manager.h" | 13 #include "chrome/browser/password_manager/password_manager.h" |
12 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" | 14 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" |
13 #include "chrome/browser/ui/autofill/password_generation_popup_view.h" | 15 #include "chrome/browser/ui/autofill/password_generation_popup_view.h" |
14 #include "chrome/browser/ui/autofill/popup_constants.h" | 16 #include "chrome/browser/ui/autofill/popup_constants.h" |
15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
17 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
18 #include "components/autofill/content/common/autofill_messages.h" | 20 #include "components/autofill/content/common/autofill_messages.h" |
19 #include "components/autofill/core/browser/password_generator.h" | 21 #include "components/autofill/core/browser/password_generator.h" |
20 #include "content/public/browser/native_web_keyboard_event.h" | 22 #include "content/public/browser/native_web_keyboard_event.h" |
21 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
23 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
24 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/base/resource/resource_bundle.h" | |
25 #include "ui/events/keycodes/keyboard_codes.h" | 28 #include "ui/events/keycodes/keyboard_codes.h" |
26 #include "ui/gfx/rect_conversions.h" | 29 #include "ui/gfx/rect_conversions.h" |
27 #include "ui/gfx/text_utils.h" | 30 #include "ui/gfx/text_utils.h" |
28 | 31 |
29 namespace autofill { | 32 namespace autofill { |
30 | 33 |
31 const int kMinimumWidth = 60; | 34 // All measurements in pixels. |
35 // The minimum required width of the popup. | |
36 const int kMinimumWidth = 300; | |
37 // The height of the divider between the password area and the help area. | |
32 const int kDividerHeight = 1; | 38 const int kDividerHeight = 1; |
33 | 39 |
40 const base::char16 kRangeSeperator = '|'; | |
Evan Stade
2014/02/12 23:10:57
spelling
also, don't define this here
Garrett Casto
2014/02/12 23:38:51
Removed.
Evan Stade
2014/02/13 02:21:10
forgot to upload latest changeset? This doesn't se
Garrett Casto
2014/02/13 22:32:27
Just missed somehow. Fixed for real.
| |
41 | |
34 base::WeakPtr<PasswordGenerationPopupControllerImpl> | 42 base::WeakPtr<PasswordGenerationPopupControllerImpl> |
35 PasswordGenerationPopupControllerImpl::GetOrCreate( | 43 PasswordGenerationPopupControllerImpl::GetOrCreate( |
36 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous, | 44 base::WeakPtr<PasswordGenerationPopupControllerImpl> previous, |
37 const gfx::RectF& bounds, | 45 const gfx::RectF& bounds, |
38 const PasswordForm& form, | 46 const PasswordForm& form, |
39 PasswordGenerator* generator, | 47 PasswordGenerator* generator, |
40 PasswordManager* password_manager, | 48 PasswordManager* password_manager, |
41 PasswordGenerationPopupObserver* observer, | 49 PasswordGenerationPopupObserver* observer, |
42 content::WebContents* web_contents, | 50 content::WebContents* web_contents, |
43 gfx::NativeView container_view) { | 51 gfx::NativeView container_view) { |
44 if (previous.get() && | 52 if (previous.get() && |
45 previous->element_bounds() == bounds && | 53 previous->element_bounds() == bounds && |
46 previous->web_contents() == web_contents && | 54 previous->web_contents() == web_contents && |
47 previous->container_view() == container_view) { | 55 previous->container_view() == container_view) { |
48 // TODO(gcasto): Should we clear state here? | 56 // TODO(gcasto): Any state that we should clear here? |
49 return previous; | 57 return previous; |
50 } | 58 } |
51 | 59 |
52 if (previous.get()) | 60 if (previous.get()) |
53 previous->Hide(); | 61 previous->Hide(); |
54 | 62 |
55 PasswordGenerationPopupControllerImpl* controller = | 63 PasswordGenerationPopupControllerImpl* controller = |
56 new PasswordGenerationPopupControllerImpl( | 64 new PasswordGenerationPopupControllerImpl( |
57 bounds, | 65 bounds, |
58 form, | 66 form, |
(...skipping 12 matching lines...) Expand all Loading... | |
71 PasswordManager* password_manager, | 79 PasswordManager* password_manager, |
72 PasswordGenerationPopupObserver* observer, | 80 PasswordGenerationPopupObserver* observer, |
73 content::WebContents* web_contents, | 81 content::WebContents* web_contents, |
74 gfx::NativeView container_view) | 82 gfx::NativeView container_view) |
75 : form_(form), | 83 : form_(form), |
76 generator_(generator), | 84 generator_(generator), |
77 password_manager_(password_manager), | 85 password_manager_(password_manager), |
78 observer_(observer), | 86 observer_(observer), |
79 controller_common_(bounds, container_view, web_contents), | 87 controller_common_(bounds, container_view, web_contents), |
80 view_(NULL), | 88 view_(NULL), |
81 current_password_(base::ASCIIToUTF16(generator->Generate())), | 89 font_list_(ResourceBundle::GetSharedInstance().GetFontList( |
90 ResourceBundle::SmallFont)), | |
82 password_selected_(false), | 91 password_selected_(false), |
92 display_password_(false), | |
83 weak_ptr_factory_(this) { | 93 weak_ptr_factory_(this) { |
84 controller_common_.SetKeyPressCallback( | 94 controller_common_.SetKeyPressCallback( |
85 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, | 95 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, |
86 base::Unretained(this))); | 96 base::Unretained(this))); |
97 | |
98 std::vector<base::string16> pieces; | |
99 base::SplitStringDontTrim( | |
100 l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT), | |
101 kRangeSeperator, | |
102 &pieces); | |
103 DCHECK(pieces.size() == 2); | |
Evan Stade
2014/02/12 23:10:57
confused why this is 2 and not 3?
also, nit: DCHE
Garrett Casto
2014/02/12 23:38:51
Done, and changed to 3. Forgot to re-upload after
| |
104 link_range_ = gfx::Range(pieces[0].size(), | |
105 pieces[0].size() + pieces[1].size()); | |
106 help_text_ = JoinString(pieces, base::string16()); | |
87 } | 107 } |
88 | 108 |
89 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl() | 109 PasswordGenerationPopupControllerImpl::~PasswordGenerationPopupControllerImpl() |
90 {} | 110 {} |
91 | 111 |
92 base::WeakPtr<PasswordGenerationPopupControllerImpl> | 112 base::WeakPtr<PasswordGenerationPopupControllerImpl> |
93 PasswordGenerationPopupControllerImpl::GetWeakPtr() { | 113 PasswordGenerationPopupControllerImpl::GetWeakPtr() { |
94 return weak_ptr_factory_.GetWeakPtr(); | 114 return weak_ptr_factory_.GetWeakPtr(); |
95 } | 115 } |
96 | 116 |
(...skipping 20 matching lines...) Expand all Loading... | |
117 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() { | 137 bool PasswordGenerationPopupControllerImpl::PossiblyAcceptPassword() { |
118 if (password_selected_) { | 138 if (password_selected_) { |
119 PasswordAccepted(); // This will delete |this|. | 139 PasswordAccepted(); // This will delete |this|. |
120 return true; | 140 return true; |
121 } | 141 } |
122 | 142 |
123 return false; | 143 return false; |
124 } | 144 } |
125 | 145 |
126 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) { | 146 void PasswordGenerationPopupControllerImpl::PasswordSelected(bool selected) { |
147 if (!display_password_) | |
148 return; | |
149 | |
127 password_selected_ = selected; | 150 password_selected_ = selected; |
151 view_->PasswordSelectionUpdated(); | |
128 view_->UpdateBoundsAndRedrawPopup(); | 152 view_->UpdateBoundsAndRedrawPopup(); |
129 } | 153 } |
130 | 154 |
131 void PasswordGenerationPopupControllerImpl::PasswordAccepted() { | 155 void PasswordGenerationPopupControllerImpl::PasswordAccepted() { |
156 if (!display_password_) | |
157 return; | |
158 | |
132 web_contents()->GetRenderViewHost()->Send( | 159 web_contents()->GetRenderViewHost()->Send( |
133 new AutofillMsg_GeneratedPasswordAccepted( | 160 new AutofillMsg_GeneratedPasswordAccepted( |
134 web_contents()->GetRenderViewHost()->GetRoutingID(), | 161 web_contents()->GetRenderViewHost()->GetRoutingID(), |
135 current_password_)); | 162 current_password_)); |
136 password_manager_->SetFormHasGeneratedPassword(form_); | 163 password_manager_->SetFormHasGeneratedPassword(form_); |
137 Hide(); | 164 Hide(); |
138 } | 165 } |
139 | 166 |
140 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { | 167 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { |
141 // Minimum width we want to display the password. | 168 int minimum_required_width = kMinimumWidth; |
142 int minimum_length_for_text = | 169 if (display_password_) { |
143 2 * kHorizontalPadding + | 170 // Make sure that the width will always be large enough to display the |
144 font_list_.GetExpectedTextWidth(kMinimumWidth) + | 171 // password and suggestion on one line. |
145 2 * kPopupBorderThickness; | 172 minimum_required_width = |
173 std::max(minimum_required_width, | |
174 gfx::GetStringWidth(current_password_ + SuggestedText(), | |
175 font_list_) + 2 * kHorizontalPadding); | |
176 } | |
146 | 177 |
147 // If the width of the field is longer than the minimum, use that instead. | 178 // If the width of the field is longer than the minimum, use that instead. |
148 return std::max(minimum_length_for_text, | 179 return std::max(minimum_required_width, |
149 controller_common_.RoundedElementBounds().width()); | 180 controller_common_.RoundedElementBounds().width()); |
150 } | 181 } |
151 | 182 |
152 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { | 183 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { |
153 // Note that this wrapping isn't exactly what the popup will do. It shouldn't | 184 // 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 | 185 // 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 | 186 // 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. | 187 // default width should be wide enough to prevent this from being an issue. |
157 int total_length = gfx::GetStringWidth(HelpText() + LearnMoreLink(), | 188 int total_length = gfx::GetStringWidth(HelpText(), font_list_); |
158 font_list_); | |
159 int usable_width = width - 2 * kHorizontalPadding; | 189 int usable_width = width - 2 * kHorizontalPadding; |
160 int text_height = | 190 int text_height = |
161 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * | 191 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * |
162 font_list_.GetHeight(); | 192 font_list_.GetFontSize(); |
163 int help_section_height = text_height + 2 * kHelpVerticalPadding; | 193 int help_section_height = text_height + 2 * kHelpVerticalPadding; |
164 | 194 |
165 int password_section_height = | 195 int password_section_height = 0; |
166 font_list_.GetHeight() + 2 * kPasswordVerticalPadding; | 196 if (display_password_) { |
197 password_section_height = | |
198 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding; | |
199 } | |
167 | 200 |
168 return (2 * kPopupBorderThickness + | 201 return (2 * kPopupBorderThickness + |
169 help_section_height + | 202 help_section_height + |
170 password_section_height); | 203 password_section_height); |
171 } | 204 } |
172 | 205 |
173 void PasswordGenerationPopupControllerImpl::CalculateBounds() { | 206 void PasswordGenerationPopupControllerImpl::CalculateBounds() { |
174 int popup_width = GetDesiredWidth(); | 207 int popup_width = GetDesiredWidth(); |
175 int popup_height = GetDesiredHeight(popup_width); | 208 int popup_height = GetDesiredHeight(popup_width); |
176 | 209 |
177 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); | 210 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); |
211 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness; | |
178 | 212 |
179 // Calculate the bounds for the rest of the elements given the bounds of | 213 // Calculate the bounds for the rest of the elements given the bounds of |
180 // the popup. | 214 // the popup. |
181 password_bounds_ = gfx::Rect( | 215 if (display_password_) { |
182 kPopupBorderThickness, | 216 password_bounds_ = gfx::Rect( |
183 kPopupBorderThickness, | 217 kPopupBorderThickness, |
184 popup_bounds_.width() - 2 * kPopupBorderThickness, | 218 kPopupBorderThickness, |
185 font_list_.GetHeight() + 2 * kPasswordVerticalPadding); | 219 sub_view_width, |
220 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding); | |
186 | 221 |
187 divider_bounds_ = gfx::Rect(kPopupBorderThickness, | 222 divider_bounds_ = gfx::Rect(kPopupBorderThickness, |
188 password_bounds_.bottom(), | 223 password_bounds_.bottom(), |
189 password_bounds_.width(), | 224 sub_view_width, |
190 kDividerHeight); | 225 kDividerHeight); |
226 } else { | |
227 password_bounds_ = gfx::Rect(); | |
228 divider_bounds_ = gfx::Rect(); | |
229 } | |
191 | 230 |
231 int help_y = std::max(kPopupBorderThickness, divider_bounds_.bottom()); | |
192 int help_height = | 232 int help_height = |
193 popup_bounds_.height() - divider_bounds_.bottom() - kPopupBorderThickness; | 233 popup_bounds_.height() - help_y - kPopupBorderThickness; |
194 help_bounds_ = gfx::Rect( | 234 help_bounds_ = gfx::Rect( |
195 kPopupBorderThickness, | 235 kPopupBorderThickness, |
196 divider_bounds_.bottom(), | 236 help_y, |
197 password_bounds_.width(), | 237 sub_view_width, |
198 help_height); | 238 help_height); |
199 } | 239 } |
200 | 240 |
201 void PasswordGenerationPopupControllerImpl::Show() { | 241 void PasswordGenerationPopupControllerImpl::Show(bool display_password) { |
242 display_password_ = display_password; | |
243 if (display_password_) | |
244 current_password_ = base::ASCIIToUTF16(generator_->Generate()); | |
245 | |
202 CalculateBounds(); | 246 CalculateBounds(); |
203 | 247 |
204 if (!view_) { | 248 if (!view_) { |
205 view_ = PasswordGenerationPopupView::Create(this); | 249 view_ = PasswordGenerationPopupView::Create(this); |
206 view_->Show(); | 250 view_->Show(); |
251 } else { | |
252 view_->UpdateBoundsAndRedrawPopup(); | |
207 } | 253 } |
208 | 254 |
209 controller_common_.RegisterKeyPressCallback(); | 255 controller_common_.RegisterKeyPressCallback(); |
210 | 256 |
211 if (observer_) | 257 if (observer_) |
212 observer_->OnPopupShown(); | 258 observer_->OnPopupShown(display_password_); |
213 } | 259 } |
214 | 260 |
215 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { | 261 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { |
216 Hide(); | 262 Hide(); |
217 } | 263 } |
218 | 264 |
219 void PasswordGenerationPopupControllerImpl::Hide() { | 265 void PasswordGenerationPopupControllerImpl::Hide() { |
220 controller_common_.RemoveKeyPressCallback(); | 266 controller_common_.RemoveKeyPressCallback(); |
221 | 267 |
222 if (view_) | 268 if (view_) |
223 view_->Hide(); | 269 view_->Hide(); |
224 | 270 |
225 if (observer_) | 271 if (observer_) |
226 observer_->OnPopupHidden(); | 272 observer_->OnPopupHidden(); |
227 | 273 |
228 delete this; | 274 delete this; |
229 } | 275 } |
230 | 276 |
231 void PasswordGenerationPopupControllerImpl::ViewDestroyed() { | 277 void PasswordGenerationPopupControllerImpl::ViewDestroyed() { |
232 view_ = NULL; | 278 view_ = NULL; |
233 | 279 |
234 Hide(); | 280 Hide(); |
235 } | 281 } |
236 | 282 |
237 void PasswordGenerationPopupControllerImpl::OnHelpLinkClicked() { | 283 void PasswordGenerationPopupControllerImpl::OnSavedPasswordsLinkClicked() { |
284 // TODO(gcasto): Change this to navigate to account central once passwords | |
285 // are visible there. | |
238 Browser* browser = | 286 Browser* browser = |
239 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); | 287 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); |
240 content::OpenURLParams params( | 288 content::OpenURLParams params( |
241 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), | 289 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), |
242 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); | 290 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); |
243 browser->OpenURL(params); | 291 browser->OpenURL(params); |
244 } | 292 } |
245 | 293 |
246 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( | 294 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( |
247 const gfx::Point& point) { | 295 const gfx::Point& point) { |
(...skipping 18 matching lines...) Expand all Loading... | |
266 | 314 |
267 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const { | 315 bool PasswordGenerationPopupControllerImpl::ShouldHideOnOutsideClick() const { |
268 // Will be hidden when focus changes anyway. | 316 // Will be hidden when focus changes anyway. |
269 return false; | 317 return false; |
270 } | 318 } |
271 | 319 |
272 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { | 320 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { |
273 return controller_common_.container_view(); | 321 return controller_common_.container_view(); |
274 } | 322 } |
275 | 323 |
324 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const { | |
325 return font_list_; | |
326 } | |
327 | |
276 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { | 328 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { |
277 return popup_bounds_; | 329 return popup_bounds_; |
278 } | 330 } |
279 | 331 |
280 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() | 332 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() |
281 const { | 333 const { |
282 return password_bounds_; | 334 return password_bounds_; |
283 } | 335 } |
284 | 336 |
285 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() | 337 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() |
286 const { | 338 const { |
287 return divider_bounds_; | 339 return divider_bounds_; |
288 } | 340 } |
289 | 341 |
290 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { | 342 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { |
291 return help_bounds_; | 343 return help_bounds_; |
292 } | 344 } |
293 | 345 |
346 bool PasswordGenerationPopupControllerImpl::display_password() const { | |
347 return display_password_; | |
348 } | |
349 | |
294 bool PasswordGenerationPopupControllerImpl::password_selected() const { | 350 bool PasswordGenerationPopupControllerImpl::password_selected() const { |
295 return password_selected_; | 351 return password_selected_; |
296 } | 352 } |
297 | 353 |
298 base::string16 PasswordGenerationPopupControllerImpl::password() const { | 354 base::string16 PasswordGenerationPopupControllerImpl::password() const { |
299 return current_password_; | 355 return current_password_; |
300 } | 356 } |
301 | 357 |
302 base::string16 PasswordGenerationPopupControllerImpl::HelpText() { | 358 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { |
303 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT); | 359 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); |
304 } | 360 } |
305 | 361 |
306 base::string16 PasswordGenerationPopupControllerImpl::LearnMoreLink() { | 362 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { |
307 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_LEARN_MORE_LINK); | 363 return help_text_; |
364 } | |
365 | |
366 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { | |
367 return link_range_; | |
308 } | 368 } |
309 | 369 |
310 } // namespace autofill | 370 } // namespace autofill |
OLD | NEW |