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

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: Addressing comments. 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 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 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 870
865 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); 871 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
866 // Wallet generates MC virtual cards, so we have to disable it if MC is not 872 // Wallet generates MC virtual cards, so we have to disable it if MC is not
867 // allowed. 873 // allowed.
868 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) 874 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
869 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); 875 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
870 876
871 if (account_chooser_model_->WalletIsSelected()) 877 if (account_chooser_model_->WalletIsSelected())
872 FetchWalletCookie(); 878 FetchWalletCookie();
873 879
874 scoped_ptr< ::i18n::addressinput::Downloader> downloader( 880 validator_.reset(new AddressValidator(
875 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); 881 I18N_ADDRESS_VALIDATION_DATA_URL,
876 validator_ = AddressValidator::Build( 882 scoped_ptr<Downloader>(
877 downloader.Pass(), 883 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())),
878 ValidationRulesStorageFactory::CreateStorage(), 884 ValidationRulesStorageFactory::CreateStorage(),
879 this); 885 this));
880 886
881 SuggestionsUpdated(); 887 SuggestionsUpdated();
882 SubmitButtonDelayBegin(); 888 SubmitButtonDelayBegin();
883 view_.reset(CreateView()); 889 view_.reset(CreateView());
884 view_->Show(); 890 view_->Show();
885 GetManager()->AddObserver(this); 891 GetManager()->AddObserver(this);
886 892
887 if (!account_chooser_model_->WalletIsSelected()) 893 if (!account_chooser_model_->WalletIsSelected())
888 LogDialogLatencyToShow(); 894 LogDialogLatencyToShow();
889 } 895 }
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1941 base::string16 message = CreditCardNumberValidityMessage(value); 1947 base::string16 message = CreditCardNumberValidityMessage(value);
1942 if (!message.empty()) 1948 if (!message.empty())
1943 return message; 1949 return message;
1944 } 1950 }
1945 break; 1951 break;
1946 } 1952 }
1947 1953
1948 case CREDIT_CARD_EXP_MONTH: 1954 case CREDIT_CARD_EXP_MONTH:
1949 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { 1955 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1950 return l10n_util::GetStringUTF16( 1956 return l10n_util::GetStringUTF16(
1951 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1957 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1952 } 1958 }
1953 break; 1959 break;
1954 1960
1955 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 1961 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1956 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { 1962 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1957 return l10n_util::GetStringUTF16( 1963 return l10n_util::GetStringUTF16(
1958 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1964 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1959 } 1965 }
1960 break; 1966 break;
1961 1967
1962 case CREDIT_CARD_VERIFICATION_CODE: 1968 case CREDIT_CARD_VERIFICATION_CODE:
1963 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { 1969 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1964 return l10n_util::GetStringUTF16( 1970 return l10n_util::GetStringUTF16(
1965 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); 1971 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1966 } 1972 }
1967 break; 1973 break;
1968 1974
(...skipping 11 matching lines...) Expand all
1980 1986
1981 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. 1987 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section.
1982 break; 1988 break;
1983 1989
1984 default: 1990 default:
1985 NOTREACHED(); // Trying to validate unknown field. 1991 NOTREACHED(); // Trying to validate unknown field.
1986 break; 1992 break;
1987 } 1993 }
1988 1994
1989 return value.empty() ? l10n_util::GetStringUTF16( 1995 return value.empty() ? l10n_util::GetStringUTF16(
1990 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : 1996 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) :
1991 base::string16(); 1997 base::string16();
1992 } 1998 }
1993 1999
1994 // TODO(groby): Also add tests. 2000 // TODO(groby): Also add tests.
1995 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( 2001 ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
1996 DialogSection section, 2002 DialogSection section,
1997 const FieldValueMap& inputs) { 2003 const FieldValueMap& inputs) {
1998 ValidityMessages messages; 2004 ValidityMessages messages;
1999 if (inputs.empty()) 2005 if (inputs.empty())
2000 return messages; 2006 return messages;
2001 2007
2002 AddressValidator::Status status = AddressValidator::SUCCESS; 2008 AddressValidator::Status status = AddressValidator::SUCCESS;
2003 if (section != SECTION_CC) { 2009 if (section != SECTION_CC) {
2004 AutofillProfile profile; 2010 AutofillProfile profile;
2005 FillFormGroupFromOutputs(inputs, &profile); 2011 FillFormGroupFromOutputs(inputs, &profile);
2006 scoped_ptr<AddressData> address_data = 2012 scoped_ptr<AddressData> address_data =
2007 i18n::CreateAddressDataFromAutofillProfile( 2013 i18n::CreateAddressDataFromAutofillProfile(
2008 profile, g_browser_process->GetApplicationLocale()); 2014 profile, g_browser_process->GetApplicationLocale());
2009 address_data->language_code = AddressLanguageCodeForSection(section); 2015 address_data->language_code = AddressLanguageCodeForSection(section);
2010 2016
2011 AddressProblems problems; 2017 Localization localization;
2012 status = GetValidator()->ValidateAddress(*address_data, 2018 localization.SetGetter(l10n_util::GetStringUTF8);
2013 AddressProblemFilter(), 2019 FieldProblemMap problems;
2014 &problems); 2020 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems);
2015 common::AddressType address_type = section == SECTION_SHIPPING ? 2021 common::AddressType address_type = section == SECTION_SHIPPING ?
2016 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; 2022 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
2017 for (size_t i = 0; i < problems.size(); ++i) { 2023
2018 const AddressProblem& problem = problems[i]; 2024 for (FieldProblemMap::const_iterator iter = problems.begin();
2019 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; 2025 iter != problems.end(); ++iter) {
2020 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); 2026 bool sure = iter->second != MISSING_REQUIRED_FIELD;
2021 messages.Set(i18ninput::TypeForField(problem.field, address_type), 2027 base::string16 text = base::UTF8ToUTF16(
2028 localization.GetErrorMessage(*address_data,
2029 iter->first,
2030 iter->second,
2031 true, // Enable examples.
2032 false)); // Disable links.
2033 messages.Set(i18ninput::TypeForField(iter->first, address_type),
2022 ValidityMessage(text, sure)); 2034 ValidityMessage(text, sure));
2023 } 2035 }
2024 } 2036 }
2025 2037
2026 for (FieldValueMap::const_iterator iter = inputs.begin(); 2038 for (FieldValueMap::const_iterator iter = inputs.begin();
2027 iter != inputs.end(); ++iter) { 2039 iter != inputs.end(); ++iter) {
2028 const ServerFieldType type = iter->first; 2040 const ServerFieldType type = iter->first;
2029 base::string16 text = InputValidityMessage(section, type, iter->second); 2041 base::string16 text = InputValidityMessage(section, type, iter->second);
2030 2042
2031 // Skip empty/unchanged fields in edit mode. If the individual field does 2043 // Skip empty/unchanged fields in edit mode. If the individual field does
(...skipping 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after
3429 return; 3441 return;
3430 3442
3431 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { 3443 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) {
3432 popup_values->push_back(base::UTF8ToUTF16( 3444 popup_values->push_back(base::UTF8ToUTF16(
3433 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); 3445 i18n_validator_suggestions_[i].GetFieldValue(focused_field)));
3434 3446
3435 // Disambiguate the suggestion by showing the smallest administrative 3447 // Disambiguate the suggestion by showing the smallest administrative
3436 // region of the suggested address: 3448 // region of the suggested address:
3437 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY 3449 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3438 popup_labels->push_back(base::string16()); 3450 popup_labels->push_back(base::string16());
3439 for (int field = ::i18n::addressinput::DEPENDENT_LOCALITY; 3451 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) {
3440 field >= ::i18n::addressinput::ADMIN_AREA;
3441 --field) {
3442 const std::string& field_value = 3452 const std::string& field_value =
3443 i18n_validator_suggestions_[i].GetFieldValue( 3453 i18n_validator_suggestions_[i].GetFieldValue(
3444 static_cast<AddressField>(field)); 3454 static_cast<AddressField>(field));
3445 if (focused_field != field && !field_value.empty()) { 3455 if (focused_field != field && !field_value.empty()) {
3446 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); 3456 popup_labels->back().assign(base::UTF8ToUTF16(field_value));
3447 break; 3457 break;
3448 } 3458 }
3449 } 3459 }
3450 } 3460 }
3451 popup_icons->resize(popup_values->size()); 3461 popup_icons->resize(popup_values->size());
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
3614 3624
3615 FieldValueMap detail_outputs; 3625 FieldValueMap detail_outputs;
3616 view_->GetUserInput(section, &detail_outputs); 3626 view_->GetUserInput(section, &detail_outputs);
3617 return !InputsAreValid(section, detail_outputs).HasSureErrors(); 3627 return !InputsAreValid(section, detail_outputs).HasSureErrors();
3618 } 3628 }
3619 3629
3620 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { 3630 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
3621 AddressData address_data; 3631 AddressData address_data;
3622 address_data.region_code = CountryCodeForSection(section); 3632 address_data.region_code = CountryCodeForSection(section);
3623 AddressValidator::Status status = GetValidator()->ValidateAddress( 3633 AddressValidator::Status status = GetValidator()->ValidateAddress(
3624 address_data, AddressProblemFilter(), NULL); 3634 address_data, NULL, NULL);
3625 return status == AddressValidator::SUCCESS; 3635 return status == AddressValidator::SUCCESS;
3626 } 3636 }
3627 3637
3628 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( 3638 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3629 const base::string16& year, 3639 const base::string16& year,
3630 const base::string16& month) const { 3640 const base::string16& month) const {
3631 // If the expiration is in the past as per the local clock, it's invalid. 3641 // If the expiration is in the past as per the local clock, it's invalid.
3632 base::Time now = base::Time::Now(); 3642 base::Time now = base::Time::Now();
3633 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) 3643 if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3634 return false; 3644 return false;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3666 3676
3667 return false; 3677 return false;
3668 } 3678 }
3669 3679
3670 bool AutofillDialogControllerImpl::HasInvalidAddress( 3680 bool AutofillDialogControllerImpl::HasInvalidAddress(
3671 const AutofillProfile& profile) { 3681 const AutofillProfile& profile) {
3672 scoped_ptr<AddressData> address_data = 3682 scoped_ptr<AddressData> address_data =
3673 i18n::CreateAddressDataFromAutofillProfile( 3683 i18n::CreateAddressDataFromAutofillProfile(
3674 profile, g_browser_process->GetApplicationLocale()); 3684 profile, g_browser_process->GetApplicationLocale());
3675 3685
3676 AddressProblems problems; 3686 FieldProblemMap problems;
3677 GetValidator()->ValidateAddress(*address_data, 3687 GetValidator()->ValidateAddress(*address_data, NULL, &problems);
3678 AddressProblemFilter(),
3679 &problems);
3680 return !problems.empty(); 3688 return !problems.empty();
3681 } 3689 }
3682 3690
3683 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { 3691 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3684 return SectionIsActive(SECTION_SHIPPING) && 3692 return SectionIsActive(SECTION_SHIPPING) &&
3685 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; 3693 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3686 } 3694 }
3687 3695
3688 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { 3696 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3689 // It's possible that the user checked [X] Save details locally before 3697 // It's possible that the user checked [X] Save details locally before
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
4168 view_->UpdateButtonStrip(); 4176 view_->UpdateButtonStrip();
4169 } 4177 }
4170 4178
4171 void AutofillDialogControllerImpl::FetchWalletCookie() { 4179 void AutofillDialogControllerImpl::FetchWalletCookie() {
4172 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4180 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4173 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4181 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4174 signin_helper_->StartWalletCookieValueFetch(); 4182 signin_helper_->StartWalletCookieValueFetch();
4175 } 4183 }
4176 4184
4177 } // namespace autofill 4185 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698