File: soda_installer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (338 lines) | stat: -rw-r--r-- 11,859 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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.

#include "components/soda/soda_installer.h"

#include <optional>
#include <string>

#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/observer_list.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "components/live_caption/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/soda/constants.h"
#include "components/soda/pref_names.h"
#include "media/base/media_switches.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace {

constexpr int kSodaCleanUpDelayInDays = 30;
const constexpr char* const kDefaultEnabledLanguages[] = {
    "en-US", "fr-FR",       "it-IT",       "de-DE", "es-ES", "ja-JP",
    "hi-IN", "pt-BR",       "ko-KR",       "pl-PL", "th-TH", "tr-TR",
    "id-ID", "cmn-Hans-CN", "cmn-Hant-TW", "vi-VN", "ru-RU"};

}  // namespace

namespace speech {

SodaInstaller* g_instance = nullptr;

// static
SodaInstaller* SodaInstaller::GetInstance() {
#if BUILDFLAG(IS_CHROMEOS)
  DCHECK(
      base::FeatureList::IsEnabled(ash::features::kOnDeviceSpeechRecognition));
#endif  // BUILDFLAG(IS_CHROMEOS)
  return g_instance;
}

SodaInstaller::SodaInstaller() {
  DCHECK_EQ(nullptr, g_instance);
  g_instance = this;
}

SodaInstaller::~SodaInstaller() {
  DCHECK_EQ(this, g_instance);
  g_instance = nullptr;
}

// static
void SodaInstaller::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
  registry->RegisterTimePref(prefs::kSodaScheduledDeletionTime, base::Time());
  SodaInstaller::RegisterRegisteredLanguagePackPref(registry);

#if !BUILDFLAG(IS_CHROMEOS)
  // Handle non-Chrome-OS logic here. We need to keep the implementation of this
  // method in the parent class to avoid duplicate declaration build errors
  // (specifically on Windows).
  registry->RegisterFilePathPref(prefs::kSodaBinaryPath, base::FilePath());

  // Register language pack config path preferences.
  for (const SodaLanguagePackComponentConfig& config :
       kLanguageComponentConfigs) {
    registry->RegisterFilePathPref(config.config_path_pref, base::FilePath());
  }
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

void SodaInstaller::Init(PrefService* profile_prefs,
                         PrefService* global_prefs) {
#if BUILDFLAG(IS_CHROMEOS)
  if (!base::FeatureList::IsEnabled(
          ash::features::kOnDeviceSpeechRecognition) ||
      soda_installer_initialized_) {
#else  // !BUILDFLAG(IS_CHROMEOS)
  if (soda_installer_initialized_) {
#endif
    return;
  }

  base::Time deletion_time =
      global_prefs->GetTime(prefs::kSodaScheduledDeletionTime);
  // Register SODA if a feature is actively using SODA or used it recently. The
  // deletion time will be set if a feature using SODA was disabled.
  if (IsAnyFeatureUsingSodaEnabled(profile_prefs) ||
      (!deletion_time.is_null() && deletion_time > base::Time::Now())) {
    soda_installer_initialized_ = true;
    SodaInstaller::GetInstance()->InstallSoda(global_prefs);
    InitLanguages(profile_prefs, global_prefs);
  } else {
    if (!deletion_time.is_null() && deletion_time <= base::Time::Now()) {
      UninstallSoda(global_prefs);
      soda_installer_initialized_ = false;
    }
  }
}

void SodaInstaller::InitLanguages(PrefService* profile_prefs,
                                  PrefService* global_prefs) {
  if (global_prefs->GetList(prefs::kSodaRegisteredLanguagePacks).empty()) {
    RegisterLanguage(prefs::GetLiveCaptionLanguageCode(profile_prefs),
                     global_prefs);
  }

  for (const auto& language :
       global_prefs->GetList(prefs::kSodaRegisteredLanguagePacks)) {
    SodaInstaller::GetInstance()->InstallLanguage(language.GetString(),
                                                  global_prefs);
  }
}

void SodaInstaller::SetUninstallTimer(PrefService* profile_prefs,
                                      PrefService* global_prefs) {
  // Do not schedule uninstallation if any SODA client features are still
  // enabled.
  if (IsAnyFeatureUsingSodaEnabled(profile_prefs))
    return;

  // Schedule deletion.
  global_prefs->SetTime(
      prefs::kSodaScheduledDeletionTime,
      base::Time::Now() + base::Days(kSodaCleanUpDelayInDays));
}

std::string SodaInstaller::GetLanguageDlcNameForLocale(
    const std::string& locale) const {
  return std::string();
}

bool SodaInstaller::IsSodaInstalled(LanguageCode language_code) const {
  return (soda_binary_installed_ && IsLanguageInstalled(language_code));
}

bool SodaInstaller::IsSodaBinaryInstalled() const {
  return soda_binary_installed_;
}

const std::set<LanguageCode> SodaInstaller::InstalledLanguages() const {
  return installed_languages_;
}

bool SodaInstaller::IsLanguageInstalled(LanguageCode language_code) const {
  return base::Contains(installed_languages_, language_code);
}

void SodaInstaller::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void SodaInstaller::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void SodaInstaller::NotifySodaInstalledForTesting(LanguageCode language_code) {
  // TODO: Call the actual functions in SodaInstallerImpl and
  // SodaInstallerImpleChromeOS that do this logic
  // (e.g. SodaInstallerImpl::OnSodaBinaryInstalled) rather than faking it.

  // If language code is none, this signifies that the SODA binary installed.
  if (language_code == LanguageCode::kNone) {
    soda_binary_installed_ = true;
    is_soda_downloading_ = false;
    for (LanguageCode installed_language : installed_languages_) {
      NotifyOnSodaInstalled(installed_language);
    }
    return;
  }

  // Otherwise, this means a language pack installed.
  installed_languages_.insert(language_code);
  if (base::Contains(language_pack_progress_, language_code))
    language_pack_progress_.erase(language_code);
  if (soda_binary_installed_)
    NotifyOnSodaInstalled(language_code);
}

void SodaInstaller::NotifySodaErrorForTesting(LanguageCode language_code,
                                              ErrorCode error_code) {
  // TODO: Call the actual functions in SodaInstallerImpl and
  // SodaInstallerImpleChromeOS that do this logic rather than faking it.
  if (language_code == LanguageCode::kNone) {
    // Error with the SODA binary download.
    soda_binary_installed_ = false;
    is_soda_downloading_ = false;
    language_pack_progress_.clear();
  } else {
    // Error with the language pack download.
    if (base::Contains(language_pack_progress_, language_code))
      language_pack_progress_.erase(language_code);
  }
  NotifyOnSodaInstallError(language_code, error_code);
}

void SodaInstaller::UninstallSodaForTesting() {
  soda_binary_installed_ = false;
  is_soda_downloading_ = false;
  soda_installer_initialized_ = false;
  installed_languages_.clear();
  language_pack_progress_.clear();
}

void SodaInstaller::NotifySodaProgressForTesting(int progress,
                                                 LanguageCode language_code) {
  // TODO: Call the actual functions in SodaInstallerImpl and
  // SodaInstallerImpleChromeOS that do this logic rather than faking it.
  if (language_code == LanguageCode::kNone) {
    // SODA binary download progress.
    soda_binary_installed_ = false;
    is_soda_downloading_ = true;
  } else {
    // Language pack download progress.
    if (base::Contains(language_pack_progress_, language_code))
      language_pack_progress_.insert({language_code, progress});
    else
      language_pack_progress_[language_code] = progress;
  }
  NotifyOnSodaProgress(language_code, progress);
}

bool SodaInstaller::IsAnyLanguagePackInstalledForTesting() const {
  return !installed_languages_.empty();
}

void SodaInstaller::RegisterRegisteredLanguagePackPref(
    PrefRegistrySimple* registry) {
  // TODO: Default to one of the user's languages.
  base::Value::List default_languages;
  default_languages.Append(base::Value(kUsEnglishLocale));
  registry->RegisterListPref(prefs::kSodaRegisteredLanguagePacks,
                             std::move(default_languages));
}

void SodaInstaller::NotifyOnSodaInstalled(LanguageCode language_code) {
  error_codes_.erase(language_code);
  for (Observer& observer : observers_)
    observer.OnSodaInstalled(language_code);
}

void SodaInstaller::NotifyOnSodaInstallError(LanguageCode language_code,
                                             ErrorCode error_code) {
  error_codes_[language_code] = error_code;
  for (Observer& observer : observers_)
    observer.OnSodaInstallError(language_code, error_code);
}

void SodaInstaller::NotifyOnSodaProgress(LanguageCode language_code,
                                         int progress) {
  for (Observer& observer : observers_)
    observer.OnSodaProgress(language_code, progress);
}

void SodaInstaller::RegisterLanguage(const std::string& language,
                                     PrefService* global_prefs) {
  ScopedListPrefUpdate update(global_prefs,
                              prefs::kSodaRegisteredLanguagePacks);
  if (!base::Contains(*update, base::Value(language))) {
    update->Append(language);
  }
}

void SodaInstaller::UnregisterLanguage(const std::string& language,
                                       PrefService* global_prefs) {
  ScopedListPrefUpdate update(global_prefs,
                              prefs::kSodaRegisteredLanguagePacks);
  if (base::Contains(*update, base::Value(language))) {
    update->EraseValue(base::Value(language));
  }
}

void SodaInstaller::UnregisterLanguages(PrefService* global_prefs) {
  ScopedListPrefUpdate update(global_prefs,
                              prefs::kSodaRegisteredLanguagePacks);
  update->clear();
}

bool SodaInstaller::IsLanguageEnabled(const std::string& language) {
  return base::Contains(GetLiveCaptionEnabledLanguages(), language);
}

bool SodaInstaller::IsSodaLanguageDownloading(
    LanguageCode language_code) const {
  return (is_soda_downloading_ && IsLanguageInstalled(language_code)) ||
         base::Contains(language_pack_progress_, language_code);
}

bool SodaInstaller::IsSodaDownloading(LanguageCode language_code) const {
  return is_soda_downloading_ ||
         base::Contains(language_pack_progress_, language_code);
}

std::optional<SodaInstaller::ErrorCode> SodaInstaller::GetSodaInstallErrorCode(
    LanguageCode language_code) const {
  if (IsSodaDownloading(language_code))
    return std::nullopt;

  const auto error_code = error_codes_.find(language_code);
  if (error_code != error_codes_.end())
    return error_code->second;
  return std::nullopt;
}

bool SodaInstaller::IsAnyFeatureUsingSodaEnabled(PrefService* prefs) {
#if BUILDFLAG(IS_CHROMEOS)
  return prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
         prefs->GetBoolean(ash::prefs::kAccessibilityDictationEnabled) ||
         prefs->GetBoolean(ash::prefs::kProjectorCreationFlowEnabled) ||
         prefs->GetString(
             ash::prefs::kClassManagementToolsAvailabilitySetting) == "teacher";
#else  // !BUILDFLAG(IS_CHROMEOS)
  return prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
         prefs->GetBoolean(prefs::kHeadlessCaptionEnabled);
#endif
}

std::vector<std::string> SodaInstaller::GetLiveCaptionEnabledLanguages() const {
  std::vector<std::string> enabled_languages = base::SplitString(
      base::GetFieldTrialParamValueByFeature(
          media::kLiveCaptionExperimentalLanguages, "available_languages"),
      ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

  for (const char* const enabled_language : kDefaultEnabledLanguages) {
    enabled_languages.push_back(enabled_language);
  }

  return enabled_languages;
}

}  // namespace speech