| 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 16 matching lines...) Expand all Loading... |
| 27 #include "components/autofill/core/browser/autofill_data_util.h" | 27 #include "components/autofill/core/browser/autofill_data_util.h" |
| 28 #include "components/autofill/core/browser/autofill_type.h" | 28 #include "components/autofill/core/browser/autofill_type.h" |
| 29 #include "components/autofill/core/browser/credit_card.h" | 29 #include "components/autofill/core/browser/credit_card.h" |
| 30 #include "components/autofill/core/browser/field_types.h" | 30 #include "components/autofill/core/browser/field_types.h" |
| 31 #include "components/autofill/core/browser/personal_data_manager.h" | 31 #include "components/autofill/core/browser/personal_data_manager.h" |
| 32 #include "components/autofill/core/browser/validation.h" | 32 #include "components/autofill/core/browser/validation.h" |
| 33 #include "components/autofill/core/common/autofill_clock.h" | 33 #include "components/autofill/core/common/autofill_clock.h" |
| 34 #include "components/autofill/core/common/autofill_constants.h" | 34 #include "components/autofill/core/common/autofill_constants.h" |
| 35 #include "components/payments/content/payment_request_spec.h" | 35 #include "components/payments/content/payment_request_spec.h" |
| 36 #include "components/payments/content/payment_request_state.h" | 36 #include "components/payments/content/payment_request_state.h" |
| 37 #include "components/payments/core/payment_request_data_util.h" |
| 37 #include "components/strings/grit/components_strings.h" | 38 #include "components/strings/grit/components_strings.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 39 #include "ui/gfx/geometry/insets.h" | 40 #include "ui/gfx/geometry/insets.h" |
| 40 #include "ui/native_theme/native_theme.h" | 41 #include "ui/native_theme/native_theme.h" |
| 41 #include "ui/views/controls/button/md_text_button.h" | 42 #include "ui/views/controls/button/md_text_button.h" |
| 42 #include "ui/views/controls/image_view.h" | 43 #include "ui/views/controls/image_view.h" |
| 43 #include "ui/views/controls/label.h" | 44 #include "ui/views/controls/label.h" |
| 44 #include "ui/views/controls/textfield/textfield.h" | 45 #include "ui/views/controls/textfield/textfield.h" |
| 45 #include "ui/views/layout/box_layout.h" | 46 #include "ui/views/layout/box_layout.h" |
| 46 #include "ui/views/layout/fill_layout.h" | 47 #include "ui/views/layout/fill_layout.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 void ComboboxModelChanged(views::Combobox* combobox) override {} | 130 void ComboboxModelChanged(views::Combobox* combobox) override {} |
| 130 | 131 |
| 131 private: | 132 private: |
| 132 EditorViewController* controller_; | 133 EditorViewController* controller_; |
| 133 const std::string app_locale_; | 134 const std::string app_locale_; |
| 134 bool initially_valid_; | 135 bool initially_valid_; |
| 135 | 136 |
| 136 DISALLOW_COPY_AND_ASSIGN(ExpirationDateValidationDelegate); | 137 DISALLOW_COPY_AND_ASSIGN(ExpirationDateValidationDelegate); |
| 137 }; | 138 }; |
| 138 | 139 |
| 139 // Formats card number. For example, "4111111111111111" is formatted into | |
| 140 // "4111 1111 1111 1111". | |
| 141 base::string16 FormatCardNumber(const base::string16& text) { | |
| 142 // Do not format masked card numbers, which start with a letter. | |
| 143 base::string16 number = autofill::CreditCard::StripSeparators(text); | |
| 144 if (number.empty() || !base::IsAsciiDigit(number[0])) | |
| 145 return text; | |
| 146 | |
| 147 std::vector<size_t> positions = {4U, 9U, 14U}; | |
| 148 if (autofill::CreditCard::GetCardNetwork(number) == | |
| 149 autofill::kAmericanExpressCard) { | |
| 150 positions = {4U, 11U}; | |
| 151 } | |
| 152 | |
| 153 static const base::char16 kSeparator = base::ASCIIToUTF16(" ")[0]; | |
| 154 for (size_t i : positions) { | |
| 155 if (number.size() > i) | |
| 156 number.insert(i, 1U, kSeparator); | |
| 157 } | |
| 158 | |
| 159 return number; | |
| 160 } | |
| 161 | |
| 162 } // namespace | 140 } // namespace |
| 163 | 141 |
| 164 CreditCardEditorViewController::CreditCardEditorViewController( | 142 CreditCardEditorViewController::CreditCardEditorViewController( |
| 165 PaymentRequestSpec* spec, | 143 PaymentRequestSpec* spec, |
| 166 PaymentRequestState* state, | 144 PaymentRequestState* state, |
| 167 PaymentRequestDialogView* dialog, | 145 PaymentRequestDialogView* dialog, |
| 168 BackNavigationType back_navigation, | 146 BackNavigationType back_navigation, |
| 169 int next_ui_tag, | 147 int next_ui_tag, |
| 170 base::OnceClosure on_edited, | 148 base::OnceClosure on_edited, |
| 171 base::OnceCallback<void(const autofill::CreditCard&)> on_added, | 149 base::OnceCallback<void(const autofill::CreditCard&)> on_added, |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 } | 339 } |
| 362 | 340 |
| 363 base::string16 CreditCardEditorViewController::GetInitialValueForType( | 341 base::string16 CreditCardEditorViewController::GetInitialValueForType( |
| 364 autofill::ServerFieldType type) { | 342 autofill::ServerFieldType type) { |
| 365 if (!credit_card_to_edit_ || type == kBillingAddressType) | 343 if (!credit_card_to_edit_ || type == kBillingAddressType) |
| 366 return base::string16(); | 344 return base::string16(); |
| 367 | 345 |
| 368 base::string16 info = credit_card_to_edit_->GetInfo( | 346 base::string16 info = credit_card_to_edit_->GetInfo( |
| 369 autofill::AutofillType(type), state()->GetApplicationLocale()); | 347 autofill::AutofillType(type), state()->GetApplicationLocale()); |
| 370 | 348 |
| 371 return type == autofill::CREDIT_CARD_NUMBER ? FormatCardNumber(info) : info; | 349 return type == autofill::CREDIT_CARD_NUMBER |
| 350 ? data_util::FormatCardNumberForDisplay(info) |
| 351 : info; |
| 372 } | 352 } |
| 373 | 353 |
| 374 bool CreditCardEditorViewController::ValidateModelAndSave() { | 354 bool CreditCardEditorViewController::ValidateModelAndSave() { |
| 375 if (IsEditingServerCard()) { | 355 if (IsEditingServerCard()) { |
| 376 views::Combobox* address_combobox = static_cast<views::Combobox*>( | 356 views::Combobox* address_combobox = static_cast<views::Combobox*>( |
| 377 dialog()->GetViewByID(GetInputFieldViewId(kBillingAddressType))); | 357 dialog()->GetViewByID(GetInputFieldViewId(kBillingAddressType))); |
| 378 if (address_combobox->invalid()) | 358 if (address_combobox->invalid()) |
| 379 return false; | 359 return false; |
| 380 | 360 |
| 381 autofill::AddressComboboxModel* model = | 361 autofill::AddressComboboxModel* model = |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 ~CreditCardValidationDelegate() {} | 600 ~CreditCardValidationDelegate() {} |
| 621 | 601 |
| 622 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 602 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 623 ShouldFormat() { | 603 ShouldFormat() { |
| 624 return field_.type == autofill::CREDIT_CARD_NUMBER; | 604 return field_.type == autofill::CREDIT_CARD_NUMBER; |
| 625 } | 605 } |
| 626 | 606 |
| 627 base::string16 | 607 base::string16 |
| 628 CreditCardEditorViewController::CreditCardValidationDelegate::Format( | 608 CreditCardEditorViewController::CreditCardValidationDelegate::Format( |
| 629 const base::string16& text) { | 609 const base::string16& text) { |
| 630 return FormatCardNumber(text); | 610 return data_util::FormatCardNumberForDisplay(text); |
| 631 } | 611 } |
| 632 | 612 |
| 633 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 613 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 634 IsValidTextfield(views::Textfield* textfield) { | 614 IsValidTextfield(views::Textfield* textfield) { |
| 635 return ValidateValue(textfield->text(), nullptr); | 615 return ValidateValue(textfield->text(), nullptr); |
| 636 } | 616 } |
| 637 | 617 |
| 638 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 618 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 639 IsValidCombobox(views::Combobox* combobox) { | 619 IsValidCombobox(views::Combobox* combobox) { |
| 640 return ValidateCombobox(combobox, nullptr); | 620 return ValidateCombobox(combobox, nullptr); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), | 696 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), |
| 717 error_message); | 697 error_message); |
| 718 } | 698 } |
| 719 | 699 |
| 720 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { | 700 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { |
| 721 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; | 701 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; |
| 722 return true; | 702 return true; |
| 723 } | 703 } |
| 724 | 704 |
| 725 } // namespace payments | 705 } // namespace payments |
| OLD | NEW |