Chromium Code Reviews| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 years.reserve(kNumberOfExpirationYears); | 80 years.reserve(kNumberOfExpirationYears); |
| 81 | 81 |
| 82 base::Time::Exploded now_exploded; | 82 base::Time::Exploded now_exploded; |
| 83 autofill::AutofillClock::Now().LocalExplode(&now_exploded); | 83 autofill::AutofillClock::Now().LocalExplode(&now_exploded); |
| 84 for (int i = 0; i < kNumberOfExpirationYears; i++) { | 84 for (int i = 0; i < kNumberOfExpirationYears; i++) { |
| 85 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); | 85 years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i))); |
| 86 } | 86 } |
| 87 return years; | 87 return years; |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Validates the two comboboxes used for expiration date. | |
| 91 class ExpirationDateValidationDelegate : public ValidationDelegate { | |
| 92 public: | |
| 93 ExpirationDateValidationDelegate(EditorViewController* controller, | |
| 94 const std::string& app_locale) | |
| 95 : controller_(controller), app_locale_(app_locale) {} | |
| 96 | |
| 97 // Only the delegate knows how to validate the textfield. | |
| 98 bool ValidateTextfield(views::Textfield* textfield) override { return true; } | |
| 99 | |
| 100 bool ValidateCombobox(views::Combobox* combobox) override { | |
| 101 // Get the combined date from the month and year dropdowns. | |
| 102 views::View* view_parent = combobox->parent(); | |
| 103 | |
| 104 views::Combobox* month_combobox = static_cast<views::Combobox*>( | |
| 105 view_parent->GetViewByID(autofill::CREDIT_CARD_EXP_MONTH)); | |
| 106 base::string16 month = | |
| 107 month_combobox->model()->GetItemAt(month_combobox->selected_index()); | |
| 108 | |
| 109 views::Combobox* year_combobox = static_cast<views::Combobox*>( | |
| 110 view_parent->GetViewByID(autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
| 111 base::string16 year = | |
| 112 year_combobox->model()->GetItemAt(year_combobox->selected_index()); | |
| 113 | |
| 114 autofill::CreditCard card; | |
| 115 card.SetExpirationMonthFromString(month, app_locale_); | |
| 116 card.SetExpirationYearFromString(year); | |
| 117 | |
| 118 bool is_expired = card.IsExpired(autofill::AutofillClock::Now()); | |
| 119 controller_->DisplayErrorMessageForField( | |
| 120 autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, | |
| 121 is_expired ? l10n_util::GetStringUTF16( | |
| 122 IDS_PAYMENTS_VALIDATION_INVALID_CREDIT_CARD_EXPIRED) | |
| 123 : base::string16()); | |
| 124 | |
| 125 month_combobox->SetInvalid(is_expired); | |
| 126 year_combobox->SetInvalid(is_expired); | |
| 127 | |
| 128 return !is_expired; | |
| 129 } | |
| 130 | |
| 131 void ComboboxModelChanged(views::Combobox* combobox) override {} | |
| 132 | |
| 133 private: | |
| 134 EditorViewController* controller_; | |
| 135 const std::string& app_locale_; | |
|
anthonyvd
2017/05/19 19:16:59
Maybe it doesn't matter in this case because the p
Mathieu
2017/05/19 21:13:46
Done.
| |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(ExpirationDateValidationDelegate); | |
| 138 }; | |
| 139 | |
| 90 } // namespace | 140 } // namespace |
| 91 | 141 |
| 92 CreditCardEditorViewController::CreditCardEditorViewController( | 142 CreditCardEditorViewController::CreditCardEditorViewController( |
| 93 PaymentRequestSpec* spec, | 143 PaymentRequestSpec* spec, |
| 94 PaymentRequestState* state, | 144 PaymentRequestState* state, |
| 95 PaymentRequestDialogView* dialog, | 145 PaymentRequestDialogView* dialog, |
| 96 BackNavigationType back_navigation, | 146 BackNavigationType back_navigation, |
| 97 int next_ui_tag, | 147 int next_ui_tag, |
| 98 base::OnceClosure on_edited, | 148 base::OnceClosure on_edited, |
| 99 base::OnceCallback<void(const autofill::CreditCard&)> on_added, | 149 base::OnceCallback<void(const autofill::CreditCard&)> on_added, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 card_icon_view->SetImageSize(kCardIconSize); | 208 card_icon_view->SetImageSize(kCardIconSize); |
| 159 | 209 |
| 160 icons_row->AddChildView(card_icon_view.release()); | 210 icons_row->AddChildView(card_icon_view.release()); |
| 161 } | 211 } |
| 162 view->AddChildView(icons_row.release()); | 212 view->AddChildView(icons_row.release()); |
| 163 | 213 |
| 164 return view; | 214 return view; |
| 165 } | 215 } |
| 166 | 216 |
| 167 std::unique_ptr<views::View> | 217 std::unique_ptr<views::View> |
| 218 CreditCardEditorViewController::CreateCustomFieldView( | |
| 219 autofill::ServerFieldType type) { | |
| 220 DCHECK(type == autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | |
|
anthonyvd
2017/05/19 19:16:59
nit: DCHECK_EQ
Mathieu
2017/05/19 21:13:46
Done.
| |
| 221 | |
| 222 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); | |
| 223 std::unique_ptr<views::GridLayout> combobox_layout = | |
| 224 base::MakeUnique<views::GridLayout>(view.get()); | |
| 225 // Column set for short fields. | |
| 226 views::ColumnSet* columns = combobox_layout->AddColumnSet(0); | |
| 227 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 1, | |
| 228 views::GridLayout::USE_PREF, 0, 0); | |
| 229 // Space between the two comboboxes. | |
| 230 constexpr int kHorizontalSpacing = 8; | |
| 231 columns->AddPaddingColumn(0, kHorizontalSpacing); | |
| 232 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 1, | |
| 233 views::GridLayout::USE_PREF, 0, 0); | |
| 234 | |
| 235 combobox_layout->StartRow(0, 0); | |
| 236 constexpr int kInputFieldHeight = 28; | |
| 237 EditorField tmp_month{autofill::CREDIT_CARD_EXP_MONTH, base::string16(), | |
| 238 EditorField::LengthHint::HINT_SHORT, | |
| 239 /* required= */ true, | |
|
anthonyvd
2017/05/19 19:16:59
/*required=*/true
Mathieu
2017/05/19 21:13:46
Done.
| |
| 240 EditorField::ControlType::COMBOBOX}; | |
| 241 std::unique_ptr<ValidatingCombobox> month_combobox = | |
| 242 CreateComboboxForField(tmp_month); | |
| 243 combobox_layout->AddView(month_combobox.release(), 1, 1, | |
| 244 views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
| 245 kInputFieldHeight); | |
| 246 | |
| 247 EditorField tmp_year{autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, base::string16(), | |
| 248 EditorField::LengthHint::HINT_SHORT, | |
| 249 /* required= */ true, | |
|
anthonyvd
2017/05/19 19:16:59
/*required=*/true
Mathieu
2017/05/19 21:13:46
Done.
| |
| 250 EditorField::ControlType::COMBOBOX}; | |
| 251 std::unique_ptr<ValidatingCombobox> year_combobox = | |
| 252 CreateComboboxForField(tmp_year); | |
| 253 combobox_layout->AddView(year_combobox.release(), 1, 1, | |
| 254 views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
| 255 kInputFieldHeight); | |
| 256 | |
| 257 view->SetLayoutManager(combobox_layout.release()); | |
| 258 return view; | |
| 259 } | |
| 260 | |
| 261 std::unique_ptr<views::View> | |
| 168 CreditCardEditorViewController::CreateExtraViewForField( | 262 CreditCardEditorViewController::CreateExtraViewForField( |
| 169 autofill::ServerFieldType type) { | 263 autofill::ServerFieldType type) { |
| 170 if (type != kBillingAddressType) | 264 if (type != kBillingAddressType) |
| 171 return nullptr; | 265 return nullptr; |
| 172 | 266 |
| 173 std::unique_ptr<views::View> button_view = base::MakeUnique<views::View>(); | 267 std::unique_ptr<views::View> button_view = base::MakeUnique<views::View>(); |
| 174 button_view->SetLayoutManager(new views::FillLayout); | 268 button_view->SetLayoutManager(new views::FillLayout); |
| 175 | 269 |
| 176 // The button to add new billing addresses. | 270 // The button to add new billing addresses. |
| 177 std::unique_ptr<views::Button> add_button( | 271 std::unique_ptr<views::Button> add_button( |
| 178 views::MdTextButton::Create(this, l10n_util::GetStringUTF16(IDS_ADD))); | 272 views::MdTextButton::Create(this, l10n_util::GetStringUTF16(IDS_ADD))); |
| 179 add_button->set_id( | 273 add_button->set_id( |
| 180 static_cast<int>(DialogViewID::ADD_BILLING_ADDRESS_BUTTON)); | 274 static_cast<int>(DialogViewID::ADD_BILLING_ADDRESS_BUTTON)); |
| 181 add_button->set_tag(add_billing_address_button_tag_); | 275 add_button->set_tag(add_billing_address_button_tag_); |
| 182 button_view->AddChildView(add_button.release()); | 276 button_view->AddChildView(add_button.release()); |
| 183 return button_view; | 277 return button_view; |
| 184 } | 278 } |
| 185 | 279 |
| 186 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 280 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
| 187 return std::vector<EditorField>{ | 281 return std::vector<EditorField>{ |
| 188 {autofill::CREDIT_CARD_NUMBER, | 282 {autofill::CREDIT_CARD_NUMBER, |
| 189 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | 283 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
| 190 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, | 284 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, |
| 191 {autofill::CREDIT_CARD_NAME_FULL, | 285 {autofill::CREDIT_CARD_NAME_FULL, |
| 192 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | 286 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
| 193 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, | 287 EditorField::LengthHint::HINT_SHORT, /* required= */ true}, |
| 194 {autofill::CREDIT_CARD_EXP_MONTH, | 288 {autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, |
| 195 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), | 289 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE), |
| 196 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 290 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 197 EditorField::ControlType::COMBOBOX}, | 291 EditorField::ControlType::CUSTOMFIELD}, |
| 198 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
| 199 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), | |
| 200 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | |
| 201 EditorField::ControlType::COMBOBOX}, | |
| 202 {kBillingAddressType, | 292 {kBillingAddressType, |
| 203 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), | 293 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), |
| 204 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 294 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 205 EditorField::ControlType::COMBOBOX}}; | 295 EditorField::ControlType::COMBOBOX}}; |
| 206 } | 296 } |
| 207 | 297 |
| 208 base::string16 CreditCardEditorViewController::GetInitialValueForType( | 298 base::string16 CreditCardEditorViewController::GetInitialValueForType( |
| 209 autofill::ServerFieldType type) { | 299 autofill::ServerFieldType type) { |
| 210 if (!credit_card_to_edit_ || type == kBillingAddressType) | 300 if (!credit_card_to_edit_ || type == kBillingAddressType) |
| 211 return base::string16(); | 301 return base::string16(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 } | 382 } |
| 293 | 383 |
| 294 return true; | 384 return true; |
| 295 } | 385 } |
| 296 | 386 |
| 297 std::unique_ptr<ValidationDelegate> | 387 std::unique_ptr<ValidationDelegate> |
| 298 CreditCardEditorViewController::CreateValidationDelegate( | 388 CreditCardEditorViewController::CreateValidationDelegate( |
| 299 const EditorField& field) { | 389 const EditorField& field) { |
| 300 // The supported card networks for non-cc-number types are not passed to avoid | 390 // The supported card networks for non-cc-number types are not passed to avoid |
| 301 // the data copy in the delegate. | 391 // the data copy in the delegate. |
| 392 if (field.type == autofill::CREDIT_CARD_EXP_MONTH || | |
| 393 field.type == autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) | |
| 394 return base::MakeUnique<ExpirationDateValidationDelegate>( | |
| 395 this, state()->GetApplicationLocale()); | |
| 302 return base::MakeUnique< | 396 return base::MakeUnique< |
| 303 CreditCardEditorViewController::CreditCardValidationDelegate>( | 397 CreditCardEditorViewController::CreditCardValidationDelegate>( |
| 304 field, this, | 398 field, this, |
| 305 field.type == autofill::CREDIT_CARD_NUMBER | 399 field.type == autofill::CREDIT_CARD_NUMBER |
| 306 ? spec()->supported_card_networks() | 400 ? spec()->supported_card_networks() |
| 307 : std::vector<std::string>()); | 401 : std::vector<std::string>()); |
| 308 } | 402 } |
| 309 | 403 |
| 310 std::unique_ptr<ui::ComboboxModel> | 404 std::unique_ptr<ui::ComboboxModel> |
| 311 CreditCardEditorViewController::GetComboboxModelForType( | 405 CreditCardEditorViewController::GetComboboxModelForType( |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 410 | 504 |
| 411 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 505 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 412 ValidateValue(const base::string16& value) { | 506 ValidateValue(const base::string16& value) { |
| 413 if (!value.empty()) { | 507 if (!value.empty()) { |
| 414 base::string16 error_message; | 508 base::string16 error_message; |
| 415 bool is_valid = | 509 bool is_valid = |
| 416 field_.type == autofill::CREDIT_CARD_NUMBER | 510 field_.type == autofill::CREDIT_CARD_NUMBER |
| 417 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( | 511 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( |
| 418 value, supported_card_networks_, &error_message) | 512 value, supported_card_networks_, &error_message) |
| 419 : autofill::IsValidForType(value, field_.type, &error_message); | 513 : autofill::IsValidForType(value, field_.type, &error_message); |
| 420 controller_->DisplayErrorMessageForField(field_, error_message); | 514 controller_->DisplayErrorMessageForField(field_.type, error_message); |
| 421 return is_valid; | 515 return is_valid; |
| 422 } | 516 } |
| 423 | 517 |
| 424 bool is_required_valid = !field_.required; | 518 bool is_required_valid = !field_.required; |
| 425 const base::string16 displayed_message = | 519 const base::string16 displayed_message = |
| 426 is_required_valid ? base::ASCIIToUTF16("") | 520 is_required_valid ? base::ASCIIToUTF16("") |
| 427 : l10n_util::GetStringUTF16( | 521 : l10n_util::GetStringUTF16( |
| 428 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 522 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 429 controller_->DisplayErrorMessageForField(field_, displayed_message); | 523 controller_->DisplayErrorMessageForField(field_.type, displayed_message); |
| 430 return is_required_valid; | 524 return is_required_valid; |
| 431 } | 525 } |
| 432 | 526 |
| 433 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { | 527 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { |
| 434 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; | 528 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; |
| 435 return true; | 529 return true; |
| 436 } | 530 } |
| 437 | 531 |
| 438 } // namespace payments | 532 } // namespace payments |
| OLD | NEW |