Upstream version 5.34.104.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   // |supported_input_methods| is a list of all input methods supported,
33   // including ones not active. The list is used to initialize member variables
34   // in this class.
35   InputMethodUtil(InputMethodDelegate* delegate,
36                   scoped_ptr<InputMethodDescriptors> supported_input_methods);
37   ~InputMethodUtil();
38
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   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 GetInputMethodLongName(
62       const InputMethodDescriptor& input_method) const;
63
64   // Converts an input method ID to an input method descriptor. Returns NULL
65   // when |input_method_id| is unknown.
66   // Example: "pinyin" => { id: "pinyin", display_name: "Pinyin",
67   //                        keyboard_layout: "us", language_code: "zh" }
68   const InputMethodDescriptor* GetInputMethodDescriptorFromId(
69       const std::string& input_method_id) const;
70
71   // Gets input method IDs that belong to |language_code|.
72   // If |type| is |kKeyboardLayoutsOnly|, the function does not return input
73   // methods that are not for keybord layout switching. Returns true on success.
74   // Note that the function might return false or |language_code| is unknown.
75   //
76   // The retured input method IDs are sorted by populalirty per
77   // chromeos/platform/assets/input_methods/whitelist.txt.
78   bool GetInputMethodIdsFromLanguageCode(
79       const std::string& language_code,
80       InputMethodType type,
81       std::vector<std::string>* out_input_method_ids) const;
82
83   // Gets the input method IDs suitable for the first user login, based on
84   // the given language code (UI language), and the descriptor of the
85   // current input method.
86   void GetFirstLoginInputMethodIds(
87       const std::string& language_code,
88       const InputMethodDescriptor& current_input_method,
89       std::vector<std::string>* out_input_method_ids) const;
90
91   // Gets the language codes associated with the given input method IDs.
92   // The returned language codes won't have duplicates.
93   void GetLanguageCodesFromInputMethodIds(
94       const std::vector<std::string>& input_method_ids,
95       std::vector<std::string>* out_language_codes) const;
96
97   // Gets first input method associated with the language.
98   // Returns empty string on error.
99   std::string GetLanguageDefaultInputMethodId(const std::string& language_code);
100
101   // Updates the internal cache of hardware layouts.
102   void UpdateHardwareLayoutCache();
103
104   // Set hardware keyboard layout for testing purpose. This is for simulating
105   // "keyboard_layout" entry in VPD values.
106   void SetHardwareKeyboardLayoutForTesting(const std::string& layout);
107
108   // Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng"
109   // for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard.
110   const std::vector<std::string>& GetHardwareInputMethodIds();
111
112   // Returns the login-allowed input method ID of the hardware keyboard, e.g.
113   // "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please
114   // note that this is not a subset of returned value of
115   // GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only
116   // non-login keyboard, this function will returns "xkb:us::eng" as the
117   // fallback keyboard.
118   const std::vector<std::string>& GetHardwareLoginInputMethodIds();
119
120   // Returns true if given input method can be used to input login data.
121   bool IsLoginKeyboard(const std::string& input_method_id) const;
122
123   // Returns true if the given input method id is supported.
124   bool IsValidInputMethodId(const std::string& input_method_id) const;
125
126   // Returns true if the given input method id is for a keyboard layout.
127   static bool IsKeyboardLayout(const std::string& input_method_id);
128
129   // Sets the list of component extension IMEs.
130   void SetComponentExtensions(const InputMethodDescriptors& imes);
131
132   // Returns the fallback input method descriptor (the very basic US
133   // keyboard). This function is mostly used for testing, but may be used
134   // as the fallback, when there is no other choice.
135   static InputMethodDescriptor GetFallbackInputMethodDescriptor();
136
137  protected:
138   // protected: for unit testing as well.
139   bool GetInputMethodIdsFromLanguageCodeInternal(
140       const std::multimap<std::string, std::string>& language_code_to_ids,
141       const std::string& normalized_language_code,
142       InputMethodType type,
143       std::vector<std::string>* out_input_method_ids) const;
144
145   // protected: for unit testing as well.
146   void ReloadInternalMaps();
147
148   // All input methods that are supported, including ones not active.
149   // protected: for testing.
150   scoped_ptr<InputMethodDescriptors> supported_input_methods_;
151
152   // Gets the keyboard layout name from the given input method ID.
153   // If the ID is invalid, an empty string will be returned.
154   // This function only supports xkb layouts.
155   //
156   // Examples:
157   //
158   // "xkb:us::eng"       => "us"
159   // "xkb:us:dvorak:eng" => "us(dvorak)"
160   // "xkb:gb::eng"       => "gb"
161   // "pinyin"            => "us" (because Pinyin uses US keyboard layout)
162   std::string GetKeyboardLayoutName(const std::string& input_method_id) const;
163
164  private:
165   bool TranslateStringInternal(const std::string& english_string,
166                                base::string16 *out_string) const;
167
168   // Map from language code to associated input method IDs, etc.
169   typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap;
170   // Map from input method ID to associated input method descriptor.
171   typedef std::map<
172     std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap;
173   // Map from XKB layout ID to associated input method descriptor.
174   typedef std::map<std::string, InputMethodDescriptor> XkbIdToDescriptorMap;
175   // Map from component extention IME id to associated input method descriptor.
176   typedef std::map<std::string, InputMethodDescriptor> ComponentExtIMEMap;
177
178   LanguageCodeToIdsMap language_code_to_ids_;
179   std::map<std::string, std::string> id_to_language_code_;
180   InputMethodIdToDescriptorMap id_to_descriptor_;
181   XkbIdToDescriptorMap xkb_id_to_descriptor_;
182   ComponentExtIMEMap component_extension_ime_id_to_descriptor_;
183
184   typedef base::hash_map<std::string, int> HashType;
185   HashType english_to_resource_id_;
186
187   InputMethodDelegate* delegate_;
188
189   base::ThreadChecker thread_checker_;
190   std::vector<std::string> hardware_layouts_;
191   std::vector<std::string> hardware_login_layouts_;
192
193   DISALLOW_COPY_AND_ASSIGN(InputMethodUtil);
194 };
195
196 }  // namespace input_method
197 }  // namespace chromeos
198
199 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_