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
|
// 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/payments/constants.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"
#include "ui/views/controls/throbber.h"
namespace autofill {
SaveAndFillDialog::SaveAndFillDialog(
base::WeakPtr<SaveAndFillDialogController> controller,
base::RepeatingCallback<void(const GURL&)> on_legal_message_link_clicked)
: controller_(controller),
on_legal_message_link_clicked_(on_legal_message_link_clicked) {
// 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));
SetButtonEnabled(ui::mojom::DialogButton::kOk, false);
SetButtonLabel(ui::mojom::DialogButton::kOk,
controller_->GetAcceptButtonText());
SetShowCloseButton(false);
InitViews();
}
SaveAndFillDialog::~SaveAndFillDialog() = default;
void SaveAndFillDialog::AddedToWidget() {
GetWidget()->MakeCloseSynchronous(base::BindOnce(
&SaveAndFillDialog::OnDialogClosed, base::Unretained(this)));
focus_manager_ = GetFocusManager();
if (focus_manager_) {
focus_manager_->AddFocusChangeListener(this);
}
if (controller_->GetDialogState() == SaveAndFillDialogState::kUploadDialog) {
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=*/controller_->IsValidCreditCardNumber(new_contents),
/*error_message=*/controller_->GetInvalidCardNumberErrorMessage());
} else if (sender == &cvc_data_.GetInputTextField()) {
cvc_data_.SetErrorState(
/*is_valid=*/controller_->IsValidCvc(new_contents),
/*error_message=*/controller_->GetInvalidCvcErrorMessage());
} else if (sender == &name_on_card_data_.GetInputTextField()) {
name_on_card_data_.SetErrorState(
/*is_valid=*/controller_->IsValidNameOnCard(new_contents),
/*error_message=*/controller_->GetInvalidNameOnCardErrorMessage());
} else if (sender == &expiration_date_data_.GetInputTextField()) {
size_t new_cursor_position;
std::u16string formatted_input = controller_->FormatExpirationDateInput(
/*input=*/new_contents,
/*old_cursor_position=*/sender->GetCursorPosition(),
/*new_cursor_position=*/new_cursor_position);
// Only update the textfield and cursor if the formatting resulted in a
// change.
if (new_contents != formatted_input) {
sender->SetText(formatted_input);
sender->SelectSelectionModel(
gfx::SelectionModel(new_cursor_position, gfx::CURSOR_FORWARD));
}
expiration_date_data_.SetErrorState(
/*is_valid=*/controller_->IsValidExpirationDate(formatted_input),
/*error_message=*/controller_->GetInvalidExpirationDateErrorMessage());
}
// Enable the save button iff all textfields are valid.
SetButtonEnabled(ui::mojom::DialogButton::kOk,
card_number_data_.is_valid_input &&
cvc_data_.is_valid_input &&
expiration_date_data_.is_valid_input &&
name_on_card_data_.is_valid_input);
}
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));
container_view_ = AddChildView(std::make_unique<views::View>());
container_view_->SetUseDefaultFillLayout(true);
CreateMainContentView();
CreatePendingView();
if (controller_->GetDialogState() == SaveAndFillDialogState::kPendingDialog) {
ToggleThrobberVisibility(/*visible=*/true);
} else if (controller_->GetDialogState() ==
SaveAndFillDialogState::kUploadDialog) {
ToggleThrobberVisibility(/*visible=*/false);
}
}
void SaveAndFillDialog::CreateMainContentView() {
container_view_->AddChildView(
views::Builder<views::BoxLayoutView>()
.CopyAddressTo(&main_view_)
.SetOrientation(views::BoxLayout::Orientation::kVertical)
.SetBetweenChildSpacing(
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL))
.Build());
main_view_->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);
main_view_->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);
// CVC is an optional field, so it is considered valid by default when the
// dialog first appears.
cvc_data_.SetErrorState(
/*is_valid=*/true,
/*error_message=*/std::u16string());
// Create the horizontal row for expiration date, cvc, and icon.
main_view_->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());
name_on_card_data_ = CreateLabelAndTextfieldView(
/*label_text=*/controller_->GetNameOnCardLabel(),
/*error_message=*/controller_->GetInvalidNameOnCardErrorMessage());
name_on_card_data_.GetInputTextField().SetController(this);
main_view_->AddChildView(std::move(name_on_card_data_.container));
if (controller_->GetDialogState() == SaveAndFillDialogState::kUploadDialog) {
main_view_->AddChildView(CreateLegalMessageView());
}
}
void SaveAndFillDialog::CreatePendingView() {
container_view_->AddChildView(
views::Builder<views::BoxLayoutView>()
.CopyAddressTo(&pending_view_)
.SetMainAxisAlignment(views::BoxLayout::MainAxisAlignment::kCenter)
.SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kCenter)
.AddChild(
views::Builder<views::Throbber>(
std::make_unique<views::Throbber>(kDialogThrobberDiameter))
.CopyAddressTo(&throbber_))
.SetVisible(false)
.Build());
}
void SaveAndFillDialog::ToggleThrobberVisibility(bool visible) {
visible ? throbber_->Start() : throbber_->Stop();
main_view_->SetVisible(!visible);
pending_view_->SetVisible(visible);
}
payments::PaymentsAutofillClient::UserProvidedCardSaveAndFillDetails
SaveAndFillDialog::GetUserProvidedDataFromInput() const {
payments::PaymentsAutofillClient::UserProvidedCardSaveAndFillDetails
user_provided_card_details;
user_provided_card_details.card_number =
card_number_data_.GetInputTextField().GetText();
user_provided_card_details.cardholder_name =
name_on_card_data_.GetInputTextField().GetText();
user_provided_card_details.security_code =
cvc_data_.GetInputTextField().GetText();
const std::u16string exp_text =
std::u16string(expiration_date_data_.GetInputTextField().GetText());
size_t slash_pos = exp_text.find(u'/');
// This should never happen, as the expiration date field is validated and
// formatted before the Save button is enabled.
CHECK(slash_pos != std::u16string::npos && slash_pos > 0 &&
slash_pos < exp_text.length() - 1);
user_provided_card_details.expiration_date_month =
exp_text.substr(0, slash_pos);
user_provided_card_details.expiration_date_year =
exp_text.substr(slash_pos + 1);
return user_provided_card_details;
}
void SaveAndFillDialog::OnDialogClosed(views::Widget::ClosedReason reason) {
if (reason == views::Widget::ClosedReason::kAcceptButtonClicked) {
controller_->OnUserAcceptedDialog(GetUserProvidedDataFromInput());
} else if (reason == views::Widget::ClosedReason::kCancelButtonClicked) {
controller_->OnUserCanceledDialog();
} else {
controller_->Dismiss();
}
}
std::unique_ptr<views::View> SaveAndFillDialog::CreateLegalMessageView() {
const LegalMessageLines& message_lines = controller_->GetLegalMessageLines();
if (message_lines.empty()) {
return nullptr;
}
return autofill::CreateLegalMessageView(
message_lines, std::u16string(), ui::ImageModel(),
base::BindRepeating(on_legal_message_link_clicked_));
}
} // namespace autofill
|