Chromium Code Reviews| Index: chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| diff --git a/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc b/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| index 529118ab290145944415d73da8be20eaca37dcb0..104ac0830cc3ae162387b73d92e08ff70e26b738 100644 |
| --- a/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| +++ b/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| @@ -7,6 +7,7 @@ |
| #include <string> |
| #include "base/logging.h" |
| +#include "base/string_number_conversions.h" |
| #include "base/string_split.h" |
| #include "base/string_util.h" |
| #include "base/utf_string_conversions.h" |
| @@ -21,6 +22,7 @@ |
| #include "chrome/browser/autofill/wallet/wallet_service_url.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
| +#include "chrome/browser/ui/autofill/data_model_wrapper.h" |
| #include "chrome/common/form_data.h" |
| #include "content/public/browser/navigation_controller.h" |
| #include "content/public/browser/navigation_details.h" |
| @@ -380,6 +382,10 @@ string16 AutofillDialogControllerImpl::SuggestionTextForSection( |
| if (item_key.empty()) |
| return string16(); |
| + // TODO(estade): fix this. |
| + if (CanPayWithWallet()) |
| + return string16(); |
| + |
| if (section == SECTION_EMAIL) |
| return model->GetLabelAt(model->checked_item()); |
| @@ -388,31 +394,69 @@ string16 AutofillDialogControllerImpl::SuggestionTextForSection( |
| return card->TypeAndLastFourDigits(); |
| } |
| - const std::string app_locale = AutofillCountry::ApplicationLocale(); |
| - AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key); |
| + scoped_ptr<DataModelWrapper> wrapper(CreateWrapper(section)); |
| string16 comma = ASCIIToUTF16(", "); |
| - string16 label = profile->GetInfo(NAME_FULL, app_locale) + |
| - comma + profile->GetInfo(ADDRESS_HOME_LINE1, app_locale); |
| - string16 address2 = profile->GetInfo(ADDRESS_HOME_LINE2, app_locale); |
| + string16 label = wrapper->GetInfo(NAME_FULL) + |
|
ahutter
2013/02/12 15:46:42
Are we sure this way of displaying addresses local
Evan Stade
2013/02/12 18:10:22
it definitely doesn't. https://code.google.com/p/c
|
| + comma + wrapper->GetInfo(ADDRESS_HOME_LINE1); |
| + string16 address2 = wrapper->GetInfo(ADDRESS_HOME_LINE2); |
| if (!address2.empty()) |
| label += comma + address2; |
| label += ASCIIToUTF16("\n") + |
| - profile->GetInfo(ADDRESS_HOME_CITY, app_locale) + comma + |
| - profile->GetInfo(ADDRESS_HOME_STATE, app_locale) + ASCIIToUTF16(" ") + |
| - profile->GetInfo(ADDRESS_HOME_ZIP, app_locale); |
| + wrapper->GetInfo(ADDRESS_HOME_CITY) + comma + |
| + wrapper->GetInfo(ADDRESS_HOME_STATE) + ASCIIToUTF16(" ") + |
| + wrapper->GetInfo(ADDRESS_HOME_ZIP); |
| return label; |
| } |
| +DataModelWrapper* AutofillDialogControllerImpl::CreateWrapper( |
| + DialogSection section) { |
| + SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); |
| + std::string item_key = model->GetItemKeyAt(model->checked_item()); |
| + if (item_key.empty()) |
| + return NULL; |
| + |
| + if (CanPayWithWallet()) { |
| + int index; |
| + if (!base::StringToInt(item_key, &index)) |
| + NOTREACHED(); |
| + |
| + if (section == SECTION_CC) |
| + return new WalletInstrumentWrapper(wallet_items_->instruments()[index]); |
| + |
| + return new WalletAddressWrapper(wallet_items_->addresses()[index]); |
| + } |
| + |
| + if (section == SECTION_CC) { |
| + CreditCard* card = GetManager()->GetCreditCardByGUID(item_key); |
| + DCHECK(card); |
| + return new AutofillCreditCardWrapper(card); |
| + } |
| + |
| + // Calculate the variant by looking at how many items come from the same |
| + // FormGroup. TODO(estade): add a test for this. |
| + size_t variant = 0; |
| + for (int i = model->checked_item() - 1; i >= 0; --i) { |
| + if (model->GetItemKeyAt(i) == item_key) |
| + variant++; |
| + else |
| + break; |
| + } |
| + |
| + AutofillProfile* profile = GetManager()->GetProfileByGUID(item_key); |
| + DCHECK(profile); |
| + return new AutofillDataModelWrapper(profile, variant); |
| +} |
| + |
| gfx::Image AutofillDialogControllerImpl::SuggestionIconForSection( |
| DialogSection section) { |
| if (section != SECTION_CC) |
| return gfx::Image(); |
| - std::string item_key = |
| - suggested_cc_.GetItemKeyAt(suggested_cc_.checked_item()); |
| - ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| - CreditCard* card = GetManager()->GetCreditCardByGUID(item_key); |
| - return rb.GetImageNamed(card->IconResourceId()); |
| + scoped_ptr<DataModelWrapper> model(CreateWrapper(section)); |
| + if (!model.get()) |
| + return gfx::Image(); |
| + |
| + return model->GetIcon(); |
| } |
| void AutofillDialogControllerImpl::EditClickedForSection( |
| @@ -427,14 +471,8 @@ void AutofillDialogControllerImpl::EditClickedForSection( |
| (*inputs)[0].autofilled_value = model->GetLabelAt(model->checked_item()); |
| } else { |
| - std::string guid = model->GetItemKeyAt(model->checked_item()); |
| - DCHECK(!guid.empty()); |
| - |
| - FormGroup* form_group = section == SECTION_CC ? |
| - static_cast<FormGroup*>(GetManager()->GetCreditCardByGUID(guid)) : |
| - static_cast<FormGroup*>(GetManager()->GetProfileByGUID(guid)); |
| - DCHECK(form_group); |
| - FillInputFromFormGroup(form_group, inputs); |
| + scoped_ptr<DataModelWrapper> model(CreateWrapper(section)); |
| + model->FillInputs(inputs); |
| } |
| section_editing_state_[section] = true; |
| @@ -701,6 +739,8 @@ void AutofillDialogControllerImpl::OnDidGetWalletItems( |
| WalletRequestCompleted(true); |
| if (items_changed) { |
| + GenerateSuggestionsModels(); |
| + view_->ModelChanged(); |
| view_->UpdateAccountChooser(); |
| view_->UpdateNotificationArea(); |
| } |
| @@ -811,6 +851,8 @@ void AutofillDialogControllerImpl::WalletRequestCompleted(bool success) { |
| if (!success) { |
| had_wallet_error_ = true; |
| wallet_items_.reset(); |
| + GenerateSuggestionsModels(); |
| + view_->ModelChanged(); |
| view_->UpdateAccountChooser(); |
| view_->UpdateNotificationArea(); |
| return; |
| @@ -826,35 +868,51 @@ void AutofillDialogControllerImpl::GenerateSuggestionsModels() { |
| suggested_email_.Reset(); |
| suggested_shipping_.Reset(); |
| - PersonalDataManager* manager = GetManager(); |
| - const std::vector<CreditCard*>& cards = manager->credit_cards(); |
| - for (size_t i = 0; i < cards.size(); ++i) { |
| - suggested_cc_.AddKeyedItem(cards[i]->guid(), cards[i]->Label()); |
| - } |
| - // TODO(estade): real strings and i18n. |
| - suggested_cc_.AddKeyedItem("", ASCIIToUTF16("Enter new card")); |
| - |
| - const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); |
| - const std::string app_locale = AutofillCountry::ApplicationLocale(); |
| - for (size_t i = 0; i < profiles.size(); ++i) { |
| - if (!IsCompleteProfile(*profiles[i])) |
| - continue; |
| - |
| - // Add all email addresses. |
| - std::vector<string16> values; |
| - profiles[i]->GetMultiInfo(EMAIL_ADDRESS, app_locale, &values); |
| - for (size_t j = 0; j < values.size(); ++j) { |
| - if (!values[j].empty()) |
| - suggested_email_.AddKeyedItem(profiles[i]->guid(), values[j]); |
| + if (CanPayWithWallet()) { |
| + if (wallet_items_.get()) { |
| + // TODO(estade): seems we need to hardcode the email address. |
| + // TODO(estade): CC and billing need to be combined into one section, |
| + // and suggestions added here. |
| + const std::vector<wallet::Address*>& addresses = |
| + wallet_items_->addresses(); |
| + for (size_t i = 0; i < addresses.size(); ++i) { |
| + suggested_shipping_.AddKeyedItem(base::IntToString(i), |
| + addresses[i]->DisplayName()); |
| + } |
| } |
| + } else { |
| + PersonalDataManager* manager = GetManager(); |
| + const std::vector<CreditCard*>& cards = manager->credit_cards(); |
| + for (size_t i = 0; i < cards.size(); ++i) { |
| + suggested_cc_.AddKeyedItem(cards[i]->guid(), cards[i]->Label()); |
| + } |
| + |
| + const std::vector<AutofillProfile*>& profiles = manager->GetProfiles(); |
| + const std::string app_locale = AutofillCountry::ApplicationLocale(); |
| + for (size_t i = 0; i < profiles.size(); ++i) { |
| + if (!IsCompleteProfile(*profiles[i])) |
| + continue; |
| + |
| + // Add all email addresses. |
| + std::vector<string16> values; |
| + profiles[i]->GetMultiInfo(EMAIL_ADDRESS, app_locale, &values); |
| + for (size_t j = 0; j < values.size(); ++j) { |
| + if (!values[j].empty()) |
| + suggested_email_.AddKeyedItem(profiles[i]->guid(), values[j]); |
| + } |
| - // Don't add variants for addresses: the email variants are handled above, |
| - // name is part of credit card and we'll just ignore phone number variants. |
| - suggested_billing_.AddKeyedItem(profiles[i]->guid(), profiles[i]->Label()); |
| - suggested_shipping_.AddKeyedItem(profiles[i]->guid(), profiles[i]->Label()); |
| + // Don't add variants for addresses: the email variants are handled above, |
| + // name is part of credit card and we'll just ignore phone number |
| + // variants. |
| + suggested_billing_.AddKeyedItem(profiles[i]->guid(), |
| + profiles[i]->Label()); |
| + suggested_shipping_.AddKeyedItem(profiles[i]->guid(), |
| + profiles[i]->Label()); |
| + } |
| } |
| // TODO(estade): real strings and i18n. |
| + suggested_cc_.AddKeyedItem("", ASCIIToUTF16("Enter new card")); |
| suggested_billing_.AddKeyedItem("", ASCIIToUTF16("Enter new billing")); |
| suggested_email_.AddKeyedItem("", ASCIIToUTF16("Enter new email")); |
| suggested_shipping_.AddKeyedItem("", ASCIIToUTF16("Enter new shipping")); |
| @@ -877,25 +935,13 @@ void AutofillDialogControllerImpl::FillOutputForSectionWithComparator( |
| DialogSection section, |
| const InputFieldComparator& compare) { |
| SuggestionsMenuModel* model = SuggestionsMenuModelForSection(section); |
| - std::string guid = model->GetItemKeyAt(model->checked_item()); |
| + std::string item_key = model->GetItemKeyAt(model->checked_item()); |
| PersonalDataManager* manager = GetManager(); |
| - if (!guid.empty() && !section_editing_state_[section]) { |
| - FormGroup* form_group = section == SECTION_CC ? |
| - static_cast<FormGroup*>(manager->GetCreditCardByGUID(guid)) : |
| - static_cast<FormGroup*>(manager->GetProfileByGUID(guid)); |
| - DCHECK(form_group); |
| - |
| - // Calculate the variant by looking at how many items come from the same |
| - // FormGroup. TODO(estade): add a test for this. |
| - size_t variant = 0; |
| - for (int i = model->checked_item() - 1; i >= 0; --i) { |
| - if (model->GetItemKeyAt(i) == guid) |
| - variant++; |
| - else |
| - break; |
| - } |
| - |
| - FillFormStructureForSection(*form_group, variant, section, compare); |
| + if (!item_key.empty() && !section_editing_state_[section]) { |
| + scoped_ptr<DataModelWrapper> model(CreateWrapper(section)); |
| + // Only fill in data that is associated with this section. |
| + const DetailInputs& inputs = RequestedFieldsForSection(section); |
| + model->FillFormStructure(inputs, compare, &form_structure_); |
| // CVC needs special-casing because the CreditCard class doesn't store |
| // or handle them. |