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 return credit_card_to_edit_->GetInfo(autofill::AutofillType(type), |
| 171 state()->GetApplicationLocale()); |
| 172 } |
| 173 |
163 bool CreditCardEditorViewController::ValidateModelAndSave() { | 174 bool CreditCardEditorViewController::ValidateModelAndSave() { |
| 175 const std::string& locale = state()->GetApplicationLocale(); |
| 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.SetInfo(autofill::AutofillType(field.second.type), |
| 188 field.first->text(), locale); |
174 } | 189 } |
175 for (const auto& field : comboboxes()) { | 190 for (const auto& field : comboboxes()) { |
176 // ValidatingCombobox* is the key, EditorField is the value. | 191 // ValidatingCombobox* is the key, EditorField is the value. |
177 DCHECK_EQ(autofill::CREDIT_CARD, | 192 DCHECK_EQ(autofill::CREDIT_CARD, |
178 autofill::AutofillType(field.second.type).group()); | 193 autofill::AutofillType(field.second.type).group()); |
179 ValidatingCombobox* combobox = field.first; | 194 ValidatingCombobox* combobox = field.first; |
180 if (combobox->invalid()) | 195 if (combobox->invalid()) |
181 return false; | 196 return false; |
182 | 197 |
183 credit_card.SetRawInfo(field.second.type, | 198 credit_card.SetInfo(autofill::AutofillType(field.second.type), |
184 combobox->GetTextForRow(combobox->selected_index())); | 199 combobox->GetTextForRow(combobox->selected_index()), |
| 200 locale); |
185 } | 201 } |
186 | 202 |
187 // TODO(mathp): Display global error message. | 203 // TODO(mathp): Display global error message. |
188 if (!credit_card.IsValid()) | 204 if (!credit_card.IsValid()) |
189 return false; | 205 return false; |
190 | 206 |
191 // Add the card (will not add a duplicate). | 207 if (!credit_card_to_edit_) { |
192 state()->GetPersonalDataManager()->AddCreditCard(credit_card); | 208 // Add the card (will not add a duplicate). |
| 209 state()->GetPersonalDataManager()->AddCreditCard(credit_card); |
| 210 } else { |
| 211 // We were in edit mode. Copy the data from the temporary object to retain |
| 212 // the edited object's other properties (use count, use date, guid, etc.). |
| 213 for (const auto& field : text_fields()) { |
| 214 credit_card_to_edit_->SetInfo( |
| 215 autofill::AutofillType(field.second.type), |
| 216 credit_card.GetInfo(autofill::AutofillType(field.second.type), |
| 217 locale), |
| 218 locale); |
| 219 } |
| 220 for (const auto& field : comboboxes()) { |
| 221 credit_card_to_edit_->SetInfo( |
| 222 autofill::AutofillType(field.second.type), |
| 223 credit_card.GetInfo(autofill::AutofillType(field.second.type), |
| 224 locale), |
| 225 locale); |
| 226 } |
| 227 state()->GetPersonalDataManager()->UpdateCreditCard(*credit_card_to_edit_); |
| 228 } |
193 | 229 |
194 return true; | 230 return true; |
195 } | 231 } |
196 | 232 |
197 std::unique_ptr<ValidationDelegate> | 233 std::unique_ptr<ValidationDelegate> |
198 CreditCardEditorViewController::CreateValidationDelegate( | 234 CreditCardEditorViewController::CreateValidationDelegate( |
199 const EditorField& field) { | 235 const EditorField& field) { |
200 // The supported card networks for non-cc-number types are not passed to avoid | 236 // The supported card networks for non-cc-number types are not passed to avoid |
201 // the data copy in the delegate. | 237 // the data copy in the delegate. |
202 return base::MakeUnique< | 238 return base::MakeUnique< |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 bool is_required_valid = !field_.required; | 307 bool is_required_valid = !field_.required; |
272 const base::string16 displayed_message = | 308 const base::string16 displayed_message = |
273 is_required_valid ? base::ASCIIToUTF16("") | 309 is_required_valid ? base::ASCIIToUTF16("") |
274 : l10n_util::GetStringUTF16( | 310 : l10n_util::GetStringUTF16( |
275 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 311 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
276 controller_->DisplayErrorMessageForField(field_, displayed_message); | 312 controller_->DisplayErrorMessageForField(field_, displayed_message); |
277 return is_required_valid; | 313 return is_required_valid; |
278 } | 314 } |
279 | 315 |
280 } // namespace payments | 316 } // namespace payments |
OLD | NEW |