chromium/components/soda/constants.h

// Copyright 2020 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_SODA_CONSTANTS_H_
#define COMPONENTS_SODA_CONSTANTS_H_

#include <cstdint>
#include <optional>
#include <string>

#include "base/files/file_path.h"
#include "components/soda/pref_names.h"
#include "components/strings/grit/components_strings.h"

namespace speech {

extern const char kUsEnglishLocale[];
extern const char kEnglishLocaleNoCountry[];

// Metrics names for keeping track of SODA installation.
extern const char kSodaBinaryInstallationResult[];
extern const char kSodaBinaryInstallationSuccessTimeTaken[];
extern const char kSodaBinaryInstallationFailureTimeTaken[];

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(LanguageCode)
enum class LanguageCode {};
// LINT.ThenChange(/tools/metrics/histograms/metadata/accessibility/enums.xml:SodaLanguageCode)

// Describes all metadata needed to dynamically install SODA language pack
// components.
struct SodaLanguagePackComponentConfig {};

constexpr SodaLanguagePackComponentConfig kLanguageComponentConfigs[] =;

// Location of the libsoda binary within the SODA installation directory.
extern const base::FilePath::CharType kSodaBinaryRelativePath[];

// Name of the of the libsoda binary used in browser tests.
extern const base::FilePath::CharType kSodaTestBinaryRelativePath[];

// Location of the SODA component relative to the components directory.
extern const base::FilePath::CharType kSodaInstallationRelativePath[];

// Location of the SODA language packs relative to the components
// directory.
extern const base::FilePath::CharType kSodaLanguagePacksRelativePath[];

// Location of the SODA files used in browser tests.
extern const base::FilePath::CharType kSodaTestResourcesRelativePath[];

// Location of the SODA models directory relative to the language pack
// installation directory.
extern const base::FilePath::CharType kSodaLanguagePackDirectoryRelativePath[];

// Get the absolute path of the SODA component directory.
const base::FilePath GetSodaDirectory();

// Get the absolute path of the SODA directory containing the language packs.
const base::FilePath GetSodaLanguagePacksDirectory();

// Get the absolute path of the SODA directory containing the language packs
// used in browser tests.
const base::FilePath GetSodaTestResourcesDirectory();

// Get the absolute path of the latest SODA language pack for a given language
// (e.g. en-US).
const base::FilePath GetLatestSodaLanguagePackDirectory(
    const std::string& language);

// Get the directory containing the latest version of SODA. In most cases
// there will only be one version of SODA, but it is possible for there to be
// multiple versions if a newer version of SODA was recently downloaded before
// the old version gets cleaned up. Returns an empty path if SODA is not
// installed.
const base::FilePath GetLatestSodaDirectory();

// Get the path to the SODA binary. Returns an empty path if SODA is not
// installed.
const base::FilePath GetSodaBinaryPath();

// Get the path to the SODA binary used in browser tests. Returns an empty path
// if SODA is not installed.
const base::FilePath GetSodaTestBinaryPath();

std::optional<SodaLanguagePackComponentConfig> GetLanguageComponentConfig(
    LanguageCode language_code);

std::optional<SodaLanguagePackComponentConfig> GetLanguageComponentConfig(
    const std::string& language_name);

// Get the language component config matching a given language subtag. For
// example, the "fr-CA" language name will return the language component config
// for "fr-FR".
std::optional<SodaLanguagePackComponentConfig>
GetLanguageComponentConfigMatchingLanguageSubtag(
    const std::string& language_name);

LanguageCode GetLanguageCodeByComponentId(const std::string& component_id);

std::string GetLanguageName(LanguageCode language_code);

LanguageCode GetLanguageCode(const std::string& language_name);

const std::u16string GetLanguageDisplayName(const std::string& language_name,
                                            const std::string& display_locale);

// Returns the `SodaInstaller.Language.{language}.InstallationSuccessTime` uma
// metric string for the language code.
const std::string GetInstallationSuccessTimeMetricForLanguagePack(
    const LanguageCode& language_code);
const std::string GetInstallationSuccessTimeMetricForLanguage(
    const std::string& language);

// Returns the `SodaInstaller.Language.{language}.InstallationFailureTime` uma
// metric string for the language code.
const std::string GetInstallationFailureTimeMetricForLanguagePack(
    const LanguageCode& language_code);
const std::string GetInstallationFailureTimeMetricForLanguage(
    const std::string& language);

// Returns the `SodaInstaller.Language.{language}.InstallationResult` uma
// metric string for the language code..
const std::string GetInstallationResultMetricForLanguagePack(
    const LanguageCode& language_code);
const std::string GetInstallationResultMetricForLanguage(
    const std::string& language);

}  // namespace speech

#endif  // COMPONENTS_SODA_CONSTANTS_H_