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