1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/payments/payment_method_view_controller.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_forward.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog_view.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h"
#include "chrome/browser/ui/views/payments/payment_request_row_view.h"
#include "chrome/browser/ui/views/payments/payment_request_views_util.h"
#include "components/payments/content/payment_request_state.h"
#include "components/payments/core/autofill_payment_instrument.h"
#include "components/payments/core/payment_instrument.h"
#include "components/payments/core/strings_util.h"
#include "components/strings/grit/components_strings.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/vector_icons.h"
namespace payments {
namespace {
enum class PaymentMethodViewControllerTags : int {
// The tag for the button that triggers the "add card" flow. Starts at
// |PAYMENT_REQUEST_COMMON_TAG_MAX| not to conflict with tags common to all
// views.
ADD_CREDIT_CARD_BUTTON = static_cast<int>(
PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX),
// This value is passed to inner views so they can use it as a starting tag.
MAX_TAG,
};
class PaymentMethodListItem : public PaymentRequestItemList::Item {
public:
// Does not take ownership of |instrument|, which should not be null and
// should outlive this object. |list| is the PaymentRequestItemList object
// that will own this.
PaymentMethodListItem(PaymentInstrument* instrument,
PaymentRequestSpec* spec,
PaymentRequestState* state,
PaymentRequestItemList* list,
PaymentRequestDialogView* dialog,
bool selected)
: PaymentRequestItemList::Item(spec,
state,
list,
selected,
/*clickable=*/true,
/*show_edit_button=*/instrument->type() ==
PaymentInstrument::Type::AUTOFILL),
instrument_(instrument),
dialog_(dialog) {
Init();
}
~PaymentMethodListItem() override {}
private:
void ShowEditor() {
switch (instrument_->type()) {
case PaymentInstrument::Type::AUTOFILL:
// Since we are a list item, we only care about the on_edited callback.
dialog_->ShowCreditCardEditor(
BackNavigationType::kPaymentSheet,
static_cast<int>(PaymentMethodViewControllerTags::MAX_TAG),
/*on_edited=*/
base::BindOnce(&PaymentRequestState::SetSelectedInstrument,
base::Unretained(state()), instrument_),
/*on_added=*/
base::OnceCallback<void(const autofill::CreditCard&)>(),
static_cast<AutofillPaymentInstrument*>(instrument_)
->credit_card());
return;
case PaymentInstrument::Type::NATIVE_MOBILE_APP:
case PaymentInstrument::Type::SERVICE_WORKER_APP:
// We cannot edit a native mobile app instrument and service worker
// based payment instrument.
return;
}
NOTREACHED();
}
// PaymentRequestItemList::Item:
std::unique_ptr<views::View> CreateExtraView() override {
std::unique_ptr<views::ImageView> icon_view = CreateInstrumentIconView(
instrument_->icon_resource_id(), instrument_->icon_image_skia(),
instrument_->GetLabel());
return icon_view;
}
std::unique_ptr<views::View> CreateContentView(
base::string16* accessible_content) override {
DCHECK(accessible_content);
auto card_info_container = std::make_unique<views::View>();
card_info_container->set_can_process_events_within_subtree(false);
auto box_layout = std::make_unique<views::BoxLayout>(
views::BoxLayout::kVertical,
gfx::Insets(kPaymentRequestRowVerticalInsets, 0));
box_layout->set_cross_axis_alignment(
views::BoxLayout::CROSS_AXIS_ALIGNMENT_START);
card_info_container->SetLayoutManager(std::move(box_layout));
base::string16 label = instrument_->GetLabel();
if (!label.empty())
card_info_container->AddChildView(new views::Label(label));
base::string16 sublabel = instrument_->GetSublabel();
if (!sublabel.empty())
card_info_container->AddChildView(new views::Label(sublabel));
base::string16 missing_info;
if (!instrument_->IsCompleteForPayment()) {
missing_info = instrument_->GetMissingInfoLabel();
auto missing_info_label =
std::make_unique<views::Label>(missing_info, CONTEXT_BODY_TEXT_SMALL);
missing_info_label->SetEnabledColor(
missing_info_label->GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LinkEnabled));
card_info_container->AddChildView(missing_info_label.release());
}
*accessible_content = l10n_util::GetStringFUTF16(
IDS_PAYMENTS_PROFILE_LABELS_ACCESSIBLE_FORMAT, label, sublabel,
missing_info);
return card_info_container;
}
void SelectedStateChanged() override {
if (selected()) {
state()->SetSelectedInstrument(instrument_);
dialog_->GoBack();
}
}
base::string16 GetNameForDataType() override {
return l10n_util::GetStringUTF16(IDS_PAYMENTS_METHOD_OF_PAYMENT_LABEL);
}
bool CanBeSelected() override {
// If an instrument can't be selected, PerformSelectionFallback is called,
// where the instrument can be made complete.
return instrument_->IsCompleteForPayment();
}
void PerformSelectionFallback() override { ShowEditor(); }
void EditButtonPressed() override { ShowEditor(); }
PaymentInstrument* instrument_;
PaymentRequestDialogView* dialog_;
DISALLOW_COPY_AND_ASSIGN(PaymentMethodListItem);
};
std::unique_ptr<views::View> CreateHeaderView(const base::string16& text) {
auto label = std::make_unique<views::Label>(text);
label->SetMultiLine(true);
label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label->SetBorder(views::CreateEmptyBorder(
kPaymentRequestRowVerticalInsets, kPaymentRequestRowHorizontalInsets, 0,
kPaymentRequestRowHorizontalInsets));
return label;
}
} // namespace
PaymentMethodViewController::PaymentMethodViewController(
PaymentRequestSpec* spec,
PaymentRequestState* state,
PaymentRequestDialogView* dialog)
: PaymentRequestSheetController(spec, state, dialog),
payment_method_list_(dialog) {
const std::vector<std::unique_ptr<PaymentInstrument>>& available_instruments =
state->available_instruments();
for (const auto& instrument : available_instruments) {
auto item = std::make_unique<PaymentMethodListItem>(
instrument.get(), spec, state, &payment_method_list_, dialog,
instrument.get() == state->selected_instrument());
payment_method_list_.AddItem(std::move(item));
}
}
PaymentMethodViewController::~PaymentMethodViewController() {}
base::string16 PaymentMethodViewController::GetSheetTitle() {
return l10n_util::GetStringUTF16(
IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME);
}
void PaymentMethodViewController::FillContentView(views::View* content_view) {
auto layout = std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical);
layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
layout->set_cross_axis_alignment(
views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
content_view->SetLayoutManager(std::move(layout));
base::string16 sub_header =
GetCardTypesAreAcceptedText(spec()->supported_card_types_set());
if (!sub_header.empty())
content_view->AddChildView(CreateHeaderView(sub_header).release());
std::unique_ptr<views::View> list_view =
payment_method_list_.CreateListView();
list_view->set_id(
static_cast<int>(DialogViewID::PAYMENT_METHOD_SHEET_LIST_VIEW));
content_view->AddChildView(list_view.release());
}
std::unique_ptr<views::View>
PaymentMethodViewController::CreateExtraFooterView() {
if (!spec()->supports_basic_card())
return nullptr;
auto extra_view = std::make_unique<views::View>();
extra_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::kHorizontal, gfx::Insets(),
kPaymentRequestButtonSpacing));
views::LabelButton* button = views::MdTextButton::CreateSecondaryUiButton(
this, l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD));
button->set_tag(static_cast<int>(
PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON));
button->set_id(
static_cast<int>(DialogViewID::PAYMENT_METHOD_ADD_CARD_BUTTON));
button->SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
extra_view->AddChildView(button);
return extra_view;
}
void PaymentMethodViewController::ButtonPressed(views::Button* sender,
const ui::Event& event) {
switch (sender->tag()) {
case static_cast<int>(
PaymentMethodViewControllerTags::ADD_CREDIT_CARD_BUTTON):
// Only provide the |on_added| callback, in response to this button.
dialog()->ShowCreditCardEditor(
BackNavigationType::kPaymentSheet,
static_cast<int>(PaymentMethodViewControllerTags::MAX_TAG),
/*on_edited=*/base::OnceClosure(),
/*on_added=*/
base::BindOnce(&PaymentRequestState::AddAutofillPaymentInstrument,
base::Unretained(state()), /*selected=*/true),
/*credit_card=*/nullptr);
break;
default:
PaymentRequestSheetController::ButtonPressed(sender, event);
break;
}
}
} // namespace payments
|