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 4005cd637740de78e4b893536150352e6f725c0c..890909e2246a269ce3f9a5cf3299038573a7947e 100644 |
| --- a/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| +++ b/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc |
| @@ -13,6 +13,7 @@ |
| #include "apps/ui/native_app_window.h" |
| #include "base/base64.h" |
| #include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| #include "base/i18n/case_conversion.h" |
| #include "base/i18n/rtl.h" |
| #include "base/logging.h" |
| @@ -26,6 +27,7 @@ |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/time/time.h" |
| #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| +#include "chrome/browser/autofill/validation_rules_storage_factory.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/autofill/autofill_dialog_common.h" |
| @@ -79,6 +81,10 @@ |
| #include "grit/theme_resources.h" |
| #include "grit/webkit_resources.h" |
| #include "net/cert/cert_status_flags.h" |
| +#include "third_party/libaddressinput/chromium/chrome_downloader_impl.h" |
| +#include "third_party/libaddressinput/chromium/chrome_storage_impl.h" |
| +#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h" |
| +#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_problem.h" |
| #include "ui/base/base_window.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/base/models/combobox_model.h" |
| @@ -88,6 +94,13 @@ |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/skia_util.h" |
| +using ::i18n::addressinput::AddressData; |
| +using ::i18n::addressinput::AddressField; |
| +using ::i18n::addressinput::AddressProblem; |
| +using ::i18n::addressinput::AddressProblemFilter; |
| +using ::i18n::addressinput::AddressProblems; |
| +using ::i18n::addressinput::AddressValidator; |
| + |
| namespace autofill { |
| namespace { |
| @@ -618,6 +631,19 @@ void AutofillDialogControllerImpl::Show() { |
| if (account_chooser_model_->WalletIsSelected()) |
| FetchWalletCookie(); |
| + if (i18ninput::Enabled()) { |
| + scoped_ptr< ::i18n::addressinput::Downloader> downloader( |
| + new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); |
| + validator_ = AddressValidator::Build( |
| + downloader.Pass(), |
| + ValidationRulesStorageFactory::CreateStorage(), |
| + this); |
| + } |
| + |
| + AddressValidator* validator = GetValidator(); |
| + if (validator) |
| + validator->LoadRules(GetManager()->GetDefaultCountryCodeForNewAddress()); |
| + |
| // TODO(estade): don't show the dialog if the site didn't specify the right |
| // fields. First we must figure out what the "right" fields are. |
| SuggestionsUpdated(); |
| @@ -1162,20 +1188,18 @@ void AutofillDialogControllerImpl::RestoreUserInputFromSnapshot( |
| if (snapshot.empty()) |
| return; |
| - FieldMapWrapper wrapper(snapshot); |
| for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) { |
| DialogSection section = static_cast<DialogSection>(i); |
| if (!SectionIsActive(section)) |
| continue; |
| DetailInputs* inputs = MutableRequestedFieldsForSection(section); |
| - wrapper.FillInputs(inputs); |
| - |
| for (size_t i = 0; i < inputs->size(); ++i) { |
| - if (InputWasEdited((*inputs)[i].type, (*inputs)[i].initial_value)) { |
| + DetailInput* input = &(*inputs)[i]; |
| + input->initial_value = |
| + GetInfoFromInputs(snapshot, section, AutofillType(input->type)); |
| + if (InputWasEdited(input->type, input->initial_value)) |
| SuggestionsMenuModelForSection(section)->SetCheckedItem(kAddNewItemKey); |
| - break; |
| - } |
| } |
| } |
| } |
| @@ -1648,7 +1672,14 @@ base::string16 AutofillDialogControllerImpl::InputValidityMessage( |
| } |
| } |
| - switch (AutofillType(type).GetStorableType()) { |
| + AutofillType autofill_type(type); |
| + if (i18ninput::Enabled() && |
| + (autofill_type.group() == ADDRESS_HOME || |
| + autofill_type.group() == ADDRESS_BILLING)) { |
| + return base::string16(); |
|
Evan Stade
2014/01/31 04:09:52
add TODO to remove this and all ADDRESS_ stuff bel
Dan Beam
2014/02/01 00:43:22
Done.
|
| + } |
| + |
| + switch (autofill_type.GetStorableType()) { |
| case EMAIL_ADDRESS: |
| if (!value.empty() && !IsValidEmailAddress(value)) { |
| return l10n_util::GetStringUTF16( |
| @@ -1699,8 +1730,9 @@ base::string16 AutofillDialogControllerImpl::InputValidityMessage( |
| break; |
| case ADDRESS_HOME_STATE: |
| - if (!value.empty() && !autofill::IsValidState(value) && |
| + if (!value.empty() &&!autofill::IsValidState(value) && |
| CountryCodeForSection(section) == "US") { |
| + DCHECK(!i18ninput::Enabled()); |
| return l10n_util::GetStringUTF16( |
| IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_REGION); |
| } |
| @@ -1709,6 +1741,7 @@ base::string16 AutofillDialogControllerImpl::InputValidityMessage( |
| case ADDRESS_HOME_ZIP: |
| if (!value.empty() && !autofill::IsValidZip(value) && |
| CountryCodeForSection(section) == "US") { |
| + DCHECK(!i18ninput::Enabled()); |
| return l10n_util::GetStringUTF16( |
| IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_ZIP_CODE); |
| } |
| @@ -1745,10 +1778,39 @@ ValidityMessages AutofillDialogControllerImpl::InputsAreValid( |
| DialogSection section, |
| const FieldValueMap& inputs) { |
| ValidityMessages messages; |
| + if (inputs.empty()) |
| + return messages; |
| + |
| FieldValueMap field_values; |
| + |
| + AddressValidator* validator = GetValidator(); |
| + if (validator && section != SECTION_CC) { |
| + AddressProblems problems; |
|
Evan Stade
2014/01/31 04:09:52
declare as low as possible
Dan Beam
2014/02/01 00:43:22
Done.
|
| + AddressData address_data; |
| + i18ninput::CreateAddressData( |
| + base::Bind(&GetInfoFromInputs, base::ConstRef(inputs), section), |
| + &address_data); |
| + DCHECK_EQ(2U, address_data.country_code.size()); |
| + if (validator->ValidateAddress(address_data, |
| + AddressProblemFilter(), |
| + &problems) == AddressValidator::SUCCESS) { |
|
Evan Stade
2014/01/31 04:09:52
so if not successful, we just assume the address i
Dan Beam
2014/02/01 00:43:22
added TODOs
|
| + common::AddressType address_type = section == SECTION_SHIPPING ? |
| + common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; |
| + for (size_t i = 0; i < problems.size(); ++i) { |
| + const AddressProblem& problem = problems[i]; |
| + bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; |
| + base::string16 text = l10n_util::GetStringUTF16(problem.description_id); |
| + messages.Set(i18ninput::TypeForField(problem.field, address_type), |
| + ValidityMessage(text, sure)); |
| + } |
| + } |
| + } |
| + |
| for (FieldValueMap::const_iterator iter = inputs.begin(); |
| iter != inputs.end(); ++iter) { |
| const ServerFieldType type = iter->first; |
| + if (!messages.GetMessageOrDefault(type).text.empty()) |
|
Evan Stade
2014/01/31 04:09:52
I don't think you need this with the changes in ht
Dan Beam
2014/02/01 00:43:22
Will resolve when yours lands.
|
| + continue; |
| base::string16 text = InputValidityMessage(section, type, iter->second); |
| @@ -2575,7 +2637,6 @@ AutofillDialogControllerImpl::AutofillDialogControllerImpl( |
| suggested_shipping_(this), |
| cares_about_shipping_(true), |
| popup_input_type_(UNKNOWN_TYPE), |
| - weak_ptr_factory_(this), |
| waiting_for_explicit_sign_in_response_(false), |
| has_accepted_legal_documents_(false), |
| is_submitting_(false), |
| @@ -2583,7 +2644,8 @@ AutofillDialogControllerImpl::AutofillDialogControllerImpl( |
| wallet_server_validation_recoverable_(true), |
| data_was_passed_back_(false), |
| was_ui_latency_logged_(false), |
| - card_generated_animation_(2000, 60, this) { |
| + card_generated_animation_(2000, 60, this), |
| + weak_ptr_factory_(this) { |
| // TODO(estade): remove duplicates from |form_structure|? |
| DCHECK(!callback_.is_null()); |
| } |
| @@ -2592,6 +2654,28 @@ AutofillDialogView* AutofillDialogControllerImpl::CreateView() { |
| return AutofillDialogView::Create(this); |
| } |
| +// static |
| +base::string16 AutofillDialogControllerImpl::GetInfoFromInputs( |
| + const FieldValueMap& inputs, |
| + DialogSection section, |
| + const AutofillType& type) { |
| + ServerFieldType field_type = type.GetStorableType(); |
| + if (section != SECTION_SHIPPING) |
| + field_type = AutofillType::GetEquivalentBillingFieldType(field_type); |
| + |
| + base::string16 info; |
| + FieldValueMap::const_iterator it = inputs.find(field_type); |
| + if (it != inputs.end()) |
| + info = it->second; |
| + |
| + if (!info.empty() && type.html_type() == HTML_TYPE_COUNTRY_CODE) { |
| + info = base::ASCIIToUTF16(AutofillCountry::GetCountryCode( |
| + info, g_browser_process->GetApplicationLocale())); |
| + } |
| + |
| + return info; |
| +} |
| + |
| PersonalDataManager* AutofillDialogControllerImpl::GetManager() const { |
| return PersonalDataManagerFactory::GetForProfile(profile_); |
| } |
| @@ -2605,6 +2689,10 @@ wallet::WalletClient* AutofillDialogControllerImpl::GetWalletClient() { |
| return &wallet_client_; |
| } |
| +AddressValidator* AutofillDialogControllerImpl::GetValidator() { |
| + return validator_.get(); |
| +} |
| + |
| bool AutofillDialogControllerImpl::IsPayingWithWallet() const { |
| return account_chooser_model_->WalletIsSelected() && |
| SignedInState() == SIGNED_IN; |
| @@ -3105,6 +3193,10 @@ bool AutofillDialogControllerImpl::RebuildInputsForCountry( |
| std::string country_code = AutofillCountry::GetCountryCode( |
| country_name, g_browser_process->GetApplicationLocale()); |
| common::BuildInputsForSection(section, country_code, inputs); |
| + |
| + if (GetValidator()) |
|
Evan Stade
2014/01/31 04:09:52
instead of if (GetValidator()), how about if (i18n
Dan Beam
2014/02/01 00:43:22
Done.
|
| + GetValidator()->LoadRules(country_code); |
| + |
| return true; |
| } |
| @@ -3398,6 +3490,12 @@ void AutofillDialogControllerImpl::AnimationEnded( |
| DoFinishSubmit(); |
| } |
| +void AutofillDialogControllerImpl::OnAddressValidationRulesLoaded( |
| + const std::string& country_code, |
| + bool success) { |
| + // TODO(dbeam): ask |view_| to re-validate its contents if necessary. |
| +} |
| + |
| void AutofillDialogControllerImpl::DoFinishSubmit() { |
| FillOutputForSection(SECTION_CC); |
| FillOutputForSection(SECTION_BILLING); |