File: secure_payment_confirmation_views_util.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (178 lines) | stat: -rw-r--r-- 6,812 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 The Chromium Authors
// 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/secure_payment_confirmation_views_util.h"

#include "base/feature_list.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/views/accessibility/non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/grit/theme_resources.h"
#include "components/payments/core/features.h"
#include "components/payments/core/sizes.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/border.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace payments {

namespace {

const gfx::VectorIcon& GetPlatformVectorIcon(bool dark_mode) {
#if BUILDFLAG(IS_WIN)
  return dark_mode ? kSecurePaymentConfirmationFaceDarkIcon
                   : kSecurePaymentConfirmationFaceIcon;
#else
  return dark_mode ? kSecurePaymentConfirmationFingerprintDarkIcon
                   : kSecurePaymentConfirmationFingerprintIcon;
#endif
}

int GetSecurePaymentConfirmationHeaderWidth() {
  return ChromeLayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH);
}

ui::ImageModel GetHeaderImageSkia(bool dark_mode) {
  return ui::ImageModel::FromResourceId(dark_mode ? IDR_SAVE_CARD_DARK
                                                  : IDR_SAVE_CARD);
}

class SecurePaymentConfirmationHeaderIconView : public NonAccessibleImageView {
  METADATA_HEADER(SecurePaymentConfirmationHeaderIconView,
                  NonAccessibleImageView)

 public:
  explicit SecurePaymentConfirmationHeaderIconView(bool use_cart_image = false)
      : use_cart_image_{use_cart_image} {
    const gfx::Size header_size(
        GetSecurePaymentConfirmationHeaderWidth(),
        use_cart_image_ ? kShoppingCartHeaderIconHeight : kHeaderIconHeight);
    SetPreferredSize(header_size);
    SetVerticalAlignment(views::ImageView::Alignment::kLeading);
  }
  ~SecurePaymentConfirmationHeaderIconView() override = default;

  // NonAccessibleImageView:
  void OnThemeChanged() override {
    NonAccessibleImageView::OnThemeChanged();
    SetImage(use_cart_image_
                 ? GetHeaderImageSkia(GetNativeTheme()->ShouldUseDarkColors())
                 : ui::ImageModel::FromVectorIcon(
                       GetPlatformVectorIcon(
                           GetNativeTheme()->ShouldUseDarkColors()),
                       gfx::kPlaceholderColor));
  }

 private:
  bool use_cart_image_;
};

BEGIN_METADATA(SecurePaymentConfirmationHeaderIconView)
END_METADATA

}  // namespace

std::unique_ptr<views::View> CreateSecurePaymentConfirmationHeaderIcon(
    int header_icon_id,
    bool use_cart_image) {
  auto image_view =
      std::make_unique<SecurePaymentConfirmationHeaderIconView>(use_cart_image);
  image_view->SetID(header_icon_id);
  image_view->SetProperty(views::kMarginsKey,
                          gfx::Insets().set_top(kHeaderIconTopPadding));
  return image_view;
}

std::unique_ptr<views::Label> CreateSecurePaymentConfirmationTitleLabel(
    const std::u16string& title) {
  std::unique_ptr<views::Label> title_label = std::make_unique<views::Label>(
      title, views::style::CONTEXT_DIALOG_TITLE, views::style::STYLE_PRIMARY);
  title_label->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
  title_label->SetLineHeight(kTitleLineHeight);

  return title_label;
}

std::unique_ptr<views::ImageView> CreateSecurePaymentConfirmationIconView(
    const gfx::ImageSkia& image) {
  std::unique_ptr<views::ImageView> icon_view =
      std::make_unique<views::ImageView>();
  icon_view->SetImage(ui::ImageModel::FromImageSkia(image));

  gfx::Size image_size = image.size();
  // Resize to a constant height, with a variable width in the acceptable range
  // based on the aspect ratio.
  float aspect_ratio =
      static_cast<float>(image_size.width()) / image_size.height();
  int preferred_width =
      static_cast<int>(kSecurePaymentConfirmationIconHeightPx * aspect_ratio);
  int icon_width = std::max(
      std::min(preferred_width, kSecurePaymentConfirmationIconMaximumWidthPx),
      kSecurePaymentConfirmationIconDefaultWidthPx);
  icon_view->SetImageSize(
      gfx::Size(icon_width, kSecurePaymentConfirmationIconHeightPx));
  icon_view->SetPaintToLayer();
  icon_view->layer()->SetFillsBoundsOpaquely(false);

  return icon_view;
}

std::u16string FormatMerchantLabel(
    const std::optional<std::u16string>& merchant_name,
    const std::optional<std::u16string>& merchant_origin) {
  DCHECK(merchant_name.has_value() || merchant_origin.has_value());

  if (merchant_name.has_value() && merchant_origin.has_value()) {
    return base::StrCat(
        {merchant_name.value(), u" (", merchant_origin.value(), u")"});
  }
  return merchant_name.value_or(merchant_origin.value_or(u""));
}

std::unique_ptr<views::StyledLabel> CreateSecurePaymentConfirmationOptOutView(
    const std::u16string& relying_party_id,
    const std::u16string& opt_out_label,
    const std::u16string& opt_out_link_label,
    base::RepeatingClosure on_click) {
  // The opt-out text consists of a base label, filled in with the relying party
  // ID and a 'call to action' link.
  std::vector<std::u16string> subst{relying_party_id, opt_out_link_label};
  std::vector<size_t> offsets;
  std::u16string opt_out_text =
      base::ReplaceStringPlaceholders(opt_out_label, subst, &offsets);
  DCHECK_EQ(2U, offsets.size());

  views::StyledLabel::RangeStyleInfo link_style =
      views::StyledLabel::RangeStyleInfo::CreateForLink(on_click);

  return views::Builder<views::StyledLabel>()
      .SetText(opt_out_text)
      .SetTextContext(ChromeTextContext::CONTEXT_DIALOG_BODY_TEXT_SMALL)
      .SetDefaultTextStyle(views::style::STYLE_SECONDARY)
      .AddStyleRange(
          gfx::Range(offsets[1], offsets[1] + opt_out_link_label.length()),
          link_style)
      .SetProperty(views::kMarginsKey,
                   gfx::Insets::TLBR(
                       kSecondarySmallTextInsets, kSecondarySmallTextInsets,
                       kSecondarySmallTextInsets, kSecondarySmallTextInsets))
      .SizeToFit(views::LayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH))
      .Build();
}

}  // namespace payments