constants.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/soda/constants.h"
  5. #include <string>
  6. #include "base/files/file_enumerator.h"
  7. #include "base/files/file_path.h"
  8. #include "base/notreached.h"
  9. #include "base/path_service.h"
  10. #include "base/strings/strcat.h"
  11. #include "build/build_config.h"
  12. #include "components/component_updater/component_updater_paths.h"
  13. #include "components/crx_file/id_util.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace speech {
  16. const char kUsEnglishLocale[] = "en-US";
  17. const char kSodaBinaryInstallationResult[] =
  18. "SodaInstaller.BinaryInstallationResult";
  19. const char kSodaBinaryInstallationSuccessTimeTaken[] =
  20. "SodaInstaller.BinaryInstallationSuccessTime";
  21. const char kSodaBinaryInstallationFailureTimeTaken[] =
  22. "SodaInstaller.BinaryInstallationFailureTime";
  23. #if BUILDFLAG(IS_WIN)
  24. constexpr base::FilePath::CharType kSodaBinaryRelativePath[] =
  25. FILE_PATH_LITERAL("SODAFiles/SODA.dll");
  26. #else
  27. constexpr base::FilePath::CharType kSodaBinaryRelativePath[] =
  28. FILE_PATH_LITERAL("SODAFiles/libsoda.so");
  29. #endif
  30. constexpr base::FilePath::CharType kSodaTestBinaryRelativePath[] =
  31. FILE_PATH_LITERAL("libsoda.so");
  32. constexpr base::FilePath::CharType kSodaTestResourcesRelativePath[] =
  33. FILE_PATH_LITERAL("third_party/soda/resources/");
  34. constexpr base::FilePath::CharType kSodaInstallationRelativePath[] =
  35. FILE_PATH_LITERAL("SODA");
  36. constexpr base::FilePath::CharType kSodaLanguagePacksRelativePath[] =
  37. FILE_PATH_LITERAL("SODALanguagePacks");
  38. constexpr base::FilePath::CharType kSodaLanguagePackDirectoryRelativePath[] =
  39. FILE_PATH_LITERAL("SODAModels");
  40. const base::FilePath GetSodaDirectory() {
  41. base::FilePath components_dir;
  42. base::PathService::Get(component_updater::DIR_COMPONENT_USER,
  43. &components_dir);
  44. return components_dir.empty()
  45. ? base::FilePath()
  46. : components_dir.Append(kSodaInstallationRelativePath);
  47. }
  48. const base::FilePath GetSodaLanguagePacksDirectory() {
  49. base::FilePath components_dir;
  50. base::PathService::Get(component_updater::DIR_COMPONENT_USER,
  51. &components_dir);
  52. return components_dir.empty()
  53. ? base::FilePath()
  54. : components_dir.Append(kSodaLanguagePacksRelativePath);
  55. }
  56. const base::FilePath GetSodaTestResourcesDirectory() {
  57. base::FilePath test_data_root;
  58. base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &test_data_root);
  59. DCHECK(!test_data_root.empty());
  60. return test_data_root.empty()
  61. ? base::FilePath()
  62. : test_data_root.Append(kSodaTestResourcesRelativePath);
  63. }
  64. const base::FilePath GetLatestSodaLanguagePackDirectory(
  65. const std::string& language) {
  66. base::FileEnumerator enumerator(
  67. GetSodaLanguagePacksDirectory().AppendASCII(language), false,
  68. base::FileEnumerator::DIRECTORIES);
  69. // Use the lexographical order of the directory names to determine the latest
  70. // version. This mirrors the logic in the component updater.
  71. base::FilePath latest_version_dir;
  72. for (base::FilePath version_dir = enumerator.Next(); !version_dir.empty();
  73. version_dir = enumerator.Next()) {
  74. latest_version_dir =
  75. latest_version_dir < version_dir ? version_dir : latest_version_dir;
  76. }
  77. return latest_version_dir.Append(kSodaLanguagePackDirectoryRelativePath);
  78. }
  79. const base::FilePath GetLatestSodaDirectory() {
  80. base::FileEnumerator enumerator(GetSodaDirectory(), false,
  81. base::FileEnumerator::DIRECTORIES);
  82. base::FilePath latest_version_dir;
  83. for (base::FilePath version_dir = enumerator.Next(); !version_dir.empty();
  84. version_dir = enumerator.Next()) {
  85. latest_version_dir =
  86. latest_version_dir < version_dir ? version_dir : latest_version_dir;
  87. }
  88. return latest_version_dir;
  89. }
  90. const base::FilePath GetSodaBinaryPath() {
  91. base::FilePath soda_dir = GetLatestSodaDirectory();
  92. return soda_dir.empty() ? base::FilePath()
  93. : soda_dir.Append(kSodaBinaryRelativePath);
  94. }
  95. const base::FilePath GetSodaTestBinaryPath() {
  96. base::FilePath test_dir = GetSodaTestResourcesDirectory();
  97. return test_dir.empty() ? base::FilePath()
  98. : test_dir.Append(kSodaTestBinaryRelativePath);
  99. }
  100. absl::optional<SodaLanguagePackComponentConfig> GetLanguageComponentConfig(
  101. LanguageCode language_code) {
  102. for (const SodaLanguagePackComponentConfig& config :
  103. kLanguageComponentConfigs) {
  104. if (config.language_code == language_code) {
  105. return config;
  106. }
  107. }
  108. return absl::nullopt;
  109. }
  110. absl::optional<SodaLanguagePackComponentConfig> GetLanguageComponentConfig(
  111. const std::string& language_name) {
  112. for (const SodaLanguagePackComponentConfig& config :
  113. kLanguageComponentConfigs) {
  114. if (config.language_name == language_name) {
  115. return config;
  116. }
  117. }
  118. return absl::nullopt;
  119. }
  120. LanguageCode GetLanguageCodeByComponentId(const std::string& component_id) {
  121. for (const SodaLanguagePackComponentConfig& config :
  122. kLanguageComponentConfigs) {
  123. if (crx_file::id_util::GenerateIdFromHash(config.public_key_sha,
  124. sizeof(config.public_key_sha)) ==
  125. component_id) {
  126. return config.language_code;
  127. }
  128. }
  129. return LanguageCode::kNone;
  130. }
  131. std::string GetLanguageName(LanguageCode language_code) {
  132. std::string language_name;
  133. if (language_code != LanguageCode::kNone) {
  134. absl::optional<SodaLanguagePackComponentConfig> language_config =
  135. GetLanguageComponentConfig(language_code);
  136. if (language_config.has_value()) {
  137. language_name = language_config.value().language_name;
  138. }
  139. }
  140. return language_name;
  141. }
  142. LanguageCode GetLanguageCode(const std::string& language_name) {
  143. absl::optional<SodaLanguagePackComponentConfig> language_config =
  144. GetLanguageComponentConfig(language_name);
  145. if (language_config.has_value()) {
  146. return language_config.value().language_code;
  147. }
  148. return LanguageCode::kNone;
  149. }
  150. int GetLanguageDisplayName(const std::string& language_name) {
  151. absl::optional<SodaLanguagePackComponentConfig> language_config =
  152. GetLanguageComponentConfig(language_name);
  153. if (language_config.has_value()) {
  154. return language_config.value().display_name;
  155. }
  156. return 0;
  157. }
  158. const std::string GetInstallationSuccessTimeMetricForLanguagePack(
  159. const LanguageCode& language_code) {
  160. auto config = GetLanguageComponentConfig(language_code);
  161. DCHECK(config && config->language_name);
  162. return base::StrCat({"SodaInstaller.Language.", config->language_name,
  163. ".InstallationSuccessTime"});
  164. }
  165. const std::string GetInstallationFailureTimeMetricForLanguagePack(
  166. const LanguageCode& language_code) {
  167. auto config = GetLanguageComponentConfig(language_code);
  168. DCHECK(config && config->language_name);
  169. return base::StrCat({"SodaInstaller.Language.", config->language_name,
  170. ".InstallationFailureTime"});
  171. }
  172. const std::string GetInstallationResultMetricForLanguagePack(
  173. const LanguageCode& language_code) {
  174. auto config = GetLanguageComponentConfig(language_code);
  175. DCHECK(config && config->language_name);
  176. return base::StrCat({"SodaInstaller.Language.", config->language_name,
  177. ".InstallationResult"});
  178. }
  179. } // namespace speech