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/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
12 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
| 13 #include "components/autofill/core/browser/autofill_type.h" |
| 14 #include "components/autofill/core/browser/credit_card.h" |
13 #include "components/autofill/core/browser/field_types.h" | 15 #include "components/autofill/core/browser/field_types.h" |
| 16 #include "components/autofill/core/common/autofill_constants.h" |
14 #include "components/payments/payment_request.h" | 17 #include "components/payments/payment_request.h" |
15 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/views/controls/textfield/textfield.h" |
16 | 20 |
17 namespace payments { | 21 namespace payments { |
18 | 22 |
19 CreditCardEditorViewController::CreditCardEditorViewController( | 23 CreditCardEditorViewController::CreditCardEditorViewController( |
20 PaymentRequest* request, | 24 PaymentRequest* request, |
21 PaymentRequestDialogView* dialog) | 25 PaymentRequestDialogView* dialog) |
22 : EditorViewController(request, dialog) {} | 26 : EditorViewController(request, dialog) {} |
23 | 27 |
24 CreditCardEditorViewController::~CreditCardEditorViewController() {} | 28 CreditCardEditorViewController::~CreditCardEditorViewController() {} |
25 | 29 |
26 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 30 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
27 return std::vector<EditorField>{ | 31 return std::vector<EditorField>{ |
| 32 {autofill::CREDIT_CARD_NAME_FULL, |
| 33 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
| 34 EditorField::LengthHint::HINT_LONG}, |
28 {autofill::CREDIT_CARD_NUMBER, | 35 {autofill::CREDIT_CARD_NUMBER, |
29 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | 36 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
30 EditorField::LengthHint::HINT_LONG}, | 37 EditorField::LengthHint::HINT_LONG}, |
31 {autofill::CREDIT_CARD_NAME_FULL, | |
32 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | |
33 EditorField::LengthHint::HINT_LONG}, | |
34 {autofill::CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, | 38 {autofill::CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, |
35 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE), | 39 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE), |
36 EditorField::LengthHint::HINT_SHORT}}; | 40 EditorField::LengthHint::HINT_SHORT}}; |
37 } | 41 } |
38 | 42 |
39 bool CreditCardEditorViewController::ValidateModelAndSave() { | 43 bool CreditCardEditorViewController::ValidateModelAndSave() { |
40 // TODO(mathp): Actual validation and saving the model on disk. | 44 autofill::CreditCard credit_card; |
| 45 credit_card.set_origin(autofill::kSettingsOrigin); |
| 46 for (const auto& field : text_fields()) { |
| 47 // ValidatingTextfield* is the key, EditorField is the value. |
| 48 base::string16 value = field.first->text(); |
| 49 DCHECK_EQ(autofill::CREDIT_CARD, |
| 50 autofill::AutofillType(field.second.type).group()); |
| 51 if (!ValidateTextfield(field.first)) |
| 52 return false; |
| 53 |
| 54 credit_card.SetRawInfo(field.second.type, value); |
| 55 } |
| 56 |
| 57 // TODO(mathp): Display global error message. |
| 58 if (!credit_card.IsValid()) |
| 59 return false; |
| 60 |
| 61 // Add the card (will not add a duplicate). |
| 62 request()->personal_data_manager()->AddCreditCard(credit_card); |
| 63 |
41 return true; | 64 return true; |
42 } | 65 } |
43 | 66 |
44 } // namespace payments | 67 } // namespace payments |
OLD | NEW |