| 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/memory/ptr_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 182 |
| 183 // Add the card (will not add a duplicate). | 183 // Add the card (will not add a duplicate). |
| 184 request()->personal_data_manager()->AddCreditCard(credit_card); | 184 request()->personal_data_manager()->AddCreditCard(credit_card); |
| 185 | 185 |
| 186 return true; | 186 return true; |
| 187 } | 187 } |
| 188 | 188 |
| 189 std::unique_ptr<ValidationDelegate> | 189 std::unique_ptr<ValidationDelegate> |
| 190 CreditCardEditorViewController::CreateValidationDelegate( | 190 CreditCardEditorViewController::CreateValidationDelegate( |
| 191 const EditorField& field) { | 191 const EditorField& field) { |
| 192 // The supported card networks for non-cc-number types are not passed to avoid |
| 193 // the data copy in the delegate. |
| 192 return base::MakeUnique< | 194 return base::MakeUnique< |
| 193 CreditCardEditorViewController::CreditCardValidationDelegate>(field, | 195 CreditCardEditorViewController::CreditCardValidationDelegate>( |
| 194 this); | 196 field, this, |
| 197 field.type == autofill::CREDIT_CARD_NUMBER |
| 198 ? request()->supported_card_networks() |
| 199 : std::vector<std::string>()); |
| 195 } | 200 } |
| 196 | 201 |
| 197 std::unique_ptr<ui::ComboboxModel> | 202 std::unique_ptr<ui::ComboboxModel> |
| 198 CreditCardEditorViewController::GetComboboxModelForType( | 203 CreditCardEditorViewController::GetComboboxModelForType( |
| 199 const autofill::ServerFieldType& type) { | 204 const autofill::ServerFieldType& type) { |
| 200 switch (type) { | 205 switch (type) { |
| 201 case autofill::CREDIT_CARD_EXP_MONTH: { | 206 case autofill::CREDIT_CARD_EXP_MONTH: { |
| 202 int default_index = 0; | 207 int default_index = 0; |
| 203 std::vector<base::string16> months = | 208 std::vector<base::string16> months = |
| 204 GetExpirationMonthItems(&default_index); | 209 GetExpirationMonthItems(&default_index); |
| 205 return std::unique_ptr<ui::ComboboxModel>( | 210 return std::unique_ptr<ui::ComboboxModel>( |
| 206 new PreselectedComboboxModel(months, default_index)); | 211 new PreselectedComboboxModel(months, default_index)); |
| 207 } | 212 } |
| 208 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: | 213 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 209 return std::unique_ptr<ui::ComboboxModel>( | 214 return std::unique_ptr<ui::ComboboxModel>( |
| 210 new ui::SimpleComboboxModel(GetExpirationYearItems())); | 215 new ui::SimpleComboboxModel(GetExpirationYearItems())); |
| 211 default: | 216 default: |
| 212 NOTREACHED(); | 217 NOTREACHED(); |
| 213 break; | 218 break; |
| 214 } | 219 } |
| 215 return std::unique_ptr<ui::ComboboxModel>(); | 220 return std::unique_ptr<ui::ComboboxModel>(); |
| 216 } | 221 } |
| 217 | 222 |
| 218 CreditCardEditorViewController::CreditCardValidationDelegate:: | 223 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 219 CreditCardValidationDelegate(const EditorField& field, | 224 CreditCardValidationDelegate( |
| 220 EditorViewController* controller) | 225 const EditorField& field, |
| 221 : field_(field), controller_(controller) {} | 226 EditorViewController* controller, |
| 227 const std::vector<std::string>& supported_card_networks) |
| 228 : field_(field), |
| 229 controller_(controller), |
| 230 supported_card_networks_(supported_card_networks.begin(), |
| 231 supported_card_networks.end()) {} |
| 222 CreditCardEditorViewController::CreditCardValidationDelegate:: | 232 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 223 ~CreditCardValidationDelegate() {} | 233 ~CreditCardValidationDelegate() {} |
| 224 | 234 |
| 225 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 235 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 226 ValidateTextfield(views::Textfield* textfield) { | 236 ValidateTextfield(views::Textfield* textfield) { |
| 227 return ValidateValue(textfield->text()); | 237 return ValidateValue(textfield->text()); |
| 228 } | 238 } |
| 229 | 239 |
| 230 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 240 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 231 ValidateCombobox(views::Combobox* combobox) { | 241 ValidateCombobox(views::Combobox* combobox) { |
| 232 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); | 242 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); |
| 233 } | 243 } |
| 234 | 244 |
| 235 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 245 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 236 ValidateValue(const base::string16& value) { | 246 ValidateValue(const base::string16& value) { |
| 237 if (!value.empty()) { | 247 if (!value.empty()) { |
| 238 base::string16 error_message; | 248 base::string16 error_message; |
| 239 bool is_valid = | 249 bool is_valid = |
| 240 autofill::IsValidForType(value, field_.type, &error_message); | 250 field_.type == autofill::CREDIT_CARD_NUMBER |
| 251 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( |
| 252 value, supported_card_networks_, &error_message) |
| 253 : autofill::IsValidForType(value, field_.type, &error_message); |
| 241 controller_->DisplayErrorMessageForField(field_, error_message); | 254 controller_->DisplayErrorMessageForField(field_, error_message); |
| 242 return is_valid; | 255 return is_valid; |
| 243 } | 256 } |
| 244 | 257 |
| 245 bool is_required_valid = !field_.required; | 258 bool is_required_valid = !field_.required; |
| 246 const base::string16 displayed_message = | 259 const base::string16 displayed_message = |
| 247 is_required_valid ? base::ASCIIToUTF16("") | 260 is_required_valid ? base::ASCIIToUTF16("") |
| 248 : l10n_util::GetStringUTF16( | 261 : l10n_util::GetStringUTF16( |
| 249 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 262 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 250 controller_->DisplayErrorMessageForField(field_, displayed_message); | 263 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 251 return is_required_valid; | 264 return is_required_valid; |
| 252 } | 265 } |
| 253 | 266 |
| 254 } // namespace payments | 267 } // namespace payments |
| OLD | NEW |