File: save_card_bubble_views.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (307 lines) | stat: -rw-r--r-- 12,543 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2015 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/autofill/payments/save_card_bubble_views.h"

#include <memory>

#include "build/build_config.h"
#include "chrome/browser/ui/autofill/payments/save_card_ui.h"
#include "chrome/browser/ui/views/autofill/payments/dialog_view_ids.h"
#include "chrome/browser/ui/views/autofill/payments/payments_view_util.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "components/autofill/core/browser/data_model/payments/credit_card.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/payments/legal_message_line.h"
#include "components/autofill/core/browser/ui/payments/payments_ui_closed_reasons.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/grit/components_scaled_resources.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/bubble/tooltip_icon.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/style/typography.h"

namespace autofill {

constexpr char16_t kEllipsisDotSeparator[] = u"\u2022";

int GetObfuscationLength() {
  return base::FeatureList::IsEnabled(
             features::kAutofillEnableNewFopDisplayDesktop)
             ? 2
             : 4;
}

SaveCardBubbleViews::SaveCardBubbleViews(views::View* anchor_view,
                                         content::WebContents* web_contents,
                                         SaveCardBubbleController* controller)
    : AutofillLocationBarBubble(anchor_view, web_contents),
      controller_(controller) {
  DCHECK(controller);
  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 controller->GetAcceptButtonText());
  SetButtonLabel(ui::mojom::DialogButton::kCancel,
                 controller->GetDeclineButtonText());
  SetAcceptCallback(base::BindOnce(&SaveCardBubbleViews::OnDialogAccepted,
                                   base::Unretained(this)));

  SetShowCloseButton(true);
  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH));
}

void SaveCardBubbleViews::Show(DisplayReason reason) {
  ShowForReason(reason);
  AssignIdsToDialogButtons();
}

void SaveCardBubbleViews::Hide() {
  CloseBubble();

  // If |controller_| is null, WindowClosing() won't invoke OnBubbleClosed(), so
  // do that here. This will clear out |controller_|'s reference to |this|. Note
  // that WindowClosing() happens only after the _asynchronous_ Close() task
  // posted in CloseBubble() completes, but we need to fix references sooner.
  if (controller_) {
    controller_->OnBubbleClosed(
        GetPaymentsUiClosedReasonFromWidget(GetWidget()));
  }
  controller_ = nullptr;
}

void SaveCardBubbleViews::OnDialogAccepted() {
  if (controller_) {
    controller_->OnSaveButton({});
  }
}

void SaveCardBubbleViews::OnBeforeBubbleWidgetInit(
    views::Widget::InitParams* params,
    views::Widget* widget) const {
  params->name = "SaveCardBubble";
}

void SaveCardBubbleViews::AddedToWidget() {
  // Use a custom title container if offering to upload a server card.
  // Done when this view is added to the widget, so the bubble frame
  // view is guaranteed to exist.
  if (!controller_->IsUploadSave()) {
    return;
  }

  GetBubbleFrameView()->SetTitleView(
      std::make_unique<TitleWithIconAfterLabelView>(
          GetWindowTitle(), TitleWithIconAfterLabelView::Icon::GOOGLE_PAY));
}

std::u16string SaveCardBubbleViews::GetWindowTitle() const {
  return controller_ ? controller_->GetWindowTitle() : std::u16string();
}

void SaveCardBubbleViews::WindowClosing() {
  if (controller_) {
    controller_->OnBubbleClosed(
        GetPaymentsUiClosedReasonFromWidget(GetWidget()));
    controller_ = nullptr;
  }
}

views::View* SaveCardBubbleViews::GetFootnoteViewForTesting() {
  return footnote_view_;
}

const std::u16string SaveCardBubbleViews::GetCardIdentifierString() const {
  return controller_->GetCard().CardNameAndLastFourDigits(
      /*customized_nickname=*/u"", GetObfuscationLength());
}

SaveCardBubbleViews::~SaveCardBubbleViews() = default;

// Overridden
std::unique_ptr<views::View> SaveCardBubbleViews::CreateMainContentView() {
  ChromeLayoutProvider* const provider = ChromeLayoutProvider::Get();

  auto view = std::make_unique<views::BoxLayoutView>();
  view->SetOrientation(views::BoxLayout::Orientation::kVertical);
  view->SetBetweenChildSpacing(
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL));

  // If applicable, add the upload explanation label. Appears above the card
  // info.
  std::u16string explanation = controller_->GetExplanatoryMessage();
  if (!explanation.empty()) {
    auto* const explanation_label =
        view->AddChildView(std::make_unique<views::Label>(
            explanation, views::style::CONTEXT_DIALOG_BODY_TEXT,
            views::style::STYLE_SECONDARY));
    explanation_label->SetMultiLine(true);
    explanation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  }

  // Add the card network or card art image, last four digits and expiration
  // date or CVC icon.
  auto* description_view =
      view->AddChildView(std::make_unique<views::BoxLayoutView>());
  description_view->SetBetweenChildSpacing(
      provider->GetDistanceMetric(views::DISTANCE_RELATED_BUTTON_HORIZONTAL));

  const CreditCard& card = controller_->GetCard();
  auto* const card_network_icon = description_view->AddChildView(
      std::make_unique<views::ImageView>(controller_->GetCreditCardImage()));
  card_network_icon->SetTooltipText(card.NetworkForDisplay());
  auto* card_identifier_view =
      description_view->AddChildView(GetCardIdentifierView());

  // Flex |card_identifier_view| to fill up space before the expiry date or CVC
  // icon.
  if (controller()->GetBubbleType() == BubbleType::LOCAL_CVC_SAVE ||
      controller()->GetBubbleType() == BubbleType::UPLOAD_CVC_SAVE) {
    description_view->SetFlexForView(card_identifier_view, 1);
  }

  return view;
}

void SaveCardBubbleViews::InitFootnoteView(views::View* footnote_view) {
  DCHECK(!footnote_view_);
  footnote_view_ = footnote_view;
  footnote_view_->SetID(DialogViewId::FOOTNOTE_VIEW);
}

std::unique_ptr<views::View> SaveCardBubbleViews::GetCardIdentifierView() {
  bool is_cvc_only_save =
      controller()->GetBubbleType() == BubbleType::LOCAL_CVC_SAVE ||
      controller()->GetBubbleType() == BubbleType::UPLOAD_CVC_SAVE;

  // Display the card expiration date in a separate line for credit card saves.
  // For CVC only save, the card name, last 4 digit and CVC icon will be shown
  // in the same line.
  auto card_identifier_view = std::make_unique<views::View>();
  auto* layout = card_identifier_view->SetLayoutManager(
      std::make_unique<views::FlexLayout>());
  if (is_cvc_only_save || base::FeatureList::IsEnabled(
                              features::kAutofillEnableNewFopDisplayDesktop)) {
    layout->SetCollapseMargins(true);
    layout->SetDefault(
        views::kMarginsKey,
        gfx::Insets::TLBR(0, 0, 0,
                          ChromeLayoutProvider::Get()->GetDistanceMetric(
                              views::DISTANCE_RELATED_BUTTON_HORIZONTAL)));
  } else {
    layout->SetOrientation(views::LayoutOrientation::kVertical);
    layout->SetCrossAxisAlignment(views::LayoutAlignment::kStart);
  }

  const CreditCard& card = controller_->GetCard();
  auto* const card_identifier_label =
      card_identifier_view->AddChildView(std::make_unique<views::Label>(
          is_cvc_only_save ? card.CardNameForAutofillDisplay()
                           : GetCardIdentifierString(),
          views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY));
  // Disable multi line for CVC-only save (prompted after card usage) as the
  // name can be very long with card name and art enabled. This change does not
  // affect credit card upload save (prompted after a new or local card is
  // entered).
  card_identifier_label->SetMultiLine(!is_cvc_only_save);
  card_identifier_label->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);

  if (is_cvc_only_save) {
    // Add card last four, the spacing, and the CVC icon.
    card_identifier_view->AddChildView(std::make_unique<views::Label>(
        card.ObfuscatedNumberWithVisibleLastFourDigits(),
        views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY));
    auto* gap_view =
        card_identifier_view->AddChildView(std::make_unique<views::View>());
    card_identifier_view->AddChildView(
        std::make_unique<views::ImageView>(ui::ImageModel::FromImage(
            ui::ResourceBundle::GetSharedInstance().GetImageNamed(
                IDR_CREDIT_CARD_CVC_HINT_BACK))));

    // Shrink the `card_identifier_view` to fit the view, when there is not
    // enough space to accommodate all child views.
    card_identifier_label->SetProperty(
        views::kFlexBehaviorKey,
        views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
                                 views::MaximumFlexSizeRule::kPreferred)
            .WithOrder(1));
    // Extend the `gap_view` to fill the view, when there is more space to
    // accommodate all child views.
    gap_view->SetProperty(
        views::kFlexBehaviorKey,
        views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
                                 views::MaximumFlexSizeRule::kUnbounded)
            .WithOrder(2));
  } else if (!card.IsExpired(base::Time::Now())) {
    if (base::FeatureList::IsEnabled(
            features::kAutofillEnableNewFopDisplayDesktop)) {
      card_identifier_view->AddChildView(std::make_unique<views::Label>(
          kEllipsisDotSeparator, views::style::CONTEXT_DIALOG_BODY_TEXT,
          views::style::STYLE_SECONDARY));
    }
    // Add card expiration date for card saves.
    auto* expiration_date_label =
        card_identifier_view->AddChildView(std::make_unique<views::Label>(
            card.AbbreviatedExpirationDateForDisplay(false),
            views::style::CONTEXT_DIALOG_BODY_TEXT,
            views::style::STYLE_SECONDARY));
    expiration_date_label->SetID(DialogViewId::EXPIRATION_DATE_LABEL);
  }

  return card_identifier_view;
}

void SaveCardBubbleViews::AssignIdsToDialogButtons() {
  auto* ok_button = GetOkButton();
  if (ok_button) {
    ok_button->SetID(DialogViewId::OK_BUTTON);
  }
  auto* cancel_button = GetCancelButton();
  if (cancel_button) {
    cancel_button->SetID(DialogViewId::CANCEL_BUTTON);
  }
}

void SaveCardBubbleViews::Init() {
  ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));

  // For server cards, there is an explanation between the title and the
  // controls; use DialogContentType::kText. For local cards, since there is no
  // explanation, use DialogContentType::kControl instead.
  // There are legal messages before the buttons for server cards, so use
  // DialogContentType::kText. For local card, since there is no legal message,
  // use DialogContentType::kControl instead.
  set_margins(provider->GetDialogInsetsForContentType(
      controller_->GetExplanatoryMessage().empty()
          ? views::DialogContentType::kControl
          : views::DialogContentType::kText,
      !controller_->GetLegalMessageLines().empty()
          ? views::DialogContentType::kText
          : views::DialogContentType::kControl));
  AddChildView(CreateMainContentView());
}

BEGIN_METADATA(SaveCardBubbleViews)
END_METADATA

}  // namespace autofill