| 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( | |
| 83 ResourceBundle::SmallFont)), | |
| 84 password_selected_(false), | 82 password_selected_(false), |
| 85 display_password_(false), | 83 display_password_(false), |
| 86 weak_ptr_factory_(this) { | 84 weak_ptr_factory_(this) { |
| 87 controller_common_.SetKeyPressCallback( | 85 controller_common_.SetKeyPressCallback( |
| 88 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, | 86 base::Bind(&PasswordGenerationPopupControllerImpl::HandleKeyPressEvent, |
| 89 base::Unretained(this))); | 87 base::Unretained(this))); |
| 90 | 88 |
| 91 std::vector<base::string16> pieces; | 89 std::vector<base::string16> pieces; |
| 92 base::SplitStringDontTrim( | 90 base::SplitStringDontTrim( |
| 93 l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT), | 91 l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_PROMPT), |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 return; | 148 return; |
| 151 | 149 |
| 152 web_contents()->GetRenderViewHost()->Send( | 150 web_contents()->GetRenderViewHost()->Send( |
| 153 new AutofillMsg_GeneratedPasswordAccepted( | 151 new AutofillMsg_GeneratedPasswordAccepted( |
| 154 web_contents()->GetRenderViewHost()->GetRoutingID(), | 152 web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 155 current_password_)); | 153 current_password_)); |
| 156 password_manager_->SetFormHasGeneratedPassword(form_); | 154 password_manager_->SetFormHasGeneratedPassword(form_); |
| 157 Hide(); | 155 Hide(); |
| 158 } | 156 } |
| 159 | 157 |
| 160 int PasswordGenerationPopupControllerImpl::GetDesiredWidth() { | 158 int PasswordGenerationPopupControllerImpl::GetMinimumWidth() { |
| 161 // Minimum width in pixels. | 159 // Minimum width in pixels. |
| 162 const int minimum_required_width = 300; | 160 const int minimum_required_width = 350; |
| 163 | 161 |
| 164 // 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. |
| 165 int width = std::max(minimum_required_width, | 163 return std::max(minimum_required_width, |
| 166 controller_common_.RoundedElementBounds().width()); | 164 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 } | 165 } |
| 201 | 166 |
| 202 void PasswordGenerationPopupControllerImpl::CalculateBounds() { | 167 void PasswordGenerationPopupControllerImpl::CalculateBounds() { |
| 203 int popup_width = GetDesiredWidth(); | 168 gfx::Size bounds = view_->GetBounds(); |
| 204 int popup_height = GetDesiredHeight(popup_width); | |
| 205 | 169 |
| 206 popup_bounds_ = controller_common_.GetPopupBounds(popup_height, popup_width); | 170 popup_bounds_ = controller_common_.GetPopupBounds(bounds.width(), |
| 207 int sub_view_width = popup_bounds_.width() - 2 * kPopupBorderThickness; | 171 bounds.height()); |
| 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 } | 172 } |
| 236 | 173 |
| 237 void PasswordGenerationPopupControllerImpl::Show(bool display_password) { | 174 void PasswordGenerationPopupControllerImpl::Show(bool display_password) { |
| 238 display_password_ = display_password; | 175 display_password_ = display_password; |
| 239 if (display_password_) | 176 if (display_password_) |
| 240 current_password_ = base::ASCIIToUTF16(generator_->Generate()); | 177 current_password_ = base::ASCIIToUTF16(generator_->Generate()); |
| 241 | 178 |
| 242 CalculateBounds(); | |
| 243 | |
| 244 if (!view_) { | 179 if (!view_) { |
| 245 view_ = PasswordGenerationPopupView::Create(this); | 180 view_ = PasswordGenerationPopupView::Create(this); |
| 181 CalculateBounds(); |
| 246 view_->Show(); | 182 view_->Show(); |
| 247 } else { | 183 } else { |
| 184 CalculateBounds(); |
| 248 view_->UpdateBoundsAndRedrawPopup(); | 185 view_->UpdateBoundsAndRedrawPopup(); |
| 249 } | 186 } |
| 250 | 187 |
| 251 controller_common_.RegisterKeyPressCallback(); | 188 controller_common_.RegisterKeyPressCallback(); |
| 252 | 189 |
| 253 if (observer_) | 190 if (observer_) |
| 254 observer_->OnPopupShown(display_password_); | 191 observer_->OnPopupShown(display_password_); |
| 255 } | 192 } |
| 256 | 193 |
| 257 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { | 194 void PasswordGenerationPopupControllerImpl::HideAndDestroy() { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 282 Browser* browser = | 219 Browser* browser = |
| 283 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); | 220 chrome::FindBrowserWithWebContents(controller_common_.web_contents()); |
| 284 content::OpenURLParams params( | 221 content::OpenURLParams params( |
| 285 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), | 222 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), |
| 286 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); | 223 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); |
| 287 browser->OpenURL(params); | 224 browser->OpenURL(params); |
| 288 } | 225 } |
| 289 | 226 |
| 290 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( | 227 void PasswordGenerationPopupControllerImpl::SetSelectionAtPoint( |
| 291 const gfx::Point& point) { | 228 const gfx::Point& point) { |
| 292 PasswordSelected(password_bounds_.Contains(point)); | 229 PasswordSelected(view_->IsPointInPasswordBounds(point)); |
| 293 } | 230 } |
| 294 | 231 |
| 295 bool PasswordGenerationPopupControllerImpl::AcceptSelectedLine() { | 232 bool PasswordGenerationPopupControllerImpl::AcceptSelectedLine() { |
| 296 if (!password_selected_) | 233 if (!password_selected_) |
| 297 return false; | 234 return false; |
| 298 | 235 |
| 299 PasswordAccepted(); | 236 PasswordAccepted(); |
| 300 return true; | 237 return true; |
| 301 } | 238 } |
| 302 | 239 |
| 303 void PasswordGenerationPopupControllerImpl::SelectionCleared() { | 240 void PasswordGenerationPopupControllerImpl::SelectionCleared() { |
| 304 PasswordSelected(false); | 241 PasswordSelected(false); |
| 305 } | 242 } |
| 306 | 243 |
| 307 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { | 244 gfx::NativeView PasswordGenerationPopupControllerImpl::container_view() { |
| 308 return controller_common_.container_view(); | 245 return controller_common_.container_view(); |
| 309 } | 246 } |
| 310 | 247 |
| 311 const gfx::FontList& PasswordGenerationPopupControllerImpl::font_list() const { | |
| 312 return font_list_; | |
| 313 } | |
| 314 | |
| 315 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { | 248 const gfx::Rect& PasswordGenerationPopupControllerImpl::popup_bounds() const { |
| 316 return popup_bounds_; | 249 return popup_bounds_; |
| 317 } | 250 } |
| 318 | 251 |
| 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 { | 252 bool PasswordGenerationPopupControllerImpl::display_password() const { |
| 334 return display_password_; | 253 return display_password_; |
| 335 } | 254 } |
| 336 | 255 |
| 337 bool PasswordGenerationPopupControllerImpl::password_selected() const { | 256 bool PasswordGenerationPopupControllerImpl::password_selected() const { |
| 338 return password_selected_; | 257 return password_selected_; |
| 339 } | 258 } |
| 340 | 259 |
| 341 base::string16 PasswordGenerationPopupControllerImpl::password() const { | 260 base::string16 PasswordGenerationPopupControllerImpl::password() const { |
| 342 return current_password_; | 261 return current_password_; |
| 343 } | 262 } |
| 344 | 263 |
| 345 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { | 264 base::string16 PasswordGenerationPopupControllerImpl::SuggestedText() { |
| 346 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); | 265 return l10n_util::GetStringUTF16(IDS_PASSWORD_GENERATION_SUGGESTION); |
| 347 } | 266 } |
| 348 | 267 |
| 349 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { | 268 const base::string16& PasswordGenerationPopupControllerImpl::HelpText() { |
| 350 return help_text_; | 269 return help_text_; |
| 351 } | 270 } |
| 352 | 271 |
| 353 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { | 272 const gfx::Range& PasswordGenerationPopupControllerImpl::HelpTextLinkRange() { |
| 354 return link_range_; | 273 return link_range_; |
| 355 } | 274 } |
| 356 | 275 |
| 357 } // namespace autofill | 276 } // namespace autofill |
| OLD | NEW |