diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-09-03 13:32:17 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-10-01 14:31:55 +0200 |
commit | 21ba0c5d4bf8fba15dddd97cd693bad2358b77fd (patch) | |
tree | 91be119f694044dfc1ff9fdc054459e925de9df0 /chromium/components/soda/soda_installer_impl_chromeos.cc | |
parent | 03c549e0392f92c02536d3f86d5e1d8dfa3435ac (diff) | |
download | qtwebengine-chromium-21ba0c5d4bf8fba15dddd97cd693bad2358b77fd.tar.gz |
BASELINE: Update Chromium to 92.0.4515.166
Change-Id: I42a050486714e9e54fc271f2a8939223a02ae364
Diffstat (limited to 'chromium/components/soda/soda_installer_impl_chromeos.cc')
-rw-r--r-- | chromium/components/soda/soda_installer_impl_chromeos.cc | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/chromium/components/soda/soda_installer_impl_chromeos.cc b/chromium/components/soda/soda_installer_impl_chromeos.cc new file mode 100644 index 00000000000..d24c22f43f0 --- /dev/null +++ b/chromium/components/soda/soda_installer_impl_chromeos.cc @@ -0,0 +1,199 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/soda/soda_installer_impl_chromeos.h" + +#include "base/bind.h" +#include "base/feature_list.h" +#include "base/no_destructor.h" +#include "chromeos/dbus/dlcservice/dlcservice_client.h" +#include "components/live_caption/pref_names.h" +#include "components/prefs/pref_service.h" +#include "components/soda/pref_names.h" +#include "media/base/media_switches.h" +#include "ui/base/l10n/l10n_util.h" + +namespace { + +constexpr char kSodaDlcName[] = "libsoda"; +constexpr char kSodaEnglishUsDlcName[] = "libsoda-model-en-us"; + +} // namespace + +namespace speech { + +SodaInstaller* SodaInstaller::GetInstance() { + static base::NoDestructor<SodaInstallerImplChromeOS> instance; + return instance.get(); +} + +// static +void SodaInstaller::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { + registry->RegisterTimePref(prefs::kSodaScheduledDeletionTime, base::Time()); + SodaInstaller::RegisterRegisteredLanguagePackPref(registry); +} + +SodaInstallerImplChromeOS::SodaInstallerImplChromeOS() = default; + +SodaInstallerImplChromeOS::~SodaInstallerImplChromeOS() = default; + +base::FilePath SodaInstallerImplChromeOS::GetSodaBinaryPath() const { + return soda_lib_path_; +} + +base::FilePath SodaInstallerImplChromeOS::GetLanguagePath() const { + return language_path_; +} + +void SodaInstallerImplChromeOS::InstallSoda(PrefService* global_prefs) { + if (!base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) + return; + + // Clear cached path in case this is a reinstallation (path could + // change). + SetSodaBinaryPath(base::FilePath()); + + soda_binary_installed_ = false; + is_soda_downloading_ = true; + soda_progress_ = 0.0; + + // Install SODA DLC. + chromeos::DlcserviceClient::Get()->Install( + kSodaDlcName, + base::BindOnce(&SodaInstallerImplChromeOS::OnSodaInstalled, + base::Unretained(this)), + base::BindRepeating(&SodaInstallerImplChromeOS::OnSodaProgress, + base::Unretained(this))); +} + +void SodaInstallerImplChromeOS::InstallLanguage(const std::string& language, + PrefService* global_prefs) { + if (!base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) + return; + + SodaInstaller::RegisterLanguage(language, global_prefs); + // Clear cached path in case this is a reinstallation (path could + // change). + SetLanguagePath(base::FilePath()); + + // TODO(crbug.com/1055150): Compare user's language to a list of + // supported languages and map it to a DLC ID. For now, default to + // installing the SODA English-US DLC. + DCHECK_EQ(language, "en-US"); + + language_installed_ = false; + is_language_downloading_ = true; + language_progress_ = 0.0; + + chromeos::DlcserviceClient::Get()->Install( + kSodaEnglishUsDlcName, + base::BindOnce(&SodaInstallerImplChromeOS::OnLanguageInstalled, + base::Unretained(this)), + base::BindRepeating(&SodaInstallerImplChromeOS::OnLanguageProgress, + base::Unretained(this))); +} + +bool SodaInstallerImplChromeOS::IsSodaInstalled() const { + DCHECK(base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)); + return (soda_binary_installed_ && language_installed_) || + soda_installed_for_test_; +} + +bool SodaInstallerImplChromeOS::IsLanguageInstalled( + const std::string& locale_or_language) const { + // TODO(crbug.com/1161569): SODA is only available for English right now. + // Update this to check installation of language pack when available. + return (l10n_util::GetLanguage(locale_or_language) == "en" && + language_installed_) || + soda_installed_for_test_; +} + +void SodaInstallerImplChromeOS::UninstallSoda(PrefService* global_prefs) { + soda_binary_installed_ = false; + SetSodaBinaryPath(base::FilePath()); + chromeos::DlcserviceClient::Get()->Uninstall( + kSodaDlcName, base::BindOnce(&SodaInstallerImplChromeOS::OnDlcUninstalled, + base::Unretained(this), kSodaDlcName)); + language_installed_ = false; + SodaInstaller::UnregisterLanguages(global_prefs); + SetLanguagePath(base::FilePath()); + chromeos::DlcserviceClient::Get()->Uninstall( + kSodaEnglishUsDlcName, + base::BindOnce(&SodaInstallerImplChromeOS::OnDlcUninstalled, + base::Unretained(this), kSodaEnglishUsDlcName)); + global_prefs->SetTime(prefs::kSodaScheduledDeletionTime, base::Time()); +} + +void SodaInstallerImplChromeOS::SetSodaBinaryPath(base::FilePath new_path) { + soda_lib_path_ = new_path; +} + +void SodaInstallerImplChromeOS::SetLanguagePath(base::FilePath new_path) { + language_path_ = new_path; +} + +void SodaInstallerImplChromeOS::OnSodaInstalled( + const chromeos::DlcserviceClient::InstallResult& install_result) { + if (install_result.error == dlcservice::kErrorNone) { + soda_binary_installed_ = true; + SetSodaBinaryPath(base::FilePath(install_result.root_path)); + if (language_installed_) { + NotifyOnSodaInstalled(); + } + } else { + soda_binary_installed_ = false; + soda_progress_ = 0.0; + NotifyOnSodaError(); + } + is_soda_downloading_ = false; +} + +void SodaInstallerImplChromeOS::OnLanguageInstalled( + const chromeos::DlcserviceClient::InstallResult& install_result) { + if (install_result.error == dlcservice::kErrorNone) { + language_installed_ = true; + SetLanguagePath(base::FilePath(install_result.root_path)); + if (soda_binary_installed_) { + NotifyOnSodaInstalled(); + } + } else { + language_installed_ = false; + language_progress_ = 0.0; + + // TODO: Notify the observer of the specific language pack that failed to + // install. ChromeOS currently only supports the en-US language pack. + NotifyOnSodaLanguagePackError(speech::LanguageCode::kEnUs); + } + is_language_downloading_ = false; +} + +void SodaInstallerImplChromeOS::OnSodaProgress(double progress) { + soda_progress_ = progress; + OnSodaCombinedProgress(); +} + +void SodaInstallerImplChromeOS::OnLanguageProgress(double progress) { + language_progress_ = progress; + + // TODO: Notify the observer of the specific language pack that is currently + // being installed. ChromeOS currently only supports the en-US language pack. + NotifyOnSodaLanguagePackProgress(progress, speech::LanguageCode::kEnUs); +} + +void SodaInstallerImplChromeOS::OnSodaCombinedProgress() { + // TODO(crbug.com/1055150): Consider updating this implementation. + // e.g.: (1) starting progress from 0% if we are downloading language + // only (2) weighting download progress proportionally to DLC binary size. + const double progress = (soda_progress_ + language_progress_) / 2; + NotifyOnSodaProgress(int{100 * progress}); +} + +void SodaInstallerImplChromeOS::OnDlcUninstalled(const std::string& dlc_id, + const std::string& err) { + if (err != dlcservice::kErrorNone) { + LOG(ERROR) << "Failed to uninstall DLC " << dlc_id << ". Error: " << err; + } +} + +} // namespace speech |