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 <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 years.reserve(kNumberOfExpirationYears); | 80 years.reserve(kNumberOfExpirationYears); |
81 | 81 |
82 base::Time::Exploded now_exploded; | 82 base::Time::Exploded now_exploded; |
83 autofill::AutofillClock::Now().LocalExplode(&now_exploded); | 83 autofill::AutofillClock::Now().LocalExplode(&now_exploded); |
84 for (int i = 0; i < kNumberOfExpirationYears; i++) { | 84 for (int i = 0; i < kNumberOfExpirationYears; i++) { |
85 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); | 85 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); |
86 } | 86 } |
87 return years; | 87 return years; |
88 } | 88 } |
89 | 89 |
90 // Validates the two comboboxes used for expiration date. | |
91 class ExpirationDateValidationDelegate : public ValidationDelegate { | |
92 public: | |
93 ExpirationDateValidationDelegate(EditorViewController* controller, | |
94 const std::string& app_locale) | |
95 : controller_(controller), app_locale_(app_locale) {} | |
96 | |
97 // Only the delegate knows how to validate the textfield. | |
98 bool ValidateTextfield(views::Textfield* textfield) override { return true; } | |
99 | |
100 bool ValidateCombobox(views::Combobox* combobox) override { | |
101 // Get the combined date from the month and year dropdowns. | |
102 views::View* view_parent = combobox->parent(); | |
103 | |
104 views::Combobox* month_combobox = static_cast<views::Combobox*>( | |
105 view_parent->GetViewByID(autofill::CREDIT_CARD_EXP_MONTH)); | |
anthonyvd
2017/05/19 21:27:00
Does this break? You're not adding the offset?
Mathieu
2017/05/20 01:20:25
Done.
| |
106 base::string16 month = | |
107 month_combobox->model()->GetItemAt(month_combobox->selected_index()); | |
108 | |
109 views::Combobox* year_combobox = static_cast<views::Combobox*>( | |
110 view_parent->GetViewByID(autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
anthonyvd
2017/05/19 21:27:00
idem
Mathieu
2017/05/20 01:20:25
Done.
| |
111 base::string16 year = | |
112 year_combobox->model()->GetItemAt(year_combobox->selected_index()); | |
113 | |
114 autofill::CreditCard card; | |
115 card.SetExpirationMonthFromString(month, app_locale_); | |
116 card.SetExpirationYearFromString(year); | |
117 | |
118 bool is_expired = card.IsExpired(autofill::AutofillClock::Now()); | |
119 controller_->DisplayErrorMessageForField( | |
120 autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, | |
121 is_expired ? l10n_util::GetStringUTF16( | |
122 IDS_PAYMENTS_VALIDATION_INVALID_CREDIT_CARD_EXPIRED) | |
123 : base::string16()); | |
124 | |
125 month_combobox->SetInvalid(is_expired); | |
126 year_combobox->SetInvalid(is_expired); | |
127 | |
128 return !is_expired; | |
129 } | |
130 | |
131 void ComboboxModelChanged(views::Combobox* combobox) override {} | |
132 | |
133 private: | |
134 EditorViewController* controller_; | |
135 const std::string app_locale_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(ExpirationDateValidationDelegate); | |
138 }; | |
139 | |
90 } // namespace | 140 } // namespace |
91 | 141 |
92 CreditCardEditorViewController::CreditCardEditorViewController( | 142 CreditCardEditorViewController::CreditCardEditorViewController( |
93 PaymentRequestSpec* spec, | 143 PaymentRequestSpec* spec, |
94 PaymentRequestState* state, | 144 PaymentRequestState* state, |
95 PaymentRequestDialogView* dialog, | 145 PaymentRequestDialogView* dialog, |
96 BackNavigationType back_navigation, | 146 BackNavigationType back_navigation, |
97 int next_ui_tag, | 147 int next_ui_tag, |
98 base::OnceClosure on_edited, | 148 base::OnceClosure on_edited, |
99 base::OnceCallback<void(const autofill::CreditCard&)> on_added, | 149 base::OnceCallback<void(const autofill::CreditCard&)> on_added, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 card_icon_view->SetImageSize(kCardIconSize); | 208 card_icon_view->SetImageSize(kCardIconSize); |
159 | 209 |
160 icons_row->AddChildView(card_icon_view.release()); | 210 icons_row->AddChildView(card_icon_view.release()); |
161 } | 211 } |
162 view->AddChildView(icons_row.release()); | 212 view->AddChildView(icons_row.release()); |
163 | 213 |
164 return view; | 214 return view; |
165 } | 215 } |
166 | 216 |
167 std::unique_ptr<views::View> | 217 std::unique_ptr<views::View> |
218 CreditCardEditorViewController::CreateCustomFieldView( | |
219 autofill::ServerFieldType type) { | |
220 DCHECK_EQ(type, autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | |
221 | |
222 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); | |
223 std::unique_ptr<views::GridLayout> combobox_layout = | |
224 base::MakeUnique<views::GridLayout>(view.get()); | |
225 // Column set for short fields. | |
226 views::ColumnSet* columns = combobox_layout->AddColumnSet(0); | |
227 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 1, | |
228 views::GridLayout::USE_PREF, 0, 0); | |
229 // Space between the two comboboxes. | |
230 constexpr int kHorizontalSpacing = 8; | |
231 columns->AddPaddingColumn(0, kHorizontalSpacing); | |
232 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 1, | |
233 views::GridLayout::USE_PREF, 0, 0); | |
234 | |
235 combobox_layout->StartRow(0, 0); | |
236 constexpr int kInputFieldHeight = 28; | |
237 EditorField tmp_month{autofill::CREDIT_CARD_EXP_MONTH, base::string16(), | |
238 EditorField::LengthHint::HINT_SHORT, | |
239 /*required=*/true, EditorField::ControlType::COMBOBOX}; | |
240 std::unique_ptr<ValidatingCombobox> month_combobox = | |
241 CreateComboboxForField(tmp_month); | |
242 combobox_layout->AddView(month_combobox.release(), 1, 1, | |
243 views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
244 kInputFieldHeight); | |
245 | |
246 EditorField tmp_year{autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, base::string16(), | |
247 EditorField::LengthHint::HINT_SHORT, | |
248 /*required=*/true, EditorField::ControlType::COMBOBOX}; | |
249 std::unique_ptr<ValidatingCombobox> year_combobox = | |
250 CreateComboboxForField(tmp_year); | |
251 combobox_layout->AddView(year_combobox.release(), 1, 1, | |
252 views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
253 kInputFieldHeight); | |
254 | |
255 view->SetLayoutManager(combobox_layout.release()); | |
256 return view; | |
257 } | |
258 | |
259 std::unique_ptr<views::View> | |
168 CreditCardEditorViewController::CreateExtraViewForField( | 260 CreditCardEditorViewController::CreateExtraViewForField( |
169 autofill::ServerFieldType type) { | 261 autofill::ServerFieldType type) { |
170 if (type != kBillingAddressType) | 262 if (type != kBillingAddressType) |
171 return nullptr; | 263 return nullptr; |
172 | 264 |
173 std::unique_ptr<views::View> button_view = base::MakeUnique<views::View>(); | 265 std::unique_ptr<views::View> button_view = base::MakeUnique<views::View>(); |
174 button_view->SetLayoutManager(new views::FillLayout); | 266 button_view->SetLayoutManager(new views::FillLayout); |
175 | 267 |
176 // The button to add new billing addresses. | 268 // The button to add new billing addresses. |
177 std::unique_ptr<views::Button> add_button( | 269 std::unique_ptr<views::Button> add_button( |
178 views::MdTextButton::Create(this, l10n_util::GetStringUTF16(IDS_ADD))); | 270 views::MdTextButton::Create(this, l10n_util::GetStringUTF16(IDS_ADD))); |
179 add_button->set_id( | 271 add_button->set_id( |
180 static_cast<int>(DialogViewID::ADD_BILLING_ADDRESS_BUTTON)); | 272 static_cast<int>(DialogViewID::ADD_BILLING_ADDRESS_BUTTON)); |
181 add_button->set_tag(add_billing_address_button_tag_); | 273 add_button->set_tag(add_billing_address_button_tag_); |
182 button_view->AddChildView(add_button.release()); | 274 button_view->AddChildView(add_button.release()); |
183 return button_view; | 275 return button_view; |
184 } | 276 } |
185 | 277 |
186 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 278 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
187 return std::vector<EditorField>{ | 279 return std::vector<EditorField>{ |
188 {autofill::CREDIT_CARD_NUMBER, | 280 {autofill::CREDIT_CARD_NUMBER, |
189 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | 281 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
190 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, | 282 EditorField::LengthHint::HINT_SHORT, /*required=*/true}, |
191 {autofill::CREDIT_CARD_NAME_FULL, | 283 {autofill::CREDIT_CARD_NAME_FULL, |
192 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | 284 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
193 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, | 285 EditorField::LengthHint::HINT_SHORT, /*required=*/true}, |
194 {autofill::CREDIT_CARD_EXP_MONTH, | 286 {autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, |
195 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), | 287 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE), |
196 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 288 EditorField::LengthHint::HINT_SHORT, /*required=*/true, |
197 EditorField::ControlType::COMBOBOX}, | 289 EditorField::ControlType::CUSTOMFIELD}, |
198 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
199 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), | |
200 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | |
201 EditorField::ControlType::COMBOBOX}, | |
202 {kBillingAddressType, | 290 {kBillingAddressType, |
203 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), | 291 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), |
204 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 292 EditorField::LengthHint::HINT_SHORT, /*required=*/true, |
205 EditorField::ControlType::COMBOBOX}}; | 293 EditorField::ControlType::COMBOBOX}}; |
206 } | 294 } |
207 | 295 |
208 base::string16 CreditCardEditorViewController::GetInitialValueForType( | 296 base::string16 CreditCardEditorViewController::GetInitialValueForType( |
209 autofill::ServerFieldType type) { | 297 autofill::ServerFieldType type) { |
210 if (!credit_card_to_edit_ || type == kBillingAddressType) | 298 if (!credit_card_to_edit_ || type == kBillingAddressType) |
211 return base::string16(); | 299 return base::string16(); |
212 | 300 |
213 return credit_card_to_edit_->GetInfo(autofill::AutofillType(type), | 301 return credit_card_to_edit_->GetInfo(autofill::AutofillType(type), |
214 state()->GetApplicationLocale()); | 302 state()->GetApplicationLocale()); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 } | 380 } |
293 | 381 |
294 return true; | 382 return true; |
295 } | 383 } |
296 | 384 |
297 std::unique_ptr<ValidationDelegate> | 385 std::unique_ptr<ValidationDelegate> |
298 CreditCardEditorViewController::CreateValidationDelegate( | 386 CreditCardEditorViewController::CreateValidationDelegate( |
299 const EditorField& field) { | 387 const EditorField& field) { |
300 // The supported card networks for non-cc-number types are not passed to avoid | 388 // The supported card networks for non-cc-number types are not passed to avoid |
301 // the data copy in the delegate. | 389 // the data copy in the delegate. |
390 if (field.type == autofill::CREDIT_CARD_EXP_MONTH || | |
391 field.type == autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) | |
392 return base::MakeUnique<ExpirationDateValidationDelegate>( | |
393 this, state()->GetApplicationLocale()); | |
302 return base::MakeUnique< | 394 return base::MakeUnique< |
303 CreditCardEditorViewController::CreditCardValidationDelegate>( | 395 CreditCardEditorViewController::CreditCardValidationDelegate>( |
304 field, this, | 396 field, this, |
305 field.type == autofill::CREDIT_CARD_NUMBER | 397 field.type == autofill::CREDIT_CARD_NUMBER |
306 ? spec()->supported_card_networks() | 398 ? spec()->supported_card_networks() |
307 : std::vector<std::string>()); | 399 : std::vector<std::string>()); |
308 } | 400 } |
309 | 401 |
310 std::unique_ptr<ui::ComboboxModel> | 402 std::unique_ptr<ui::ComboboxModel> |
311 CreditCardEditorViewController::GetComboboxModelForType( | 403 CreditCardEditorViewController::GetComboboxModelForType( |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 | 502 |
411 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 503 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
412 ValidateValue(const base::string16& value) { | 504 ValidateValue(const base::string16& value) { |
413 if (!value.empty()) { | 505 if (!value.empty()) { |
414 base::string16 error_message; | 506 base::string16 error_message; |
415 bool is_valid = | 507 bool is_valid = |
416 field_.type == autofill::CREDIT_CARD_NUMBER | 508 field_.type == autofill::CREDIT_CARD_NUMBER |
417 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( | 509 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( |
418 value, supported_card_networks_, &error_message) | 510 value, supported_card_networks_, &error_message) |
419 : autofill::IsValidForType(value, field_.type, &error_message); | 511 : autofill::IsValidForType(value, field_.type, &error_message); |
420 controller_->DisplayErrorMessageForField(field_, error_message); | 512 controller_->DisplayErrorMessageForField(field_.type, error_message); |
421 return is_valid; | 513 return is_valid; |
422 } | 514 } |
423 | 515 |
424 bool is_required_valid = !field_.required; | 516 bool is_required_valid = !field_.required; |
425 const base::string16 displayed_message = | 517 const base::string16 displayed_message = |
426 is_required_valid ? base::ASCIIToUTF16("") | 518 is_required_valid ? base::ASCIIToUTF16("") |
427 : l10n_util::GetStringUTF16( | 519 : l10n_util::GetStringUTF16( |
428 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 520 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
429 controller_->DisplayErrorMessageForField(field_, displayed_message); | 521 controller_->DisplayErrorMessageForField(field_.type, displayed_message); |
430 return is_required_valid; | 522 return is_required_valid; |
431 } | 523 } |
432 | 524 |
433 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { | 525 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { |
434 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; | 526 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; |
435 return true; | 527 return true; |
436 } | 528 } |
437 | 529 |
438 } // namespace payments | 530 } // namespace payments |
OLD | NEW |