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

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: Rebase on top of https://codereview.chromium.org/359213002/ 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 728 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 848
843 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); 849 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
844 // Wallet generates MC virtual cards, so we have to disable it if MC is not 850 // Wallet generates MC virtual cards, so we have to disable it if MC is not
845 // allowed. 851 // allowed.
846 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) 852 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
847 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); 853 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
848 854
849 if (account_chooser_model_->WalletIsSelected()) 855 if (account_chooser_model_->WalletIsSelected())
850 FetchWalletCookie(); 856 FetchWalletCookie();
851 857
852 scoped_ptr< ::i18n::addressinput::Downloader> downloader( 858 validator_.reset(new AddressValidator(
853 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); 859 I18N_ADDRESS_VALIDATION_DATA_URL,
854 validator_ = AddressValidator::Build( 860 scoped_ptr<Downloader>(
855 downloader.Pass(), 861 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())),
856 ValidationRulesStorageFactory::CreateStorage(), 862 ValidationRulesStorageFactory::CreateStorage(),
857 this); 863 this));
858 864
859 SuggestionsUpdated(); 865 SuggestionsUpdated();
860 SubmitButtonDelayBegin(); 866 SubmitButtonDelayBegin();
861 view_.reset(CreateView()); 867 view_.reset(CreateView());
862 view_->Show(); 868 view_->Show();
863 GetManager()->AddObserver(this); 869 GetManager()->AddObserver(this);
864 870
865 if (!account_chooser_model_->WalletIsSelected()) 871 if (!account_chooser_model_->WalletIsSelected())
866 LogDialogLatencyToShow(); 872 LogDialogLatencyToShow();
867 } 873 }
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1919 base::string16 message = CreditCardNumberValidityMessage(value); 1925 base::string16 message = CreditCardNumberValidityMessage(value);
1920 if (!message.empty()) 1926 if (!message.empty())
1921 return message; 1927 return message;
1922 } 1928 }
1923 break; 1929 break;
1924 } 1930 }
1925 1931
1926 case CREDIT_CARD_EXP_MONTH: 1932 case CREDIT_CARD_EXP_MONTH:
1927 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { 1933 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1928 return l10n_util::GetStringUTF16( 1934 return l10n_util::GetStringUTF16(
1929 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1935 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1930 } 1936 }
1931 break; 1937 break;
1932 1938
1933 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 1939 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1934 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { 1940 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1935 return l10n_util::GetStringUTF16( 1941 return l10n_util::GetStringUTF16(
1936 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1942 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1937 } 1943 }
1938 break; 1944 break;
1939 1945
1940 case CREDIT_CARD_VERIFICATION_CODE: 1946 case CREDIT_CARD_VERIFICATION_CODE:
1941 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { 1947 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1942 return l10n_util::GetStringUTF16( 1948 return l10n_util::GetStringUTF16(
1943 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); 1949 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1944 } 1950 }
1945 break; 1951 break;
1946 1952
(...skipping 11 matching lines...) Expand all
1958 1964
1959 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. 1965 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section.
1960 break; 1966 break;
1961 1967
1962 default: 1968 default:
1963 NOTREACHED(); // Trying to validate unknown field. 1969 NOTREACHED(); // Trying to validate unknown field.
1964 break; 1970 break;
1965 } 1971 }
1966 1972
1967 return value.empty() ? l10n_util::GetStringUTF16( 1973 return value.empty() ? l10n_util::GetStringUTF16(
1968 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : 1974 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) :
1969 base::string16(); 1975 base::string16();
1970 } 1976 }
1971 1977
1972 // TODO(groby): Also add tests. 1978 // TODO(groby): Also add tests.
1973 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( 1979 ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
1974 DialogSection section, 1980 DialogSection section,
1975 const FieldValueMap& inputs) { 1981 const FieldValueMap& inputs) {
1976 ValidityMessages messages; 1982 ValidityMessages messages;
1977 if (inputs.empty()) 1983 if (inputs.empty())
1978 return messages; 1984 return messages;
1979 1985
1980 AddressValidator::Status status = AddressValidator::SUCCESS; 1986 AddressValidator::Status status = AddressValidator::SUCCESS;
1981 if (section != SECTION_CC) { 1987 if (section != SECTION_CC) {
1982 AutofillProfile profile; 1988 AutofillProfile profile;
1983 FillFormGroupFromOutputs(inputs, &profile); 1989 FillFormGroupFromOutputs(inputs, &profile);
1984 scoped_ptr<AddressData> address_data = 1990 scoped_ptr<AddressData> address_data =
1985 i18n::CreateAddressDataFromAutofillProfile( 1991 i18n::CreateAddressDataFromAutofillProfile(
1986 profile, g_browser_process->GetApplicationLocale()); 1992 profile, g_browser_process->GetApplicationLocale());
1987 address_data->language_code = AddressLanguageCodeForSection(section); 1993 address_data->language_code = AddressLanguageCodeForSection(section);
1988 1994
1989 AddressProblems problems; 1995 Localization localization;
1990 status = GetValidator()->ValidateAddress(*address_data, 1996 localization.SetGetter(l10n_util::GetStringUTF8);
1991 AddressProblemFilter(), 1997 FieldProblemMap problems;
1992 &problems); 1998 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems);
1993 common::AddressType address_type = section == SECTION_SHIPPING ? 1999 common::AddressType address_type = section == SECTION_SHIPPING ?
1994 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; 2000 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
1995 for (size_t i = 0; i < problems.size(); ++i) { 2001
1996 const AddressProblem& problem = problems[i]; 2002 for (FieldProblemMap::const_iterator iter = problems.begin();
1997 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; 2003 iter != problems.end(); ++iter) {
1998 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); 2004 bool sure = iter->second != MISSING_REQUIRED_FIELD;
1999 messages.Set(i18ninput::TypeForField(problem.field, address_type), 2005 base::string16 text = base::UTF8ToUTF16(
2006 localization.GetErrorMessage(*address_data,
2007 iter->first,
2008 iter->second,
2009 true, // Enable examples.
2010 false)); // Disable links.
2011 messages.Set(i18ninput::TypeForField(iter->first, address_type),
2000 ValidityMessage(text, sure)); 2012 ValidityMessage(text, sure));
2001 } 2013 }
2002 } 2014 }
2003 2015
2004 for (FieldValueMap::const_iterator iter = inputs.begin(); 2016 for (FieldValueMap::const_iterator iter = inputs.begin();
2005 iter != inputs.end(); ++iter) { 2017 iter != inputs.end(); ++iter) {
2006 const ServerFieldType type = iter->first; 2018 const ServerFieldType type = iter->first;
2007 base::string16 text = InputValidityMessage(section, type, iter->second); 2019 base::string16 text = InputValidityMessage(section, type, iter->second);
2008 2020
2009 // Skip empty/unchanged fields in edit mode. If the individual field does 2021 // Skip empty/unchanged fields in edit mode. If the individual field does
(...skipping 1379 matching lines...) Expand 10 before | Expand all | Expand 10 after
3389 return; 3401 return;
3390 3402
3391 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { 3403 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) {
3392 popup_values->push_back(base::UTF8ToUTF16( 3404 popup_values->push_back(base::UTF8ToUTF16(
3393 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); 3405 i18n_validator_suggestions_[i].GetFieldValue(focused_field)));
3394 3406
3395 // Disambiguate the suggestion by showing the smallest administrative 3407 // Disambiguate the suggestion by showing the smallest administrative
3396 // region of the suggested address: 3408 // region of the suggested address:
3397 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY 3409 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY
3398 popup_labels->push_back(base::string16()); 3410 popup_labels->push_back(base::string16());
3399 for (int field = ::i18n::addressinput::DEPENDENT_LOCALITY; 3411 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) {
3400 field >= ::i18n::addressinput::ADMIN_AREA;
3401 --field) {
3402 const std::string& field_value = 3412 const std::string& field_value =
3403 i18n_validator_suggestions_[i].GetFieldValue( 3413 i18n_validator_suggestions_[i].GetFieldValue(
3404 static_cast<AddressField>(field)); 3414 static_cast<AddressField>(field));
3405 if (focused_field != field && !field_value.empty()) { 3415 if (focused_field != field && !field_value.empty()) {
3406 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); 3416 popup_labels->back().assign(base::UTF8ToUTF16(field_value));
3407 break; 3417 break;
3408 } 3418 }
3409 } 3419 }
3410 } 3420 }
3411 popup_icons->resize(popup_values->size()); 3421 popup_icons->resize(popup_values->size());
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
3574 3584
3575 FieldValueMap detail_outputs; 3585 FieldValueMap detail_outputs;
3576 view_->GetUserInput(section, &detail_outputs); 3586 view_->GetUserInput(section, &detail_outputs);
3577 return !InputsAreValid(section, detail_outputs).HasSureErrors(); 3587 return !InputsAreValid(section, detail_outputs).HasSureErrors();
3578 } 3588 }
3579 3589
3580 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { 3590 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
3581 AddressData address_data; 3591 AddressData address_data;
3582 address_data.region_code = CountryCodeForSection(section); 3592 address_data.region_code = CountryCodeForSection(section);
3583 AddressValidator::Status status = GetValidator()->ValidateAddress( 3593 AddressValidator::Status status = GetValidator()->ValidateAddress(
3584 address_data, AddressProblemFilter(), NULL); 3594 address_data, NULL, NULL);
3585 return status == AddressValidator::SUCCESS; 3595 return status == AddressValidator::SUCCESS;
3586 } 3596 }
3587 3597
3588 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( 3598 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3589 const base::string16& year, 3599 const base::string16& year,
3590 const base::string16& month) const { 3600 const base::string16& month) const {
3591 // If the expiration is in the past as per the local clock, it's invalid. 3601 // If the expiration is in the past as per the local clock, it's invalid.
3592 base::Time now = base::Time::Now(); 3602 base::Time now = base::Time::Now();
3593 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) 3603 if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3594 return false; 3604 return false;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3626 3636
3627 return false; 3637 return false;
3628 } 3638 }
3629 3639
3630 bool AutofillDialogControllerImpl::HasInvalidAddress( 3640 bool AutofillDialogControllerImpl::HasInvalidAddress(
3631 const AutofillProfile& profile) { 3641 const AutofillProfile& profile) {
3632 scoped_ptr<AddressData> address_data = 3642 scoped_ptr<AddressData> address_data =
3633 i18n::CreateAddressDataFromAutofillProfile( 3643 i18n::CreateAddressDataFromAutofillProfile(
3634 profile, g_browser_process->GetApplicationLocale()); 3644 profile, g_browser_process->GetApplicationLocale());
3635 3645
3636 AddressProblems problems; 3646 FieldProblemMap problems;
3637 GetValidator()->ValidateAddress(*address_data, 3647 GetValidator()->ValidateAddress(*address_data, NULL, &problems);
3638 AddressProblemFilter(),
3639 &problems);
3640 return !problems.empty(); 3648 return !problems.empty();
3641 } 3649 }
3642 3650
3643 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { 3651 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3644 return SectionIsActive(SECTION_SHIPPING) && 3652 return SectionIsActive(SECTION_SHIPPING) &&
3645 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; 3653 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3646 } 3654 }
3647 3655
3648 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { 3656 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3649 // It's possible that the user checked [X] Save details locally before 3657 // It's possible that the user checked [X] Save details locally before
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
4128 view_->UpdateButtonStrip(); 4136 view_->UpdateButtonStrip();
4129 } 4137 }
4130 4138
4131 void AutofillDialogControllerImpl::FetchWalletCookie() { 4139 void AutofillDialogControllerImpl::FetchWalletCookie() {
4132 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4140 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4133 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4141 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4134 signin_helper_->StartWalletCookieValueFetch(); 4142 signin_helper_->StartWalletCookieValueFetch();
4135 } 4143 }
4136 4144
4137 } // namespace autofill 4145 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698