File: save_and_fill_dialog.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 7,108 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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_and_fill_dialog.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/ui/payments/save_and_fill_dialog_controller.h"
#include "components/autofill/core/common/credit_card_number_validation.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/mojom/dialog_button.mojom.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/textfield/textfield.h"

namespace autofill {

SaveAndFillDialog::SaveAndFillDialog(
    base::WeakPtr<SaveAndFillDialogController> controller)
    : controller_(controller) {
  // Set the ownership of the delegate, not the View. The View is owned by the
  // Widget as a child view.
  // TODO(crbug.com/338254375): Remove the following line once this is the
  // default state for widgets.
  SetOwnershipOfNewWidget(views::Widget::InitParams::CLIENT_OWNS_WIDGET);
  SetModalType(ui::mojom::ModalType::kChild);
  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kOk) |
             static_cast<int>(ui::mojom::DialogButton::kCancel));
  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 controller_->GetAcceptButtonText());
  SetShowCloseButton(false);
  InitViews();
}

SaveAndFillDialog::~SaveAndFillDialog() = default;

void SaveAndFillDialog::AddedToWidget() {
  focus_manager_ = GetFocusManager();
  if (focus_manager_) {
    focus_manager_->AddFocusChangeListener(this);
  }

  if (controller_->IsUploadSaveAndFill()) {
    GetBubbleFrameView()->SetTitleView(
        std::make_unique<TitleWithIconAfterLabelView>(
            GetWindowTitle(), TitleWithIconAfterLabelView::Icon::GOOGLE_PAY));
  } else {
    auto title_view = std::make_unique<views::Label>(
        GetWindowTitle(), views::style::CONTEXT_DIALOG_TITLE);
    title_view->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
    title_view->SetMultiLine(true);
    GetBubbleFrameView()->SetTitleView(std::move(title_view));
  }
}

void SaveAndFillDialog::RemovedFromWidget() {
  if (focus_manager_) {
    focus_manager_->RemoveFocusChangeListener(this);
    focus_manager_ = nullptr;
  }
}

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

void SaveAndFillDialog::ContentsChanged(views::Textfield* sender,
                                        const std::u16string& new_contents) {
  if (sender == &card_number_data_.GetInputTextField()) {
    card_number_data_.SetErrorState(
        /*is_valid_input=*/controller_->IsValidCreditCardNumber(new_contents),
        /*error_message=*/controller_->GetInvalidCardNumberErrorMessage());
  }
}

void SaveAndFillDialog::OnDidChangeFocus(views::View* before,
                                         views::View* now) {
  if (before == &card_number_data_.GetInputTextField()) {
    card_number_data_.GetInputTextField().SetText(
        GetFormattedCardNumberForDisplay(
            card_number_data_.GetInputTextField().GetText()));
  }
}

void SaveAndFillDialog::InitViews() {
  auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      ChromeLayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_RELATED_CONTROL_VERTICAL)));
  layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kCenter);
  set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
      views::DialogContentType::kControl, views::DialogContentType::kControl));

  AddChildView(views::Builder<views::Label>()
                   .SetText(controller_->GetExplanatoryMessage())
                   .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
                   .SetTextStyle(views::style::STYLE_SECONDARY)
                   .SetMultiLine(true)
                   .SetHorizontalAlignment(gfx::ALIGN_TO_HEAD)
                   .Build());

  card_number_data_ = CreateLabelAndTextfieldView(
      /*label_text=*/controller_->GetCardNumberLabel(),
      /*error_message=*/controller_->GetInvalidCardNumberErrorMessage());
  card_number_data_.GetInputTextField().SetTextInputType(
      ui::TextInputType::TEXT_INPUT_TYPE_NUMBER);
  card_number_data_.GetInputTextField().SetController(this);
  AddChildView(std::move(card_number_data_.container));

  expiration_date_data_ = CreateLabelAndTextfieldView(
      /*label_text=*/controller_->GetExpirationDateLabel(),
      /*error_message=*/std::u16string());
  expiration_date_data_.GetInputTextField().SetTextInputType(
      ui::TextInputType::TEXT_INPUT_TYPE_DATE);
  expiration_date_data_.GetInputTextField().SetController(this);
  expiration_date_data_.GetInputTextField().SetPlaceholderText(
      l10n_util::GetStringUTF16(
          IDS_AUTOFILL_SAVE_AND_FILL_DIALOG_EXPIRATION_DATE_PLACEHOLDER));
  expiration_date_data_.GetInputTextField().SetDefaultWidthInChars(18);

  cvc_data_ = CreateLabelAndTextfieldView(
      /*label_text=*/controller_->GetCvcLabel(),
      /*error_message=*/std::u16string());
  cvc_data_.GetInputTextField().SetTextInputType(
      ui::TextInputType::TEXT_INPUT_TYPE_NUMBER);
  cvc_data_.GetInputTextField().SetController(this);
  cvc_data_.GetInputTextField().SetPlaceholderText(l10n_util::GetStringUTF16(
      IDS_AUTOFILL_SAVE_AND_FILL_DIALOG_CVC_PLACEHOLDER));
  cvc_data_.GetInputTextField().SetDefaultWidthInChars(18);

  // Create the horizontal row for expiration date, cvc, and icon.
  AddChildView(
      views::Builder<views::BoxLayoutView>()
          .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
          .SetBetweenChildSpacing(
              ChromeLayoutProvider::Get()->GetDistanceMetric(
                  views::DISTANCE_RELATED_CONTROL_HORIZONTAL))
          .AddChild(views::Builder<views::View>(
              std::move(expiration_date_data_.container)))
          .AddChild(views::Builder<views::View>(std::move(cvc_data_.container)))
          .AddChild(views::Builder<views::ImageView>().SetImage(
              ui::ImageModel::FromImage(
                  ui::ResourceBundle::GetSharedInstance().GetImageNamed(
                      IDR_CREDIT_CARD_CVC_HINT_BACK))))
          .Build());

  // TODO(crbug.com/378163937): Implement validation rule for the `name on card`
  // field.
  AddChildView(std::move(CreateLabelAndTextfieldView(
                             /*label_text=*/controller_->GetNameOnCardLabel(),
                             /*error_message=*/std::u16string())
                             .container));
}

}  // namespace autofill