| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); | 75 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); |
| 76 } | 76 } |
| 77 return years; | 77 return years; |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 CreditCardEditorViewController::CreditCardEditorViewController( | 82 CreditCardEditorViewController::CreditCardEditorViewController( |
| 83 PaymentRequestSpec* spec, | 83 PaymentRequestSpec* spec, |
| 84 PaymentRequestState* state, | 84 PaymentRequestState* state, |
| 85 PaymentRequestDialogView* dialog) | 85 PaymentRequestDialogView* dialog, |
| 86 : EditorViewController(spec, state, dialog) {} | 86 autofill::CreditCard* credit_card) |
| 87 : EditorViewController(spec, state, dialog), |
| 88 credit_card_to_edit_(credit_card) {} |
| 87 | 89 |
| 88 CreditCardEditorViewController::~CreditCardEditorViewController() {} | 90 CreditCardEditorViewController::~CreditCardEditorViewController() {} |
| 89 | 91 |
| 90 // Creates the "Cards accepted" view with a row of icons at the top of the | 92 // Creates the "Cards accepted" view with a row of icons at the top of the |
| 91 // credit card editor. | 93 // credit card editor. |
| 92 // +----------------------------------------------+ | 94 // +----------------------------------------------+ |
| 93 // | Cards Accepted | | 95 // | Cards Accepted | |
| 94 // | | | 96 // | | |
| 95 // | | VISA | | MC | | AMEX | | | 97 // | | VISA | | MC | | AMEX | | |
| 96 // +----------------------------------------------+ | 98 // +----------------------------------------------+ |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 {autofill::CREDIT_CARD_EXP_MONTH, | 155 {autofill::CREDIT_CARD_EXP_MONTH, |
| 154 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), | 156 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), |
| 155 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 157 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 156 EditorField::ControlType::COMBOBOX}, | 158 EditorField::ControlType::COMBOBOX}, |
| 157 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, | 159 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 158 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), | 160 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), |
| 159 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 161 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 160 EditorField::ControlType::COMBOBOX}}; | 162 EditorField::ControlType::COMBOBOX}}; |
| 161 } | 163 } |
| 162 | 164 |
| 165 base::string16 CreditCardEditorViewController::GetInitialValueForType( |
| 166 autofill::ServerFieldType type) { |
| 167 if (!credit_card_to_edit_) |
| 168 return base::string16(); |
| 169 |
| 170 // It is fine to use GetRawInfo here because credit card types are not |
| 171 // complex/compound. |
| 172 return credit_card_to_edit_->GetRawInfo(type); |
| 173 } |
| 174 |
| 163 bool CreditCardEditorViewController::ValidateModelAndSave() { | 175 bool CreditCardEditorViewController::ValidateModelAndSave() { |
| 176 // Use a temporary object for validation. |
| 164 autofill::CreditCard credit_card; | 177 autofill::CreditCard credit_card; |
| 165 credit_card.set_origin(autofill::kSettingsOrigin); | 178 credit_card.set_origin(autofill::kSettingsOrigin); |
| 179 |
| 166 for (const auto& field : text_fields()) { | 180 for (const auto& field : text_fields()) { |
| 167 // ValidatingTextfield* is the key, EditorField is the value. | 181 // ValidatingTextfield* is the key, EditorField is the value. |
| 168 DCHECK_EQ(autofill::CREDIT_CARD, | 182 DCHECK_EQ(autofill::CREDIT_CARD, |
| 169 autofill::AutofillType(field.second.type).group()); | 183 autofill::AutofillType(field.second.type).group()); |
| 170 if (field.first->invalid()) | 184 if (field.first->invalid()) |
| 171 return false; | 185 return false; |
| 172 | 186 |
| 173 credit_card.SetRawInfo(field.second.type, field.first->text()); | 187 credit_card.SetRawInfo(field.second.type, field.first->text()); |
| 174 } | 188 } |
| 175 for (const auto& field : comboboxes()) { | 189 for (const auto& field : comboboxes()) { |
| 176 // ValidatingCombobox* is the key, EditorField is the value. | 190 // ValidatingCombobox* is the key, EditorField is the value. |
| 177 DCHECK_EQ(autofill::CREDIT_CARD, | 191 DCHECK_EQ(autofill::CREDIT_CARD, |
| 178 autofill::AutofillType(field.second.type).group()); | 192 autofill::AutofillType(field.second.type).group()); |
| 179 ValidatingCombobox* combobox = field.first; | 193 ValidatingCombobox* combobox = field.first; |
| 180 if (combobox->invalid()) | 194 if (combobox->invalid()) |
| 181 return false; | 195 return false; |
| 182 | 196 |
| 183 credit_card.SetRawInfo(field.second.type, | 197 credit_card.SetRawInfo(field.second.type, |
| 184 combobox->GetTextForRow(combobox->selected_index())); | 198 combobox->GetTextForRow(combobox->selected_index())); |
| 185 } | 199 } |
| 186 | 200 |
| 187 // TODO(mathp): Display global error message. | 201 // TODO(mathp): Display global error message. |
| 188 if (!credit_card.IsValid()) | 202 if (!credit_card.IsValid()) |
| 189 return false; | 203 return false; |
| 190 | 204 |
| 191 // Add the card (will not add a duplicate). | 205 if (!credit_card_to_edit_) { |
| 192 state()->GetPersonalDataManager()->AddCreditCard(credit_card); | 206 // Add the card (will not add a duplicate). |
| 207 state()->GetPersonalDataManager()->AddCreditCard(credit_card); |
| 208 } else { |
| 209 // We were in edit mode. Copy the data from the temporary object to retain |
| 210 // the edited object's other properties (use count, use date, guid, etc.). |
| 211 for (const auto& field : text_fields()) { |
| 212 credit_card_to_edit_->SetRawInfo( |
| 213 field.second.type, credit_card.GetRawInfo(field.second.type)); |
| 214 } |
| 215 for (const auto& field : comboboxes()) { |
| 216 credit_card_to_edit_->SetRawInfo( |
| 217 field.second.type, credit_card.GetRawInfo(field.second.type)); |
| 218 } |
| 219 state()->GetPersonalDataManager()->UpdateCreditCard(*credit_card_to_edit_); |
| 220 } |
| 193 | 221 |
| 194 return true; | 222 return true; |
| 195 } | 223 } |
| 196 | 224 |
| 197 std::unique_ptr<ValidationDelegate> | 225 std::unique_ptr<ValidationDelegate> |
| 198 CreditCardEditorViewController::CreateValidationDelegate( | 226 CreditCardEditorViewController::CreateValidationDelegate( |
| 199 const EditorField& field) { | 227 const EditorField& field) { |
| 200 // The supported card networks for non-cc-number types are not passed to avoid | 228 // The supported card networks for non-cc-number types are not passed to avoid |
| 201 // the data copy in the delegate. | 229 // the data copy in the delegate. |
| 202 return base::MakeUnique< | 230 return base::MakeUnique< |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 bool is_required_valid = !field_.required; | 299 bool is_required_valid = !field_.required; |
| 272 const base::string16 displayed_message = | 300 const base::string16 displayed_message = |
| 273 is_required_valid ? base::ASCIIToUTF16("") | 301 is_required_valid ? base::ASCIIToUTF16("") |
| 274 : l10n_util::GetStringUTF16( | 302 : l10n_util::GetStringUTF16( |
| 275 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 303 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 276 controller_->DisplayErrorMessageForField(field_, displayed_message); | 304 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 277 return is_required_valid; | 305 return is_required_valid; |
| 278 } | 306 } |
| 279 | 307 |
| 280 } // namespace payments | 308 } // namespace payments |
| OLD | NEW |