| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/hash_tables.h" | 14 #include "base/hash_tables.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "chrome/browser/chromeos/input_method/input_method_descriptor.h" | 16 #include "chrome/browser/chromeos/input_method/input_method_descriptor.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 namespace input_method { | 19 namespace input_method { |
| 20 | 20 |
| 21 class InputMethodDelegate; |
| 22 |
| 21 // The list of language that do not have associated input methods in IBus. | 23 // The list of language that do not have associated input methods in IBus. |
| 22 // For these languages, we associate input methods here. | 24 // For these languages, we associate input methods here. |
| 23 struct ExtraLanguage { | 25 struct ExtraLanguage { |
| 24 const char* language_code; | 26 const char* language_code; |
| 25 const char* input_method_id; | 27 const char* input_method_id; |
| 26 }; | 28 }; |
| 27 extern const ExtraLanguage kExtraLanguages[]; | 29 extern const ExtraLanguage kExtraLanguages[]; |
| 28 extern const size_t kExtraLanguagesLength; | 30 extern const size_t kExtraLanguagesLength; |
| 29 | 31 |
| 30 enum InputMethodType { | 32 enum InputMethodType { |
| 31 kKeyboardLayoutsOnly, | 33 kKeyboardLayoutsOnly, |
| 32 kAllInputMethods, | 34 kAllInputMethods, |
| 33 }; | 35 }; |
| 34 | 36 |
| 35 // A class which provides miscellaneous input method utility functions. | 37 // A class which provides miscellaneous input method utility functions. |
| 36 class InputMethodUtil { | 38 class InputMethodUtil { |
| 37 public: | 39 public: |
| 38 // |supported_input_methods| is a list of all input methods supported, | 40 // |supported_input_methods| is a list of all input methods supported, |
| 39 // including ones not active. The list is used to initialize member variables | 41 // including ones not active. The list is used to initialize member variables |
| 40 // in this class. | 42 // in this class. |
| 41 explicit InputMethodUtil( | 43 InputMethodUtil(InputMethodDelegate* delegate, |
| 42 scoped_ptr<InputMethodDescriptors> supported_input_methods); | 44 scoped_ptr<InputMethodDescriptors> supported_input_methods); |
| 43 ~InputMethodUtil(); | 45 ~InputMethodUtil(); |
| 44 | 46 |
| 45 // Converts a string sent from IBus IME engines, which is written in English, | 47 // Converts a string sent from IBus IME engines, which is written in English, |
| 46 // into Chrome's string ID, then pulls internationalized resource string from | 48 // into Chrome's string ID, then pulls internationalized resource string from |
| 47 // the resource bundle and returns it. These functions are not thread-safe. | 49 // the resource bundle and returns it. These functions are not thread-safe. |
| 48 // Non-UI threads are not allowed to call them. | 50 // Non-UI threads are not allowed to call them. |
| 49 string16 TranslateString(const std::string& english_string) const; | 51 string16 TranslateString(const std::string& english_string) const; |
| 50 | 52 |
| 51 // Gets the keyboard layout name from the given input method ID. | 53 // Gets the keyboard layout name from the given input method ID. |
| 52 // If the ID is invalid, an empty string will be returned. | 54 // If the ID is invalid, an empty string will be returned. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 std::vector<std::string>* out_language_codes) const; | 124 std::vector<std::string>* out_language_codes) const; |
| 123 | 125 |
| 124 // Returns the input method ID of the hardware keyboard. e.g. "xkb:us::eng" | 126 // Returns the input method ID of the hardware keyboard. e.g. "xkb:us::eng" |
| 125 // for the US Qwerty keyboard. | 127 // for the US Qwerty keyboard. |
| 126 std::string GetHardwareInputMethodId() const; | 128 std::string GetHardwareInputMethodId() const; |
| 127 | 129 |
| 128 // This function should be called when Chrome's application locale is | 130 // This function should be called when Chrome's application locale is |
| 129 // changed, so that the internal maps of this library is reloaded. | 131 // changed, so that the internal maps of this library is reloaded. |
| 130 void OnLocaleChanged(); | 132 void OnLocaleChanged(); |
| 131 | 133 |
| 132 // Sets an input method ID of the hardware keyboard for testing. | |
| 133 void SetHardwareInputMethodIdForTesting(const std::string& input_method_id); | |
| 134 | |
| 135 // Returns true if the given input method id is supported. | 134 // Returns true if the given input method id is supported. |
| 136 bool IsValidInputMethodId(const std::string& input_method_id) const; | 135 bool IsValidInputMethodId(const std::string& input_method_id) const; |
| 137 | 136 |
| 138 // Returns true if the given input method id is for a keyboard layout. | 137 // Returns true if the given input method id is for a keyboard layout. |
| 139 static bool IsKeyboardLayout(const std::string& input_method_id); | 138 static bool IsKeyboardLayout(const std::string& input_method_id); |
| 140 | 139 |
| 141 // Returns true if the given input method id is for an extension input method. | 140 // Returns true if the given input method id is for an extension input method. |
| 142 static bool IsExtensionInputMethod(const std::string& input_method_id); | 141 static bool IsExtensionInputMethod(const std::string& input_method_id); |
| 143 | 142 |
| 144 // Converts a language code to a language display name, using the | 143 // Converts a language code to a language display name, using the |
| 145 // current application locale. MaybeRewriteLanguageName() is called | 144 // current application locale. MaybeRewriteLanguageName() is called |
| 146 // internally. | 145 // internally. |
| 147 // Examples: "fi" => "Finnish" | 146 // Examples: "fi" => "Finnish" |
| 148 // "en-US" => "English (United States)" | 147 // "en-US" => "English (United States)" |
| 149 static string16 GetLanguageDisplayNameFromCode( | 148 string16 GetLanguageDisplayNameFromCode( |
| 150 const std::string& language_code); | 149 const std::string& language_code); |
| 151 | 150 |
| 152 // Converts a language code to a language native display name. | 151 // Converts a language code to a language native display name. |
| 153 // MaybeRewriteLanguageName() is called internally. | 152 // MaybeRewriteLanguageName() is called internally. |
| 154 // Examples: "fi" => "suomi" (rather than Finnish) | 153 // Examples: "fi" => "suomi" (rather than Finnish) |
| 155 // "en-US" => "English (United States)" | 154 // "en-US" => "English (United States)" |
| 156 static string16 GetLanguageNativeDisplayNameFromCode( | 155 static string16 GetLanguageNativeDisplayNameFromCode( |
| 157 const std::string& language_code); | 156 const std::string& language_code); |
| 158 | 157 |
| 159 protected: | 158 protected: |
| 160 // This method is ONLY for unit testing. Returns true if the given string is | 159 // This method is ONLY for unit testing. Returns true if the given string is |
| 161 // supported (i.e. the string is associated with a resource ID). protected: | 160 // supported (i.e. the string is associated with a resource ID). protected: |
| 162 // for testability. | 161 // for testability. |
| 163 bool StringIsSupported(const std::string& english_string) const; | 162 bool StringIsSupported(const std::string& english_string) const; |
| 164 | 163 |
| 165 // protected: for unit testing as well. | 164 // protected: for unit testing as well. |
| 166 bool GetInputMethodIdsFromLanguageCodeInternal( | 165 bool GetInputMethodIdsFromLanguageCodeInternal( |
| 167 const std::multimap<std::string, std::string>& language_code_to_ids, | 166 const std::multimap<std::string, std::string>& language_code_to_ids, |
| 168 const std::string& normalized_language_code, | 167 const std::string& normalized_language_code, |
| 169 InputMethodType type, | 168 InputMethodType type, |
| 170 std::vector<std::string>* out_input_method_ids) const; | 169 std::vector<std::string>* out_input_method_ids) const; |
| 171 | 170 |
| 172 // protected: for unit testing as well. | 171 // protected: for unit testing as well. |
| 173 void ReloadInternalMaps(); | 172 void ReloadInternalMaps(); |
| 174 | 173 |
| 175 // Sorts the given language codes by their corresponding language names, using | 174 // Sorts the given language codes by their corresponding language names, using |
| 176 // the unicode string comparator. Uses unstable sorting. protected: for unit | 175 // the unicode string comparator. Uses unstable sorting. protected: for unit |
| 177 // testing as well. | 176 // testing as well. |
| 178 static void SortLanguageCodesByNames( | 177 void SortLanguageCodesByNames( |
| 179 std::vector<std::string>* language_codes); | 178 std::vector<std::string>* language_codes); |
| 180 | 179 |
| 181 // All input methods that are supported, including ones not active. | 180 // All input methods that are supported, including ones not active. |
| 182 // protected: for testing. | 181 // protected: for testing. |
| 183 scoped_ptr<InputMethodDescriptors> supported_input_methods_; | 182 scoped_ptr<InputMethodDescriptors> supported_input_methods_; |
| 184 | 183 |
| 185 private: | 184 private: |
| 186 bool TranslateStringInternal(const std::string& english_string, | 185 bool TranslateStringInternal(const std::string& english_string, |
| 187 string16 *out_string) const; | 186 string16 *out_string) const; |
| 188 | 187 |
| 189 // Map from language code to associated input method IDs, etc. | 188 // Map from language code to associated input method IDs, etc. |
| 190 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; | 189 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; |
| 191 // Map from input method ID to associated input method descriptor. | 190 // Map from input method ID to associated input method descriptor. |
| 192 typedef std::map< | 191 typedef std::map< |
| 193 std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap; | 192 std::string, InputMethodDescriptor> InputMethodIdToDescriptorMap; |
| 194 // Map from XKB layout ID to associated input method descriptor. | 193 // Map from XKB layout ID to associated input method descriptor. |
| 195 typedef std::map<std::string, InputMethodDescriptor> XkbIdToDescriptorMap; | 194 typedef std::map<std::string, InputMethodDescriptor> XkbIdToDescriptorMap; |
| 196 | 195 |
| 197 LanguageCodeToIdsMap language_code_to_ids_; | 196 LanguageCodeToIdsMap language_code_to_ids_; |
| 198 std::map<std::string, std::string> id_to_language_code_; | 197 std::map<std::string, std::string> id_to_language_code_; |
| 199 InputMethodIdToDescriptorMap id_to_descriptor_; | 198 InputMethodIdToDescriptorMap id_to_descriptor_; |
| 200 XkbIdToDescriptorMap xkb_id_to_descriptor_; | 199 XkbIdToDescriptorMap xkb_id_to_descriptor_; |
| 201 | 200 |
| 202 typedef base::hash_map<std::string, int> HashType; | 201 typedef base::hash_map<std::string, int> HashType; |
| 203 HashType english_to_resource_id_; | 202 HashType english_to_resource_id_; |
| 204 | 203 |
| 205 std::string hardware_input_method_id_for_testing_; | 204 InputMethodDelegate* delegate_; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(InputMethodUtil); |
| 206 }; | 207 }; |
| 207 | 208 |
| 208 } // namespace input_method | 209 } // namespace input_method |
| 209 } // namespace chromeos | 210 } // namespace chromeos |
| 210 | 211 |
| 211 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ | 212 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_ |
| OLD | NEW |