OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | |
7 | |
8 #include <cstddef> | |
9 #include <map> | |
10 #include <memory> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/containers/hash_tables.h" | |
15 #include "base/macros.h" | |
16 #include "base/strings/string16.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "ui/base/ime/chromeos/input_method_descriptor.h" | |
19 | |
20 namespace chromeos { | |
21 namespace input_method { | |
22 | |
23 class InputMethodDelegate; | |
24 | |
25 enum InputMethodType { | |
26 kKeyboardLayoutsOnly, | |
27 kAllInputMethods, | |
28 }; | |
29 | |
30 // A class which provides miscellaneous input method utility functions. | |
31 class InputMethodUtil { | |
32 public: | |
33 explicit InputMethodUtil(InputMethodDelegate* delegate); | |
34 ~InputMethodUtil(); | |
35 | |
36 // Converts a string sent from IBus IME engines, which is written in English, | |
37 // into Chrome's string ID, then pulls internationalized resource string from | |
38 // the resource bundle and returns it. These functions are not thread-safe. | |
39 // Non-UI threads are not allowed to call them. | |
40 // The english_string to should be a xkb id with "xkb:...:...:..." format. | |
41 // TODO(shuchen): this method should be removed when finish the wrapping of | |
42 // xkb to extension. | |
43 base::string16 TranslateString(const std::string& english_string) const; | |
44 | |
45 // Converts an input method ID to a language code of the IME. Returns "Eng" | |
46 // when |input_method_id| is unknown. | |
47 // Example: "hangul" => "ko" | |
48 std::string GetLanguageCodeFromInputMethodId( | |
49 const std::string& input_method_id) const; | |
50 | |
51 // Converts an input method ID to a display name of the IME. Returns | |
52 // an empty strng when |input_method_id| is unknown. | |
53 // Examples: "pinyin" => "Pinyin" | |
54 std::string GetInputMethodDisplayNameFromId( | |
55 const std::string& input_method_id) const; | |
56 | |
57 base::string16 GetInputMethodShortName( | |
58 const InputMethodDescriptor& input_method) const; | |
59 base::string16 GetInputMethodMediumName( | |
60 const InputMethodDescriptor& input_method) const; | |
61 base::string16 GetInputMethodLongNameStripped( | |
62 const InputMethodDescriptor& input_method) const; | |
63 base::string16 GetInputMethodLongName( | |
64 const InputMethodDescriptor& input_method) const; | |
65 | |
66 // Converts an input method ID to an input method descriptor. Returns NULL | |
67 // when |input_method_id| is unknown. | |
68 // Example: "pinyin" => { id: "pinyin", display_name: "Pinyin", | |
69 // keyboard_layout: "us", language_code: "zh" } | |
70 const InputMethodDescriptor* GetInputMethodDescriptorFromId( | |
71 const std::string& input_method_id) const; | |
72 | |
73 // Gets input method IDs that belong to |language_code|. | |
74 // If |type| is |kKeyboardLayoutsOnly|, the function does not return input | |
75 // methods that are not for keybord layout switching. Returns true on success. | |
76 // Note that the function might return false or |language_code| is unknown. | |
77 // | |
78 // The retured input method IDs are sorted by populalirty per | |
79 // chromeos/platform/assets/input_methods/whitelist.txt. | |
80 bool GetInputMethodIdsFromLanguageCode( | |
81 const std::string& language_code, | |
82 InputMethodType type, | |
83 std::vector<std::string>* out_input_method_ids) const; | |
84 | |
85 // Gets the input method IDs suitable for the first user login, based on | |
86 // the given language code (UI language), and the descriptor of the | |
87 // preferred input method. | |
88 void GetFirstLoginInputMethodIds( | |
89 const std::string& language_code, | |
90 const InputMethodDescriptor& preferred_input_method, | |
91 std::vector<std::string>* out_input_method_ids) const; | |
92 | |
93 // Gets the language codes associated with the given input method IDs. | |
94 // The returned language codes won't have duplicates. | |
95 void GetLanguageCodesFromInputMethodIds( | |
96 const std::vector<std::string>& input_method_ids, | |
97 std::vector<std::string>* out_language_codes) const; | |
98 | |
99 // Gets first input method associated with the language. | |
100 // Returns empty string on error. | |
101 std::string GetLanguageDefaultInputMethodId(const std::string& language_code); | |
102 | |
103 // Migrates the input method id as below: | |
104 // - Legacy xkb id to extension based id, e.g. | |
105 // xkb:us::eng -> _comp_ime_...xkb:us::eng | |
106 // - VPD well formatted id to extension based input method id, e.g. | |
107 // m17n:vi_telex -> _comp_ime_...vkd_vi_telex | |
108 // - ChromiumOS input method ID to ChromeOS one, or vice versa, e.g. | |
109 // _comp_ime_xxxxxx...xkb:us::eng -> _comp_ime_yyyyyy...xkb:us::eng | |
110 std::string MigrateInputMethod(const std::string& input_method_id); | |
111 | |
112 // Migrates the input method IDs. | |
113 // Returns true if the given input method id list is modified, | |
114 // returns false otherwise. | |
115 // This method should not be removed because it's required to transfer XKB | |
116 // input method ID from VPD into extension-based XKB input method ID. | |
117 bool MigrateInputMethods(std::vector<std::string>* input_method_ids); | |
118 | |
119 // Updates the internal cache of hardware layouts. | |
120 void UpdateHardwareLayoutCache(); | |
121 | |
122 // Set hardware keyboard layout for testing purpose. This is for simulating | |
123 // "keyboard_layout" entry in VPD values. | |
124 void SetHardwareKeyboardLayoutForTesting(const std::string& layout); | |
125 | |
126 // Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng" | |
127 // for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard. | |
128 const std::vector<std::string>& GetHardwareInputMethodIds(); | |
129 | |
130 // Returns the login-allowed input method ID of the hardware keyboard, e.g. | |
131 // "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please | |
132 // note that this is not a subset of returned value of | |
133 // GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only | |
134 // non-login keyboard, this function will returns "xkb:us::eng" as the | |
135 // fallback keyboard. | |
136 const std::vector<std::string>& GetHardwareLoginInputMethodIds(); | |
137 | |
138 // Returns the localized display name for the given input method. | |
139 std::string GetLocalizedDisplayName( | |
140 const InputMethodDescriptor& descriptor) const; | |
141 | |
142 // Returns true if given input method can be used to input login data. | |
143 bool IsLoginKeyboard(const std::string& input_method_id) const; | |
144 | |
145 // Returns true if the given input method id is supported. | |
146 bool IsValidInputMethodId(const std::string& input_method_id) const; | |
147 | |
148 // Returns true if the given input method id is for a keyboard layout. | |
149 static bool IsKeyboardLayout(const std::string& input_method_id); | |
150 | |
151 // Resets the list of component extension IMEs. | |
152 void ResetInputMethods(const InputMethodDescriptors& imes); | |
153 | |
154 // Appends the additional list of component extension IMEs. | |
155 void AppendInputMethods(const InputMethodDescriptors& imes); | |
156 | |
157 // Initializes the extension based xkb IMEs for testing. | |
158 void InitXkbInputMethodsForTesting(const InputMethodDescriptors& imes); | |
159 | |
160 // Map from input method ID to associated input method descriptor. | |
161 typedef std::map< | |
162 std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap; | |
163 | |
164 // Gets the id to desctiptor map for testing. | |
165 const InputMethodIdToDescriptorMap& GetIdToDesciptorMapForTesting(); | |
166 | |
167 // Returns the fallback input method descriptor (the very basic US | |
168 // keyboard). This function is mostly used for testing, but may be used | |
169 // as the fallback, when there is no other choice. | |
170 static InputMethodDescriptor GetFallbackInputMethodDescriptor(); | |
171 | |
172 protected: | |
173 // protected: for unit testing as well. | |
174 bool GetInputMethodIdsFromLanguageCodeInternal( | |
175 const std::multimap<std::string, std::string>& language_code_to_ids, | |
176 const std::string& normalized_language_code, | |
177 InputMethodType type, | |
178 std::vector<std::string>* out_input_method_ids) const; | |
179 | |
180 // Gets the keyboard layout name from the given input method ID. | |
181 // If the ID is invalid, an empty string will be returned. | |
182 // This function only supports xkb layouts. | |
183 // | |
184 // Examples: | |
185 // | |
186 // "xkb:us::eng" => "us" | |
187 // "xkb:us:dvorak:eng" => "us(dvorak)" | |
188 // "xkb:gb::eng" => "gb" | |
189 // "pinyin" => "us" (because Pinyin uses US keyboard layout) | |
190 std::string GetKeyboardLayoutName(const std::string& input_method_id) const; | |
191 | |
192 private: | |
193 bool TranslateStringInternal(const std::string& english_string, | |
194 base::string16 *out_string) const; | |
195 | |
196 // Get long name of the given input method. |short_name| is to specify whether | |
197 // to get the long name for OOBE screen, because OOBE screen displays shorter | |
198 // name (e.g. 'US' instead of 'US keyboard'). | |
199 base::string16 GetInputMethodLongNameInternal( | |
200 const InputMethodDescriptor& input_method, bool short_name) const; | |
201 | |
202 // Map from language code to associated input method IDs, etc. | |
203 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; | |
204 | |
205 LanguageCodeToIdsMap language_code_to_ids_; | |
206 InputMethodIdToDescriptorMap id_to_descriptor_; | |
207 | |
208 typedef base::hash_map<std::string, int> HashType; | |
209 HashType english_to_resource_id_; | |
210 | |
211 InputMethodDelegate* delegate_; | |
212 | |
213 base::ThreadChecker thread_checker_; | |
214 std::vector<std::string> hardware_layouts_; | |
215 std::vector<std::string> hardware_login_layouts_; | |
216 std::vector<std::string> cached_hardware_layouts_; | |
217 | |
218 DISALLOW_COPY_AND_ASSIGN(InputMethodUtil); | |
219 }; | |
220 | |
221 } // namespace input_method | |
222 } // namespace chromeos | |
223 | |
224 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | |
OLD | NEW |