| OLD | NEW |
| 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 Loading... |
| 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 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 | 831 |
| 826 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); | 832 acceptable_cc_types_ = form_structure_.PossibleValues(CREDIT_CARD_TYPE); |
| 827 // Wallet generates MC virtual cards, so we have to disable it if MC is not | 833 // Wallet generates MC virtual cards, so we have to disable it if MC is not |
| 828 // allowed. | 834 // allowed. |
| 829 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) | 835 if (ShouldDisallowCcType(CreditCard::TypeForDisplay(kMasterCard))) |
| 830 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); | 836 DisableWallet(wallet::WalletClient::UNSUPPORTED_MERCHANT); |
| 831 | 837 |
| 832 if (account_chooser_model_->WalletIsSelected()) | 838 if (account_chooser_model_->WalletIsSelected()) |
| 833 FetchWalletCookie(); | 839 FetchWalletCookie(); |
| 834 | 840 |
| 835 scoped_ptr< ::i18n::addressinput::Downloader> downloader( | 841 validator_.reset(new AddressValidator( |
| 836 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())); | 842 I18N_ADDRESS_VALIDATION_DATA_URL, |
| 837 validator_ = AddressValidator::Build( | 843 scoped_ptr<Downloader>( |
| 838 downloader.Pass(), | 844 new autofill::ChromeDownloaderImpl(profile_->GetRequestContext())), |
| 839 ValidationRulesStorageFactory::CreateStorage(), | 845 ValidationRulesStorageFactory::CreateStorage(), |
| 840 this); | 846 this)); |
| 841 | 847 |
| 842 SuggestionsUpdated(); | 848 SuggestionsUpdated(); |
| 843 SubmitButtonDelayBegin(); | 849 SubmitButtonDelayBegin(); |
| 844 view_.reset(CreateView()); | 850 view_.reset(CreateView()); |
| 845 view_->Show(); | 851 view_->Show(); |
| 846 GetManager()->AddObserver(this); | 852 GetManager()->AddObserver(this); |
| 847 | 853 |
| 848 if (!account_chooser_model_->WalletIsSelected()) | 854 if (!account_chooser_model_->WalletIsSelected()) |
| 849 LogDialogLatencyToShow(); | 855 LogDialogLatencyToShow(); |
| 850 } | 856 } |
| (...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1902 base::string16 message = CreditCardNumberValidityMessage(value); | 1908 base::string16 message = CreditCardNumberValidityMessage(value); |
| 1903 if (!message.empty()) | 1909 if (!message.empty()) |
| 1904 return message; | 1910 return message; |
| 1905 } | 1911 } |
| 1906 break; | 1912 break; |
| 1907 } | 1913 } |
| 1908 | 1914 |
| 1909 case CREDIT_CARD_EXP_MONTH: | 1915 case CREDIT_CARD_EXP_MONTH: |
| 1910 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { | 1916 if (!InputWasEdited(CREDIT_CARD_EXP_MONTH, value)) { |
| 1911 return l10n_util::GetStringUTF16( | 1917 return l10n_util::GetStringUTF16( |
| 1912 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); | 1918 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD); |
| 1913 } | 1919 } |
| 1914 break; | 1920 break; |
| 1915 | 1921 |
| 1916 case CREDIT_CARD_EXP_4_DIGIT_YEAR: | 1922 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 1917 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { | 1923 if (!InputWasEdited(CREDIT_CARD_EXP_4_DIGIT_YEAR, value)) { |
| 1918 return l10n_util::GetStringUTF16( | 1924 return l10n_util::GetStringUTF16( |
| 1919 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD); | 1925 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD); |
| 1920 } | 1926 } |
| 1921 break; | 1927 break; |
| 1922 | 1928 |
| 1923 case CREDIT_CARD_VERIFICATION_CODE: | 1929 case CREDIT_CARD_VERIFICATION_CODE: |
| 1924 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { | 1930 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) { |
| 1925 return l10n_util::GetStringUTF16( | 1931 return l10n_util::GetStringUTF16( |
| 1926 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); | 1932 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_SECURITY_CODE); |
| 1927 } | 1933 } |
| 1928 break; | 1934 break; |
| 1929 | 1935 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1941 | 1947 |
| 1942 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. | 1948 case PHONE_BILLING_WHOLE_NUMBER: // Used in billing section. |
| 1943 break; | 1949 break; |
| 1944 | 1950 |
| 1945 default: | 1951 default: |
| 1946 NOTREACHED(); // Trying to validate unknown field. | 1952 NOTREACHED(); // Trying to validate unknown field. |
| 1947 break; | 1953 break; |
| 1948 } | 1954 } |
| 1949 | 1955 |
| 1950 return value.empty() ? l10n_util::GetStringUTF16( | 1956 return value.empty() ? l10n_util::GetStringUTF16( |
| 1951 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD) : | 1957 IDS_LIBADDRESSINPUT_MISSING_REQUIRED_FIELD) : |
| 1952 base::string16(); | 1958 base::string16(); |
| 1953 } | 1959 } |
| 1954 | 1960 |
| 1955 // TODO(groby): Also add tests. | 1961 // TODO(groby): Also add tests. |
| 1956 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( | 1962 ValidityMessages AutofillDialogControllerImpl::InputsAreValid( |
| 1957 DialogSection section, | 1963 DialogSection section, |
| 1958 const FieldValueMap& inputs) { | 1964 const FieldValueMap& inputs) { |
| 1959 ValidityMessages messages; | 1965 ValidityMessages messages; |
| 1960 if (inputs.empty()) | 1966 if (inputs.empty()) |
| 1961 return messages; | 1967 return messages; |
| 1962 | 1968 |
| 1963 AddressValidator::Status status = AddressValidator::SUCCESS; | 1969 AddressValidator::Status status = AddressValidator::SUCCESS; |
| 1964 if (section != SECTION_CC) { | 1970 if (section != SECTION_CC) { |
| 1965 AutofillProfile profile; | 1971 AutofillProfile profile; |
| 1966 FillFormGroupFromOutputs(inputs, &profile); | 1972 FillFormGroupFromOutputs(inputs, &profile); |
| 1967 scoped_ptr<AddressData> address_data = | 1973 scoped_ptr<AddressData> address_data = |
| 1968 i18n::CreateAddressDataFromAutofillProfile( | 1974 i18n::CreateAddressDataFromAutofillProfile( |
| 1969 profile, g_browser_process->GetApplicationLocale()); | 1975 profile, g_browser_process->GetApplicationLocale()); |
| 1970 address_data->language_code = AddressLanguageCodeForSection(section); | 1976 address_data->language_code = AddressLanguageCodeForSection(section); |
| 1971 | 1977 |
| 1972 AddressProblems problems; | 1978 Localization localization; |
| 1973 status = GetValidator()->ValidateAddress(*address_data, | 1979 localization.SetGetter(l10n_util::GetStringUTF8); |
| 1974 AddressProblemFilter(), | 1980 FieldProblemMap problems; |
| 1975 &problems); | 1981 status = GetValidator()->ValidateAddress(*address_data, NULL, &problems); |
| 1976 common::AddressType address_type = section == SECTION_SHIPPING ? | 1982 common::AddressType address_type = section == SECTION_SHIPPING ? |
| 1977 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; | 1983 common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING; |
| 1978 for (size_t i = 0; i < problems.size(); ++i) { | 1984 |
| 1979 const AddressProblem& problem = problems[i]; | 1985 for (FieldProblemMap::const_iterator iter = problems.begin(); |
| 1980 bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD; | 1986 iter != problems.end(); ++iter) { |
| 1981 base::string16 text = l10n_util::GetStringUTF16(problem.description_id); | 1987 bool sure = iter->second != MISSING_REQUIRED_FIELD; |
| 1982 messages.Set(i18ninput::TypeForField(problem.field, address_type), | 1988 base::string16 text = base::UTF8ToUTF16( |
| 1989 localization.GetErrorMessage(*address_data, |
| 1990 iter->first, |
| 1991 iter->second, |
| 1992 true, // Enable examples. |
| 1993 false)); // Disable links. |
| 1994 messages.Set(i18ninput::TypeForField(iter->first, address_type), |
| 1983 ValidityMessage(text, sure)); | 1995 ValidityMessage(text, sure)); |
| 1984 } | 1996 } |
| 1985 } | 1997 } |
| 1986 | 1998 |
| 1987 for (FieldValueMap::const_iterator iter = inputs.begin(); | 1999 for (FieldValueMap::const_iterator iter = inputs.begin(); |
| 1988 iter != inputs.end(); ++iter) { | 2000 iter != inputs.end(); ++iter) { |
| 1989 const ServerFieldType type = iter->first; | 2001 const ServerFieldType type = iter->first; |
| 1990 base::string16 text = InputValidityMessage(section, type, iter->second); | 2002 base::string16 text = InputValidityMessage(section, type, iter->second); |
| 1991 | 2003 |
| 1992 // Skip empty/unchanged fields in edit mode. If the individual field does | 2004 // Skip empty/unchanged fields in edit mode. If the individual field does |
| (...skipping 1379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3372 return; | 3384 return; |
| 3373 | 3385 |
| 3374 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { | 3386 for (size_t i = 0; i < i18n_validator_suggestions_.size(); ++i) { |
| 3375 popup_values->push_back(base::UTF8ToUTF16( | 3387 popup_values->push_back(base::UTF8ToUTF16( |
| 3376 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); | 3388 i18n_validator_suggestions_[i].GetFieldValue(focused_field))); |
| 3377 | 3389 |
| 3378 // Disambiguate the suggestion by showing the smallest administrative | 3390 // Disambiguate the suggestion by showing the smallest administrative |
| 3379 // region of the suggested address: | 3391 // region of the suggested address: |
| 3380 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY | 3392 // ADMIN_AREA > LOCALITY > DEPENDENT_LOCALITY |
| 3381 popup_labels->push_back(base::string16()); | 3393 popup_labels->push_back(base::string16()); |
| 3382 for (int field = ::i18n::addressinput::DEPENDENT_LOCALITY; | 3394 for (int field = DEPENDENT_LOCALITY; field >= ADMIN_AREA; --field) { |
| 3383 field >= ::i18n::addressinput::ADMIN_AREA; | |
| 3384 --field) { | |
| 3385 const std::string& field_value = | 3395 const std::string& field_value = |
| 3386 i18n_validator_suggestions_[i].GetFieldValue( | 3396 i18n_validator_suggestions_[i].GetFieldValue( |
| 3387 static_cast<AddressField>(field)); | 3397 static_cast<AddressField>(field)); |
| 3388 if (focused_field != field && !field_value.empty()) { | 3398 if (focused_field != field && !field_value.empty()) { |
| 3389 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); | 3399 popup_labels->back().assign(base::UTF8ToUTF16(field_value)); |
| 3390 break; | 3400 break; |
| 3391 } | 3401 } |
| 3392 } | 3402 } |
| 3393 } | 3403 } |
| 3394 popup_icons->resize(popup_values->size()); | 3404 popup_icons->resize(popup_values->size()); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3555 if (!IsManuallyEditingSection(section)) | 3565 if (!IsManuallyEditingSection(section)) |
| 3556 return true; | 3566 return true; |
| 3557 | 3567 |
| 3558 FieldValueMap detail_outputs; | 3568 FieldValueMap detail_outputs; |
| 3559 view_->GetUserInput(section, &detail_outputs); | 3569 view_->GetUserInput(section, &detail_outputs); |
| 3560 return !InputsAreValid(section, detail_outputs).HasSureErrors(); | 3570 return !InputsAreValid(section, detail_outputs).HasSureErrors(); |
| 3561 } | 3571 } |
| 3562 | 3572 |
| 3563 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { | 3573 bool AutofillDialogControllerImpl::RulesAreLoaded(DialogSection section) { |
| 3564 AddressData address_data; | 3574 AddressData address_data; |
| 3565 address_data.country_code = CountryCodeForSection(section); | 3575 address_data.region_code = CountryCodeForSection(section); |
| 3566 AddressValidator::Status status = GetValidator()->ValidateAddress( | 3576 AddressValidator::Status status = GetValidator()->ValidateAddress( |
| 3567 address_data, AddressProblemFilter(), NULL); | 3577 address_data, NULL, NULL); |
| 3568 return status == AddressValidator::SUCCESS; | 3578 return status == AddressValidator::SUCCESS; |
| 3569 } | 3579 } |
| 3570 | 3580 |
| 3571 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( | 3581 bool AutofillDialogControllerImpl::IsCreditCardExpirationValid( |
| 3572 const base::string16& year, | 3582 const base::string16& year, |
| 3573 const base::string16& month) const { | 3583 const base::string16& month) const { |
| 3574 // If the expiration is in the past as per the local clock, it's invalid. | 3584 // If the expiration is in the past as per the local clock, it's invalid. |
| 3575 base::Time now = base::Time::Now(); | 3585 base::Time now = base::Time::Now(); |
| 3576 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) | 3586 if (!autofill::IsValidCreditCardExpirationDate(year, month, now)) |
| 3577 return false; | 3587 return false; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3609 | 3619 |
| 3610 return false; | 3620 return false; |
| 3611 } | 3621 } |
| 3612 | 3622 |
| 3613 bool AutofillDialogControllerImpl::HasInvalidAddress( | 3623 bool AutofillDialogControllerImpl::HasInvalidAddress( |
| 3614 const AutofillProfile& profile) { | 3624 const AutofillProfile& profile) { |
| 3615 scoped_ptr<AddressData> address_data = | 3625 scoped_ptr<AddressData> address_data = |
| 3616 i18n::CreateAddressDataFromAutofillProfile( | 3626 i18n::CreateAddressDataFromAutofillProfile( |
| 3617 profile, g_browser_process->GetApplicationLocale()); | 3627 profile, g_browser_process->GetApplicationLocale()); |
| 3618 | 3628 |
| 3619 AddressProblems problems; | 3629 FieldProblemMap problems; |
| 3620 GetValidator()->ValidateAddress(*address_data, | 3630 GetValidator()->ValidateAddress(*address_data, NULL, &problems); |
| 3621 AddressProblemFilter(), | |
| 3622 &problems); | |
| 3623 return !problems.empty(); | 3631 return !problems.empty(); |
| 3624 } | 3632 } |
| 3625 | 3633 |
| 3626 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { | 3634 bool AutofillDialogControllerImpl::ShouldUseBillingForShipping() { |
| 3627 return SectionIsActive(SECTION_SHIPPING) && | 3635 return SectionIsActive(SECTION_SHIPPING) && |
| 3628 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; | 3636 suggested_shipping_.GetItemKeyForCheckedItem() == kSameAsBillingKey; |
| 3629 } | 3637 } |
| 3630 | 3638 |
| 3631 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { | 3639 bool AutofillDialogControllerImpl::ShouldSaveDetailsLocally() { |
| 3632 // It's possible that the user checked [X] Save details locally before | 3640 // It's possible that the user checked [X] Save details locally before |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4111 view_->UpdateButtonStrip(); | 4119 view_->UpdateButtonStrip(); |
| 4112 } | 4120 } |
| 4113 | 4121 |
| 4114 void AutofillDialogControllerImpl::FetchWalletCookie() { | 4122 void AutofillDialogControllerImpl::FetchWalletCookie() { |
| 4115 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); | 4123 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); |
| 4116 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); | 4124 signin_helper_.reset(new wallet::WalletSigninHelper(this, request_context)); |
| 4117 signin_helper_->StartWalletCookieValueFetch(); | 4125 signin_helper_->StartWalletCookieValueFetch(); |
| 4118 } | 4126 } |
| 4119 | 4127 |
| 4120 } // namespace autofill | 4128 } // namespace autofill |
| OLD | NEW |