| 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/payment_method_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/payment_method_view_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 12 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 13 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 13 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 14 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" | 14 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" |
| 15 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 15 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/autofill/core/browser/autofill_type.h" | |
| 18 #include "components/autofill/core/browser/credit_card.h" | |
| 19 #include "components/payments/content/payment_request_state.h" | 17 #include "components/payments/content/payment_request_state.h" |
| 18 #include "components/payments/core/payment_instrument.h" |
| 20 #include "components/strings/grit/components_strings.h" | 19 #include "components/strings/grit/components_strings.h" |
| 21 #include "third_party/skia/include/core/SkColor.h" | 20 #include "third_party/skia/include/core/SkColor.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/paint_vector_icon.h" | 22 #include "ui/gfx/paint_vector_icon.h" |
| 24 #include "ui/views/controls/button/label_button.h" | 23 #include "ui/views/controls/button/label_button.h" |
| 25 #include "ui/views/controls/button/md_text_button.h" | 24 #include "ui/views/controls/button/md_text_button.h" |
| 26 #include "ui/views/layout/box_layout.h" | 25 #include "ui/views/layout/box_layout.h" |
| 27 #include "ui/views/layout/grid_layout.h" | 26 #include "ui/views/layout/grid_layout.h" |
| 28 #include "ui/views/vector_icons.h" | 27 #include "ui/views/vector_icons.h" |
| 29 | 28 |
| 30 namespace payments { | 29 namespace payments { |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 constexpr int kFirstTagValue = static_cast<int>( | 33 constexpr int kFirstTagValue = static_cast<int>( |
| 35 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 34 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 36 | 35 |
| 37 enum class PaymentMethodViewControllerTags : int { | 36 enum class PaymentMethodViewControllerTags : int { |
| 38 // The tag for the button that triggers the "add card" flow. Starts at | 37 // The tag for the button that triggers the "add card" flow. Starts at |
| 39 // |kFirstTagValue| not to conflict with tags common to all views. | 38 // |kFirstTagValue| not to conflict with tags common to all views. |
| 40 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, | 39 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item { | 42 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item { |
| 44 public: | 43 public: |
| 45 // Does not take ownership of |card|, which should not be null and should | 44 // Does not take ownership of |instrument|, which should not be null and |
| 46 // outlive this object. |list| is the PaymentRequestItemList object that will | 45 // should outlive this object. |list| is the PaymentRequestItemList object |
| 47 // own this. | 46 // that will own this. |
| 48 PaymentMethodListItem(autofill::CreditCard* card, | 47 PaymentMethodListItem(PaymentInstrument* instrument, |
| 49 PaymentRequestSpec* spec, | 48 PaymentRequestSpec* spec, |
| 50 PaymentRequestState* state, | 49 PaymentRequestState* state, |
| 51 PaymentRequestItemList* list, | 50 PaymentRequestItemList* list, |
| 52 bool selected) | 51 bool selected) |
| 53 : payments::PaymentRequestItemList::Item(spec, state, list, selected), | 52 : payments::PaymentRequestItemList::Item(spec, state, list, selected), |
| 54 card_(card) {} | 53 instrument_(instrument) {} |
| 55 ~PaymentMethodListItem() override {} | 54 ~PaymentMethodListItem() override {} |
| 56 | 55 |
| 57 private: | 56 private: |
| 58 // payments::PaymentRequestItemList::Item: | 57 // payments::PaymentRequestItemList::Item: |
| 59 std::unique_ptr<views::View> CreateItemView() override { | 58 std::unique_ptr<views::View> CreateItemView() override { |
| 60 std::unique_ptr<PaymentRequestRowView> row = | 59 std::unique_ptr<PaymentRequestRowView> row = |
| 61 base::MakeUnique<PaymentRequestRowView>(this); | 60 base::MakeUnique<PaymentRequestRowView>(this); |
| 62 views::GridLayout* layout = new views::GridLayout(row.get()); | 61 views::GridLayout* layout = new views::GridLayout(row.get()); |
| 63 layout->SetInsets( | 62 layout->SetInsets( |
| 64 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, | 63 kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 93 base::MakeUnique<views::View>(); | 92 base::MakeUnique<views::View>(); |
| 94 card_info_container->set_can_process_events_within_subtree(false); | 93 card_info_container->set_can_process_events_within_subtree(false); |
| 95 | 94 |
| 96 std::unique_ptr<views::BoxLayout> box_layout = | 95 std::unique_ptr<views::BoxLayout> box_layout = |
| 97 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, | 96 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, |
| 98 kPaymentRequestRowVerticalInsets, 0); | 97 kPaymentRequestRowVerticalInsets, 0); |
| 99 box_layout->set_cross_axis_alignment( | 98 box_layout->set_cross_axis_alignment( |
| 100 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | 99 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 101 card_info_container->SetLayoutManager(box_layout.release()); | 100 card_info_container->SetLayoutManager(box_layout.release()); |
| 102 | 101 |
| 102 card_info_container->AddChildView(new views::Label(instrument_->label())); |
| 103 card_info_container->AddChildView( | 103 card_info_container->AddChildView( |
| 104 new views::Label(card_->TypeAndLastFourDigits())); | 104 new views::Label(instrument_->sublabel())); |
| 105 card_info_container->AddChildView(new views::Label( | |
| 106 card_->GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), | |
| 107 g_browser_process->GetApplicationLocale()))); | |
| 108 // TODO(anthonyvd): Add the "card is incomplete" label once the | 105 // TODO(anthonyvd): Add the "card is incomplete" label once the |
| 109 // completedness logic is implemented. | 106 // completedness logic is implemented. |
| 110 layout->AddView(card_info_container.release()); | 107 layout->AddView(card_info_container.release()); |
| 111 | 108 |
| 112 checkmark_ = CreateCheckmark(selected()); | 109 checkmark_ = CreateCheckmark(selected()); |
| 113 layout->AddView(checkmark_.get()); | 110 layout->AddView(checkmark_.get()); |
| 114 | 111 |
| 115 std::unique_ptr<views::ImageView> card_icon_view = | 112 std::unique_ptr<views::ImageView> card_icon_view = CreateInstrumentIconView( |
| 116 CreateCardIconView(card_->type()); | 113 instrument_->icon_resource_id(), instrument_->label()); |
| 117 card_icon_view->SetImageSize(gfx::Size(32, 20)); | 114 card_icon_view->SetImageSize(gfx::Size(32, 20)); |
| 118 layout->AddView(card_icon_view.release()); | 115 layout->AddView(card_icon_view.release()); |
| 119 | 116 |
| 120 return std::move(row); | 117 return std::move(row); |
| 121 } | 118 } |
| 122 | 119 |
| 123 void SelectedStateChanged() override { | 120 void SelectedStateChanged() override { |
| 124 // This could be called before CreateItemView, so before |checkmark_| is | 121 // This could be called before CreateItemView, so before |checkmark_| is |
| 125 // instantiated. | 122 // instantiated. |
| 126 if (checkmark_) | 123 if (checkmark_) |
| 127 checkmark_->SetVisible(selected()); | 124 checkmark_->SetVisible(selected()); |
| 128 | 125 |
| 129 state()->SetSelectedCreditCard(card_); | 126 state()->SetSelectedInstrument(instrument_); |
| 130 } | 127 } |
| 131 | 128 |
| 132 // views::ButtonListener: | 129 // views::ButtonListener: |
| 133 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | 130 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
| 134 if (IsComplete()) { | 131 if (IsComplete()) { |
| 135 list()->SelectItem(this); | 132 list()->SelectItem(this); |
| 136 } else { | 133 } else { |
| 137 // TODO(anthonyvd): Display the editor, pre-populated with the data that | 134 // TODO(anthonyvd): Display the editor, pre-populated with the data that |
| 138 // already exists in |card|. | 135 // already exists in |card|. |
| 139 } | 136 } |
| 140 } | 137 } |
| 141 | 138 |
| 142 bool IsComplete() const { | 139 bool IsComplete() const { |
| 143 // TODO(anthonyvd): Hook this up to the card completedness logic when it's | 140 // TODO(anthonyvd): Hook this up to the card completedness logic when it's |
| 144 // implemented in PaymentRequest. | 141 // implemented in PaymentRequest. |
| 145 return true; | 142 return true; |
| 146 } | 143 } |
| 147 | 144 |
| 148 autofill::CreditCard* card_; | 145 PaymentInstrument* instrument_; |
| 149 std::unique_ptr<views::ImageView> checkmark_; | 146 std::unique_ptr<views::ImageView> checkmark_; |
| 150 | 147 |
| 151 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); | 148 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); |
| 152 }; | 149 }; |
| 153 | 150 |
| 154 } // namespace | 151 } // namespace |
| 155 | 152 |
| 156 PaymentMethodViewController::PaymentMethodViewController( | 153 PaymentMethodViewController::PaymentMethodViewController( |
| 157 PaymentRequestSpec* spec, | 154 PaymentRequestSpec* spec, |
| 158 PaymentRequestState* state, | 155 PaymentRequestState* state, |
| 159 PaymentRequestDialogView* dialog) | 156 PaymentRequestDialogView* dialog) |
| 160 : PaymentRequestSheetController(spec, state, dialog) { | 157 : PaymentRequestSheetController(spec, state, dialog) { |
| 161 const std::vector<autofill::CreditCard*>& available_cards = | 158 const std::vector<std::unique_ptr<PaymentInstrument>>& available_instruments = |
| 162 state->credit_cards(); | 159 state->available_instruments(); |
| 163 | 160 |
| 164 for (autofill::CreditCard* card : available_cards) { | 161 for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 162 available_instruments) { |
| 165 std::unique_ptr<PaymentMethodListItem> item = | 163 std::unique_ptr<PaymentMethodListItem> item = |
| 166 base::MakeUnique<PaymentMethodListItem>( | 164 base::MakeUnique<PaymentMethodListItem>( |
| 167 card, spec, state, &payment_method_list_, | 165 instrument.get(), spec, state, &payment_method_list_, |
| 168 card == state->selected_credit_card()); | 166 instrument.get() == state->selected_instrument()); |
| 169 payment_method_list_.AddItem(std::move(item)); | 167 payment_method_list_.AddItem(std::move(item)); |
| 170 } | 168 } |
| 171 } | 169 } |
| 172 | 170 |
| 173 PaymentMethodViewController::~PaymentMethodViewController() {} | 171 PaymentMethodViewController::~PaymentMethodViewController() {} |
| 174 | 172 |
| 175 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { | 173 std::unique_ptr<views::View> PaymentMethodViewController::CreateView() { |
| 176 std::unique_ptr<views::View> list_view = | 174 std::unique_ptr<views::View> list_view = |
| 177 payment_method_list_.CreateListView(); | 175 payment_method_list_.CreateListView(); |
| 178 list_view->set_id( | 176 list_view->set_id( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 209 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
| 212 dialog()->ShowCreditCardEditor(); | 210 dialog()->ShowCreditCardEditor(); |
| 213 break; | 211 break; |
| 214 default: | 212 default: |
| 215 PaymentRequestSheetController::ButtonPressed(sender, event); | 213 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 216 break; | 214 break; |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 | 217 |
| 220 } // namespace payments | 218 } // namespace payments |
| OLD | NEW |