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_
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 "chromeos/ime/input_method_descriptor.h"
20 namespace input_method
{
22 class InputMethodDelegate
;
24 enum InputMethodType
{
29 // A class which provides miscellaneous input method utility functions.
30 class InputMethodUtil
{
32 // |supported_input_methods| is a list of all input methods supported,
33 // including ones not active. The list is used to initialize member variables
35 InputMethodUtil(InputMethodDelegate
* delegate
,
36 scoped_ptr
<InputMethodDescriptors
> supported_input_methods
);
39 // Converts a string sent from IBus IME engines, which is written in English,
40 // into Chrome's string ID, then pulls internationalized resource string from
41 // the resource bundle and returns it. These functions are not thread-safe.
42 // Non-UI threads are not allowed to call them.
43 // The english_string to should be a xkb id with "xkb:...:...:..." format.
44 // TODO(shuchen): this method should be removed when finish the wrapping of
46 base::string16
TranslateString(const std::string
& english_string
) const;
48 // Converts an input method ID to a language code of the IME. Returns "Eng"
49 // when |input_method_id| is unknown.
50 // Example: "hangul" => "ko"
51 std::string
GetLanguageCodeFromInputMethodId(
52 const std::string
& input_method_id
) const;
54 // Converts an input method ID to a display name of the IME. Returns
55 // an empty strng when |input_method_id| is unknown.
56 // Examples: "pinyin" => "Pinyin"
57 std::string
GetInputMethodDisplayNameFromId(
58 const std::string
& input_method_id
) const;
60 base::string16
GetInputMethodShortName(
61 const InputMethodDescriptor
& input_method
) const;
62 base::string16
GetInputMethodMediumName(
63 const InputMethodDescriptor
& input_method
) const;
64 base::string16
GetInputMethodLongName(
65 const InputMethodDescriptor
& input_method
) const;
67 // Converts an input method ID to an input method descriptor. Returns NULL
68 // when |input_method_id| is unknown.
69 // Example: "pinyin" => { id: "pinyin", display_name: "Pinyin",
70 // keyboard_layout: "us", language_code: "zh" }
71 const InputMethodDescriptor
* GetInputMethodDescriptorFromId(
72 const std::string
& input_method_id
) const;
74 // Gets input method IDs that belong to |language_code|.
75 // If |type| is |kKeyboardLayoutsOnly|, the function does not return input
76 // methods that are not for keybord layout switching. Returns true on success.
77 // Note that the function might return false or |language_code| is unknown.
79 // The retured input method IDs are sorted by populalirty per
80 // chromeos/platform/assets/input_methods/whitelist.txt.
81 bool GetInputMethodIdsFromLanguageCode(
82 const std::string
& language_code
,
84 std::vector
<std::string
>* out_input_method_ids
) const;
86 // Gets the input method IDs suitable for the first user login, based on
87 // the given language code (UI language), and the descriptor of the
88 // current input method.
89 void GetFirstLoginInputMethodIds(
90 const std::string
& language_code
,
91 const InputMethodDescriptor
& current_input_method
,
92 std::vector
<std::string
>* out_input_method_ids
) const;
94 // Gets the language codes associated with the given input method IDs.
95 // The returned language codes won't have duplicates.
96 void GetLanguageCodesFromInputMethodIds(
97 const std::vector
<std::string
>& input_method_ids
,
98 std::vector
<std::string
>* out_language_codes
) const;
100 // Gets first input method associated with the language.
101 // Returns empty string on error.
102 std::string
GetLanguageDefaultInputMethodId(const std::string
& language_code
);
104 // Migrates the legacy xkb id to extension based xkb id.
105 // Returns true if the given input method id list is modified,
106 // returns false otherwise.
107 // TODO(shuchen): Remove this function after few milestones are passed.
108 // See: http://crbug.com/345604
109 bool MigrateXkbInputMethods(
110 std::vector
<std::string
>* input_method_ids
);
112 // Updates the internal cache of hardware layouts.
113 void UpdateHardwareLayoutCache();
115 // Set hardware keyboard layout for testing purpose. This is for simulating
116 // "keyboard_layout" entry in VPD values.
117 void SetHardwareKeyboardLayoutForTesting(const std::string
& layout
);
119 // Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng"
120 // for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard.
121 const std::vector
<std::string
>& GetHardwareInputMethodIds();
123 // Returns the login-allowed input method ID of the hardware keyboard, e.g.
124 // "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please
125 // note that this is not a subset of returned value of
126 // GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only
127 // non-login keyboard, this function will returns "xkb:us::eng" as the
128 // fallback keyboard.
129 const std::vector
<std::string
>& GetHardwareLoginInputMethodIds();
131 // Returns true if given input method can be used to input login data.
132 bool IsLoginKeyboard(const std::string
& input_method_id
) const;
134 // Returns true if the given input method id is supported.
135 bool IsValidInputMethodId(const std::string
& input_method_id
) const;
137 // Returns true if the given input method id is for a keyboard layout.
138 static bool IsKeyboardLayout(const std::string
& input_method_id
);
140 // Sets the list of component extension IMEs.
141 void SetComponentExtensions(const InputMethodDescriptors
& imes
);
143 // Initializes the extension based xkb IMEs for testing.
144 void InitXkbInputMethodsForTesting();
146 // Returns the fallback input method descriptor (the very basic US
147 // keyboard). This function is mostly used for testing, but may be used
148 // as the fallback, when there is no other choice.
149 static InputMethodDescriptor
GetFallbackInputMethodDescriptor();
152 // protected: for unit testing as well.
153 bool GetInputMethodIdsFromLanguageCodeInternal(
154 const std::multimap
<std::string
, std::string
>& language_code_to_ids
,
155 const std::string
& normalized_language_code
,
156 InputMethodType type
,
157 std::vector
<std::string
>* out_input_method_ids
) const;
159 // protected: for unit testing as well.
160 void ReloadInternalMaps();
162 // All input methods that are supported, including ones not active.
163 // protected: for testing.
164 scoped_ptr
<InputMethodDescriptors
> supported_input_methods_
;
166 // Gets the keyboard layout name from the given input method ID.
167 // If the ID is invalid, an empty string will be returned.
168 // This function only supports xkb layouts.
172 // "xkb:us::eng" => "us"
173 // "xkb:us:dvorak:eng" => "us(dvorak)"
174 // "xkb:gb::eng" => "gb"
175 // "pinyin" => "us" (because Pinyin uses US keyboard layout)
176 std::string
GetKeyboardLayoutName(const std::string
& input_method_id
) const;
179 bool TranslateStringInternal(const std::string
& english_string
,
180 base::string16
*out_string
) const;
182 // Map from language code to associated input method IDs, etc.
183 typedef std::multimap
<std::string
, std::string
> LanguageCodeToIdsMap
;
184 // Map from input method ID to associated input method descriptor.
186 std::string
, InputMethodDescriptor
> InputMethodIdToDescriptorMap
;
187 // Map from XKB layout ID to associated input method descriptor.
188 typedef std::map
<std::string
, InputMethodDescriptor
> XkbIdToDescriptorMap
;
189 // Map from component extention IME id to associated input method descriptor.
190 typedef std::map
<std::string
, InputMethodDescriptor
> ComponentExtIMEMap
;
192 LanguageCodeToIdsMap language_code_to_ids_
;
193 std::map
<std::string
, std::string
> id_to_language_code_
;
194 InputMethodIdToDescriptorMap id_to_descriptor_
;
195 XkbIdToDescriptorMap xkb_id_to_descriptor_
;
196 std::map
<std::string
, std::string
> xkb_layout_to_indicator_
;
198 typedef base::hash_map
<std::string
, int> HashType
;
199 HashType english_to_resource_id_
;
201 InputMethodDelegate
* delegate_
;
203 base::ThreadChecker thread_checker_
;
204 std::vector
<std::string
> hardware_layouts_
;
205 std::vector
<std::string
> hardware_login_layouts_
;
206 std::vector
<std::string
> cached_hardware_layouts_
;
208 DISALLOW_COPY_AND_ASSIGN(InputMethodUtil
);
211 } // namespace input_method
212 } // namespace chromeos
214 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_