chromium/components/autofill/core/browser/form_parsing/regex_patterns.h

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

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_REGEX_PATTERNS_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_REGEX_PATTERNS_H_

#include <optional>
#include <string_view>

#include "base/containers/span.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_parsing/autofill_parsing_utils.h"
#include "components/autofill/core/browser/form_parsing/buildflags.h"
#include "components/autofill/core/common/language_code.h"

namespace autofill {

// A MatchPatternRef dereferences to a MatchingPattern.
class MatchPatternRef {};

// The different sets of patterns available for parsing.
// Each enum constant corresponds to a JSON file.
// When adding a new value, it's require to add it to
// `HeuristicSource` as well.
enum class PatternSource : uint8_t {};

// The active pattern and the available patterns depend on the build config and
// the Finch config. If the active `HeuristicSource` is not a `PatternSource`,
// then a nullopt is returned.
std::optional<PatternSource> GetActivePatternSource();

// Looks up the patterns for the given name and language.
// The name is typically a field type.
//
// If there are no patterns for the given name and language, falls back to the
// union of all patterns for the given type (across all languages).
//
// Hits a CHECK if there are no patterns for the given name at all.
//
// The returned patterns are sorted by their MatchingPattern::positive_score in
// decreasing order.
base::span<const MatchPatternRef> GetMatchPatterns(
    std::string_view name,
    std::optional<LanguageCode> language_code,
    PatternSource pattern_source);

base::span<const MatchPatternRef> GetMatchPatterns(
    FieldType type,
    std::optional<LanguageCode> language_code,
    PatternSource pattern_source);

// Returns true iff there at least one pattern for some PatternSource and
// pattern name.
bool IsSupportedLanguageCode(LanguageCode language_code);

// Checks if all the matching patterns for the given PatternSources and
// language are the same - meaning that computing predictions for both is
// unnecessary, since it will yield the same result.
bool AreMatchingPatternsEqual(PatternSource a,
                              PatternSource b,
                              LanguageCode language_code);

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_REGEX_PATTERNS_H_