soda_installer_impl_chromeos.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/soda_installer_impl_chromeos.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/containers/contains.h"
  8. #include "base/feature_list.h"
  9. #include "base/metrics/histogram_functions.h"
  10. #include "base/numerics/safe_conversions.h"
  11. #include "chromeos/dbus/dlcservice/dlcservice.pb.h"
  12. #include "chromeos/dbus/dlcservice/dlcservice_client.h"
  13. #include "components/live_caption/pref_names.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "components/soda/pref_names.h"
  16. #include "components/soda/soda_installer.h"
  17. #include "media/base/media_switches.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. namespace speech {
  20. namespace {
  21. constexpr char kSodaDlcName[] = "libsoda";
  22. constexpr char kSodaEnglishUsDlcName[] = "libsoda-model-en-us";
  23. SodaInstaller::ErrorCode DlcCodeToSodaErrorCode(const std::string& code) {
  24. return (code == dlcservice::kErrorNeedReboot)
  25. ? SodaInstaller::ErrorCode::kNeedsReboot
  26. : SodaInstaller::ErrorCode::kUnspecifiedError;
  27. }
  28. } // namespace
  29. SodaInstallerImplChromeOS::SodaInstallerImplChromeOS() = default;
  30. SodaInstallerImplChromeOS::~SodaInstallerImplChromeOS() = default;
  31. base::FilePath SodaInstallerImplChromeOS::GetSodaBinaryPath() const {
  32. return soda_lib_path_;
  33. }
  34. base::FilePath SodaInstallerImplChromeOS::GetLanguagePath(
  35. const std::string& language) const {
  36. // TODO(crbug.com/1161569): Support multiple languages. Currently only en-US
  37. // is supported.
  38. DCHECK_EQ(language, kUsEnglishLocale);
  39. return language_path_;
  40. }
  41. void SodaInstallerImplChromeOS::InstallSoda(PrefService* global_prefs) {
  42. if (never_download_soda_for_testing_)
  43. return;
  44. // Clear cached path in case this is a reinstallation (path could
  45. // change).
  46. SetSodaBinaryPath(base::FilePath());
  47. soda_binary_installed_ = false;
  48. is_soda_downloading_ = true;
  49. soda_progress_ = 0.0;
  50. // Install SODA DLC.
  51. dlcservice::InstallRequest install_request;
  52. install_request.set_id(kSodaDlcName);
  53. chromeos::DlcserviceClient::Get()->Install(
  54. install_request,
  55. base::BindOnce(&SodaInstallerImplChromeOS::OnSodaInstalled,
  56. base::Unretained(this), base::Time::Now()),
  57. base::BindRepeating(&SodaInstallerImplChromeOS::OnSodaProgress,
  58. base::Unretained(this)));
  59. }
  60. void SodaInstallerImplChromeOS::InstallLanguage(const std::string& language,
  61. PrefService* global_prefs) {
  62. if (never_download_soda_for_testing_)
  63. return;
  64. // TODO(crbug.com/1161569): SODA is only available for en-US right now.
  65. DCHECK_EQ(language, kUsEnglishLocale);
  66. SodaInstaller::RegisterLanguage(language, global_prefs);
  67. // Clear cached path in case this is a reinstallation (path could
  68. // change).
  69. SetLanguagePath(base::FilePath());
  70. // TODO(crbug.com/1055150): Compare user's language to a list of
  71. // supported languages and map it to a DLC ID. For now, default to
  72. // installing the SODA English-US DLC.
  73. DCHECK_EQ(language, kUsEnglishLocale);
  74. language_pack_progress_.insert({LanguageCode::kEnUs, 0.0});
  75. dlcservice::InstallRequest install_request;
  76. install_request.set_id(kSodaEnglishUsDlcName);
  77. chromeos::DlcserviceClient::Get()->Install(
  78. install_request,
  79. base::BindOnce(&SodaInstallerImplChromeOS::OnLanguageInstalled,
  80. base::Unretained(this), LanguageCode::kEnUs,
  81. base::Time::Now()),
  82. base::BindRepeating(&SodaInstallerImplChromeOS::OnLanguageProgress,
  83. base::Unretained(this)));
  84. }
  85. std::vector<std::string> SodaInstallerImplChromeOS::GetAvailableLanguages()
  86. const {
  87. // TODO(crbug.com/1161569): SODA is only available for English right now.
  88. // Update this to check available languages.
  89. return {kUsEnglishLocale};
  90. }
  91. void SodaInstallerImplChromeOS::UninstallSoda(PrefService* global_prefs) {
  92. soda_binary_installed_ = false;
  93. SetSodaBinaryPath(base::FilePath());
  94. chromeos::DlcserviceClient::Get()->Uninstall(
  95. kSodaDlcName, base::BindOnce(&SodaInstallerImplChromeOS::OnDlcUninstalled,
  96. base::Unretained(this), kSodaDlcName));
  97. installed_languages_.clear();
  98. language_pack_progress_.clear();
  99. SodaInstaller::UnregisterLanguages(global_prefs);
  100. SetLanguagePath(base::FilePath());
  101. chromeos::DlcserviceClient::Get()->Uninstall(
  102. kSodaEnglishUsDlcName,
  103. base::BindOnce(&SodaInstallerImplChromeOS::OnDlcUninstalled,
  104. base::Unretained(this), kSodaEnglishUsDlcName));
  105. global_prefs->SetTime(prefs::kSodaScheduledDeletionTime, base::Time());
  106. }
  107. void SodaInstallerImplChromeOS::SetSodaBinaryPath(base::FilePath new_path) {
  108. soda_lib_path_ = new_path;
  109. }
  110. void SodaInstallerImplChromeOS::SetLanguagePath(base::FilePath new_path) {
  111. language_path_ = new_path;
  112. }
  113. void SodaInstallerImplChromeOS::OnSodaInstalled(
  114. const base::Time start_time,
  115. const chromeos::DlcserviceClient::InstallResult& install_result) {
  116. is_soda_downloading_ = false;
  117. if (install_result.error == dlcservice::kErrorNone) {
  118. soda_binary_installed_ = true;
  119. SetSodaBinaryPath(base::FilePath(install_result.root_path));
  120. // TODO(crbug.com/1161569): SODA is only available for English right now.
  121. // Update this to notify on all installed languages.
  122. if (IsLanguageInstalled(LanguageCode::kEnUs))
  123. NotifyOnSodaInstalled(LanguageCode::kEnUs);
  124. base::UmaHistogramTimes(kSodaBinaryInstallationSuccessTimeTaken,
  125. base::Time::Now() - start_time);
  126. } else {
  127. soda_binary_installed_ = false;
  128. soda_progress_ = 0.0;
  129. NotifyOnSodaInstallError(LanguageCode::kNone,
  130. DlcCodeToSodaErrorCode(install_result.error));
  131. base::UmaHistogramTimes(kSodaBinaryInstallationFailureTimeTaken,
  132. base::Time::Now() - start_time);
  133. }
  134. base::UmaHistogramBoolean(kSodaBinaryInstallationResult,
  135. install_result.error == dlcservice::kErrorNone);
  136. }
  137. void SodaInstallerImplChromeOS::OnLanguageInstalled(
  138. const LanguageCode language_code,
  139. const base::Time start_time,
  140. const chromeos::DlcserviceClient::InstallResult& install_result) {
  141. language_pack_progress_.erase(language_code);
  142. if (install_result.error == dlcservice::kErrorNone) {
  143. installed_languages_.insert(language_code);
  144. SetLanguagePath(base::FilePath(install_result.root_path));
  145. if (soda_binary_installed_) {
  146. NotifyOnSodaInstalled(language_code);
  147. }
  148. base::UmaHistogramTimes(
  149. GetInstallationSuccessTimeMetricForLanguagePack(language_code),
  150. base::Time::Now() - start_time);
  151. } else {
  152. // TODO: Notify the observer of the specific language pack that failed
  153. // to install. ChromeOS currently only supports the en-US language pack.
  154. NotifyOnSodaInstallError(language_code,
  155. DlcCodeToSodaErrorCode(install_result.error));
  156. base::UmaHistogramTimes(
  157. GetInstallationFailureTimeMetricForLanguagePack(language_code),
  158. base::Time::Now() - start_time);
  159. }
  160. base::UmaHistogramBoolean(
  161. GetInstallationResultMetricForLanguagePack(language_code),
  162. install_result.error == dlcservice::kErrorNone);
  163. }
  164. void SodaInstallerImplChromeOS::OnSodaProgress(double progress) {
  165. soda_progress_ = progress;
  166. OnSodaCombinedProgress();
  167. }
  168. void SodaInstallerImplChromeOS::OnLanguageProgress(double progress) {
  169. language_pack_progress_[LanguageCode::kEnUs] = progress;
  170. OnSodaCombinedProgress();
  171. }
  172. void SodaInstallerImplChromeOS::OnSodaCombinedProgress() {
  173. // TODO(crbug.com/1055150): Consider updating this implementation.
  174. // e.g.: (1) starting progress from 0% if we are downloading language
  175. // only (2) weighting download progress proportionally to DLC binary size.
  176. double language_progress = 0.0;
  177. if (base::Contains(language_pack_progress_, LanguageCode::kEnUs))
  178. language_progress = language_pack_progress_[LanguageCode::kEnUs];
  179. const double progress = (soda_progress_ + language_progress) / 2;
  180. // TODO: Notify the observer of the specific language pack that is currently
  181. // being installed. ChromeOS currently only supports the en-US language pack.
  182. NotifyOnSodaProgress(LanguageCode::kEnUs, base::ClampFloor(100 * progress));
  183. }
  184. void SodaInstallerImplChromeOS::OnDlcUninstalled(const std::string& dlc_id,
  185. const std::string& err) {
  186. if (err != dlcservice::kErrorNone) {
  187. LOG(ERROR) << "Failed to uninstall DLC " << dlc_id << ". Error: " << err;
  188. }
  189. }
  190. } // namespace speech