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