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