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

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

Issue 389733002: Reland "Use upstream libaddressinput in Chrome." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix GN build. Created 6 years, 5 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 #include "content/public/browser/navigation_entry.h" 73 #include "content/public/browser/navigation_entry.h"
74 #include "content/public/browser/notification_service.h" 74 #include "content/public/browser/notification_service.h"
75 #include "content/public/browser/notification_types.h" 75 #include "content/public/browser/notification_types.h"
76 #include "content/public/browser/render_view_host.h" 76 #include "content/public/browser/render_view_host.h"
77 #include "content/public/browser/web_contents.h" 77 #include "content/public/browser/web_contents.h"
78 #include "content/public/common/url_constants.h" 78 #include "content/public/common/url_constants.h"
79 #include "grit/chromium_strings.h" 79 #include "grit/chromium_strings.h"
80 #include "grit/component_scaled_resources.h" 80 #include "grit/component_scaled_resources.h"
81 #include "grit/components_strings.h" 81 #include "grit/components_strings.h"
82 #include "grit/generated_resources.h" 82 #include "grit/generated_resources.h"
83 #include "grit/libaddressinput_strings.h"
84 #include "grit/platform_locale_settings.h" 83 #include "grit/platform_locale_settings.h"
85 #include "grit/theme_resources.h" 84 #include "grit/theme_resources.h"
86 #include "net/cert/cert_status_flags.h" 85 #include "net/cert/cert_status_flags.h"
87 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h" 86 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
88 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h" 87 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
89 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" 88 #include "third_party/libaddressinput/messages.h"
90 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_problem.h" 89 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.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"
91 #include "ui/base/base_window.h" 93 #include "ui/base/base_window.h"
92 #include "ui/base/l10n/l10n_util.h" 94 #include "ui/base/l10n/l10n_util.h"
93 #include "ui/base/models/combobox_model.h" 95 #include "ui/base/models/combobox_model.h"
94 #include "ui/base/resource/resource_bundle.h" 96 #include "ui/base/resource/resource_bundle.h"
95 #include "ui/gfx/canvas.h" 97 #include "ui/gfx/canvas.h"
96 #include "ui/gfx/image/image_skia_operations.h" 98 #include "ui/gfx/image/image_skia_operations.h"
97 #include "ui/gfx/skia_util.h" 99 #include "ui/gfx/skia_util.h"
98 100
99 using ::i18n::addressinput::AddressData; 101 using ::i18n::addressinput::AddressData;
100 using ::i18n::addressinput::AddressField; 102 using ::i18n::addressinput::AddressField;
101 using ::i18n::addressinput::AddressProblem; 103 using ::i18n::addressinput::AddressProblem;
102 using ::i18n::addressinput::AddressProblemFilter; 104 using ::i18n::addressinput::ADMIN_AREA;
103 using ::i18n::addressinput::AddressProblems; 105 using ::i18n::addressinput::DEPENDENT_LOCALITY;
104 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;
105 110
106 namespace autofill { 111 namespace autofill {
107 112
108 namespace { 113 namespace {
109 114
110 const char kAddNewItemKey[] = "add-new-item"; 115 const char kAddNewItemKey[] = "add-new-item";
111 const char kManageItemsKey[] = "manage-items"; 116 const char kManageItemsKey[] = "manage-items";
112 const char kSameAsBillingKey[] = "same-as-billing"; 117 const char kSameAsBillingKey[] = "same-as-billing";
113 118
114 // URLs for Wallet error messages. 119 // URLs for Wallet error messages.
(...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 854
850 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); 855 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
851 // Wallet generates MC virtual cards, so we have to disable it if MC is not 856 // Wallet generates MC virtual cards, so we have to disable it if MC is not
852 // allowed. 857 // allowed.
853 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) 858 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
854 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); 859 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
855 860
856 if (account_chooser_model_->WalletIsSelected()) 861 if (account_chooser_model_->WalletIsSelected())
857 FetchWalletCookie(); 862 FetchWalletCookie();
858 863
859 scoped_ptr< ::i18n::addressinput::Downloader> downloader( 864 validator_.reset(new AddressValidator(
860 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); 865 I18N_ADDRESS_VALIDATION_DATA_URL,
861 validator_ = AddressValidator::Build( 866 scoped_ptr<Downloader>(
862 downloader.Pass(), 867 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())),
863 ValidationRulesStorageFactory::CreateStorage(), 868 ValidationRulesStorageFactory::CreateStorage(),
864 this); 869 this));
865 870
866 SuggestionsUpdated(); 871 SuggestionsUpdated();
867 SubmitButtonDelayBegin(); 872 SubmitButtonDelayBegin();
868 view_.reset(CreateView()); 873 view_.reset(CreateView());
869 view_->Show(); 874 view_->Show();
870 GetManager()->AddObserver(this); 875 GetManager()->AddObserver(this);
871 876
872 if (!account_chooser_model_->WalletIsSelected()) 877 if (!account_chooser_model_->WalletIsSelected())
873 LogDialogLatencyToShow(); 878 LogDialogLatencyToShow();
874 } 879 }
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1926 base::string16 message = CreditCardNumberValidityMessage(value); 1931 base::string16 message = CreditCardNumberValidityMessage(value);
1927 if (!message.empty()) 1932 if (!message.empty())
1928 return message; 1933 return message;
1929 } 1934 }
1930 break; 1935 break;
1931 } 1936 }
1932 1937
1933 case CREDIT_CARD_EXP_MONTH: 1938 case CREDIT_CARD_EXP_MONTH:
1934 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { 1939 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1935 return l10n_util::GetStringUTF16( 1940 return l10n_util::GetStringUTF16(
1936 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1941 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1937 } 1942 }
1938 break; 1943 break;
1939 1944
1940 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 1945 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1941 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { 1946 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1942 return l10n_util::GetStringUTF16( 1947 return l10n_util::GetStringUTF16(
1943 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1948 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1944 } 1949 }
1945 break; 1950 break;
1946 1951
1947 case CREDIT_CARD_VERIFICATION_CODE: 1952 case CREDIT_CARD_VERIFICATION_CODE:
1948 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { 1953 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1949 return l10n_util::GetStringUTF16( 1954 return l10n_util::GetStringUTF16(
1950 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); 1955 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1951 } 1956 }
1952 break; 1957 break;
1953 1958
(...skipping 11 matching lines...) Expand all
1965 1970
1966 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. 1971 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section.
1967 break; 1972 break;
1968 1973
1969 default: 1974 default:
1970 NOTREACHED(); // Trying to validate unknown field. 1975 NOTREACHED(); // Trying to validate unknown field.
1971 break; 1976 break;
1972 } 1977 }
1973 1978
1974 return value.empty() ? l10n_util::GetStringUTF16( 1979 return value.empty() ? l10n_util::GetStringUTF16(
1975 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : 1980 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) :
1976 base::string16(); 1981 base::string16();
1977 } 1982 }
1978 1983
1979 // TODO(groby): Also add tests. 1984 // TODO(groby): Also add tests.
1980 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( 1985 ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
1981 DialogSection section, 1986 DialogSection section,
1982 const FieldValueMap& inputs) { 1987 const FieldValueMap& inputs) {
1983 ValidityMessages messages; 1988 ValidityMessages messages;
1984 if (inputs.empty()) 1989 if (inputs.empty())
1985 return messages; 1990 return messages;
1986 1991
1987 AddressValidator::Status status = AddressValidator::SUCCESS; 1992 AddressValidator::Status status = AddressValidator::SUCCESS;
1988 if (section != SECTION_CC) { 1993 if (section != SECTION_CC) {
1989 AutofillProfile profile; 1994 AutofillProfile profile;
1990 FillFormGroupFromOutputs(inputs, &profile); 1995 FillFormGroupFromOutputs(inputs, &profile);
1991 scoped_ptr<AddressData> address_data = 1996 scoped_ptr<AddressData> address_data =
1992 i18n::CreateAddressDataFromAutofillProfile( 1997 i18n::CreateAddressDataFromAutofillProfile(
1993 profile, g_browser_process->GetApplicationLocale()); 1998 profile, g_browser_process->GetApplicationLocale());
1994 address_data->language_code = AddressLanguageCodeForSection(section); 1999 address_data->language_code = AddressLanguageCodeForSection(section);
1995 2000
1996 AddressProblems problems; 2001 Localization localization;
1997 status = GetValidator()->ValidateAddress(*address_data, 2002 localization.SetGetter(l10n_util::GetStringUTF8);
1998 AddressProblemFilter(), 2003 FieldProblemMap problems;
1999 &problems); 2004 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems);
2000 common::AddressType address_type = section == SECTION_SHIPPING ? 2005 common::AddressType address_type = section == SECTION_SHIPPING ?
2001 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; 2006 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
2002 for (size_t i = 0; i < problems.size(); ++i) { 2007
2003 const AddressProblem& problem = problems[i]; 2008 for (FieldProblemMap::const_iterator iter = problems.begin();
2004 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; 2009 iter != problems.end(); ++iter) {
2005 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); 2010 bool sure = iter->second != MISSING_REQUIRED_FIELD;
2006 messages.Set(i18ninput::TypeForField(problem.field, address_type), 2011 base::string16 text = base::UTF8ToUTF16(localization.GetErrorMessage(
2012 *address_data, iter->first, iter->second, true, false));
2013 messages.Set(i18ninput::TypeForField(iter->first, address_type),
2007 ValidityMessage(text, sure)); 2014 ValidityMessage(text, sure));
2008 } 2015 }
2009 } 2016 }
2010 2017
2011 for (FieldValueMap::const_iterator iter = inputs.begin(); 2018 for (FieldValueMap::const_iterator iter = inputs.begin();
2012 iter != inputs.end(); ++iter) { 2019 iter != inputs.end(); ++iter) {
2013 const ServerFieldType type = iter->first; 2020 const ServerFieldType type = iter->first;
2014 base::string16 text = InputValidityMessage(section, type, iter->second); 2021 base::string16 text = InputValidityMessage(section, type, iter->second);
2015 2022
2016 // Skip empty/unchanged fields in edit mode. If the individual field does 2023 // Skip empty/unchanged fields in edit mode. If the individual field does
(...skipping 1399 matching lines...) Expand 10 before | Expand all | Expand 10 after
3416 return; 3423 return;
3417 3424
3418 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { 3425 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) {
3419 popup_values->push_back(base::UTF8ToUTF16( 3426 popup_values->push_back(base::UTF8ToUTF16(
3420 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); 3427 i18n_validator_suggestions_[i].GetFieldValue(focused_field)));
3421 3428
3422 // Disambiguate the suggestion by showing the smallest administrative 3429 // Disambiguate the suggestion by showing the smallest administrative
3423 // region of the suggested address: 3430 // region of the suggested address:
3424 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY 3431 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3425 popup_labels->push_back(base::string16()); 3432 popup_labels->push_back(base::string16());
3426 for (int field = ::i18n::addressinput::DEPENDENT_LOCALITY; 3433 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) {
3427 field >= ::i18n::addressinput::ADMIN_AREA;
3428 --field) {
3429 const std::string& field_value = 3434 const std::string& field_value =
3430 i18n_validator_suggestions_[i].GetFieldValue( 3435 i18n_validator_suggestions_[i].GetFieldValue(
3431 static_cast<AddressField>(field)); 3436 static_cast<AddressField>(field));
3432 if (focused_field != field && !field_value.empty()) { 3437 if (focused_field != field && !field_value.empty()) {
3433 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); 3438 popup_labels->back().assign(base::UTF8ToUTF16(field_value));
3434 break; 3439 break;
3435 } 3440 }
3436 } 3441 }
3437 } 3442 }
3438 popup_icons->resize(popup_values->size()); 3443 popup_icons->resize(popup_values->size());
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
3601 3606
3602 FieldValueMap detail_outputs; 3607 FieldValueMap detail_outputs;
3603 view_->GetUserInput(section, &detail_outputs); 3608 view_->GetUserInput(section, &detail_outputs);
3604 return !InputsAreValid(section, detail_outputs).HasSureErrors(); 3609 return !InputsAreValid(section, detail_outputs).HasSureErrors();
3605 } 3610 }
3606 3611
3607 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { 3612 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
3608 AddressData address_data; 3613 AddressData address_data;
3609 address_data.region_code = CountryCodeForSection(section); 3614 address_data.region_code = CountryCodeForSection(section);
3610 AddressValidator::Status status = GetValidator()->ValidateAddress( 3615 AddressValidator::Status status = GetValidator()->ValidateAddress(
3611 address_data, AddressProblemFilter(), NULL); 3616 address_data, NULL, NULL);
3612 return status == AddressValidator::SUCCESS; 3617 return status == AddressValidator::SUCCESS;
3613 } 3618 }
3614 3619
3615 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( 3620 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3616 const base::string16& year, 3621 const base::string16& year,
3617 const base::string16& month) const { 3622 const base::string16& month) const {
3618 // If the expiration is in the past as per the local clock, it's invalid. 3623 // If the expiration is in the past as per the local clock, it's invalid.
3619 base::Time now = base::Time::Now(); 3624 base::Time now = base::Time::Now();
3620 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) 3625 if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3621 return false; 3626 return false;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3653 3658
3654 return false; 3659 return false;
3655 } 3660 }
3656 3661
3657 bool AutofillDialogControllerImpl::HasInvalidAddress( 3662 bool AutofillDialogControllerImpl::HasInvalidAddress(
3658 const AutofillProfile& profile) { 3663 const AutofillProfile& profile) {
3659 scoped_ptr<AddressData> address_data = 3664 scoped_ptr<AddressData> address_data =
3660 i18n::CreateAddressDataFromAutofillProfile( 3665 i18n::CreateAddressDataFromAutofillProfile(
3661 profile, g_browser_process->GetApplicationLocale()); 3666 profile, g_browser_process->GetApplicationLocale());
3662 3667
3663 AddressProblems problems; 3668 FieldProblemMap problems;
3664 GetValidator()->ValidateAddress(*address_data, 3669 GetValidator()->ValidateAddress(*address_data, NULL, &problems);
3665 AddressProblemFilter(),
3666 &problems);
3667 return !problems.empty(); 3670 return !problems.empty();
3668 } 3671 }
3669 3672
3670 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { 3673 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3671 return SectionIsActive(SECTION_SHIPPING) && 3674 return SectionIsActive(SECTION_SHIPPING) &&
3672 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; 3675 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3673 } 3676 }
3674 3677
3675 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { 3678 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3676 // It's possible that the user checked [X] Save details locally before 3679 // It's possible that the user checked [X] Save details locally before
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
4155 view_->UpdateButtonStrip(); 4158 view_->UpdateButtonStrip();
4156 } 4159 }
4157 4160
4158 void AutofillDialogControllerImpl::FetchWalletCookie() { 4161 void AutofillDialogControllerImpl::FetchWalletCookie() {
4159 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4162 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4160 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4163 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4161 signin_helper_->StartWalletCookieValueFetch(); 4164 signin_helper_->StartWalletCookieValueFetch();
4162 } 4165 }
4163 4166
4164 } // namespace autofill 4167 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698