File: soda_language_pack_component_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 (179 lines) | stat: -rw-r--r-- 6,223 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/component_updater/soda_language_pack_component_installer.h"

#include <iterator>
#include <memory>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "base/version.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/soda_component_installer.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/prefs/pref_service.h"
#include "components/soda/constants.h"
#include "components/update_client/update_client_errors.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

namespace component_updater {

namespace {

constexpr char kLanguagePackManifestName[] = "SODA %s Models";

}  // namespace

SodaLanguagePackComponentInstallerPolicy::
    SodaLanguagePackComponentInstallerPolicy(
        speech::SodaLanguagePackComponentConfig language_config,
        OnSodaLanguagePackComponentReadyCallback on_ready_callback)
    : language_config_(language_config),
      on_ready_callback_(std::move(on_ready_callback)) {}

SodaLanguagePackComponentInstallerPolicy::
    ~SodaLanguagePackComponentInstallerPolicy() = default;

std::string SodaLanguagePackComponentInstallerPolicy::GetExtensionId(
    speech::LanguageCode language_code) {
  std::optional<speech::SodaLanguagePackComponentConfig> config =
      speech::GetLanguageComponentConfig(language_code);

  if (config) {
    return crx_file::id_util::GenerateIdFromHash(config.value().public_key_sha);
  }

  return std::string();
}

base::flat_set<std::string>
SodaLanguagePackComponentInstallerPolicy::GetExtensionIds() {
  base::flat_set<std::string> ids;
  for (const speech::SodaLanguagePackComponentConfig& config :
       speech::kLanguageComponentConfigs) {
    ids.insert(crx_file::id_util::GenerateIdFromHash(config.public_key_sha));
  }

  return ids;
}

void SodaLanguagePackComponentInstallerPolicy::
    UpdateSodaLanguagePackComponentOnDemand(
        speech::LanguageCode language_code) {
  const std::string crx_id =
      SodaLanguagePackComponentInstallerPolicy::GetExtensionId(language_code);
  g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
      crx_id, OnDemandUpdater::Priority::FOREGROUND,
      base::BindOnce([](update_client::Error error) {
        if (error != update_client::Error::NONE &&
            error != update_client::Error::UPDATE_IN_PROGRESS) {
          LOG(ERROR)
              << "On demand update of the SODA language component failed "
                 "with error: "
              << static_cast<int>(error);
        }
      }));
}

bool SodaLanguagePackComponentInstallerPolicy::VerifyInstallation(
    const base::Value::Dict& manifest,
    const base::FilePath& install_dir) const {
  return base::PathExists(
      install_dir.Append(speech::kSodaLanguagePackDirectoryRelativePath));
}

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

bool SodaLanguagePackComponentInstallerPolicy::RequiresNetworkEncryption()
    const {
  return true;
}

update_client::CrxInstaller::Result
SodaLanguagePackComponentInstallerPolicy::OnCustomInstall(
    const base::Value::Dict& manifest,
    const base::FilePath& install_dir) {
  return SodaComponentInstallerPolicy::SetComponentDirectoryPermission(
      install_dir);
}

void SodaLanguagePackComponentInstallerPolicy::OnCustomUninstall() {}

void SodaLanguagePackComponentInstallerPolicy::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_ANDROID)
  // The component updater may post ready tasks during shutdown. Confirm that the
  // `BrowserProcess` is still valid before setting the pref.
  if (g_browser_process) {
    g_browser_process->local_state()->SetFilePath(
        language_config_.config_path_pref,
        install_dir.Append(speech::kSodaLanguagePackDirectoryRelativePath));
  }
#endif  //! BUILDFLAG(IS_ANDROID)

  if (on_ready_callback_) {
    std::move(on_ready_callback_).Run(language_config_.language_code);
  }
}

base::FilePath SodaLanguagePackComponentInstallerPolicy::GetRelativeInstallDir()
    const {
  return base::FilePath(speech::kSodaLanguagePacksRelativePath)
      .AppendASCII(language_config_.language_name);
}

void SodaLanguagePackComponentInstallerPolicy::GetHash(
    std::vector<uint8_t>* hash) const {
  hash->assign(std::begin(language_config_.public_key_sha),
               std::end(language_config_.public_key_sha));
}

std::string SodaLanguagePackComponentInstallerPolicy::GetName() const {
  return base::StringPrintf(kLanguagePackManifestName,
                            language_config_.language_name);
}

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

void RegisterSodaLanguagePackComponent(
    speech::SodaLanguagePackComponentConfig language_config,
    ComponentUpdateService* cus,
    OnSodaLanguagePackComponentReadyCallback on_ready_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  auto installer = base::MakeRefCounted<ComponentInstaller>(
      std::make_unique<SodaLanguagePackComponentInstallerPolicy>(
          language_config, std::move(on_ready_callback)));

  installer->Register(
      cus, base::BindOnce(&SodaLanguagePackComponentInstallerPolicy::
                              UpdateSodaLanguagePackComponentOnDemand,
                          language_config.language_code));
}

}  // namespace component_updater