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