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
|
// 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.
#ifndef CHROME_BROWSER_UI_AUTOFILL_ADDRESS_EDITOR_CONTROLLER_H_
#define CHROME_BROWSER_UI_AUTOFILL_ADDRESS_EDITOR_CONTROLLER_H_
#include <memory>
#include <string_view>
#include <vector>
#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/ui/country_combobox_model.h"
namespace autofill {
class CountryComboboxModel;
class PersonalDataManager;
// Field definition for an editor field, used to build the UI.
struct EditorField {
enum class LengthHint : int { HINT_LONG, HINT_SHORT };
enum class ControlType : int { TEXTFIELD, TEXTFIELD_NUMBER, COMBOBOX };
EditorField(FieldType type,
std::u16string label,
LengthHint length_hint,
bool is_required,
ControlType control_type = ControlType::TEXTFIELD)
: type(type),
label(std::move(label)),
length_hint(length_hint),
is_required(is_required),
control_type(control_type) {}
// Data type in the field.
FieldType type;
// Label to be shown alongside the field.
std::u16string label;
// Hint about the length of this field's contents.
LengthHint length_hint;
// Whether this field should be validated against the "is required" rule.
bool is_required;
// The control type.
ControlType control_type;
};
class AddressEditorController {
public:
using OnIsValidChangeCallbackList = base::RepeatingCallbackList<void(bool)>;
AddressEditorController(const AutofillProfile& profile_to_edit,
PersonalDataManager* pdm,
bool is_validatable);
~AddressEditorController();
const std::vector<EditorField>& editor_fields() { return editor_fields_; }
bool is_validatable() const { return is_validatable_; }
// Returns `std::nullopt` if the form was not validated (`ValidateAllFields()`
// was not triggered on the view and it didn't call `SetIsValid()` on its
// turn) yet, its validity state is unknown. Otherwise returns the state
// according to the validation performed in the view.
// Note that the view doesn't perform validation if the `is_validatable()` is
// `false`, in which case this always returns `std::nullopt`.
std::optional<bool> is_valid() const { return is_valid_; }
void SetIsValid(bool is_valid);
CountryComboboxModel& GetCountryComboboxModel();
// Updates `editor_fields_` based on the current selected country.
void UpdateEditorFields(const std::string& country_code);
void SetProfileInfo(FieldType type, const std::u16string& value);
std::u16string GetProfileInfo(FieldType type);
const AutofillProfile& GetAddressProfile();
[[nodiscard]] base::CallbackListSubscription AddIsValidChangedCallback(
OnIsValidChangeCallbackList::CallbackType callback);
bool IsValid(const EditorField& field, std::u16string_view value);
private:
AutofillProfile profile_to_edit_;
const raw_ref<PersonalDataManager> pdm_;
CountryComboboxModel countries_;
// The language code to be format this address, reset every time the current
// country changes.
std::string language_code_;
// The browser locale, used to compute the values of profile fields. (e.g. the
// locale in which to return the country name).
const std::string locale_;
std::vector<EditorField> editor_fields_;
// Whether the editor should perform validation.
const bool is_validatable_ = false;
std::optional<bool> is_valid_;
// List of external callbacks subscribed to validity updates.
OnIsValidChangeCallbackList on_is_valid_change_callbacks_;
};
} // namespace autofill
#endif // CHROME_BROWSER_UI_AUTOFILL_ADDRESS_EDITOR_CONTROLLER_H_
|