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

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: Work in progress for suggestions impl. 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_downloader_impl.h" 86 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
87 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h" 87 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
88 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" 88 #include "third_party/libaddressinput/chromium/preload_address_validator.h"
89 #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"
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::FieldProblemMap;
102 using ::i18n::addressinput::AddressProblems;
103 using ::i18n::addressinput::AddressValidator;
104 105
105 namespace autofill { 106 namespace autofill {
106 107
107 namespace { 108 namespace {
108 109
109 const char kAddNewItemKey[] = "add-new-item"; 110 const char kAddNewItemKey[] = "add-new-item";
110 const char kManageItemsKey[] = "manage-items"; 111 const char kManageItemsKey[] = "manage-items";
111 const char kSameAsBillingKey[] = "same-as-billing"; 112 const char kSameAsBillingKey[] = "same-as-billing";
112 113
113 // URLs for Wallet error messages. 114 // URLs for Wallet error messages.
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 818
818 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); 819 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE);
819 // Wallet generates MC virtual cards, so we have to disable it if MC is not 820 // Wallet generates MC virtual cards, so we have to disable it if MC is not
820 // allowed. 821 // allowed.
821 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) 822 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard)))
822 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); 823 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT);
823 824
824 if (account_chooser_model_->WalletIsSelected()) 825 if (account_chooser_model_->WalletIsSelected())
825 FetchWalletCookie(); 826 FetchWalletCookie();
826 827
827 scoped_ptr< ::i18n::addressinput::Downloader> downloader( 828 validator_.reset(new AddressValidator(
828 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); 829 I18N_ADDRESS_VALIDATION_DATA_URL,
829 validator_ = AddressValidator::Build( 830 scoped_ptr< ::i18n::addressinput::Downloader>(
830 downloader.Pass(), 831 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())),
831 ValidationRulesStorageFactory::CreateStorage(), 832 ValidationRulesStorageFactory::CreateStorage(),
832 this); 833 this));
833 834
834 SuggestionsUpdated(); 835 SuggestionsUpdated();
835 SubmitButtonDelayBegin(); 836 SubmitButtonDelayBegin();
836 view_.reset(CreateView()); 837 view_.reset(CreateView());
837 view_->Show(); 838 view_->Show();
838 GetManager()->AddObserver(this); 839 GetManager()->AddObserver(this);
839 840
840 if (!account_chooser_model_->WalletIsSelected()) 841 if (!account_chooser_model_->WalletIsSelected())
841 LogDialogLatencyToShow(); 842 LogDialogLatencyToShow();
842 } 843 }
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1894 base::string16 message = CreditCardNumberValidityMessage(value); 1895 base::string16 message = CreditCardNumberValidityMessage(value);
1895 if (!message.empty()) 1896 if (!message.empty())
1896 return message; 1897 return message;
1897 } 1898 }
1898 break; 1899 break;
1899 } 1900 }
1900 1901
1901 case CREDIT_CARD_EXP_MONTH: 1902 case CREDIT_CARD_EXP_MONTH:
1902 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { 1903 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) {
1903 return l10n_util::GetStringUTF16( 1904 return l10n_util::GetStringUTF16(
1904 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1905 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1905 } 1906 }
1906 break; 1907 break;
1907 1908
1908 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 1909 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
1909 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { 1910 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) {
1910 return l10n_util::GetStringUTF16( 1911 return l10n_util::GetStringUTF16(
1911 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); 1912 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD);
1912 } 1913 }
1913 break; 1914 break;
1914 1915
1915 case CREDIT_CARD_VERIFICATION_CODE: 1916 case CREDIT_CARD_VERIFICATION_CODE:
1916 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { 1917 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) {
1917 return l10n_util::GetStringUTF16( 1918 return l10n_util::GetStringUTF16(
1918 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); 1919 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE);
1919 } 1920 }
1920 break; 1921 break;
1921 1922
(...skipping 11 matching lines...) Expand all
1933 1934
1934 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. 1935 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section.
1935 break; 1936 break;
1936 1937
1937 default: 1938 default:
1938 NOTREACHED(); // Trying to validate unknown field. 1939 NOTREACHED(); // Trying to validate unknown field.
1939 break; 1940 break;
1940 } 1941 }
1941 1942
1942 return value.empty() ? l10n_util::GetStringUTF16( 1943 return value.empty() ? l10n_util::GetStringUTF16(
1943 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : 1944 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) :
1944 base::string16(); 1945 base::string16();
1945 } 1946 }
1946 1947
1947 // TODO(groby): Also add tests. 1948 // TODO(groby): Also add tests.
1948 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( 1949 ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
1949 DialogSection section, 1950 DialogSection section,
1950 const FieldValueMap& inputs) { 1951 const FieldValueMap& inputs) {
1951 ValidityMessages messages; 1952 ValidityMessages messages;
1952 if (inputs.empty()) 1953 if (inputs.empty())
1953 return messages; 1954 return messages;
1954 1955
1955 AddressValidator::Status status = AddressValidator::SUCCESS; 1956 AddressValidator::Status status = AddressValidator::SUCCESS;
1956 if (section != SECTION_CC) { 1957 if (section != SECTION_CC) {
1957 AutofillProfile profile; 1958 AutofillProfile profile;
1958 FillFormGroupFromOutputs(inputs, &profile); 1959 FillFormGroupFromOutputs(inputs, &profile);
1959 scoped_ptr<AddressData> address_data = 1960 scoped_ptr<AddressData> address_data =
1960 i18n::CreateAddressDataFromAutofillProfile( 1961 i18n::CreateAddressDataFromAutofillProfile(
1961 profile, g_browser_process->GetApplicationLocale()); 1962 profile, g_browser_process->GetApplicationLocale());
1962 address_data->language_code = AddressLanguageCodeForSection(section); 1963 address_data->language_code = AddressLanguageCodeForSection(section);
1963 1964
1964 AddressProblems problems; 1965 ::i18n::addressinput::Localization localization;
1965 status = GetValidator()->ValidateAddress(*address_data, 1966 localization.SetGetter(
1966 AddressProblemFilter(), 1967 l10n_util::GetStringUTF8, g_browser_process->GetApplicationLocale());
Evan Stade 2014/06/12 00:18:08 I don't understand the purpose of passing the appl
please use gerrit instead 2014/06/13 19:22:09 Improved the comment in https://codereview.appspot
Evan Stade 2014/06/13 20:07:08 still don't get it. You don't need the locale here
1967 &problems); 1968
1969 FieldProblemMap problems;
1970 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems);
1968 common::AddressType address_type = section == SECTION_SHIPPING ? 1971 common::AddressType address_type = section == SECTION_SHIPPING ?
1969 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; 1972 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
1970 for (size_t i = 0; i < problems.size(); ++i) { 1973 for (FieldProblemMap::const_iterator iter = problems.begin();
1971 const AddressProblem& problem = problems[i]; 1974 iter != problems.end(); ++iter) {
1972 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; 1975 bool sure = iter->second != ::i18n::addressinput::MISSING_REQUIRED_FIELD;
1973 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); 1976 base::string16 text = base::UTF8ToUTF16(
1974 messages.Set(i18ninput::TypeForField(problem.field, address_type), 1977 localization.GetErrorMessage(*address_data,
1978 /*field*/ iter->first,
Evan Stade 2014/06/12 00:18:08 nit: remove inline comments
please use gerrit instead 2014/06/13 19:22:08 I removed the inline comments, but replaced them w
Evan Stade 2014/06/13 20:07:08 if you must document the parameters here, you can
please use gerrit instead 2014/06/16 19:02:24 Done.
1979 /*problem*/ iter->second,
1980 /*enable_examples*/ true,
1981 /*enable_links*/ false));
1982 messages.Set(i18ninput::TypeForField(iter->first, address_type),
1975 ValidityMessage(text, sure)); 1983 ValidityMessage(text, sure));
1976 } 1984 }
1977 } 1985 }
1978 1986
1979 for (FieldValueMap::const_iterator iter = inputs.begin(); 1987 for (FieldValueMap::const_iterator iter = inputs.begin();
1980 iter != inputs.end(); ++iter) { 1988 iter != inputs.end(); ++iter) {
1981 const ServerFieldType type = iter->first; 1989 const ServerFieldType type = iter->first;
1982 base::string16 text = InputValidityMessage(section, type, iter->second); 1990 base::string16 text = InputValidityMessage(section, type, iter->second);
1983 1991
1984 // Skip empty/unchanged fields in edit mode. If the individual field does 1992 // Skip empty/unchanged fields in edit mode. If the individual field does
(...skipping 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after
3539 if (!IsManuallyEditingSection(section)) 3547 if (!IsManuallyEditingSection(section))
3540 return true; 3548 return true;
3541 3549
3542 FieldValueMap detail_outputs; 3550 FieldValueMap detail_outputs;
3543 view_->GetUserInput(section, &detail_outputs); 3551 view_->GetUserInput(section, &detail_outputs);
3544 return !InputsAreValid(section, detail_outputs).HasSureErrors(); 3552 return !InputsAreValid(section, detail_outputs).HasSureErrors();
3545 } 3553 }
3546 3554
3547 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { 3555 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) {
3548 AddressData address_data; 3556 AddressData address_data;
3549 address_data.country_code = CountryCodeForSection(section); 3557 address_data.region_code = CountryCodeForSection(section);
3550 AddressValidator::Status status = GetValidator()->ValidateAddress( 3558 AddressValidator::Status status = GetValidator()->ValidateAddress(
3551 address_data, AddressProblemFilter(), NULL); 3559 address_data, NULL, NULL);
3552 return status == AddressValidator::SUCCESS; 3560 return status == AddressValidator::SUCCESS;
3553 } 3561 }
3554 3562
3555 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( 3563 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid(
3556 const base::string16& year, 3564 const base::string16& year,
3557 const base::string16& month) const { 3565 const base::string16& month) const {
3558 // If the expiration is in the past as per the local clock, it's invalid. 3566 // If the expiration is in the past as per the local clock, it's invalid.
3559 base::Time now = base::Time::Now(); 3567 base::Time now = base::Time::Now();
3560 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) 3568 if (!autofill::IsValidCreditCardExpirationDate(year, month, now))
3561 return false; 3569 return false;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3593 3601
3594 return false; 3602 return false;
3595 } 3603 }
3596 3604
3597 bool AutofillDialogControllerImpl::HasInvalidAddress( 3605 bool AutofillDialogControllerImpl::HasInvalidAddress(
3598 const AutofillProfile& profile) { 3606 const AutofillProfile& profile) {
3599 scoped_ptr<AddressData> address_data = 3607 scoped_ptr<AddressData> address_data =
3600 i18n::CreateAddressDataFromAutofillProfile( 3608 i18n::CreateAddressDataFromAutofillProfile(
3601 profile, g_browser_process->GetApplicationLocale()); 3609 profile, g_browser_process->GetApplicationLocale());
3602 3610
3603 AddressProblems problems; 3611 FieldProblemMap problems;
3604 GetValidator()->ValidateAddress(*address_data, 3612 GetValidator()->ValidateAddress(*address_data, NULL, &problems);
3605 AddressProblemFilter(),
3606 &problems);
3607 return !problems.empty(); 3613 return !problems.empty();
3608 } 3614 }
3609 3615
3610 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { 3616 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() {
3611 return SectionIsActive(SECTION_SHIPPING) && 3617 return SectionIsActive(SECTION_SHIPPING) &&
3612 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; 3618 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey;
3613 } 3619 }
3614 3620
3615 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { 3621 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() {
3616 // It's possible that the user checked [X] Save details locally before 3622 // It's possible that the user checked [X] Save details locally before
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
4095 view_->UpdateButtonStrip(); 4101 view_->UpdateButtonStrip();
4096 } 4102 }
4097 4103
4098 void AutofillDialogControllerImpl::FetchWalletCookie() { 4104 void AutofillDialogControllerImpl::FetchWalletCookie() {
4099 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); 4105 net::URLRequestContextGetter* request_context = profile_->GetRequestContext();
4100 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); 4106 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context));
4101 signin_helper_->StartWalletCookieValueFetch(); 4107 signin_helper_->StartWalletCookieValueFetch();
4102 } 4108 }
4103 4109
4104 } // namespace autofill 4110 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698