Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc

Issue 298863012: Use upstream libaddressinput in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Validate required fields without rules. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_impl.h" 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 #include "content/public/browser/navigation_entry.h" 72 #include "content/public/browser/navigation_entry.h"
73 #include "content/public/browser/notification_service.h" 73 #include "content/public/browser/notification_service.h"
74 #include "content/public/browser/notification_types.h" 74 #include "content/public/browser/notification_types.h"
75 #include "content/public/browser/render_view_host.h" 75 #include "content/public/browser/render_view_host.h"
76 #include "content/public/browser/web_contents.h" 76 #include "content/public/browser/web_contents.h"
77 #include "content/public/common/url_constants.h" 77 #include "content/public/common/url_constants.h"
78 #include "grit/chromium_strings.h" 78 #include "grit/chromium_strings.h"
79 #include "grit/component_scaled_resources.h" 79 #include "grit/component_scaled_resources.h"
80 #include "grit/components_strings.h" 80 #include "grit/components_strings.h"
81 #include "grit/generated_resources.h" 81 #include "grit/generated_resources.h"
82 #include "grit/libaddressinput_strings.h" 82 #include "grit/libaddressinput/messages.h"
83 #include "grit/platform_locale_settings.h" 83 #include "grit/platform_locale_settings.h"
84 #include "grit/theme_resources.h" 84 #include "grit/theme_resources.h"
85 #include "net/cert/cert_status_flags.h" 85 #include "net/cert/cert_status_flags.h"
86 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
86 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h" 87 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
87 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h" 88 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
88 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" 89 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
89 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_problem.h" 90 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h"
91 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_pr oblem.h"
92 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati on.h"
90 #include "ui/base/base_window.h" 93 #include "ui/base/base_window.h"
91 #include "ui/base/l10n/l10n_util.h" 94 #include "ui/base/l10n/l10n_util.h"
92 #include "ui/base/models/combobox_model.h" 95 #include "ui/base/models/combobox_model.h"
93 #include "ui/base/resource/resource_bundle.h" 96 #include "ui/base/resource/resource_bundle.h"
94 #include "ui/gfx/canvas.h" 97 #include "ui/gfx/canvas.h"
95 #include "ui/gfx/image/image_skia_operations.h" 98 #include "ui/gfx/image/image_skia_operations.h"
96 #include "ui/gfx/skia_util.h" 99 #include "ui/gfx/skia_util.h"
97 100
98 using ::i18n::addressinput::AddressData; 101 using ::i18n::addressinput::AddressData;
99 using ::i18n::addressinput::AddressField; 102 using ::i18n::addressinput::AddressField;
100 using ::i18n::addressinput::AddressProblem; 103 using ::i18n::addressinput::AddressProblem;
101 using ::i18n::addressinput::AddressProblemFilter; 104 using ::i18n::addressinput::ADMIN_AREA;
102 using ::i18n::addressinput::AddressProblems; 105 using ::i18n::addressinput::DEPENDENT_LOCALITY;
103 using ::i18n::addressinput::AddressValidator; 106 using ::i18n::addressinput::Downloader;
107 using ::i18n::addressinput::FieldProblemMap;
108 using ::i18n::addressinput::Localization;
109 using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
104 110
105 namespace autofill { 111 namespace autofill {
106 112
107 namespace { 113 namespace {
108 114
109 const char kAddNewItemKey[] = "add-new-item"; 115 const char kAddNewItemKey[] = "add-new-item";
110 const char kManageItemsKey[] = "manage-items"; 116 const char kManageItemsKey[] = "manage-items";
111 const char kSameAsBillingKey[] = "same-as-billing"; 117 const char kSameAsBillingKey[] = "same-as-billing";
112 118
113 // URLs for Wallet error messages. 119 // URLs for Wallet error messages.
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 820
815 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); 821 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
816 // Wallet generates MC virtual cards, so we have to disable it if MC is not 822 // Wallet generates MC virtual cards, so we have to disable it if MC is not
817 // allowed. 823 // allowed.
818 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) 824 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
819 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); 825 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
820 826
821 if (account_chooser_model_->WalletIsSelected()) 827 if (account_chooser_model_->WalletIsSelected())
822 FetchWalletCookie(); 828 FetchWalletCookie();
823 829
824 scoped_ptr< ::i18n::addressinput::Downloader> downloader( 830 validator_.reset(new AddressValidator(
825 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); 831 I18N_ADDRESS_VALIDATION_DATA_URL,
826 validator_ = AddressValidator::Build( 832 scoped_ptr<Downloader>(
827 downloader.Pass(), 833 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())),
828 ValidationRulesStorageFactory::CreateStorage(), 834 ValidationRulesStorageFactory::CreateStorage(),
829 this); 835 this));
830 836
831 SuggestionsUpdated(); 837 SuggestionsUpdated();
832 SubmitButtonDelayBegin(); 838 SubmitButtonDelayBegin();
833 view_.reset(CreateView()); 839 view_.reset(CreateView());
834 view_->Show(); 840 view_->Show();
835 GetManager()->AddObserver(this); 841 GetManager()->AddObserver(this);
836 842
837 if (!account_chooser_model_->WalletIsSelected()) 843 if (!account_chooser_model_->WalletIsSelected())
838 LogDialogLatencyToShow(); 844 LogDialogLatencyToShow();
839 } 845 }
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1891 base::string16 message = CreditCardNumberValidityMessage(value); 1897 base::string16 message = CreditCardNumberValidityMessage(value);
1892 if (!message.empty()) 1898 if (!message.empty())
1893 return message; 1899 return message;
1894 } 1900 }
1895 break; 1901 break;
1896 } 1902 }
1897 1903
1898 case CREDIT_CARD_EXP_MONTH: 1904 case CREDIT_CARD_EXP_MONTH:
1899 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { 1905 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1900 return l10n_util::GetStringUTF16( 1906 return l10n_util::GetStringUTF16(
1901 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1907 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1902 } 1908 }
1903 break; 1909 break;
1904 1910
1905 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 1911 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1906 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { 1912 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1907 return l10n_util::GetStringUTF16( 1913 return l10n_util::GetStringUTF16(
1908 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1914 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1909 } 1915 }
1910 break; 1916 break;
1911 1917
1912 case CREDIT_CARD_VERIFICATION_CODE: 1918 case CREDIT_CARD_VERIFICATION_CODE:
1913 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { 1919 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1914 return l10n_util::GetStringUTF16( 1920 return l10n_util::GetStringUTF16(
1915 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); 1921 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1916 } 1922 }
1917 break; 1923 break;
1918 1924
(...skipping 11 matching lines...) Expand all
1930 1936
1931 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. 1937 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section.
1932 break; 1938 break;
1933 1939
1934 default: 1940 default:
1935 NOTREACHED(); // Trying to validate unknown field. 1941 NOTREACHED(); // Trying to validate unknown field.
1936 break; 1942 break;
1937 } 1943 }
1938 1944
1939 return value.empty() ? l10n_util::GetStringUTF16( 1945 return value.empty() ? l10n_util::GetStringUTF16(
1940 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : 1946 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) :
1941 base::string16(); 1947 base::string16();
1942 } 1948 }
1943 1949
1944 // TODO(groby): Also add tests. 1950 // TODO(groby): Also add tests.
1945 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( 1951 ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
1946 DialogSection section, 1952 DialogSection section,
1947 const FieldValueMap& inputs) { 1953 const FieldValueMap& inputs) {
1948 ValidityMessages messages; 1954 ValidityMessages messages;
1949 if (inputs.empty()) 1955 if (inputs.empty())
1950 return messages; 1956 return messages;
1951 1957
1952 AddressValidator::Status status = AddressValidator::SUCCESS; 1958 AddressValidator::Status status = AddressValidator::SUCCESS;
1953 if (section != SECTION_CC) { 1959 if (section != SECTION_CC) {
1954 AutofillProfile profile; 1960 AutofillProfile profile;
1955 FillFormGroupFromOutputs(inputs, &profile); 1961 FillFormGroupFromOutputs(inputs, &profile);
1956 scoped_ptr<AddressData> address_data = 1962 scoped_ptr<AddressData> address_data =
1957 i18n::CreateAddressDataFromAutofillProfile( 1963 i18n::CreateAddressDataFromAutofillProfile(
1958 profile, g_browser_process->GetApplicationLocale()); 1964 profile, g_browser_process->GetApplicationLocale());
1959 address_data->language_code = AddressLanguageCodeForSection(section); 1965 address_data->language_code = AddressLanguageCodeForSection(section);
1960 1966
1961 AddressProblems problems; 1967 Localization localization;
1962 status = GetValidator()->ValidateAddress(*address_data, 1968 localization.SetGetter(l10n_util::GetStringUTF8);
1963 AddressProblemFilter(), 1969 FieldProblemMap problems;
1964 &problems); 1970 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems);
1965 common::AddressType address_type = section == SECTION_SHIPPING ? 1971 common::AddressType address_type = section == SECTION_SHIPPING ?
1966 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; 1972 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
1967 for (size_t i = 0; i < problems.size(); ++i) { 1973
1968 const AddressProblem& problem = problems[i]; 1974 for (FieldProblemMap::const_iterator iter = problems.begin();
1969 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; 1975 iter != problems.end(); ++iter) {
1970 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); 1976 bool sure = iter->second != MISSING_REQUIRED_FIELD;
1971 messages.Set(i18ninput::TypeForField(problem.field, address_type), 1977 base::string16 text = base::UTF8ToUTF16(
1978 localization.GetErrorMessage(*address_data,
1979 iter->first,
1980 iter->second,
1981 true, // Enable examples.
1982 false)); // Disable links.
1983 messages.Set(i18ninput::TypeForField(iter->first, address_type),
1972 ValidityMessage(text, sure)); 1984 ValidityMessage(text, sure));
1973 } 1985 }
1974 } 1986 }
1975 1987
1976 for (FieldValueMap::const_iterator iter = inputs.begin(); 1988 for (FieldValueMap::const_iterator iter = inputs.begin();
1977 iter != inputs.end(); ++iter) { 1989 iter != inputs.end(); ++iter) {
1978 const ServerFieldType type = iter->first; 1990 const ServerFieldType type = iter->first;
1979 base::string16 text = InputValidityMessage(section, type, iter->second); 1991 base::string16 text = InputValidityMessage(section, type, iter->second);
1980 1992
1981 // Skip empty/unchanged fields in edit mode. If the individual field does 1993 // Skip empty/unchanged fields in edit mode. If the individual field does
(...skipping 1379 matching lines...) Expand 10 before | Expand all | Expand 10 after
3361 return; 3373 return;
3362 3374
3363 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { 3375 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) {
3364 popup_values->push_back(base::UTF8ToUTF16( 3376 popup_values->push_back(base::UTF8ToUTF16(
3365 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); 3377 i18n_validator_suggestions_[i].GetFieldValue(focused_field)));
3366 3378
3367 // Disambiguate the suggestion by showing the smallest administrative 3379 // Disambiguate the suggestion by showing the smallest administrative
3368 // region of the suggested address: 3380 // region of the suggested address:
3369 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY 3381 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3370 popup_labels->push_back(base::string16()); 3382 popup_labels->push_back(base::string16());
3371 for (int field = ::i18n::addressinput::DEPENDENT_LOCALITY; 3383 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) {
3372 field >= ::i18n::addressinput::ADMIN_AREA;
3373 --field) {
3374 const std::string& field_value = 3384 const std::string& field_value =
3375 i18n_validator_suggestions_[i].GetFieldValue( 3385 i18n_validator_suggestions_[i].GetFieldValue(
3376 static_cast<AddressField>(field)); 3386 static_cast<AddressField>(field));
3377 if (focused_field != field && !field_value.empty()) { 3387 if (focused_field != field && !field_value.empty()) {
3378 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); 3388 popup_labels->back().assign(base::UTF8ToUTF16(field_value));
3379 break; 3389 break;
3380 } 3390 }
3381 } 3391 }
3382 } 3392 }
3383 popup_icons->resize(popup_values->size()); 3393 popup_icons->resize(popup_values->size());
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
3534 if (!IsManuallyEditingSection(section)) 3544 if (!IsManuallyEditingSection(section))
3535 return true; 3545 return true;
3536 3546
3537 FieldValueMap detail_outputs; 3547 FieldValueMap detail_outputs;
3538 view_->GetUserInput(section, &detail_outputs); 3548 view_->GetUserInput(section, &detail_outputs);
3539 return !InputsAreValid(section, detail_outputs).HasSureErrors(); 3549 return !InputsAreValid(section, detail_outputs).HasSureErrors();
3540 } 3550 }
3541 3551
3542 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { 3552 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
3543 AddressData address_data; 3553 AddressData address_data;
3544 address_data.country_code = CountryCodeForSection(section); 3554 address_data.region_code = CountryCodeForSection(section);
3545 AddressValidator::Status status = GetValidator()->ValidateAddress( 3555 AddressValidator::Status status = GetValidator()->ValidateAddress(
3546 address_data, AddressProblemFilter(), NULL); 3556 address_data, NULL, NULL);
3547 return status == AddressValidator::SUCCESS; 3557 return status == AddressValidator::SUCCESS;
3548 } 3558 }
3549 3559
3550 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( 3560 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3551 const base::string16& year, 3561 const base::string16& year,
3552 const base::string16& month) const { 3562 const base::string16& month) const {
3553 // If the expiration is in the past as per the local clock, it's invalid. 3563 // If the expiration is in the past as per the local clock, it's invalid.
3554 base::Time now = base::Time::Now(); 3564 base::Time now = base::Time::Now();
3555 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) 3565 if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3556 return false; 3566 return false;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3588 3598
3589 return false; 3599 return false;
3590 } 3600 }
3591 3601
3592 bool AutofillDialogControllerImpl::HasInvalidAddress( 3602 bool AutofillDialogControllerImpl::HasInvalidAddress(
3593 const AutofillProfile& profile) { 3603 const AutofillProfile& profile) {
3594 scoped_ptr<AddressData> address_data = 3604 scoped_ptr<AddressData> address_data =
3595 i18n::CreateAddressDataFromAutofillProfile( 3605 i18n::CreateAddressDataFromAutofillProfile(
3596 profile, g_browser_process->GetApplicationLocale()); 3606 profile, g_browser_process->GetApplicationLocale());
3597 3607
3598 AddressProblems problems; 3608 FieldProblemMap problems;
3599 GetValidator()->ValidateAddress(*address_data, 3609 GetValidator()->ValidateAddress(*address_data, NULL, &problems);
3600 AddressProblemFilter(),
3601 &problems);
3602 return !problems.empty(); 3610 return !problems.empty();
3603 } 3611 }
3604 3612
3605 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { 3613 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3606 return SectionIsActive(SECTION_SHIPPING) && 3614 return SectionIsActive(SECTION_SHIPPING) &&
3607 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; 3615 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3608 } 3616 }
3609 3617
3610 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { 3618 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3611 // It's possible that the user checked [X] Save details locally before 3619 // It's possible that the user checked [X] Save details locally before
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
4090 view_->UpdateButtonStrip(); 4098 view_->UpdateButtonStrip();
4091 } 4099 }
4092 4100
4093 void AutofillDialogControllerImpl::FetchWalletCookie() { 4101 void AutofillDialogControllerImpl::FetchWalletCookie() {
4094 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4102 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4095 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4103 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4096 signin_helper_->StartWalletCookieValueFetch(); 4104 signin_helper_->StartWalletCookieValueFetch();
4097 } 4105 }
4098 4106
4099 } // namespace autofill 4107 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698