File: wasm_tts_engine_component_installer.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (281 lines) | stat: -rw-r--r-- 11,805 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/component_updater/wasm_tts_engine_component_installer.h"

#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "chrome/browser/ui/webui/side_panel/read_anything/read_anything_prefs.h"
#include "components/prefs/pref_registry_simple.h"
#include "content/public/browser/browser_thread.h"

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#include "chrome/browser/accessibility/embedded_a11y_extension_loader.h"
#include "chrome/common/extensions/extension_constants.h"
#include "ui/accessibility/accessibility_features.h"
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

namespace {

const base::FilePath::CharType kManifestFileName[] =
    FILE_PATH_LITERAL("wasm_tts_manifest.json");
const base::FilePath::CharType kBindingsMainWasmFileName[] =
    FILE_PATH_LITERAL("bindings_main.wasm");
const base::FilePath::CharType kBindingsMainJsFileName[] =
    FILE_PATH_LITERAL("bindings_main.js");
const base::FilePath::CharType kTTSEngineJsBinFileName[] =
    FILE_PATH_LITERAL("googletts_engine_js_bin.js");
const base::FilePath::CharType kWorkletProcessorJsFileName[] =
    FILE_PATH_LITERAL("streaming_worklet_processor.js");
const base::FilePath::CharType kVoicesJsonFileName[] =
    FILE_PATH_LITERAL("voices.json");
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
const base::FilePath::CharType kManifestV3FileName[] =
    FILE_PATH_LITERAL("wasm_tts_manifest_v3.json");
const base::FilePath::CharType kOffscreenHtmlFileName[] =
    FILE_PATH_LITERAL("offscreen.html");
const base::FilePath::CharType kOffscreenCompiledFileName[] =
    FILE_PATH_LITERAL("offscreen_compiled.js");
const base::FilePath::CharType kBackgroundCompiledFileName[] =
    FILE_PATH_LITERAL("background_compiled.js");
#endif

// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
// The extension id is: bjbcblmdcnggnibecjikpoljcgkbgphl
constexpr std::array<uint8_t, 32> kWasmTtsEnginePublicKeySHA256 = {
    0x19, 0x12, 0x1b, 0xc3, 0x2d, 0x66, 0xd8, 0x14, 0x29, 0x8a, 0xfe,
    0xb9, 0x26, 0xa1, 0x6f, 0x7b, 0xc2, 0x14, 0x17, 0xf1, 0xb0, 0x1e,
    0x56, 0x89, 0xcb, 0x53, 0x8e, 0x13, 0x92, 0xc1, 0x44, 0x5d};

const char kWasmTtsEngineManifestName[] = "WASM TTS Engine";

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
class WasmTTSEngineDirectory {
 public:
  static WasmTTSEngineDirectory* Get() {
    static base::NoDestructor<WasmTTSEngineDirectory> wasm_directory;
    return wasm_directory.get();
  }

  void Get(base::OnceCallback<void(const base::FilePath&)> callback) {
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    callbacks_.push_back(std::move(callback));
    if (!dir_.empty()) {
      FireCallbacks();
    }
  }

  void Set(const base::FilePath& new_dir) {
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    CHECK(!new_dir.empty());
    dir_ = new_dir;
    FireCallbacks();
  }

 private:
  void FireCallbacks() {
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    CHECK(!dir_.empty());
    std::vector<base::OnceCallback<void(const base::FilePath&)>> callbacks;
    std::swap(callbacks, callbacks_);
    for (base::OnceCallback<void(const base::FilePath&)>& callback :
         callbacks) {
      base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(std::move(callback), dir_));
    }
  }

  base::FilePath dir_;
  std::vector<base::OnceCallback<void(const base::FilePath&)>> callbacks_;
};
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

}  // namespace

namespace component_updater {

WasmTtsEngineComponentInstallerPolicy::WasmTtsEngineComponentInstallerPolicy(
    PrefService* pref_service)
    : pref_service_(pref_service) {}

// static
void WasmTtsEngineComponentInstallerPolicy::RegisterPrefs(
    PrefRegistrySimple* registry) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  registry->RegisterTimePref(prefs::kAccessibilityReadAnythingDateLastOpened,
                             base::Time());
  registry->RegisterBooleanPref(
      prefs::kAccessibilityReadAnythingTTSEngineReinstalled, false);
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}

bool WasmTtsEngineComponentInstallerPolicy::
    SupportsGroupPolicyEnabledComponentUpdates() const {
  return true;
}

bool WasmTtsEngineComponentInstallerPolicy::RequiresNetworkEncryption() const {
  return false;
}

update_client::CrxInstaller::Result
WasmTtsEngineComponentInstallerPolicy::OnCustomInstall(
    const base::Value::Dict& /* manifest */,
    const base::FilePath& /* install_dir */) {
  return update_client::CrxInstaller::Result(0);  // Nothing custom here.
}

void WasmTtsEngineComponentInstallerPolicy::OnCustomUninstall() {}

void WasmTtsEngineComponentInstallerPolicy::ComponentReady(
    const base::Version& version,
    const base::FilePath& install_dir,
    base::Value::Dict /* manifest */) {
  VLOG(1) << "Component ready, version " << version.GetString() << " in "
          << install_dir.value();

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  if (!features::IsWasmTtsEngineAutoInstallDisabled()) {
    // Instead of installing the component extension as soon as it is ready,
    // store the install directory, so that the install can be triggered
    // via ReadAnythingService once the side panel has been opened. This
    // prevents the extension from being installed unnecessarily for those
    // who aren't using reading mode.
    // An exception is made for reinstalls at THRESHOLD_RECENT and
    // THRESHOLD_LONGER amounts of time since reading mode was last opened. At
    // these points, the WASM TTS Engine is reinstalled in order to clean up
    // any removed voices.
    WasmTTSEngineDirectory* wasm_directory = WasmTTSEngineDirectory::Get();
    wasm_directory->Set(install_dir);

    MaybeReinstallTtsEngine(install_dir);
  }
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}

// In order to uninstall unused voices when reading mode has been unused for an
// extended period of time, Chrome needs to reinstall the TTS engine. This will
// be removed the next time Chrome is restarted.
void WasmTtsEngineComponentInstallerPolicy::MaybeReinstallTtsEngine(
    const base::FilePath& install_dir) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  const base::Time current_time = base::Time::Now();
  const base::Time date_last_opened =
      pref_service_->GetTime(prefs::kAccessibilityReadAnythingDateLastOpened);
  const bool previously_reinstalled = pref_service_->GetBoolean(
      prefs::kAccessibilityReadAnythingTTSEngineReinstalled);

  // Reading mode hasn't been opened in the last 90 days, so don't reinstall
  // the engine.
  if (date_last_opened.is_null()) {
    return;
  }

  // Reading mode was opened more than 14 days ago and less than 90 days ago
  // but the engine was already installed after 14 days to remove unused voices.
  // Reading mode doesn't need to re-install the engine to clean up voices
  // until 90 days have passed.
  if (previously_reinstalled &&
      (current_time - date_last_opened) < kThresholdLonger) {
    return;
  }

  // Reading mode was opened in the last 14 days, so don't reinstall the engine.
  if ((current_time - date_last_opened) < kThresholdRecent) {
    return;
  }

  const base::FilePath::CharType* manifest_file =
      features::IsWasmTtsComponentUpdaterV3Enabled() ? kManifestV3FileName
                                                     : kManifestFileName;

  // If it's been more than 14 days since reading mode was last opened,
  // re-install the engine so that unused voices can be removed.
  EmbeddedA11yExtensionLoader::GetInstance()->Init();
  EmbeddedA11yExtensionLoader::GetInstance()->InstallExtensionWithIdAndPath(
      extension_misc::kComponentUpdaterTTSEngineExtensionId, install_dir,
      manifest_file,
      /*should_localize=*/false);

  // If reading mode hasn't been opened in longer than 14 days but less than
  // 90 days, update that the TTS engine has been reinstalled to prevent
  // subsequent reinstalls from day 14 to day 90.
  if (!previously_reinstalled) {
    pref_service_->SetBoolean(
        prefs::kAccessibilityReadAnythingTTSEngineReinstalled, true);
  }

  // If it's been more than 90 days, clear both preferences for reading mode
  // last opened state. The engine will now not be reinstalled until reading
  // mode is reopened.
  if ((current_time - date_last_opened) >= kThresholdLonger) {
    pref_service_->ClearPref(prefs::kAccessibilityReadAnythingDateLastOpened);
    pref_service_->ClearPref(
        prefs::kAccessibilityReadAnythingTTSEngineReinstalled);
  }
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}

// Called during startup and installation before ComponentReady().
bool WasmTtsEngineComponentInstallerPolicy::VerifyInstallation(
    const base::Value::Dict& /* manifest */,
    const base::FilePath& install_dir) const {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  if (features::IsWasmTtsComponentUpdaterV3Enabled()) {
    return base::PathExists(install_dir.Append(kManifestV3FileName)) &&
           base::PathExists(install_dir.Append(kBindingsMainWasmFileName)) &&
           base::PathExists(install_dir.Append(kBindingsMainJsFileName)) &&
           base::PathExists(install_dir.Append(kOffscreenHtmlFileName)) &&
           base::PathExists(install_dir.Append(kOffscreenCompiledFileName)) &&
           base::PathExists(install_dir.Append(kBackgroundCompiledFileName)) &&
           base::PathExists(install_dir.Append(kWorkletProcessorJsFileName)) &&
           base::PathExists(install_dir.Append(kVoicesJsonFileName));
  }
#endif
  return base::PathExists(install_dir.Append(kManifestFileName)) &&
         base::PathExists(install_dir.Append(kBindingsMainWasmFileName)) &&
         base::PathExists(install_dir.Append(kBindingsMainJsFileName)) &&
         base::PathExists(install_dir.Append(kTTSEngineJsBinFileName)) &&
         base::PathExists(install_dir.Append(kWorkletProcessorJsFileName)) &&
         base::PathExists(install_dir.Append(kVoicesJsonFileName));
}

base::FilePath WasmTtsEngineComponentInstallerPolicy::GetRelativeInstallDir()
    const {
  return base::FilePath(FILE_PATH_LITERAL("WasmTtsEngine"));
}

void WasmTtsEngineComponentInstallerPolicy::GetHash(
    std::vector<uint8_t>* hash) const {
  hash->assign(std::begin(kWasmTtsEnginePublicKeySHA256),
               std::end(kWasmTtsEnginePublicKeySHA256));
}

std::string WasmTtsEngineComponentInstallerPolicy::GetName() const {
  return kWasmTtsEngineManifestName;
}

update_client::InstallerAttributes
WasmTtsEngineComponentInstallerPolicy::GetInstallerAttributes() const {
  return update_client::InstallerAttributes();
}

void RegisterWasmTtsEngineComponent(ComponentUpdateService* cus,
                                    PrefService* prefs) {
  VLOG(1) << "Registering WASM TTS Engine component.";
  auto installer = base::MakeRefCounted<ComponentInstaller>(
      std::make_unique<WasmTtsEngineComponentInstallerPolicy>(prefs));
  installer->Register(cus, base::OnceClosure());
}

void WasmTtsEngineComponentInstallerPolicy::GetWasmTTSEngineDirectory(
    base::OnceCallback<void(const base::FilePath&)> callback) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  WasmTTSEngineDirectory* wasm_directory = WasmTTSEngineDirectory::Get();
  wasm_directory->Get(std::move(callback));
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}

}  // namespace component_updater