| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/views/payments/credit_card_editor_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" |
| 12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 14 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 15 #include "chrome/browser/ui/views/payments/preselected_combobox_model.h" |
| 16 #include "chrome/browser/ui/views/payments/validating_combobox.h" |
| 17 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 13 #include "chrome/grit/generated_resources.h" | 18 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/autofill/core/browser/autofill_type.h" | 19 #include "components/autofill/core/browser/autofill_type.h" |
| 15 #include "components/autofill/core/browser/credit_card.h" | 20 #include "components/autofill/core/browser/credit_card.h" |
| 16 #include "components/autofill/core/browser/field_types.h" | 21 #include "components/autofill/core/browser/field_types.h" |
| 17 #include "components/autofill/core/browser/validation.h" | 22 #include "components/autofill/core/browser/validation.h" |
| 23 #include "components/autofill/core/common/autofill_clock.h" |
| 18 #include "components/autofill/core/common/autofill_constants.h" | 24 #include "components/autofill/core/common/autofill_constants.h" |
| 19 #include "components/payments/payment_request.h" | 25 #include "components/payments/payment_request.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/views/controls/textfield/textfield.h" | 27 #include "ui/views/controls/textfield/textfield.h" |
| 22 | 28 |
| 23 namespace payments { | 29 namespace payments { |
| 24 | 30 |
| 31 namespace { |
| 32 |
| 33 // Number of years (including the current one) to be shown in the expiration |
| 34 // year dropdown. |
| 35 const int kNumberOfExpirationYears = 10; |
| 36 |
| 37 // Returns the items that are in the expiration month dropdown. Will return the |
| 38 // months in order starting at "01" until "12". Uses a clock so that the |
| 39 // |default_index| is set to the current month. |
| 40 std::vector<base::string16> GetExpirationMonthItems(int* default_index) { |
| 41 std::vector<base::string16> months; |
| 42 months.reserve(12); |
| 43 for (int i = 1; i <= 12; i++) |
| 44 months.push_back(base::UTF8ToUTF16(base::StringPrintf("%02d", i))); |
| 45 |
| 46 base::Time::Exploded now_exploded; |
| 47 autofill::AutofillClock::Now().LocalExplode(&now_exploded); |
| 48 *default_index = now_exploded.month - 1; |
| 49 |
| 50 return months; |
| 51 } |
| 52 |
| 53 // Returns the items that are in the expiration year dropdown, with the first |
| 54 // year being the current year. |
| 55 std::vector<base::string16> GetExpirationYearItems() { |
| 56 std::vector<base::string16> years; |
| 57 years.reserve(kNumberOfExpirationYears); |
| 58 |
| 59 base::Time::Exploded now_exploded; |
| 60 autofill::AutofillClock::Now().LocalExplode(&now_exploded); |
| 61 for (int i = 0; i < kNumberOfExpirationYears; i++) { |
| 62 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); |
| 63 } |
| 64 return years; |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 25 CreditCardEditorViewController::CreditCardEditorViewController( | 69 CreditCardEditorViewController::CreditCardEditorViewController( |
| 26 PaymentRequest* request, | 70 PaymentRequest* request, |
| 27 PaymentRequestDialogView* dialog) | 71 PaymentRequestDialogView* dialog) |
| 28 : EditorViewController(request, dialog) {} | 72 : EditorViewController(request, dialog) {} |
| 29 | 73 |
| 30 CreditCardEditorViewController::~CreditCardEditorViewController() {} | 74 CreditCardEditorViewController::~CreditCardEditorViewController() {} |
| 31 | 75 |
| 32 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 76 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
| 33 return std::vector<EditorField>{ | 77 return std::vector<EditorField>{ |
| 34 {autofill::CREDIT_CARD_NAME_FULL, | 78 {autofill::CREDIT_CARD_NAME_FULL, |
| 35 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | 79 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
| 36 EditorField::LengthHint::HINT_LONG, true}, | 80 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 37 {autofill::CREDIT_CARD_NUMBER, | 81 {autofill::CREDIT_CARD_NUMBER, |
| 38 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | 82 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
| 39 EditorField::LengthHint::HINT_LONG, true}, | 83 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 40 {autofill::CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, | 84 {autofill::CREDIT_CARD_EXP_MONTH, |
| 41 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE), | 85 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), |
| 42 EditorField::LengthHint::HINT_SHORT, true}}; | 86 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 87 EditorField::ControlType::COMBOBOX}, |
| 88 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 89 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), |
| 90 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 91 EditorField::ControlType::COMBOBOX}}; |
| 43 } | 92 } |
| 44 | 93 |
| 45 bool CreditCardEditorViewController::ValidateModelAndSave() { | 94 bool CreditCardEditorViewController::ValidateModelAndSave() { |
| 46 autofill::CreditCard credit_card; | 95 autofill::CreditCard credit_card; |
| 47 credit_card.set_origin(autofill::kSettingsOrigin); | 96 credit_card.set_origin(autofill::kSettingsOrigin); |
| 48 for (const auto& field : text_fields()) { | 97 for (const auto& field : text_fields()) { |
| 49 // ValidatingTextfield* is the key, EditorField is the value. | 98 // ValidatingTextfield* is the key, EditorField is the value. |
| 50 DCHECK_EQ(autofill::CREDIT_CARD, | 99 DCHECK_EQ(autofill::CREDIT_CARD, |
| 51 autofill::AutofillType(field.second.type).group()); | 100 autofill::AutofillType(field.second.type).group()); |
| 52 if (field.first->invalid()) | 101 if (field.first->invalid()) |
| 53 return false; | 102 return false; |
| 54 | 103 |
| 55 credit_card.SetRawInfo(field.second.type, field.first->text()); | 104 credit_card.SetRawInfo(field.second.type, field.first->text()); |
| 56 } | 105 } |
| 106 for (const auto& field : comboboxes()) { |
| 107 // ValidatingCombobox* is the key, EditorField is the value. |
| 108 DCHECK_EQ(autofill::CREDIT_CARD, |
| 109 autofill::AutofillType(field.second.type).group()); |
| 110 ValidatingCombobox* combobox = field.first; |
| 111 if (combobox->invalid()) |
| 112 return false; |
| 113 |
| 114 credit_card.SetRawInfo(field.second.type, |
| 115 combobox->GetTextForRow(combobox->selected_index())); |
| 116 } |
| 57 | 117 |
| 58 // TODO(mathp): Display global error message. | 118 // TODO(mathp): Display global error message. |
| 59 if (!credit_card.IsValid()) | 119 if (!credit_card.IsValid()) |
| 60 return false; | 120 return false; |
| 61 | 121 |
| 62 // Add the card (will not add a duplicate). | 122 // Add the card (will not add a duplicate). |
| 63 request()->personal_data_manager()->AddCreditCard(credit_card); | 123 request()->personal_data_manager()->AddCreditCard(credit_card); |
| 64 | 124 |
| 65 return true; | 125 return true; |
| 66 } | 126 } |
| 67 | 127 |
| 68 std::unique_ptr<ValidatingTextfield::Delegate> | 128 std::unique_ptr<ValidationDelegate> |
| 69 CreditCardEditorViewController::CreateValidationDelegate( | 129 CreditCardEditorViewController::CreateValidationDelegate( |
| 70 const EditorField& field) { | 130 const EditorField& field) { |
| 71 return base::MakeUnique<CreditCardEditorViewController::ValidationDelegate>( | 131 return base::MakeUnique< |
| 72 field); | 132 CreditCardEditorViewController::CreditCardValidationDelegate>(field); |
| 73 } | 133 } |
| 74 | 134 |
| 75 CreditCardEditorViewController::ValidationDelegate::ValidationDelegate( | 135 std::unique_ptr<ui::ComboboxModel> |
| 76 const EditorField& field) | 136 CreditCardEditorViewController::GetComboboxModelForType( |
| 137 const autofill::ServerFieldType& type) { |
| 138 switch (type) { |
| 139 case autofill::CREDIT_CARD_EXP_MONTH: { |
| 140 int default_index = 0; |
| 141 std::vector<base::string16> months = |
| 142 GetExpirationMonthItems(&default_index); |
| 143 return std::unique_ptr<ui::ComboboxModel>( |
| 144 new PreselectedComboboxModel(months, default_index)); |
| 145 } |
| 146 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 147 return std::unique_ptr<ui::ComboboxModel>( |
| 148 new ui::SimpleComboboxModel(GetExpirationYearItems())); |
| 149 default: |
| 150 NOTREACHED(); |
| 151 break; |
| 152 } |
| 153 return std::unique_ptr<ui::ComboboxModel>(); |
| 154 } |
| 155 |
| 156 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 157 CreditCardValidationDelegate(const EditorField& field) |
| 77 : field_(field) {} | 158 : field_(field) {} |
| 78 CreditCardEditorViewController::ValidationDelegate::~ValidationDelegate() {} | 159 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 160 ~CreditCardValidationDelegate() {} |
| 79 | 161 |
| 80 bool CreditCardEditorViewController::ValidationDelegate::ValidateTextfield( | 162 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 81 views::Textfield* textfield) { | 163 ValidateTextfield(views::Textfield* textfield) { |
| 82 if (!textfield->text().empty()) { | 164 return ValidateValue(textfield->text()); |
| 165 } |
| 166 |
| 167 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 168 ValidateCombobox(views::Combobox* combobox) { |
| 169 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); |
| 170 } |
| 171 |
| 172 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 173 ValidateValue(const base::string16& value) { |
| 174 if (!value.empty()) { |
| 83 base::string16 error_message; | 175 base::string16 error_message; |
| 84 // TODO(mathp): Display |error_message| around |textfield|. | 176 // TODO(mathp): Display |error_message| around |textfield|. |
| 85 return autofill::IsValidForType(textfield->text(), field_.type, | 177 return autofill::IsValidForType(value, field_.type, &error_message); |
| 86 &error_message); | |
| 87 } | 178 } |
| 88 | 179 |
| 89 // TODO(mathp): Display "required" error if applicable. | 180 // TODO(mathp): Display "required" error if applicable. |
| 90 return !field_.required; | 181 return !field_.required; |
| 91 } | 182 } |
| 92 | 183 |
| 93 } // namespace payments | 184 } // namespace payments |
| OLD | NEW |