Chromium Code Reviews| 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" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 password_manager::PasswordManager* password_manager, | 72 password_manager::PasswordManager* password_manager, |
| 73 PasswordGenerationPopupObserver* observer, | 73 PasswordGenerationPopupObserver* observer, |
| 74 content::WebContents* web_contents, | 74 content::WebContents* web_contents, |
| 75 gfx::NativeView container_view) | 75 gfx::NativeView container_view) |
| 76 : form_(form), | 76 : form_(form), |
| 77 password_manager_(password_manager), | 77 password_manager_(password_manager), |
| 78 observer_(observer), | 78 observer_(observer), |
| 79 generator_(new PasswordGenerator(max_length)), | 79 generator_(new PasswordGenerator(max_length)), |
| 80 controller_common_(bounds, container_view, web_contents), | 80 controller_common_(bounds, container_view, web_contents), |
| 81 view_(NULL), | 81 view_(NULL), |
| 82 font_list_(ResourceBundle::GetSharedInstance().GetFontList( | 82 font_list_(ResourceBundle::GetSharedInstance().GetFontList( |
|
Evan Stade
2014/06/25 00:02:41
don't think you need this here in this class any m
Garrett Casto
2014/06/25 19:38:14
Removed.
| |
| 83 ResourceBundle::SmallFont)), | 83 ResourceBundle::SmallFont)), |
| 84 password_selected_(false), | 84 password_selected_(false), |
| 85 display_password_(false), | 85 display_password_(false), |
| 86 weak_ptr_factory_(this) { | 86 weak_ptr_factory_(this) { |
| 87 controller_common_.SetKeyPressCallback( | 87 controller_common_.SetKeyPressCallback( |
| 88 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, | 88 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, |
| 89 base::Unretained(this))); | 89 base::Unretained(this))); |
| 90 | 90 |
| 91 std::vector<base::string16> pieces; | 91 std::vector<base::string16> pieces; |
| 92 base::SplitStringDontTrim( | 92 base::SplitStringDontTrim( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 web_contents()->GetRenderViewHost()->Send( | 152 web_contents()->GetRenderViewHost()->Send( |
| 153 new AutofillMsg_GeneratedPasswordAccepted( | 153 new AutofillMsg_GeneratedPasswordAccepted( |
| 154 web_contents()->GetRenderViewHost()->GetRoutingID(), | 154 web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 155 current_password_)); | 155 current_password_)); |
| 156 password_manager_->SetFormHasGeneratedPassword(form_); | 156 password_manager_->SetFormHasGeneratedPassword(form_); |
| 157 Hide(); | 157 Hide(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { | 160 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { |
| 161 // Minimum width in pixels. | 161 // Minimum width in pixels. |
| 162 const int minimum_required_width = 300; | 162 const int minimum_required_width = 350; |
| 163 | 163 |
| 164 // If the width of the field is longer than the minimum, use that instead. | 164 // If the width of the field is longer than the minimum, use that instead. |
| 165 int width = std::max(minimum_required_width, | 165 return std::max(minimum_required_width, |
| 166 controller_common_.RoundedElementBounds().width()); | 166 controller_common_.RoundedElementBounds().width()); |
| 167 | |
| 168 if (display_password_) { | |
| 169 // Make sure that the width will always be large enough to display the | |
| 170 // password and suggestion on one line. | |
| 171 width = std::max(width, | |
| 172 gfx::GetStringWidth(current_password_ + SuggestedText(), | |
| 173 font_list_) + 2 * kHorizontalPadding); | |
| 174 } | |
| 175 | |
| 176 return width; | |
| 177 } | |
| 178 | |
| 179 int PasswordGenerationPopupControllerImpl::GetDesiredHeight(int width) { | |
| 180 // Note that this wrapping isn't exactly what the popup will do. It shouldn't | |
| 181 // line break in the middle of the link, but as long as the link isn't longer | |
| 182 // than given width this shouldn't affect the height calculated here. The | |
| 183 // default width should be wide enough to prevent this from being an issue. | |
| 184 int total_length = gfx::GetStringWidth(HelpText(), font_list_); | |
| 185 int usable_width = width - 2 * kHorizontalPadding; | |
| 186 int text_height = | |
| 187 static_cast<int>(ceil(static_cast<double>(total_length)/usable_width)) * | |
| 188 font_list_.GetFontSize(); | |
| 189 int help_section_height = text_height + 2 * kHelpVerticalPadding; | |
| 190 | |
| 191 int password_section_height = 0; | |
| 192 if (display_password_) { | |
| 193 password_section_height = | |
| 194 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding; | |
| 195 } | |
| 196 | |
| 197 return (2 * kPopupBorderThickness + | |
| 198 help_section_height + | |
| 199 password_section_height); | |
| 200 } | 167 } |
| 201 | 168 |
| 202 void PasswordGenerationPopupControllerImpl::CalculateBounds() { | 169 void PasswordGenerationPopupControllerImpl::CalculateBounds() { |
| 203 int popup_width = GetDesiredWidth(); | 170 int popup_width = GetDesiredWidth(); |
| 204 int popup_height = GetDesiredHeight(popup_width); | 171 int popup_height = view_->GetHeight(popup_width); |
| 205 | 172 |
| 206 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); | 173 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); |
| 207 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness; | |
| 208 | |
| 209 // Calculate the bounds for the rest of the elements given the bounds of | |
| 210 // the popup. | |
| 211 if (display_password_) { | |
| 212 password_bounds_ = gfx::Rect( | |
| 213 kPopupBorderThickness, | |
| 214 kPopupBorderThickness, | |
| 215 sub_view_width, | |
| 216 font_list_.GetFontSize() + 2 * kPasswordVerticalPadding); | |
| 217 | |
| 218 divider_bounds_ = gfx::Rect(kPopupBorderThickness, | |
| 219 password_bounds_.bottom(), | |
| 220 sub_view_width, | |
| 221 1 /* divider heigth*/); | |
| 222 } else { | |
| 223 password_bounds_ = gfx::Rect(); | |
| 224 divider_bounds_ = gfx::Rect(); | |
| 225 } | |
| 226 | |
| 227 int help_y = std::max(kPopupBorderThickness, divider_bounds_.bottom()); | |
| 228 int help_height = | |
| 229 popup_bounds_.height() - help_y - kPopupBorderThickness; | |
| 230 help_bounds_ = gfx::Rect( | |
| 231 kPopupBorderThickness, | |
| 232 help_y, | |
| 233 sub_view_width, | |
| 234 help_height); | |
| 235 } | 174 } |
| 236 | 175 |
| 237 void PasswordGenerationPopupControllerImpl::Show(bool display_password) { | 176 void PasswordGenerationPopupControllerImpl::Show(bool display_password) { |
| 238 display_password_ = display_password; | 177 display_password_ = display_password; |
| 239 if (display_password_) | 178 if (display_password_) |
| 240 current_password_ = base::ASCIIToUTF16(generator_->Generate()); | 179 current_password_ = base::ASCIIToUTF16(generator_->Generate()); |
| 241 | 180 |
| 242 CalculateBounds(); | |
| 243 | |
| 244 if (!view_) { | 181 if (!view_) { |
| 245 view_ = PasswordGenerationPopupView::Create(this); | 182 view_ = PasswordGenerationPopupView::Create(this); |
| 183 CalculateBounds(); | |
| 246 view_->Show(); | 184 view_->Show(); |
| 247 } else { | 185 } else { |
| 186 CalculateBounds(); | |
| 248 view_->UpdateBoundsAndRedrawPopup(); | 187 view_->UpdateBoundsAndRedrawPopup(); |
| 249 } | 188 } |
| 250 | 189 |
| 251 controller_common_.RegisterKeyPressCallback(); | 190 controller_common_.RegisterKeyPressCallback(); |
| 252 | 191 |
| 253 if (observer_) | 192 if (observer_) |
| 254 observer_->OnPopupShown(display_password_); | 193 observer_->OnPopupShown(display_password_); |
| 255 } | 194 } |
| 256 | 195 |
| 257 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { | 196 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 282 Browser* browser = | 221 Browser* browser = |
| 283 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); | 222 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); |
| 284 content::OpenURLParams params( | 223 content::OpenURLParams params( |
| 285 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), | 224 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), |
| 286 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); | 225 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); |
| 287 browser->OpenURL(params); | 226 browser->OpenURL(params); |
| 288 } | 227 } |
| 289 | 228 |
| 290 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( | 229 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( |
| 291 const gfx::Point& point) { | 230 const gfx::Point& point) { |
| 292 PasswordSelected(password_bounds_.Contains(point)); | 231 PasswordSelected(view_->IsPointInPasswordBounds(point)); |
| 293 } | 232 } |
| 294 | 233 |
| 295 bool PasswordGenerationPopupControllerImpl::AcceptSelectedLine() { | 234 bool PasswordGenerationPopupControllerImpl::AcceptSelectedLine() { |
| 296 if (!password_selected_) | 235 if (!password_selected_) |
| 297 return false; | 236 return false; |
| 298 | 237 |
| 299 PasswordAccepted(); | 238 PasswordAccepted(); |
| 300 return true; | 239 return true; |
| 301 } | 240 } |
| 302 | 241 |
| 303 void PasswordGenerationPopupControllerImpl::SelectionCleared() { | 242 void PasswordGenerationPopupControllerImpl::SelectionCleared() { |
| 304 PasswordSelected(false); | 243 PasswordSelected(false); |
| 305 } | 244 } |
| 306 | 245 |
| 307 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { | 246 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { |
| 308 return controller_common_.container_view(); | 247 return controller_common_.container_view(); |
| 309 } | 248 } |
| 310 | 249 |
| 311 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const { | 250 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const { |
| 312 return font_list_; | 251 return font_list_; |
| 313 } | 252 } |
| 314 | 253 |
| 315 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { | 254 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { |
| 316 return popup_bounds_; | 255 return popup_bounds_; |
| 317 } | 256 } |
| 318 | 257 |
| 319 const gfx::Rect& PasswordGenerationPopupControllerImpl::password_bounds() | |
| 320 const { | |
| 321 return password_bounds_; | |
| 322 } | |
| 323 | |
| 324 const gfx::Rect& PasswordGenerationPopupControllerImpl::divider_bounds() | |
| 325 const { | |
| 326 return divider_bounds_; | |
| 327 } | |
| 328 | |
| 329 const gfx::Rect& PasswordGenerationPopupControllerImpl::help_bounds() const { | |
| 330 return help_bounds_; | |
| 331 } | |
| 332 | |
| 333 bool PasswordGenerationPopupControllerImpl::display_password() const { | 258 bool PasswordGenerationPopupControllerImpl::display_password() const { |
| 334 return display_password_; | 259 return display_password_; |
| 335 } | 260 } |
| 336 | 261 |
| 337 bool PasswordGenerationPopupControllerImpl::password_selected() const { | 262 bool PasswordGenerationPopupControllerImpl::password_selected() const { |
| 338 return password_selected_; | 263 return password_selected_; |
| 339 } | 264 } |
| 340 | 265 |
| 341 base::string16 PasswordGenerationPopupControllerImpl::password() const { | 266 base::string16 PasswordGenerationPopupControllerImpl::password() const { |
| 342 return current_password_; | 267 return current_password_; |
| 343 } | 268 } |
| 344 | 269 |
| 345 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { | 270 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { |
| 346 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); | 271 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); |
| 347 } | 272 } |
| 348 | 273 |
| 349 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { | 274 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { |
| 350 return help_text_; | 275 return help_text_; |
| 351 } | 276 } |
| 352 | 277 |
| 353 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { | 278 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { |
| 354 return link_range_; | 279 return link_range_; |
| 355 } | 280 } |
| 356 | 281 |
| 357 } // namespace autofill | 282 } // namespace autofill |
| OLD | NEW |