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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_IME_ASH_INPUT_METHOD_UTIL_H_
#define UI_BASE_IME_ASH_INPUT_METHOD_UTIL_H_
#include <cstddef>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/threading/thread_checker.h"
#include "ui/base/ime/ash/input_method_descriptor.h"
namespace ash {
namespace input_method {
// Map from language code to associated input method IDs, etc.
using LanguageCodeToIdsMap =
std::multimap<std::string, std::string, std::less<>>;
class InputMethodDelegate;
enum InputMethodType {
kKeyboardLayoutsOnly,
kAllInputMethods,
};
// A class which provides miscellaneous input method utility functions.
class COMPONENT_EXPORT(UI_BASE_IME_ASH) InputMethodUtil {
public:
explicit InputMethodUtil(InputMethodDelegate* delegate);
InputMethodUtil(const InputMethodUtil&) = delete;
InputMethodUtil& operator=(const InputMethodUtil&) = delete;
~InputMethodUtil();
std::u16string GetInputMethodMediumName(
const InputMethodDescriptor& input_method) const;
std::u16string GetInputMethodLongNameStripped(
const InputMethodDescriptor& input_method) const;
std::u16string GetInputMethodLongName(
const InputMethodDescriptor& input_method) const;
// Converts an input method ID to an input method descriptor. Returns nullptr
// when |input_method_id| is unknown.
// Example: "pinyin" => { id: "pinyin", display_name: "Pinyin",
// keyboard_layout: "us", language_code: "zh" }
const InputMethodDescriptor* GetInputMethodDescriptorFromId(
const std::string& input_method_id) const;
// Gets input method IDs that belong to |language_code|.
// If |type| is |kKeyboardLayoutsOnly|, the function does not return input
// methods that are not for keybord layout switching. Returns true on success.
// Note that the function might return false or |language_code| is unknown.
//
// The retured input method IDs are sorted by populalirty per
// chromeos/platform/assets/input_methods/allowlist.txt.
bool GetInputMethodIdsFromLanguageCode(
std::string_view language_code,
InputMethodType type,
std::vector<std::string>* out_input_method_ids) const;
std::vector<std::string> GetInputMethodIdsFromHandwritingLanguage(
std::string_view handwriting_language);
// Gets the input method IDs suitable for the first user login, based on
// the given language code (UI language), and the descriptor of the
// preferred input method.
void GetFirstLoginInputMethodIds(
const std::string& language_code,
const InputMethodDescriptor& preferred_input_method,
std::vector<std::string>* out_input_method_ids) const;
// Gets the language codes associated with the given input method IDs.
// The returned language codes won't have duplicates.
void GetLanguageCodesFromInputMethodIds(
const std::vector<std::string>& input_method_ids,
std::vector<std::string>* out_language_codes) const;
// Gets first input method associated with the language.
// Returns empty string on error.
std::string GetLanguageDefaultInputMethodId(const std::string& language_code);
// Migrates the input method id as below:
// - Legacy xkb id to extension based id, e.g.
// xkb:us::eng -> _comp_ime_...xkb:us::eng
// - VPD well formatted id to extension based input method id, e.g.
// m17n:vi_telex -> _comp_ime_...vkd_vi_telex
// - ChromiumOS input method ID to ChromeOS one, or vice versa, e.g.
// _comp_ime_xxxxxx...xkb:us::eng -> _comp_ime_yyyyyy...xkb:us::eng
static std::string GetMigratedInputMethod(const std::string& input_method_id);
// Migrates the input method IDs.
// Returns true if the given input method id list is modified,
// returns false otherwise.
// This method should not be removed because it's required to transfer XKB
// input method ID from VPD into extension-based XKB input method ID.
static bool GetMigratedInputMethodIDs(
std::vector<std::string>* input_method_ids);
// Updates the internal cache of hardware layouts.
void UpdateHardwareLayoutCache();
// Set hardware keyboard layout for testing purpose. This is for simulating
// "keyboard_layout" entry in VPD values.
void SetHardwareKeyboardLayoutForTesting(const std::string& layout);
// Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng"
// for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard.
const std::vector<std::string>& GetHardwareInputMethodIds();
// Returns the login-allowed input method ID of the hardware keyboard, e.g.
// "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please
// note that this is not a subset of returned value of
// GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only
// non-login keyboard, this function will returns "xkb:us::eng" as the
// fallback keyboard.
const std::vector<std::string>& GetHardwareLoginInputMethodIds();
// Returns the localized display name for the given input method.
static std::string GetLocalizedDisplayName(
const InputMethodDescriptor& descriptor);
// Returns true if given input method can be used to input login data.
bool IsLoginKeyboard(const std::string& input_method_id) const;
// Returns true if given input method is allowlisted for OOBE.
bool IsOobeAllowlisted(const std::string& input_method_id) const;
// Returns true if the given input method id is supported.
bool IsValidInputMethodId(const std::string& input_method_id) const;
// Returns true if the given input method id is for a keyboard layout.
static bool IsKeyboardLayout(const std::string& input_method_id);
// Resets the list of component extension IMEs.
void ResetInputMethods(const InputMethodDescriptors& imes);
// Appends the additional list of component extension IMEs.
void AppendInputMethods(const InputMethodDescriptors& imes);
// Initializes the extension based xkb IMEs for testing.
void InitXkbInputMethodsForTesting(const InputMethodDescriptors& imes);
// Map from input method ID to associated input method descriptor.
using InputMethodIdToDescriptorMap =
std::map<std::string, InputMethodDescriptor>;
// Returns the fallback input method descriptor (the very basic US
// keyboard). This function is mostly used for testing, but may be used
// as the fallback, when there is no other choice.
static InputMethodDescriptor GetFallbackInputMethodDescriptor();
protected:
// protected: for unit testing as well.
static bool GetInputMethodIdsFromLanguageCodeInternal(
const LanguageCodeToIdsMap& language_code_to_ids,
std::string_view normalized_language_code,
InputMethodType type,
std::vector<std::string>* out_input_method_ids);
private:
// Get long name of the given input method. |short_name| is to specify whether
// to get the long name for OOBE screen, because OOBE screen displays shorter
// name (e.g. 'US' instead of 'US keyboard').
std::u16string GetInputMethodLongNameInternal(
const InputMethodDescriptor& input_method,
bool short_name) const;
LanguageCodeToIdsMap language_code_to_ids_;
LanguageCodeToIdsMap handwriting_language_to_ids_;
InputMethodIdToDescriptorMap id_to_descriptor_;
using EnglishToIDMap = base::flat_map<std::string, int>;
EnglishToIDMap english_to_resource_id_;
raw_ptr<InputMethodDelegate> delegate_;
base::ThreadChecker thread_checker_;
std::vector<std::string> hardware_layouts_;
std::vector<std::string> hardware_login_layouts_;
std::vector<std::string> cached_hardware_layouts_;
std::set<std::string> oobe_allowlisted_ids_;
};
} // namespace input_method
} // namespace ash
#endif // UI_BASE_IME_ASH_INPUT_METHOD_UTIL_H_
|