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 // 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 45 // xkb to extension. 46 base::string16 TranslateString(const std::string& english_string) const; 47 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; 53 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; 59 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; 66 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; 73 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. 78 // 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, 83 InputMethodType type, 84 std::vector<std::string>* out_input_method_ids) const; 85 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; 93 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; 99 100 // Gets first input method associated with the language. 101 // Returns empty string on error. 102 std::string GetLanguageDefaultInputMethodId(const std::string& language_code); 103 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 // This method should not be removed because it's required to transfer XKB 108 // input method ID from VPD into extension-based XKB input method ID. 109 bool MigrateInputMethods(std::vector<std::string>* input_method_ids); 110 111 // Updates the internal cache of hardware layouts. 112 void UpdateHardwareLayoutCache(); 113 114 // Set hardware keyboard layout for testing purpose. This is for simulating 115 // "keyboard_layout" entry in VPD values. 116 void SetHardwareKeyboardLayoutForTesting(const std::string& layout); 117 118 // Fills the input method IDs of the hardware keyboard. e.g. "xkb:us::eng" 119 // for US Qwerty keyboard or "xkb:ru::rus" for Russian keyboard. 120 const std::vector<std::string>& GetHardwareInputMethodIds(); 121 122 // Returns the login-allowed input method ID of the hardware keyboard, e.g. 123 // "xkb:us::eng" but not include non-login keyboard like "xkb:ru::rus". Please 124 // note that this is not a subset of returned value of 125 // GetHardwareInputMethodIds. If GetHardwareInputMethodIds returns only 126 // non-login keyboard, this function will returns "xkb:us::eng" as the 127 // fallback keyboard. 128 const std::vector<std::string>& GetHardwareLoginInputMethodIds(); 129 130 // Returns true if given input method can be used to input login data. 131 bool IsLoginKeyboard(const std::string& input_method_id) const; 132 133 // Returns true if the given input method id is supported. 134 bool IsValidInputMethodId(const std::string& input_method_id) const; 135 136 // Returns true if the given input method id is for a keyboard layout. 137 static bool IsKeyboardLayout(const std::string& input_method_id); 138 139 // Resets the list of component extension IMEs. 140 void ResetInputMethods(const InputMethodDescriptors& imes); 141 142 // Appends the additional list of component extension IMEs. 143 void AppendInputMethods(const InputMethodDescriptors& imes); 144 145 // Initializes the extension based xkb IMEs for testing. 146 void InitXkbInputMethodsForTesting(); 147 148 // Map from input method ID to associated input method descriptor. 149 typedef std::map< 150 std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap; 151 152 // Gets the id to desctiptor map for testing. 153 const InputMethodIdToDescriptorMap& GetIdToDesciptorMapForTesting(); 154 155 // Returns the fallback input method descriptor (the very basic US 156 // keyboard). This function is mostly used for testing, but may be used 157 // as the fallback, when there is no other choice. 158 static InputMethodDescriptor GetFallbackInputMethodDescriptor(); 159 160 protected: 161 // protected: for unit testing as well. 162 bool GetInputMethodIdsFromLanguageCodeInternal( 163 const std::multimap<std::string, std::string>& language_code_to_ids, 164 const std::string& normalized_language_code, 165 InputMethodType type, 166 std::vector<std::string>* out_input_method_ids) const; 167 168 // Gets the keyboard layout name from the given input method ID. 169 // If the ID is invalid, an empty string will be returned. 170 // This function only supports xkb layouts. 171 // 172 // Examples: 173 // 174 // "xkb:us::eng" => "us" 175 // "xkb:us:dvorak:eng" => "us(dvorak)" 176 // "xkb:gb::eng" => "gb" 177 // "pinyin" => "us" (because Pinyin uses US keyboard layout) 178 std::string GetKeyboardLayoutName(const std::string& input_method_id) const; 179 180 private: 181 bool TranslateStringInternal(const std::string& english_string, 182 base::string16 *out_string) const; 183 184 // Map from language code to associated input method IDs, etc. 185 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; 186 187 LanguageCodeToIdsMap language_code_to_ids_; 188 InputMethodIdToDescriptorMap id_to_descriptor_; 189 std::map<std::string, std::string> xkb_layout_to_indicator_; 190 191 typedef base::hash_map<std::string, int> HashType; 192 HashType english_to_resource_id_; 193 194 InputMethodDelegate* delegate_; 195 196 base::ThreadChecker thread_checker_; 197 std::vector<std::string> hardware_layouts_; 198 std::vector<std::string> hardware_login_layouts_; 199 std::vector<std::string> cached_hardware_layouts_; 200 201 DISALLOW_COPY_AND_ASSIGN(InputMethodUtil); 202 }; 203 204 } // namespace input_method 205 } // namespace chromeos 206 207 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ 208