Commit 854ee3e8 authored by evliu's avatar evliu Committed by Commit Bot

Clean up unused SODA components

This CL deletes the SODA components on browser start up if the Live
Caption feature was disabled for at least 30 days.

Expected edge cases:
 - The duration could be longer than 30 days if the browser is
   restarted infrequently.
 - Other profiles may have the feature still enabled at the time of
   deletion. The SODA components should still be deleted if those
   profiles have not registered the SODA component recently.

Bug: 1143753
Change-Id: Id95d9fb7cb822b7c0664833291bf2bd5cfcc1a8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2515209
Commit-Queue: Evan Liu <evliu@google.com>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824225}
parent da3e467e
...@@ -38,6 +38,8 @@ const char* const kCaptionStylePrefsToObserve[] = { ...@@ -38,6 +38,8 @@ const char* const kCaptionStylePrefsToObserve[] = {
prefs::kAccessibilityCaptionsTextShadow, prefs::kAccessibilityCaptionsTextShadow,
prefs::kAccessibilityCaptionsBackgroundOpacity}; prefs::kAccessibilityCaptionsBackgroundOpacity};
constexpr int kSodaCleanUpDelayInDays = 30;
} // namespace } // namespace
namespace captions { namespace captions {
...@@ -113,14 +115,20 @@ bool CaptionController::IsLiveCaptionEnabled() { ...@@ -113,14 +115,20 @@ bool CaptionController::IsLiveCaptionEnabled() {
void CaptionController::UpdateSpeechRecognitionServiceEnabled() { void CaptionController::UpdateSpeechRecognitionServiceEnabled() {
if (enabled_) { if (enabled_) {
g_browser_process->local_state()->SetTime(prefs::kSodaScheduledDeletionTime,
base::Time());
// Register SODA component and download speech model. // Register SODA component and download speech model.
component_updater::RegisterSODAComponent( component_updater::RegisterSodaComponent(
g_browser_process->component_updater(), profile_->GetPrefs(), g_browser_process->component_updater(), profile_->GetPrefs(),
g_browser_process->local_state(),
base::BindOnce(&component_updater::SODAComponentInstallerPolicy:: base::BindOnce(&component_updater::SODAComponentInstallerPolicy::
UpdateSODAComponentOnDemand)); UpdateSODAComponentOnDemand));
} else { } else {
// Do nothing. The SODA component will be uninstalled and removed from the // Schedule SODA to be deleted in 30 days if the feature is not enabled
// device on the next start up. // before then.
g_browser_process->local_state()->SetTime(
prefs::kSodaScheduledDeletionTime,
base::Time::Now() + base::TimeDelta::FromDays(kSodaCleanUpDelayInDays));
} }
} }
......
...@@ -12,14 +12,23 @@ ...@@ -12,14 +12,23 @@
#include "chrome/browser/component_updater/supervised_user_whitelist_installer.h" #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
#endif #endif
#if !defined(OS_ANDROID)
#include "chrome/browser/component_updater/soda_component_installer.h"
#endif
namespace component_updater { namespace component_updater {
void RegisterPrefs(PrefRegistrySimple* registry) { void RegisterPrefs(PrefRegistrySimple* registry) {
RegisterPrefsForChromeComponentUpdaterConfigurator(registry); RegisterPrefsForChromeComponentUpdaterConfigurator(registry);
RegisterPrefsForRecoveryComponent(registry); RegisterPrefsForRecoveryComponent(registry);
#if BUILDFLAG(ENABLE_SUPERVISED_USERS) #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
SupervisedUserWhitelistInstaller::RegisterPrefs(registry); SupervisedUserWhitelistInstaller::RegisterPrefs(registry);
#endif #endif
#if !defined(OS_ANDROID)
RegisterPrefsForSodaComponent(registry);
#endif
} }
} // namespace component_updater } // namespace component_updater
...@@ -182,7 +182,8 @@ void RegisterComponentsForUpdate(bool is_off_the_record_profile, ...@@ -182,7 +182,8 @@ void RegisterComponentsForUpdate(bool is_off_the_record_profile,
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
base::UmaHistogramBoolean("Accessibility.LiveCaption.FeatureEnabled", base::UmaHistogramBoolean("Accessibility.LiveCaption.FeatureEnabled",
base::FeatureList::IsEnabled(media::kLiveCaption)); base::FeatureList::IsEnabled(media::kLiveCaption));
component_updater::RegisterSODAComponent(cus, profile_prefs, component_updater::RegisterSodaComponent(cus, profile_prefs,
g_browser_process->local_state(),
base::OnceClosure()); base::OnceClosure());
component_updater::RegisterSodaLanguageComponent(cus, profile_prefs); component_updater::RegisterSodaLanguageComponent(cus, profile_prefs);
#endif #endif
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/component_updater/component_updater_service.h" #include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h" #include "components/crx_file/id_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/soda/constants.h" #include "components/soda/constants.h"
#include "components/update_client/update_client_errors.h" #include "components/update_client/update_client_errors.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
...@@ -129,53 +130,67 @@ void UpdateSODAInstallDirPref(PrefService* prefs, ...@@ -129,53 +130,67 @@ void UpdateSODAInstallDirPref(PrefService* prefs,
#endif #endif
} }
void RegisterSODAComponent(ComponentUpdateService* cus, void RegisterPrefsForSodaComponent(PrefRegistrySimple* registry) {
PrefService* prefs, registry->RegisterTimePref(prefs::kSodaScheduledDeletionTime, base::Time());
}
void RegisterSodaComponent(ComponentUpdateService* cus,
PrefService* profile_prefs,
PrefService* global_prefs,
base::OnceClosure callback) { base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) { if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption) &&
if (!prefs->GetBoolean(prefs::kLiveCaptionEnabled) || base::FeatureList::IsEnabled(media::kLiveCaption)) {
!base::FeatureList::IsEnabled(media::kLiveCaption)) { if (profile_prefs->GetBoolean(prefs::kLiveCaptionEnabled)) {
return; global_prefs->SetTime(prefs::kSodaScheduledDeletionTime, base::Time());
} auto installer = base::MakeRefCounted<ComponentInstaller>(
auto installer = base::MakeRefCounted<ComponentInstaller>( std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating(
std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating( [](ComponentUpdateService* cus, PrefService* profile_prefs,
[](ComponentUpdateService* cus, PrefService* prefs, const base::FilePath& install_dir) {
const base::FilePath& install_dir) { content::GetUIThreadTaskRunner(
content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT}) {base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE, ->PostTask(FROM_HERE,
base::BindOnce(&UpdateSODAInstallDirPref, prefs, base::BindOnce(&UpdateSODAInstallDirPref,
install_dir)); profile_prefs, install_dir));
}, },
cus, prefs))); cus, profile_prefs)));
installer->Register(cus, std::move(callback)); installer->Register(cus, std::move(callback));
} else {
auto deletion_time =
global_prefs->GetTime(prefs::kSodaScheduledDeletionTime);
if (!deletion_time.is_null() && deletion_time < base::Time::Now()) {
base::DeletePathRecursively(speech::GetSodaDirectory());
base::DeletePathRecursively(speech::GetSodaLanguagePacksDirectory());
global_prefs->SetTime(prefs::kSodaScheduledDeletionTime, base::Time());
}
}
} }
} }
void RegisterSodaLanguageComponent(ComponentUpdateService* cus, void RegisterSodaLanguageComponent(ComponentUpdateService* cus,
PrefService* prefs) { PrefService* profile_prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
// TODO(crbug.com/1143753): Clean up this component if the Live Caption // TODO(crbug.com/1143753): Clean up this component if the Live Caption
// feature hasn't been used for some time. // feature hasn't been used for some time.
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) { if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
speech::LanguageCode language = speech::GetLanguageCode( speech::LanguageCode language = speech::GetLanguageCode(
prefs->GetString(prefs::kLiveCaptionLanguageCode)); profile_prefs->GetString(prefs::kLiveCaptionLanguageCode));
switch (language) { switch (language) {
case speech::LanguageCode::kNone: case speech::LanguageCode::kNone:
// Do nothing. // Do nothing.
break; break;
case speech::LanguageCode::kEnUs: case speech::LanguageCode::kEnUs:
RegisterSodaEnUsComponent( RegisterSodaEnUsComponent(
cus, prefs, cus, profile_prefs,
base::BindOnce(&SodaEnUsComponentInstallerPolicy:: base::BindOnce(&SodaEnUsComponentInstallerPolicy::
UpdateSodaEnUsComponentOnDemand)); UpdateSodaEnUsComponentOnDemand));
break; break;
case speech::LanguageCode::kJaJp: case speech::LanguageCode::kJaJp:
RegisterSodaJaJpComponent( RegisterSodaJaJpComponent(
cus, prefs, cus, profile_prefs,
base::BindOnce(&SodaJaJpComponentInstallerPolicy:: base::BindOnce(&SodaJaJpComponentInstallerPolicy::
UpdateSodaJaJpComponentOnDemand)); UpdateSodaJaJpComponentOnDemand));
break; break;
......
...@@ -54,14 +54,19 @@ class SODAComponentInstallerPolicy : public ComponentInstallerPolicy { ...@@ -54,14 +54,19 @@ class SODAComponentInstallerPolicy : public ComponentInstallerPolicy {
OnSODAComponentReadyCallback on_component_ready_callback_; OnSODAComponentReadyCallback on_component_ready_callback_;
}; };
// Registers user preferences related to the Speech On-Device API (SODA)
// component.
void RegisterPrefsForSodaComponent(PrefRegistrySimple* registry);
// Call once during startup to make the component update service aware of // Call once during startup to make the component update service aware of
// the File Type Policies component. // the File Type Policies component.
void RegisterSODAComponent(ComponentUpdateService* cus, void RegisterSodaComponent(ComponentUpdateService* cus,
PrefService* prefs, PrefService* profile_prefs,
PrefService* global_prefs,
base::OnceClosure callback); base::OnceClosure callback);
void RegisterSodaLanguageComponent(ComponentUpdateService* cus, void RegisterSodaLanguageComponent(ComponentUpdateService* cus,
PrefService* prefs); PrefService* profile_prefs);
} // namespace component_updater } // namespace component_updater
......
...@@ -1228,6 +1228,11 @@ const char kSodaEnUsConfigPath[] = ...@@ -1228,6 +1228,11 @@ const char kSodaEnUsConfigPath[] =
// The file path of the ja-JP Speech On-Device API (SODA) configuration file. // The file path of the ja-JP Speech On-Device API (SODA) configuration file.
const char kSodaJaJpConfigPath[] = const char kSodaJaJpConfigPath[] =
"accessibility.captions.soda_ja_jp_config_path"; "accessibility.captions.soda_ja_jp_config_path";
// The scheduled time to clean up the Speech On-Device API (SODA) files from the
// device.
const char kSodaScheduledDeletionTime[] =
"accessibility.captions.soda_scheduled_deletion_time";
#endif #endif
#if defined(OS_MAC) #if defined(OS_MAC)
......
...@@ -197,6 +197,7 @@ extern const char kLiveCaptionLanguageCode[]; ...@@ -197,6 +197,7 @@ extern const char kLiveCaptionLanguageCode[];
extern const char kSodaBinaryPath[]; extern const char kSodaBinaryPath[];
extern const char kSodaEnUsConfigPath[]; extern const char kSodaEnUsConfigPath[];
extern const char kSodaJaJpConfigPath[]; extern const char kSodaJaJpConfigPath[];
extern const char kSodaScheduledDeletionTime[];
#endif #endif
#if defined(OS_MAC) #if defined(OS_MAC)
extern const char kConfirmToQuitEnabled[]; extern const char kConfirmToQuitEnabled[];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment