File: language_packs_impl.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (225 lines) | stat: -rw-r--r-- 7,700 bytes parent folder | download | duplicates (8)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/language_packs/language_packs_impl.h"

#include <optional>
#include <string>

#include "base/no_destructor.h"
#include "chromeos/ash/components/language_packs/language_pack_manager.h"
#include "chromeos/ash/components/language_packs/public/mojom/language_packs.mojom-shared.h"
#include "mojo/public/cpp/bindings/associated_remote.h"

namespace ash::language_packs {

using ::ash::language::mojom::BasePackInfo;
using ::ash::language::mojom::ErrorCode;
using ::ash::language::mojom::FeatureId;
using ::ash::language::mojom::LanguagePackInfo;
using ::ash::language::mojom::LanguagePacks;
using ::ash::language::mojom::PackState;

namespace {

std::optional<std::string> ConvertMojoFeatureToPackId(FeatureId mojo_id) {
  switch (mojo_id) {
    case FeatureId::HANDWRITING_RECOGNITION:
      return kHandwritingFeatureId;

    case FeatureId::TTS:
      return kTtsFeatureId;

    // Catch all unknown cases here.
    default:
      return std::nullopt;
  }
}

PackState GetPackStateFromStatusCode(const PackResult::StatusCode status_code) {
  switch (status_code) {
    case PackResult::StatusCode::kNotInstalled:
      return PackState::NOT_INSTALLED;
    case PackResult::StatusCode::kInProgress:
      return PackState::INSTALLING;
    case PackResult::StatusCode::kInstalled:
      return PackState::INSTALLED;
    // Catch all remaining cases as error.
    default:
      // TODO: b/294162606 - Deprecate this value and use UNKNOWN instead.
      return PackState::ERROR;
  }
}

ErrorCode GetMojoErrorFromPackError(const PackResult::ErrorCode pack_error) {
  // This conversion is exhaustive. We don't use a default: case so that we can
  // catch missing values at compile time.
  switch (pack_error) {
    case PackResult::ErrorCode::kNone:
      return ErrorCode::kNone;
    case PackResult::ErrorCode::kOther:
      return ErrorCode::kOther;
    case PackResult::ErrorCode::kWrongId:
      return ErrorCode::kWrongId;
    case PackResult::ErrorCode::kNeedReboot:
      return ErrorCode::kNeedReboot;
    case PackResult::ErrorCode::kAllocation:
      return ErrorCode::kAllocation;
  }
}

FeatureId ToFeatureId(std::string_view feature_id) {
  if (feature_id == kHandwritingFeatureId) {
    return FeatureId::HANDWRITING_RECOGNITION;
  } else if (feature_id == kTtsFeatureId) {
    return FeatureId::TTS;
  }

  return FeatureId::UNSUPPORTED_UNKNOWN;
}

// Called when GetPackState() or InstallPack() functions from Language Packs
// are complete.
void OnOperationComplete(LanguagePacksImpl::GetPackInfoCallback mojo_callback,
                         const PackResult& pack_result) {
  auto info = LanguagePackInfo::New();
  info->pack_state = GetPackStateFromStatusCode(pack_result.pack_state);
  info->error = GetMojoErrorFromPackError(pack_result.operation_error);
  info->feature_id = ToFeatureId(pack_result.feature_id);
  if (pack_result.pack_state == PackResult::StatusCode::kInstalled) {
    info->path = pack_result.path;
  }

  std::move(mojo_callback).Run(std::move(info));
}

// Called when InstallBasePack() from Language Packs is complete.
void OnInstallBasePackComplete(
    LanguagePacksImpl::InstallBasePackCallback mojo_callback,
    const PackResult& pack_result) {
  auto info = BasePackInfo::New();
  info->pack_state = GetPackStateFromStatusCode(pack_result.pack_state);
  info->error = GetMojoErrorFromPackError(pack_result.operation_error);
  if (pack_result.pack_state == PackResult::StatusCode::kInstalled) {
    info->path = pack_result.path;
  }

  std::move(mojo_callback).Run(std::move(info));
}

void OnUninstallComplete(LanguagePacksImpl::UninstallPackCallback mojo_callback,
                         const PackResult& result) {
  std::move(mojo_callback).Run();
}

}  // namespace

LanguagePacksImpl::LanguagePacksImpl() {
  auto* manager = LanguagePackManager::GetInstance();
  if (manager) {
    // Note: RemoveObserver is never called here because this class should
    // never be destroyed (see LanguagePacksImpl::GetInstance).
    manager->AddObserver(this);
  }
}
LanguagePacksImpl::~LanguagePacksImpl() = default;

LanguagePacksImpl& LanguagePacksImpl::GetInstance() {
  static base::NoDestructor<LanguagePacksImpl> impl;
  return *impl;
}

void LanguagePacksImpl::BindReceiver(
    mojo::PendingReceiver<language::mojom::LanguagePacks> receiver) {
  receivers_.Add(this, std::move(receiver));
}

void LanguagePacksImpl::GetPackInfo(FeatureId feature_id,
                                    const std::string& language,
                                    GetPackInfoCallback mojo_callback) {
  const std::optional<std::string> pack_id =
      ConvertMojoFeatureToPackId(feature_id);

  if (pack_id.has_value()) {
    LanguagePackManager::GetPackState(
        pack_id.value(), language,
        base::BindOnce(&OnOperationComplete, std::move(mojo_callback)));
  } else {
    auto info = LanguagePackInfo::New();
    info->pack_state = PackState::ERROR;
    info->feature_id = feature_id;
    std::move(mojo_callback).Run(std::move(info));
  }
}

void LanguagePacksImpl::InstallPack(FeatureId feature_id,
                                    const std::string& language,
                                    InstallPackCallback mojo_callback) {
  const std::optional<std::string> pack_id =
      ConvertMojoFeatureToPackId(feature_id);

  if (pack_id.has_value()) {
    LanguagePackManager::InstallPack(
        pack_id.value(), language,
        base::BindOnce(&OnOperationComplete, std::move(mojo_callback)));
  } else {
    auto info = LanguagePackInfo::New();
    info->pack_state = PackState::ERROR;
    info->feature_id = feature_id;
    std::move(mojo_callback).Run(std::move(info));
  }
}

void LanguagePacksImpl::InstallBasePack(FeatureId feature_id,
                                        InstallBasePackCallback mojo_callback) {
  const std::optional<std::string> pack_id =
      ConvertMojoFeatureToPackId(feature_id);

  if (pack_id.has_value()) {
    LanguagePackManager::InstallBasePack(
        *pack_id,
        base::BindOnce(&OnInstallBasePackComplete, std::move(mojo_callback)));
  } else {
    auto info = BasePackInfo::New();
    info->pack_state = PackState::ERROR;
    std::move(mojo_callback).Run(std::move(info));
  }
}

void LanguagePacksImpl::UninstallPack(FeatureId feature_id,
                                      const std::string& language,
                                      UninstallPackCallback mojo_callback) {
  const std::optional<std::string> pack_id =
      ConvertMojoFeatureToPackId(feature_id);

  // We ignore the request if the input parameters are incorrect.
  if (pack_id.has_value()) {
    LanguagePackManager::RemovePack(
        pack_id.value(), language,
        base::BindOnce(&OnUninstallComplete, std::move(mojo_callback)));
  }
}

void LanguagePacksImpl::AddObserver(
    mojo::PendingAssociatedRemote<ash::language::mojom::LanguagePacksObserver>
        observer) {
  observers_.Add(std::move(observer));
}

void LanguagePacksImpl::OnPackStateChanged(const PackResult& pack_result) {
  auto info = LanguagePackInfo::New();
  info->pack_state = GetPackStateFromStatusCode(pack_result.pack_state);
  info->error = GetMojoErrorFromPackError(pack_result.operation_error);
  if (pack_result.pack_state == PackResult::StatusCode::kInstalled) {
    info->path = pack_result.path;
  }
  info->feature_id = ToFeatureId(pack_result.feature_id);
  info->locale = pack_result.language_code;

  for (const auto& observer : observers_) {
    observer->OnPackStateChanged(info.Clone());
  }
}

}  // namespace ash::language_packs