chromium/components/autofill/core/browser/field_filling_address_util.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/field_filling_address_util.h"

#include <optional>

#include "base/ranges/algorithm.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/address_normalizer.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/data_model_utils.h"
#include "components/autofill/core/browser/field_type_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/geo/country_names.h"
#include "components/autofill/core/browser/geo/state_names.h"
#include "components/autofill/core/browser/select_control_util.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_util.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_formatter.h"
#include "third_party/re2/src/re2/re2.h"

namespace autofill {

namespace {

// Helper method to normalize the `admin_area` for the given `country_code`.
// The value in `admin_area` will be overwritten.
bool NormalizeAdminAreaForCountryCode(std::u16string& admin_area,
                                      const std::string& country_code,
                                      AddressNormalizer& address_normalizer) {}

// Returns the SelectOption::value of `field_options` that best matches the
// normalized `value`. Returns an empty string if no match is found.
// Normalization is relative to the `country_code` and to `address_normalizer`.
std::u16string GetNormalizedStateSelectControlValue(
    std::u16string value,
    base::span<const SelectOption> field_options,
    const std::string& country_code,
    AddressNormalizer& address_normalizer,
    std::string* failure_to_fill) {}

// Gets the state value to fill in a select control.
// Returns an empty string if no value for filling was found.
std::u16string GetStateSelectControlValue(
    const std::u16string& value,
    base::span<const SelectOption> field_options,
    const std::string& country_code,
    AddressNormalizer* address_normalizer,
    std::string* failure_to_fill) {}

// Gets the country value to fill in a select control.
// Returns an empty string if no value for filling was found.
std::u16string GetCountrySelectControlValue(
    const std::u16string& value,
    base::span<const SelectOption> field_options,
    std::string* failure_to_fill = nullptr) {}

// Returns appropriate street address for `field`. Translates newlines into
// equivalent separators when necessary, i.e. when filling a single-line field.
// The separators depend on `address_language_code`.
std::u16string GetStreetAddressForInput(
    const std::u16string& address_value,
    const std::string& address_language_code,
    FormControlType form_control_type) {}

// Returns appropriate state value that matches `field`.
// The canonical state is checked if it fits in the field and at last the
// abbreviations are tried. Does not return a state if neither |state_value| nor
// the canonical state name nor its abbreviation fit into the field.
std::u16string GetStateTextForInput(const std::u16string& state_value,
                                    const std::string& country_code,
                                    uint64_t field_max_length,
                                    std::string* failure_to_fill) {}

// Finds the best suitable option in the `field` that corresponds to the
// `phone_country_code`. The strategy is:
// - If a <select> menu has an option whose value or content exactly matches the
//   phone country code (e.g. "1" for US), this is picked (e.g. <option
//   value="1">+1</option>).
// - As a fallback, the first option with text containing a phone country code
//   (ignoring leading '00' or '+') (e.g. <option value="US">USA (+1)</option>)
//   is picked The fallback broken in case there is ambiguity (e.g. there is a
//   second option
//   <option value="CA">Canada (+1)</option>)
//
// If kAutofillEnableFillingPhoneCountryCodesByAddressCountryCodes is enabled,
// Autofill will
// - pick an option whose value or content MATCHES EXACTLY the phone country
//   code (e.g. "1" for US) (old behavior as above), else
// - pick an option whose value or content CONTAINS with prefix (e.g. "+1" or
//   "001" for US) the phone country code if it's unambiguous, else
// - pick an option whose value or content MATCHES the address country code or
//   country name (after removing a phone country code like "+1") if that's
//   possible.
//   - If the options contain phone country codes ("+1", "001"), then this path
//     is only chosen if the chosen option from the parent bullet CONTAINS the
//     desired phone country code.
//   else
// - pick the FIRST option whose value or content CONTAINS the phone country
//   code with prefix (old behavior).
// TODO(crbug.com/40249216) Clean up the comment above when the feature is
// launched.
std::u16string GetPhoneCountryCodeSelectControlValue(
    const std::u16string& phone_country_code,
    base::span<const SelectOption> field_options,
    const std::string& country_code,
    std::string* failure_to_fill) {}

// Returns the appropriate `profile` value based on `field_type` to fill
// into the input `field`.
std::u16string GetValueForProfileForInput(const AutofillProfile& profile,
                                          const std::string& app_locale,
                                          const AutofillType& field_type,
                                          const FormFieldData& field_data,
                                          std::string* failure_to_fill) {}

std::u16string GetValueForProfileSelectControl(
    const AutofillProfile& profile,
    const std::u16string& value,
    const std::string& app_locale,
    base::span<const SelectOption> field_options,
    FieldType field_type,
    AddressNormalizer* address_normalizer,
    std::string* failure_to_fill) {}

}  // namespace

std::pair<std::u16string, FieldType> GetFillingValueAndTypeForProfile(
    const AutofillProfile& profile,
    const std::string& app_locale,
    const AutofillType& field_type,
    const FormFieldData& field_data,
    AddressNormalizer* address_normalizer,
    std::string* failure_to_fill) {}

std::u16string GetPhoneNumberValueForInput(
    uint64_t field_max_length,
    const std::u16string& number,
    const std::u16string& city_and_number) {}

}  // namespace autofill