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
|
// Copyright 2017 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/contact_info_editor_view_controller.h"
#include <string_view>
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/views/payments/validating_textfield.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_structured_address_component.h"
#include "components/autofill/core/browser/data_quality/validation.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/autofill/core/browser/geo/phone_number_i18n.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/payments/content/payment_request_spec.h"
#include "components/payments/content/payment_request_state.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_combobox_model.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
namespace payments {
ContactInfoEditorViewController::ContactInfoEditorViewController(
base::WeakPtr<PaymentRequestSpec> spec,
base::WeakPtr<PaymentRequestState> state,
base::WeakPtr<PaymentRequestDialogView> dialog,
BackNavigationType back_navigation_type,
base::OnceClosure on_edited,
base::OnceCallback<void(const autofill::AutofillProfile&)> on_added,
autofill::AutofillProfile* profile,
bool is_incognito)
: EditorViewController(spec,
state,
dialog,
back_navigation_type,
is_incognito),
profile_to_edit_(profile),
on_edited_(std::move(on_edited)),
on_added_(std::move(on_added)) {}
ContactInfoEditorViewController::~ContactInfoEditorViewController() = default;
bool ContactInfoEditorViewController::IsEditingExistingItem() {
return !!profile_to_edit_;
}
std::vector<EditorField>
ContactInfoEditorViewController::GetFieldDefinitions() {
std::vector<EditorField> fields;
if (!spec()) {
return fields;
}
if (spec()->request_payer_name()) {
fields.emplace_back(
autofill::NAME_FULL,
l10n_util::GetStringUTF16(IDS_PAYMENTS_NAME_FIELD_IN_CONTACT_DETAILS),
EditorField::LengthHint::HINT_SHORT, /*required=*/true);
}
if (spec()->request_payer_phone()) {
fields.emplace_back(
autofill::PHONE_HOME_WHOLE_NUMBER,
l10n_util::GetStringUTF16(IDS_PAYMENTS_PHONE_FIELD_IN_CONTACT_DETAILS),
EditorField::LengthHint::HINT_SHORT, /*required=*/true,
EditorField::ControlType::TEXTFIELD_NUMBER);
}
if (spec()->request_payer_email()) {
fields.emplace_back(
autofill::EMAIL_ADDRESS,
l10n_util::GetStringUTF16(IDS_PAYMENTS_EMAIL_FIELD_IN_CONTACT_DETAILS),
EditorField::LengthHint::HINT_SHORT, /*required=*/true);
}
return fields;
}
std::u16string ContactInfoEditorViewController::GetInitialValueForType(
autofill::FieldType type) {
if (!profile_to_edit_) {
return std::u16string();
}
return GetValueForType(*profile_to_edit_, type);
}
bool ContactInfoEditorViewController::ValidateModelAndSave() {
// TODO(crbug.com/40515884): Move this method and its helpers to a base class
// shared with the Shipping Address editor.
if (!ValidateInputFields()) {
return false;
}
if (profile_to_edit_) {
PopulateProfile(profile_to_edit_);
if (!is_incognito()) {
state()->GetPersonalDataManager()->address_data_manager().UpdateProfile(
*profile_to_edit_);
}
state()->profile_comparator()->Invalidate(*profile_to_edit_);
std::move(on_edited_).Run();
on_added_.Reset();
} else {
// There are no address fields in this form, therefore we create the profile
// with an empty country.
std::unique_ptr<autofill::AutofillProfile> profile =
std::make_unique<autofill::AutofillProfile>(
autofill::i18n_model_definition::kLegacyHierarchyCountryCode);
PopulateProfile(profile.get());
if (!is_incognito()) {
state()->GetPersonalDataManager()->address_data_manager().AddProfile(
*profile);
}
std::move(on_added_).Run(*profile);
on_edited_.Reset();
}
return true;
}
std::unique_ptr<ValidationDelegate>
ContactInfoEditorViewController::CreateValidationDelegate(
const EditorField& field) {
return std::make_unique<ContactInfoValidationDelegate>(
field, state()->GetApplicationLocale(), this);
}
std::unique_ptr<ui::ComboboxModel>
ContactInfoEditorViewController::GetComboboxModelForType(
const autofill::FieldType& type) {
NOTREACHED();
}
std::u16string ContactInfoEditorViewController::GetSheetTitle() {
// TODO(crbug.com/41313365): Title should reflect the missing information, if
// applicable.
return profile_to_edit_ ? l10n_util::GetStringUTF16(
IDS_PAYMENTS_EDIT_CONTACT_DETAILS_LABEL)
: l10n_util::GetStringUTF16(
IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL);
}
base::WeakPtr<PaymentRequestSheetController>
ContactInfoEditorViewController::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void ContactInfoEditorViewController::PopulateProfile(
autofill::AutofillProfile* profile) {
for (const auto& field : text_fields()) {
profile->SetInfoWithVerificationStatus(
field.second.type, std::u16string(field.first->GetText()),
state()->GetApplicationLocale(),
autofill::VerificationStatus::kUserVerified);
}
}
bool ContactInfoEditorViewController::GetSheetId(DialogViewID* sheet_id) {
*sheet_id = DialogViewID::CONTACT_INFO_EDITOR_SHEET;
return true;
}
std::u16string ContactInfoEditorViewController::GetValueForType(
const autofill::AutofillProfile& profile,
autofill::FieldType type) {
if (type == autofill::PHONE_HOME_WHOLE_NUMBER) {
return autofill::i18n::GetFormattedPhoneNumberForDisplay(
profile, state()->GetApplicationLocale());
}
return profile.GetInfo(type, state()->GetApplicationLocale());
}
ContactInfoEditorViewController::ContactInfoValidationDelegate::
ContactInfoValidationDelegate(const EditorField& field,
const std::string& locale,
ContactInfoEditorViewController* controller)
: field_(field), controller_(controller), locale_(locale) {}
ContactInfoEditorViewController::ContactInfoValidationDelegate::
~ContactInfoValidationDelegate() = default;
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
ShouldFormat() {
return field_.type == autofill::PHONE_HOME_WHOLE_NUMBER;
}
std::u16string
ContactInfoEditorViewController::ContactInfoValidationDelegate::Format(
std::u16string_view text) {
return base::UTF8ToUTF16(autofill::i18n::FormatPhoneForDisplay(
base::UTF16ToUTF8(text),
autofill::AutofillCountry::CountryCodeForLocale(*locale_)));
}
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
IsValidTextfield(views::Textfield* textfield,
std::u16string* error_message) {
return ValidateTextfield(textfield, error_message);
}
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
TextfieldValueChanged(views::Textfield* textfield, bool was_blurred) {
if (!was_blurred) {
return true;
}
std::u16string error_message;
bool is_valid = ValidateTextfield(textfield, &error_message);
controller_->DisplayErrorMessageForField(field_.type, error_message);
return is_valid;
}
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
ValidateTextfield(views::Textfield* textfield,
std::u16string* error_message) {
if (!controller_->spec()) {
return false;
}
// Show errors from merchant's retry() call.
autofill::AutofillProfile* invalid_contact_profile =
controller_->state()->invalid_contact_profile();
if (invalid_contact_profile && error_message &&
textfield->GetText() ==
controller_->GetValueForType(*invalid_contact_profile, field_.type)) {
*error_message = controller_->spec()->GetPayerError(field_.type);
if (!error_message->empty()) {
return false;
}
}
bool is_valid = true;
if (textfield->GetText().empty()) {
is_valid = false;
if (error_message) {
*error_message = l10n_util::GetStringUTF16(
IDS_PREF_EDIT_DIALOG_FIELD_REQUIRED_VALIDATION_MESSAGE);
}
} else {
switch (field_.type) {
case autofill::PHONE_HOME_WHOLE_NUMBER: {
const std::string default_region_code =
autofill::AutofillCountry::CountryCodeForLocale(*locale_);
if (!autofill::IsPossiblePhoneNumber(textfield->GetText(),
default_region_code)) {
is_valid = false;
if (error_message) {
*error_message = l10n_util::GetStringUTF16(
IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE);
}
}
break;
}
case autofill::EMAIL_ADDRESS:
if (!autofill::IsValidEmailAddress(textfield->GetText())) {
is_valid = false;
if (error_message) {
*error_message = l10n_util::GetStringUTF16(
IDS_PAYMENTS_EMAIL_INVALID_VALIDATION_MESSAGE);
}
}
break;
case autofill::NAME_FULL:
// We have already determined that name is nonempty, which is the only
// requirement.
break;
default:
NOTREACHED();
}
}
return is_valid;
}
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
IsValidCombobox(ValidatingCombobox* combobox,
std::u16string* error_message) {
// This UI doesn't contain any comboboxes.
NOTREACHED();
}
bool ContactInfoEditorViewController::ContactInfoValidationDelegate::
ComboboxValueChanged(ValidatingCombobox* combobox) {
// This UI doesn't contain any comboboxes.
NOTREACHED();
}
} // namespace payments
|