| 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 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback_forward.h" |
| 11 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 16 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 14 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 17 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 15 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" | 18 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" |
| 16 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 19 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 17 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 18 #include "components/payments/content/payment_request_state.h" | 21 #include "components/payments/content/payment_request_state.h" |
| 19 #include "components/payments/core/autofill_payment_instrument.h" | 22 #include "components/payments/core/autofill_payment_instrument.h" |
| 20 #include "components/payments/core/payment_instrument.h" | 23 #include "components/payments/core/payment_instrument.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 37 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); | 40 payments::PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX); |
| 38 | 41 |
| 39 enum class PaymentMethodViewControllerTags : int { | 42 enum class PaymentMethodViewControllerTags : int { |
| 40 // The tag for the button that triggers the "add card" flow. Starts at | 43 // The tag for the button that triggers the "add card" flow. Starts at |
| 41 // |kFirstTagValue| not to conflict with tags common to all views. | 44 // |kFirstTagValue| not to conflict with tags common to all views. |
| 42 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, | 45 ADD_CREDIT_CARD_BUTTON = kFirstTagValue, |
| 43 }; | 46 }; |
| 44 | 47 |
| 45 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item { | 48 class PaymentMethodListItem : public payments::PaymentRequestItemList::Item { |
| 46 public: | 49 public: |
| 47 // Does not take ownership of |instrument|, which should not be null and | 50 // Does not take ownership of |instrument|, which should not be null and |
| 48 // should outlive this object. |list| is the PaymentRequestItemList object | 51 // should outlive this object. |list| is the PaymentRequestItemList object |
| 49 // that will own this. | 52 // that will own this. |
| 50 PaymentMethodListItem(PaymentInstrument* instrument, | 53 PaymentMethodListItem(PaymentInstrument* instrument, |
| 51 PaymentRequestSpec* spec, | 54 PaymentRequestSpec* spec, |
| 52 PaymentRequestState* state, | 55 PaymentRequestState* state, |
| 53 PaymentRequestItemList* list, | 56 PaymentRequestItemList* list, |
| 54 PaymentRequestDialogView* dialog, | 57 PaymentRequestDialogView* dialog, |
| 55 bool selected) | 58 bool selected) |
| 56 : payments::PaymentRequestItemList::Item(spec, state, list, selected), | 59 : payments::PaymentRequestItemList::Item(spec, state, list, selected), |
| 57 instrument_(instrument), | 60 instrument_(instrument), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 99 |
| 97 bool CanBeSelected() const override { | 100 bool CanBeSelected() const override { |
| 98 // If an instrument can't be selected, PerformSelectionFallback is called, | 101 // If an instrument can't be selected, PerformSelectionFallback is called, |
| 99 // where the instrument can be made complete. | 102 // where the instrument can be made complete. |
| 100 return instrument_->IsCompleteForPayment(); | 103 return instrument_->IsCompleteForPayment(); |
| 101 } | 104 } |
| 102 | 105 |
| 103 void PerformSelectionFallback() override { | 106 void PerformSelectionFallback() override { |
| 104 switch (instrument_->type()) { | 107 switch (instrument_->type()) { |
| 105 case PaymentInstrument::Type::AUTOFILL: | 108 case PaymentInstrument::Type::AUTOFILL: |
| 109 // Since we are a list item, we only care about the on_edited callback. |
| 106 dialog_->ShowCreditCardEditor( | 110 dialog_->ShowCreditCardEditor( |
| 111 /*on_edited=*/base::BindOnce( |
| 112 &PaymentRequestState::SetSelectedInstrument, |
| 113 base::Unretained(state()), instrument_), |
| 114 /*on_added=*/ |
| 115 base::OnceCallback<void(const autofill::CreditCard&)>(), |
| 107 static_cast<AutofillPaymentInstrument*>(instrument_) | 116 static_cast<AutofillPaymentInstrument*>(instrument_) |
| 108 ->credit_card()); | 117 ->credit_card()); |
| 109 return; | 118 return; |
| 110 } | 119 } |
| 111 NOTREACHED(); | 120 NOTREACHED(); |
| 112 } | 121 } |
| 113 | 122 |
| 114 PaymentInstrument* instrument_; | 123 PaymentInstrument* instrument_; |
| 115 PaymentRequestDialogView* dialog_; | 124 PaymentRequestDialogView* dialog_; |
| 116 | 125 |
| 117 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); | 126 DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem); |
| 118 }; | 127 }; |
| 119 | 128 |
| 120 } // namespace | 129 } // namespace |
| 121 | 130 |
| 122 PaymentMethodViewController::PaymentMethodViewController( | 131 PaymentMethodViewController::PaymentMethodViewController( |
| 123 PaymentRequestSpec* spec, | 132 PaymentRequestSpec* spec, |
| 124 PaymentRequestState* state, | 133 PaymentRequestState* state, |
| 125 PaymentRequestDialogView* dialog) | 134 PaymentRequestDialogView* dialog) |
| 126 : PaymentRequestSheetController(spec, state, dialog) { | 135 : PaymentRequestSheetController(spec, state, dialog) { |
| 127 const std::vector<std::unique_ptr<PaymentInstrument>>& available_instruments = | 136 const std::vector<std::unique_ptr<PaymentInstrument>>& available_instruments = |
| 128 state->available_instruments(); | 137 state->available_instruments(); |
| 129 | |
| 130 for (const std::unique_ptr<PaymentInstrument>& instrument : | 138 for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 131 available_instruments) { | 139 available_instruments) { |
| 132 std::unique_ptr<PaymentMethodListItem> item = | 140 std::unique_ptr<PaymentMethodListItem> item = |
| 133 base::MakeUnique<PaymentMethodListItem>( | 141 base::MakeUnique<PaymentMethodListItem>( |
| 134 instrument.get(), spec, state, &payment_method_list_, dialog, | 142 instrument.get(), spec, state, &payment_method_list_, dialog, |
| 135 instrument.get() == state->selected_instrument()); | 143 instrument.get() == state->selected_instrument()); |
| 136 payment_method_list_.AddItem(std::move(item)); | 144 payment_method_list_.AddItem(std::move(item)); |
| 137 } | 145 } |
| 138 } | 146 } |
| 139 | 147 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 169 extra_view->AddChildView(button); | 177 extra_view->AddChildView(button); |
| 170 | 178 |
| 171 return extra_view; | 179 return extra_view; |
| 172 } | 180 } |
| 173 | 181 |
| 174 void PaymentMethodViewController::ButtonPressed(views::Button* sender, | 182 void PaymentMethodViewController::ButtonPressed(views::Button* sender, |
| 175 const ui::Event& event) { | 183 const ui::Event& event) { |
| 176 switch (sender->tag()) { | 184 switch (sender->tag()) { |
| 177 case static_cast<int>( | 185 case static_cast<int>( |
| 178 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): | 186 PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON): |
| 179 dialog()->ShowCreditCardEditor(); | 187 // Only provide the |on_added| callback, in response to this button. |
| 188 dialog()->ShowCreditCardEditor( |
| 189 /*on_edited=*/base::OnceClosure(), |
| 190 /*on_added=*/ |
| 191 base::BindOnce(&PaymentRequestState::AddAutofillPaymentInstrument, |
| 192 base::Unretained(state()), /*selected=*/true), |
| 193 /*credit_card=*/nullptr); |
| 180 break; | 194 break; |
| 181 default: | 195 default: |
| 182 PaymentRequestSheetController::ButtonPressed(sender, event); | 196 PaymentRequestSheetController::ButtonPressed(sender, event); |
| 183 break; | 197 break; |
| 184 } | 198 } |
| 185 } | 199 } |
| 186 | 200 |
| 187 } // namespace payments | 201 } // namespace payments |
| OLD | NEW |