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