Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: chrome/browser/ui/views/payments/credit_card_editor_view_controller.cc

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

Powered by Google App Engine
This is Rietveld 408576698