OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/autofill_dialog_controller.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_split.h" | 8 #include "base/string_split.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const FormData& form, | 112 const FormData& form, |
113 const GURL& source_url, | 113 const GURL& source_url, |
114 const content::SSLStatus& ssl_status, | 114 const content::SSLStatus& ssl_status, |
115 const base::Callback<void(const FormStructure*)>& callback) | 115 const base::Callback<void(const FormStructure*)>& callback) |
116 : profile_(Profile::FromBrowserContext(contents->GetBrowserContext())), | 116 : profile_(Profile::FromBrowserContext(contents->GetBrowserContext())), |
117 contents_(contents), | 117 contents_(contents), |
118 form_structure_(form), | 118 form_structure_(form), |
119 source_url_(source_url), | 119 source_url_(source_url), |
120 ssl_status_(ssl_status), | 120 ssl_status_(ssl_status), |
121 callback_(callback), | 121 callback_(callback), |
| 122 ALLOW_THIS_IN_INITIALIZER_LIST(suggested_email_(this)), |
| 123 ALLOW_THIS_IN_INITIALIZER_LIST(suggested_cc_(this)), |
| 124 ALLOW_THIS_IN_INITIALIZER_LIST(suggested_billing_(this)), |
| 125 ALLOW_THIS_IN_INITIALIZER_LIST(suggested_shipping_(this)), |
122 popup_controller_(NULL) { | 126 popup_controller_(NULL) { |
123 // TODO(estade): |this| should observe PersonalDataManager. | 127 // TODO(estade): |this| should observe PersonalDataManager. |
124 // TODO(estade): remove duplicates from |form|? | 128 // TODO(estade): remove duplicates from |form|? |
125 } | 129 } |
126 | 130 |
127 AutofillDialogController::~AutofillDialogController() { | 131 AutofillDialogController::~AutofillDialogController() { |
128 if (popup_controller_) | 132 if (popup_controller_) |
129 popup_controller_->Hide(); | 133 popup_controller_->Hide(); |
130 } | 134 } |
131 | 135 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 FilterInputs(form_structure_, | 189 FilterInputs(form_structure_, |
186 kBillingInputs, | 190 kBillingInputs, |
187 arraysize(kBillingInputs), | 191 arraysize(kBillingInputs), |
188 &requested_billing_fields_); | 192 &requested_billing_fields_); |
189 | 193 |
190 FilterInputs(form_structure_, | 194 FilterInputs(form_structure_, |
191 kShippingInputs, | 195 kShippingInputs, |
192 arraysize(kShippingInputs), | 196 arraysize(kShippingInputs), |
193 &requested_shipping_fields_); | 197 &requested_shipping_fields_); |
194 | 198 |
195 GenerateComboboxModels(); | 199 GenerateSuggestionsModels(); |
196 | 200 |
197 // TODO(estade): don't show the dialog if the site didn't specify the right | 201 // TODO(estade): don't show the dialog if the site didn't specify the right |
198 // fields. First we must figure out what the "right" fields are. | 202 // fields. First we must figure out what the "right" fields are. |
199 view_.reset(AutofillDialogView::Create(this)); | 203 view_.reset(AutofillDialogView::Create(this)); |
200 view_->Show(); | 204 view_->Show(); |
201 } | 205 } |
202 | 206 |
203 string16 AutofillDialogController::DialogTitle() const { | 207 string16 AutofillDialogController::DialogTitle() const { |
204 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE); | 208 return l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_TITLE); |
205 } | 209 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return &cc_exp_month_combobox_model_; | 317 return &cc_exp_month_combobox_model_; |
314 | 318 |
315 case CREDIT_CARD_EXP_4_DIGIT_YEAR: | 319 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
316 return &cc_exp_year_combobox_model_; | 320 return &cc_exp_year_combobox_model_; |
317 | 321 |
318 default: | 322 default: |
319 return NULL; | 323 return NULL; |
320 } | 324 } |
321 } | 325 } |
322 | 326 |
323 ui::ComboboxModel* AutofillDialogController::ComboboxModelForSection( | 327 ui::MenuModel* AutofillDialogController::MenuModelForSection( |
324 DialogSection section) { | 328 DialogSection section) { |
325 return SuggestionsModelForSection(section); | 329 return SuggestionsMenuModelForSection(section); |
| 330 } |
| 331 |
| 332 string16 AutofillDialogController::SuggestionTextForSection( |
| 333 DialogSection section) { |
| 334 SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); |
| 335 std::string item_key = model->GetItemKeyAt(model->checked_item()); |
| 336 if (item_key.empty()) |
| 337 return string16(); |
| 338 |
| 339 // TODO(estade): This doesn't display as much info as it should (for example, |
| 340 // it should show full addresses rather than just a summary). |
| 341 if (section == SECTION_CC) { |
| 342 CreditCard* card = GetManager()->GetCreditCardByGUID(item_key); |
| 343 if (card) |
| 344 return card->Label(); |
| 345 } else { |
| 346 AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key); |
| 347 if (profile) { |
| 348 const std::string app_locale = AutofillCountry::ApplicationLocale(); |
| 349 return section == SECTION_EMAIL ? |
| 350 profile->GetInfo(EMAIL_ADDRESS, app_locale) : profile->Label(); |
| 351 } |
| 352 } |
| 353 |
| 354 // TODO(estade): The FormGroup was likely deleted while menu was showing. We |
| 355 // should not let this happen. |
| 356 return string16(); |
326 } | 357 } |
327 | 358 |
328 void AutofillDialogController::ViewClosed(DialogAction action) { | 359 void AutofillDialogController::ViewClosed(DialogAction action) { |
329 if (action == ACTION_SUBMIT) { | 360 if (action == ACTION_SUBMIT) { |
330 FillOutputForSection(SECTION_EMAIL); | 361 FillOutputForSection(SECTION_EMAIL); |
331 FillOutputForSection(SECTION_CC); | 362 FillOutputForSection(SECTION_CC); |
332 FillOutputForSection(SECTION_BILLING); | 363 FillOutputForSection(SECTION_BILLING); |
333 if (view_->UseBillingForShipping()) { | 364 if (view_->UseBillingForShipping()) { |
334 FillOutputForSectionWithComparator( | 365 FillOutputForSectionWithComparator( |
335 SECTION_BILLING, | 366 SECTION_BILLING, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 } | 452 } |
422 | 453 |
423 void AutofillDialogController::ClearPreviewedForm() { | 454 void AutofillDialogController::ClearPreviewedForm() { |
424 // TODO(estade): implement. | 455 // TODO(estade): implement. |
425 } | 456 } |
426 | 457 |
427 void AutofillDialogController::ControllerDestroyed() { | 458 void AutofillDialogController::ControllerDestroyed() { |
428 popup_controller_ = NULL; | 459 popup_controller_ = NULL; |
429 } | 460 } |
430 | 461 |
431 void AutofillDialogController::GenerateComboboxModels() { | 462 void AutofillDialogController::SuggestionItemSelected( |
| 463 const SuggestionsMenuModel& model) { |
| 464 view_->UpdateSection(SectionForSuggestionsMenuModel(model)); |
| 465 } |
| 466 |
| 467 void AutofillDialogController::GenerateSuggestionsModels() { |
432 PersonalDataManager* manager = GetManager(); | 468 PersonalDataManager* manager = GetManager(); |
433 const std::vector<CreditCard*>& cards = manager->credit_cards(); | 469 const std::vector<CreditCard*>& cards = manager->credit_cards(); |
434 for (size_t i = 0; i < cards.size(); ++i) { | 470 for (size_t i = 0; i < cards.size(); ++i) { |
435 suggested_cc_.AddItem(cards[i]->guid(), cards[i]->Label()); | 471 suggested_cc_.AddKeyedItem(cards[i]->guid(), cards[i]->Label()); |
436 } | 472 } |
437 suggested_cc_.AddItem("", ASCIIToUTF16("Enter new card")); | 473 suggested_cc_.AddKeyedItem("", ASCIIToUTF16("Enter new card")); |
438 | 474 |
439 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); | 475 const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); |
440 const std::string app_locale = AutofillCountry::ApplicationLocale(); | 476 const std::string app_locale = AutofillCountry::ApplicationLocale(); |
441 for (size_t i = 0; i < profiles.size(); ++i) { | 477 for (size_t i = 0; i < profiles.size(); ++i) { |
442 // TODO(estade): deal with variants. | 478 // TODO(estade): deal with variants. |
443 if (!IsCompleteProfile(*profiles[i])) | 479 if (!IsCompleteProfile(*profiles[i])) |
444 continue; | 480 continue; |
445 | 481 |
446 string16 email = profiles[i]->GetInfo(EMAIL_ADDRESS, app_locale); | 482 string16 email = profiles[i]->GetInfo(EMAIL_ADDRESS, app_locale); |
447 if (!email.empty()) | 483 if (!email.empty()) |
448 suggested_email_.AddItem(profiles[i]->guid(), email); | 484 suggested_email_.AddKeyedItem(profiles[i]->guid(), email); |
449 suggested_billing_.AddItem(profiles[i]->guid(), profiles[i]->Label()); | 485 suggested_billing_.AddKeyedItem(profiles[i]->guid(), profiles[i]->Label()); |
450 suggested_shipping_.AddItem(profiles[i]->guid(), profiles[i]->Label()); | 486 suggested_shipping_.AddKeyedItem(profiles[i]->guid(), profiles[i]->Label()); |
451 } | 487 } |
452 suggested_billing_.AddItem("", ASCIIToUTF16("Enter new billing")); | 488 suggested_billing_.AddKeyedItem("", ASCIIToUTF16("Enter new billing")); |
453 suggested_email_.AddItem("", ASCIIToUTF16("Enter new email")); | 489 suggested_email_.AddKeyedItem("", ASCIIToUTF16("Enter new email")); |
454 suggested_shipping_.AddItem("", ASCIIToUTF16("Enter new shipping")); | 490 suggested_shipping_.AddKeyedItem("", ASCIIToUTF16("Enter new shipping")); |
455 } | 491 } |
456 | 492 |
457 | |
458 bool AutofillDialogController::IsCompleteProfile( | 493 bool AutofillDialogController::IsCompleteProfile( |
459 const AutofillProfile& profile) { | 494 const AutofillProfile& profile) { |
460 const std::string app_locale = AutofillCountry::ApplicationLocale(); | 495 const std::string app_locale = AutofillCountry::ApplicationLocale(); |
461 for (size_t i = 0; i < requested_shipping_fields_.size(); ++i) { | 496 for (size_t i = 0; i < requested_shipping_fields_.size(); ++i) { |
462 if (profile.GetInfo(requested_shipping_fields_[i].type, | 497 if (profile.GetInfo(requested_shipping_fields_[i].type, |
463 app_locale).empty()) { | 498 app_locale).empty()) { |
464 return false; | 499 return false; |
465 } | 500 } |
466 } | 501 } |
467 | 502 |
468 return true; | 503 return true; |
469 } | 504 } |
470 | 505 |
471 void AutofillDialogController::FillOutputForSectionWithComparator( | 506 void AutofillDialogController::FillOutputForSectionWithComparator( |
472 DialogSection section, const InputFieldComparator& compare) { | 507 DialogSection section, const InputFieldComparator& compare) { |
473 int suggestion_selection = view_->GetSuggestionSelection(section); | 508 SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); |
474 SuggestionsComboboxModel* model = SuggestionsModelForSection(section); | 509 std::string guid = model->GetItemKeyAt(model->checked_item()); |
475 PersonalDataManager* manager = GetManager(); | 510 PersonalDataManager* manager = GetManager(); |
476 if (suggestion_selection < model->GetItemCount() - 1) { | 511 if (!guid.empty()) { |
477 std::string guid = model->GetItemKeyAt(suggestion_selection); | |
478 FormGroup* form_group = section == SECTION_CC ? | 512 FormGroup* form_group = section == SECTION_CC ? |
479 static_cast<FormGroup*>(manager->GetCreditCardByGUID(guid)) : | 513 static_cast<FormGroup*>(manager->GetCreditCardByGUID(guid)) : |
480 static_cast<FormGroup*>(manager->GetProfileByGUID(guid)); | 514 static_cast<FormGroup*>(manager->GetProfileByGUID(guid)); |
481 // TODO(estade): we shouldn't let this happen. | 515 // TODO(estade): we shouldn't let this happen. |
482 if (!form_group) | 516 if (!form_group) |
483 return; | 517 return; |
484 | 518 |
485 FillFormStructureForSection(*form_group, section, compare); | 519 FillFormStructureForSection(*form_group, section, compare); |
486 } else { | 520 } else { |
487 // The user manually input data. | 521 // The user manually input data. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 const DetailInputs& inputs = RequestedFieldsForSection(section); | 568 const DetailInputs& inputs = RequestedFieldsForSection(section); |
535 for (size_t j = 0; j < inputs.size(); ++j) { | 569 for (size_t j = 0; j < inputs.size(); ++j) { |
536 if (compare.Run(inputs[j], *field)) { | 570 if (compare.Run(inputs[j], *field)) { |
537 form_group.FillFormField(*field, 0, field); | 571 form_group.FillFormField(*field, 0, field); |
538 break; | 572 break; |
539 } | 573 } |
540 } | 574 } |
541 } | 575 } |
542 } | 576 } |
543 | 577 |
544 SuggestionsComboboxModel* AutofillDialogController::SuggestionsModelForSection( | 578 SuggestionsMenuModel* AutofillDialogController::SuggestionsMenuModelForSection( |
545 DialogSection section) { | 579 DialogSection section) { |
546 switch (section) { | 580 switch (section) { |
547 case SECTION_EMAIL: | 581 case SECTION_EMAIL: |
548 return &suggested_email_; | 582 return &suggested_email_; |
549 case SECTION_CC: | 583 case SECTION_CC: |
550 return &suggested_cc_; | 584 return &suggested_cc_; |
551 case SECTION_BILLING: | 585 case SECTION_BILLING: |
552 return &suggested_billing_; | 586 return &suggested_billing_; |
553 case SECTION_SHIPPING: | 587 case SECTION_SHIPPING: |
554 return &suggested_shipping_; | 588 return &suggested_shipping_; |
555 } | 589 } |
556 | 590 |
557 NOTREACHED(); | 591 NOTREACHED(); |
558 return NULL; | 592 return NULL; |
559 } | 593 } |
560 | 594 |
| 595 DialogSection AutofillDialogController::SectionForSuggestionsMenuModel( |
| 596 const SuggestionsMenuModel& model) { |
| 597 if (&model == &suggested_email_) |
| 598 return SECTION_EMAIL; |
| 599 |
| 600 if (&model == &suggested_cc_) |
| 601 return SECTION_CC; |
| 602 |
| 603 if (&model == &suggested_billing_) |
| 604 return SECTION_BILLING; |
| 605 |
| 606 DCHECK_EQ(&model, &suggested_shipping_); |
| 607 return SECTION_SHIPPING; |
| 608 } |
| 609 |
561 PersonalDataManager* AutofillDialogController::GetManager() { | 610 PersonalDataManager* AutofillDialogController::GetManager() { |
562 return PersonalDataManagerFactory::GetForProfile(profile_); | 611 return PersonalDataManagerFactory::GetForProfile(profile_); |
563 } | 612 } |
564 | 613 |
565 } // namespace autofill | 614 } // namespace autofill |
OLD | NEW |