File: language_packs_util.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 (228 lines) | stat: -rw-r--r-- 7,682 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 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_util.h"

#include <optional>

#include "ash/constants/ash_pref_names.h"
#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice_client.h"
#include "chromeos/ash/components/language_packs/language_pack_manager.h"
#include "components/language/core/common/locale_util.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/session_manager_types.h"
#include "third_party/cros_system_api/dbus/dlcservice/dbus-constants.h"

namespace ash::language_packs {

namespace {

const std::string ResolveLocaleForHandwriting(const std::string& input_locale) {
  // Chinese HongKong is an exception.
  if (base::EqualsCaseInsensitiveASCII(input_locale, "zh-hk")) {
    return "zh-HK";
  }
  return std::string(language::ExtractBaseLanguage(input_locale));
}

const std::string ResolveLocaleForTts(const std::string& input_locale) {
  // Consider exceptions first.
  if (base::EqualsCaseInsensitiveASCII(input_locale, "en-au") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "en-gb") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "en-us") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "es-es") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "es-us") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "pt-br") ||
      base::EqualsCaseInsensitiveASCII(input_locale, "pt-pt")) {
    return base::ToLowerASCII(input_locale);
  }
  return std::string(language::ExtractBaseLanguage(input_locale));
}

}  // namespace

FeatureIdsEnum GetFeatureIdValueForUma(const std::string& feature_id) {
  if (feature_id == kHandwritingFeatureId) {
    return FeatureIdsEnum::kHandwriting;
  }
  if (feature_id == kTtsFeatureId) {
    return FeatureIdsEnum::kTts;
  }
  if (feature_id == kFontsFeatureId) {
    return FeatureIdsEnum::kFonts;
  }

  // Default value of unknown.
  return FeatureIdsEnum::kUnknown;
}

FeatureSuccessEnum GetSuccessValueForUma(const std::string& feature_id,
                                         const bool success) {
  if (feature_id == kHandwritingFeatureId) {
    if (success) {
      return FeatureSuccessEnum::kHandwritingSuccess;
    } else {
      return FeatureSuccessEnum::kHandwritingFailure;
    }
  }
  if (feature_id == kTtsFeatureId) {
    if (success) {
      return FeatureSuccessEnum::kTtsSuccess;
    } else {
      return FeatureSuccessEnum::kTtsFailure;
    }
  }
  if (feature_id == kFontsFeatureId) {
    if (success) {
      return FeatureSuccessEnum::kFontsSuccess;
    } else {
      return FeatureSuccessEnum::kFontsFailure;
    }
  }

  // Default value of unknown.
  if (success) {
    return FeatureSuccessEnum::kUnknownSuccess;
  } else {
    return FeatureSuccessEnum::kUnknownFailure;
  }
}

DlcErrorTypeEnum GetDlcErrorTypeForUma(const std::string& error_str) {
  if (error_str == dlcservice::kErrorNone) {
    return DlcErrorTypeEnum::kErrorNone;
  } else if (error_str == dlcservice::kErrorInternal) {
    return DlcErrorTypeEnum::kErrorInternal;
  } else if (error_str == dlcservice::kErrorBusy) {
    return DlcErrorTypeEnum::kErrorBusy;
  } else if (error_str == dlcservice::kErrorNeedReboot) {
    return DlcErrorTypeEnum::kErrorNeedReboot;
  } else if (error_str == dlcservice::kErrorInvalidDlc) {
    return DlcErrorTypeEnum::kErrorInvalidDlc;
  } else if (error_str == dlcservice::kErrorAllocation) {
    return DlcErrorTypeEnum::kErrorAllocation;
  } else if (error_str == dlcservice::kErrorNoImageFound) {
    return DlcErrorTypeEnum::kErrorNoImageFound;
  }

  // Return unknown if we can't recognize the error.
  LOG(ERROR) << "Wrong error message received from DLC Service";
  return DlcErrorTypeEnum::kErrorUnknown;
}

PackResult CreateInvalidDlcPackResult() {
  PackResult result;
  result.operation_error = PackResult::ErrorCode::kWrongId;
  result.pack_state = PackResult::StatusCode::kUnknown;
  return result;
}

PackResult ConvertDlcStateToPackResult(const dlcservice::DlcState& dlc_state) {
  PackResult result;

  switch (dlc_state.state()) {
    case dlcservice::DlcState_State_INSTALLED:
      result.pack_state = PackResult::StatusCode::kInstalled;
      result.path = dlc_state.root_path();
      break;
    case dlcservice::DlcState_State_INSTALLING:
      result.pack_state = PackResult::StatusCode::kInProgress;
      break;
    case dlcservice::DlcState_State_NOT_INSTALLED:
      result.pack_state = PackResult::StatusCode::kNotInstalled;
      break;
    default:
      result.pack_state = PackResult::StatusCode::kUnknown;
      break;
  }

  result.operation_error =
      ConvertDlcErrorToErrorCode(dlc_state.last_error_code());

  return result;
}

PackResult ConvertDlcInstallResultToPackResult(
    const DlcserviceClient::InstallResult& install_result) {
  PackResult result;

  result.operation_error = ConvertDlcErrorToErrorCode(install_result.error);

  if (result.operation_error == PackResult::ErrorCode::kNone) {
    result.pack_state = PackResult::StatusCode::kInstalled;
    result.path = install_result.root_path;
  } else {
    result.pack_state = PackResult::StatusCode::kUnknown;
  }

  return result;
}

PackResult::ErrorCode ConvertDlcErrorToErrorCode(std::string_view err) {
  if (err.empty() || err == dlcservice::kErrorNone) {
    return PackResult::ErrorCode::kNone;
  } else if (err == dlcservice::kErrorInvalidDlc) {
    return PackResult::ErrorCode::kWrongId;
  } else if (err == dlcservice::kErrorNeedReboot) {
    return PackResult::ErrorCode::kNeedReboot;
  } else if (err == dlcservice::kErrorAllocation) {
    return PackResult::ErrorCode::kAllocation;
  } else {
    // We use INTERNAL for all remaining errors thrown by DLC Service because
    // there's nothing we or the client can do about it.
    // Error code BUSY is never returned.
    return PackResult::ErrorCode::kOther;
  }
}

const std::string ResolveLocale(const std::string& feature_id,
                                const std::string& locale) {
  if (feature_id == kHandwritingFeatureId) {
    return ResolveLocaleForHandwriting(locale);
  } else if (feature_id == kTtsFeatureId) {
    return ResolveLocaleForTts(locale);
  } else if (feature_id == kFontsFeatureId) {
    // Language pack resolution is handled by the client.
    return locale;
  } else {
    DLOG(ERROR) << "ResolveLocale called with wrong feature_id";
    return "";
  }
}

bool IsOobe() {
  return session_manager::SessionManager::Get()->session_state() ==
         session_manager::SessionState::OOBE;
}

base::flat_set<std::string> MapThenFilterStrings(
    base::span<const std::string> inputs,
    base::RepeatingCallback<std::optional<std::string>(const std::string&)>
        input_mapping) {
  std::vector<std::string> output;
  for (const auto& input : inputs) {
    const std::optional<std::string> result = input_mapping.Run(input);
    if (result.has_value()) {
      output.push_back(std::move(*result));
    }
  }

  return output;
}

std::vector<std::string> ExtractInputMethodsFromPrefs(PrefService* prefs) {
  const std::string& preload_engines_str =
      prefs->GetString(prefs::kLanguagePreloadEngines);
  return base::SplitString(preload_engines_str, ",", base::TRIM_WHITESPACE,
                           base::SPLIT_WANT_ALL);
}

}  // namespace ash::language_packs