Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_util.h
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 <string>
11 #include <vector>
12
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"
18
19 namespace chromeos {
20 namespace input_method {
21
22 class InputMethodDelegate;
23
24 enum InputMethodType {
25   kKeyboardLayoutsOnly,
26   kAllInputMethods,
27 };
28
29 // A class which provides miscellaneous input method utility functions.
30 class InputMethodUtil {
31  public:
32   explicit InputMethodUtil(InputMethodDelegate* delegate);
33   ~InputMethodUtil();
34
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;
43
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;
49
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;
55
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;
64
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;
71
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.
76   //
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;
83
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   // current input method.
87   void GetFirstLoginInputMethodIds(
88       const std::string& language_code,
89       const InputMethodDescriptor& current_input_method,
90       std::vector<std::string>* out_input_method_ids) const;
91
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;
97
98   // Gets first input method associated with the language.
99   // Returns empty string on error.
100   std::string GetLanguageDefaultInputMethodId(const std::string& language_code);
101
102   // Migrates the legacy xkb id to extension based xkb id.
103   // Returns true if the given input method id list is modified,
104   // returns false otherwise.
105   // This method should not be removed because it's required to transfer XKB
106   // input method ID from VPD into extension-based XKB input method ID.
107   bool MigrateInputMethods(std::vector<std::string>* input_method_ids);
108
109   // Updates the internal cache of hardware layouts.
110   void UpdateHardwareLayoutCache();
111
112   // Set hardware keyboard layout for testing purpose. This is for simulating
113   // "keyboard_layout" entry in VPD values.
114   void SetHardwareKeyboardLayoutForTesting(const std::string& layout);
115
116   // Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng"
117   // for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard.
118   const std::vector<std::string>& GetHardwareInputMethodIds();
119
120   // Returns the login-allowed input method ID of the hardware keyboard, e.g.
121   // "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please
122   // note that this is not a subset of returned value of
123   // GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only
124   // non-login keyboard, this function will returns "xkb:us::eng" as the
125   // fallback keyboard.
126   const std::vector<std::string>& GetHardwareLoginInputMethodIds();
127
128   // Returns true if given input method can be used to input login data.
129   bool IsLoginKeyboard(const std::string& input_method_id) const;
130
131   // Returns true if the given input method id is supported.
132   bool IsValidInputMethodId(const std::string& input_method_id) const;
133
134   // Returns true if the given input method id is for a keyboard layout.
135   static bool IsKeyboardLayout(const std::string& input_method_id);
136
137   // Resets the list of component extension IMEs.
138   void ResetInputMethods(const InputMethodDescriptors& imes);
139
140   // Appends the additional list of component extension IMEs.
141   void AppendInputMethods(const InputMethodDescriptors& imes);
142
143   // Initializes the extension based xkb IMEs for testing.
144   void InitXkbInputMethodsForTesting();
145
146   // Map from input method ID to associated input method descriptor.
147   typedef std::map<
148     std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap;
149
150   // Gets the id to desctiptor map for testing.
151   const InputMethodIdToDescriptorMap& GetIdToDesciptorMapForTesting();
152
153   // Returns the fallback input method descriptor (the very basic US
154   // keyboard). This function is mostly used for testing, but may be used
155   // as the fallback, when there is no other choice.
156   static InputMethodDescriptor GetFallbackInputMethodDescriptor();
157
158  protected:
159   // protected: for unit testing as well.
160   bool GetInputMethodIdsFromLanguageCodeInternal(
161       const std::multimap<std::string, std::string>& language_code_to_ids,
162       const std::string& normalized_language_code,
163       InputMethodType type,
164       std::vector<std::string>* out_input_method_ids) const;
165
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.
169   //
170   // Examples:
171   //
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;
177
178  private:
179   bool TranslateStringInternal(const std::string& english_string,
180                                base::string16 *out_string) const;
181
182   // Get long name of the given input method. |short_name| is to specify whether
183   // to get the long name for OOBE screen, because OOBE screen displays shorter
184   // name (e.g. 'US' instead of 'US keyboard').
185   base::string16 GetInputMethodLongNameInternal(
186       const InputMethodDescriptor& input_method, bool short_name) const;
187
188   // Map from language code to associated input method IDs, etc.
189   typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap;
190
191   LanguageCodeToIdsMap language_code_to_ids_;
192   InputMethodIdToDescriptorMap id_to_descriptor_;
193
194   typedef base::hash_map<std::string, int> HashType;
195   HashType english_to_resource_id_;
196
197   InputMethodDelegate* delegate_;
198
199   base::ThreadChecker thread_checker_;
200   std::vector<std::string> hardware_layouts_;
201   std::vector<std::string> hardware_login_layouts_;
202   std::vector<std::string> cached_hardware_layouts_;
203
204   DISALLOW_COPY_AND_ASSIGN(InputMethodUtil);
205 };
206
207 }  // namespace input_method
208 }  // namespace chromeos
209
210 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_