diff options
author | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-21 09:00:42 +0000 |
---|---|---|
committer | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-21 09:00:42 +0000 |
commit | 0db3982e5f9d9f8ff43262fb00e0001ebf8c7897 (patch) | |
tree | 4e8625f24337ce345231a0d6ef58b044fcaecb92 /chrome/browser/chromeos | |
parent | 91390f66fe2393cb7842d2636dc8821950f7c5cb (diff) | |
download | chromium_src-0db3982e5f9d9f8ff43262fb00e0001ebf8c7897.zip chromium_src-0db3982e5f9d9f8ff43262fb00e0001ebf8c7897.tar.gz chromium_src-0db3982e5f9d9f8ff43262fb00e0001ebf8c7897.tar.bz2 |
Move more utility functions from language_config_model to input_method_util.
- Remove the map<string,string> argument from SortInputMethodIdsByNames() function. I'll use the reviced function to implement keyboard switching in the login window.
- Moved more utility functions from language_config_model to input_method_util.
BUG=none
TEST=trybot
Review URL: http://codereview.chromium.org/2816015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50338 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos')
6 files changed, 150 insertions, 139 deletions
diff --git a/chrome/browser/chromeos/input_method/input_method_util.cc b/chrome/browser/chromeos/input_method/input_method_util.cc index 9d7cf1e..14dc3f9 100644 --- a/chrome/browser/chromeos/input_method/input_method_util.cc +++ b/chrome/browser/chromeos/input_method/input_method_util.cc @@ -12,6 +12,7 @@ #include "base/basictypes.h" #include "base/hash_tables.h" #include "base/scoped_ptr.h" +#include "base/singleton.h" #include "base/utf_string_conversions.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_thread.h" @@ -22,9 +23,77 @@ namespace { -// Map from language code to associated input method IDs. +// Map from language code to associated input method IDs, etc. typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; -LanguageCodeToIdsMap* g_language_code_to_ids_map = NULL; +struct IdMaps { + LanguageCodeToIdsMap* language_code_to_ids; + std::map<std::string, std::string>* id_to_language_code; + std::map<std::string, std::string>* id_to_display_name; + + private: + IdMaps() : language_code_to_ids(NULL), + id_to_language_code(NULL), + id_to_display_name(NULL) { + chromeos::InputMethodLibrary* library = + chromeos::CrosLibrary::Get()->GetInputMethodLibrary(); + scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods( + library->GetSupportedInputMethods()); + if (supported_input_methods->size() <= 1) { + LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID"; + // TODO(yusukes): Handle this error in nicer way. + } + + language_code_to_ids = new LanguageCodeToIdsMap; + id_to_language_code = new std::map<std::string, std::string>; + id_to_display_name = new std::map<std::string, std::string>; + + // Build the id to descriptor map for handling kExtraLanguages later. + typedef std::map<std::string, + const chromeos::InputMethodDescriptor*> DescMap; + DescMap id_to_descriptor_map; + + for (size_t i = 0; i < supported_input_methods->size(); ++i) { + const chromeos::InputMethodDescriptor& input_method = + supported_input_methods->at(i); + const std::string language_code = + chromeos::input_method::GetLanguageCodeFromDescriptor(input_method); + AddInputMethodToMaps(language_code, input_method); + // Remember the pair. + id_to_descriptor_map.insert( + std::make_pair(input_method.id, &input_method)); + } + + // Go through the languages listed in kExtraLanguages. + using chromeos::input_method::kExtraLanguages; + for (size_t i = 0; i < arraysize(kExtraLanguages); ++i) { + const char* language_code = kExtraLanguages[i].language_code; + const char* input_method_id = kExtraLanguages[i].input_method_id; + DescMap::const_iterator iter = id_to_descriptor_map.find(input_method_id); + // If the associated input method descriptor is found, add the + // language code and the input method. + if (iter != id_to_descriptor_map.end()) { + const chromeos::InputMethodDescriptor& input_method = *(iter->second); + AddInputMethodToMaps(language_code, input_method); + } + } + } + + void AddInputMethodToMaps( + const std::string& language_code, + const chromeos::InputMethodDescriptor& input_method) { + language_code_to_ids->insert( + std::make_pair(language_code, input_method.id)); + id_to_language_code->insert( + std::make_pair(input_method.id, language_code)); + id_to_display_name->insert(std::make_pair( + input_method.id, + chromeos::input_method::GetStringUTF8(input_method.display_name))); + } + + friend struct DefaultSingletonTraits<IdMaps>; + + DISALLOW_COPY_AND_ASSIGN(IdMaps); +}; const struct EnglishToResouceId { const char* english_string_from_ibus; @@ -245,45 +314,6 @@ bool GetLocalizedString( return true; }; -// Initializes |g_language_code_to_ids_map| if necessary. -// Returns true on success. If this function returns true, it is guaranteed -// |g_language_code_to_ids_map| is non-NULL. The function might return false -// when ibus-daemon is not ready. -bool InitializeLanguageCodeToIdsMap() { - if (g_language_code_to_ids_map) { - return true; - } - - chromeos::InputMethodLibrary* library = - chromeos::CrosLibrary::Get()->GetInputMethodLibrary(); - scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods( - library->GetSupportedInputMethods()); - if (supported_input_methods->size() <= 1) { - // TODO(yusukes): Handle this error in nicer way. - LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID"; - return false; - } - - g_language_code_to_ids_map = new LanguageCodeToIdsMap; - for (size_t i = 0; i < supported_input_methods->size(); ++i) { - const std::string language_code = - chromeos::input_method::GetLanguageCodeFromDescriptor( - supported_input_methods->at(i)); - const std::string& input_method_id = supported_input_methods->at(i).id; - g_language_code_to_ids_map->insert( - std::make_pair(language_code, input_method_id)); - } - // Go through the languages listed in kExtraLanguages. - using chromeos::input_method::kExtraLanguages; - for (size_t i = 0; i < arraysize(kExtraLanguages); ++i) { - const char* language_code = kExtraLanguages[i].language_code; - const char* input_method_id = kExtraLanguages[i].input_method_id; - g_language_code_to_ids_map->insert( - std::make_pair(language_code, input_method_id)); - } - return true; -} - } // namespace namespace chromeos { @@ -409,6 +439,28 @@ std::wstring MaybeRewriteLanguageName(const std::wstring& language_name) { return language_name; } +std::string GetLanguageCodeFromInputMethodId( + const std::string& input_method_id) { + // The code should be compatible with one of codes used for UI languages, + // defined in app/l10_util.cc. + const char kDefaultLanguageCode[] = "en-US"; + std::map<std::string, std::string>::const_iterator iter + = Singleton<IdMaps>::get()->id_to_language_code->find(input_method_id); + return (iter == Singleton<IdMaps>::get()->id_to_language_code->end()) ? + // Returning |kDefaultLanguageCode| here is not for Chrome OS but for + // Ubuntu where the ibus-xkb-layouts engine could be missing. + kDefaultLanguageCode : iter->second; +} + +std::string GetInputMethodDisplayNameFromId( + const std::string& input_method_id) { + static const char kDefaultDisplayName[] = "USA"; + std::map<std::string, std::string>::const_iterator iter + = Singleton<IdMaps>::get()->id_to_display_name->find(input_method_id); + return (iter == Singleton<IdMaps>::get()->id_to_display_name->end()) ? + kDefaultDisplayName : iter->second; +} + std::wstring GetLanguageDisplayNameFromCode(const std::string& language_code) { if (!g_browser_process) { return L""; @@ -436,7 +488,12 @@ void SortLanguageCodesByNames(std::vector<std::string>* language_codes) { CompareLanguageCodesByLanguageName(collator.get())); } -void SortInputMethodIdsByNames( +void SortInputMethodIdsByNames(std::vector<std::string>* input_method_ids) { + SortInputMethodIdsByNamesInternal( + *(Singleton<IdMaps>::get()->id_to_language_code), input_method_ids); +} + +void SortInputMethodIdsByNamesInternal( const std::map<std::string, std::string>& id_to_language_code_map, std::vector<std::string>* input_method_ids) { if (!g_browser_process) { @@ -472,7 +529,7 @@ void ReorderInputMethodIdsForLanguageCode( } } -bool GetInputMethodIdsByLanguageCode( +bool GetInputMethodIdsFromLanguageCode( const std::string& normalized_language_code, bool keyboard_layout_only, std::vector<std::string>* out_input_method_ids) { @@ -480,22 +537,21 @@ bool GetInputMethodIdsByLanguageCode( out_input_method_ids->clear(); bool result = false; - if (InitializeLanguageCodeToIdsMap()) { - std::pair<LanguageCodeToIdsMap::const_iterator, - LanguageCodeToIdsMap::const_iterator> range = - g_language_code_to_ids_map->equal_range(normalized_language_code); - for (LanguageCodeToIdsMap::const_iterator iter = range.first; - iter != range.second; ++iter) { - const std::string& input_method_id = iter->second; - if ((!keyboard_layout_only) || IsKeyboardLayout(input_method_id)) { - out_input_method_ids->push_back(input_method_id); - result = true; - } - } - if (!result) { - LOG(ERROR) << "Unknown language code: " << normalized_language_code; + std::pair<LanguageCodeToIdsMap::const_iterator, + LanguageCodeToIdsMap::const_iterator> range = + Singleton<IdMaps>::get()->language_code_to_ids->equal_range( + normalized_language_code); + for (LanguageCodeToIdsMap::const_iterator iter = range.first; + iter != range.second; ++iter) { + const std::string& input_method_id = iter->second; + if ((!keyboard_layout_only) || IsKeyboardLayout(input_method_id)) { + out_input_method_ids->push_back(input_method_id); + result = true; } } + if (!result) { + LOG(ERROR) << "Unknown language code: " << normalized_language_code; + } return result; } diff --git a/chrome/browser/chromeos/input_method/input_method_util.h b/chrome/browser/chromeos/input_method/input_method_util.h index 77ebbd3..597ddec 100644 --- a/chrome/browser/chromeos/input_method/input_method_util.h +++ b/chrome/browser/chromeos/input_method/input_method_util.h @@ -77,6 +77,18 @@ std::string GetKeyboardLayoutName(const std::string& input_method_id); // methods that don't fall under any other languages. std::wstring MaybeRewriteLanguageName(const std::wstring& language_name); +// Converts an input method ID to a language code of the IME. Returns "Eng" +// when |input_method_id| is unknown. +// Example: "hangul" => "ko" +std::string GetLanguageCodeFromInputMethodId( + const std::string& input_method_id); + +// Converts an input method ID to a display name of the IME. Returns +// "USA" (US keyboard) when |input_method_id| is unknown. +// Examples: "pinyin" => "Pinyin" +// "m17n:ar:kbd" => "kbd (m17n)" +std::string GetInputMethodDisplayNameFromId(const std::string& input_method_id); + // Converts a language code to a language display name, using the // current application locale. MaybeRewriteLanguageName() is called // internally. @@ -90,7 +102,10 @@ void SortLanguageCodesByNames(std::vector<std::string>* language_codes); // Sorts the given input method ids by their corresponding language names, // using the unicode string comparator. Uses stable sorting. -void SortInputMethodIdsByNames( +void SortInputMethodIdsByNames(std::vector<std::string>* input_method_ids); + +// This function is only for unit tests. Do not use this. +void SortInputMethodIdsByNamesInternal( const std::map<std::string, std::string>& id_to_language_code_map, std::vector<std::string>* input_method_ids); @@ -108,7 +123,7 @@ void ReorderInputMethodIdsForLanguageCode( // that are not for keybord layout switching. Returns true on success. Note that // the function might return false if ibus-daemon is not running, or // |language_code| is unknown. -bool GetInputMethodIdsByLanguageCode( +bool GetInputMethodIdsFromLanguageCode( const std::string& language_code, bool keyboard_layout_only, std::vector<std::string>* out_input_method_ids); diff --git a/chrome/browser/chromeos/input_method/input_method_util_unittest.cc b/chrome/browser/chromeos/input_method/input_method_util_unittest.cc index 3c59b6a..0c1543d 100644 --- a/chrome/browser/chromeos/input_method/input_method_util_unittest.cc +++ b/chrome/browser/chromeos/input_method/input_method_util_unittest.cc @@ -102,7 +102,7 @@ TEST(LanguageConfigModelTest, SortLanguageCodesByNames) { ASSERT_EQ("t", language_codes[3]); // Others } -TEST(LanguageConfigModelTest, SortInputMethodIdsByNames) { +TEST(LanguageConfigModelTest, SortInputMethodIdsByNamesInternal) { std::map<std::string, std::string> id_to_language_code_map; id_to_language_code_map.insert(std::make_pair("mozc", "ja")); id_to_language_code_map.insert(std::make_pair("mozc-jp", "ja")); @@ -112,24 +112,24 @@ TEST(LanguageConfigModelTest, SortInputMethodIdsByNames) { std::vector<std::string> input_method_ids; // Check if this function can handle an empty list. - SortInputMethodIdsByNames(id_to_language_code_map, - &input_method_ids); + SortInputMethodIdsByNamesInternal(id_to_language_code_map, + &input_method_ids); input_method_ids.push_back("mozc"); // Japanese input_method_ids.push_back("xkb:fr::fra"); // French input_method_ids.push_back("m17n:latn-pre"); // Others - SortInputMethodIdsByNames(id_to_language_code_map, - &input_method_ids); + SortInputMethodIdsByNamesInternal(id_to_language_code_map, + &input_method_ids); ASSERT_EQ(3U, input_method_ids.size()); ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French ASSERT_EQ("mozc", input_method_ids[1]); // Japanese ASSERT_EQ("m17n:latn-pre", input_method_ids[2]); // Others // Add a duplicate entry and see if it works. - // Note that SortInputMethodIdsByNames uses std::stable_sort. + // Note that SortInputMethodIdsByNamesInternal uses std::stable_sort. input_method_ids.push_back("xkb:jp::jpn"); // also Japanese - SortInputMethodIdsByNames(id_to_language_code_map, - &input_method_ids); + SortInputMethodIdsByNamesInternal(id_to_language_code_map, + &input_method_ids); ASSERT_EQ(4U, input_method_ids.size()); ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French ASSERT_EQ("mozc", input_method_ids[1]); // Japanese @@ -137,8 +137,8 @@ TEST(LanguageConfigModelTest, SortInputMethodIdsByNames) { ASSERT_EQ("m17n:latn-pre", input_method_ids[3]); // Others input_method_ids.push_back("mozc-jp"); // also Japanese - SortInputMethodIdsByNames(id_to_language_code_map, - &input_method_ids); + SortInputMethodIdsByNamesInternal(id_to_language_code_map, + &input_method_ids); ASSERT_EQ(5U, input_method_ids.size()); ASSERT_EQ("xkb:fr::fra", input_method_ids[0]); // French ASSERT_EQ("mozc", input_method_ids[1]); // Japanese diff --git a/chrome/browser/chromeos/options/language_config_model.cc b/chrome/browser/chromeos/options/language_config_model.cc index b10ca2b..81f9946 100644 --- a/chrome/browser/chromeos/options/language_config_model.cc +++ b/chrome/browser/chromeos/options/language_config_model.cc @@ -19,14 +19,6 @@ namespace chromeos { -namespace { - -// The code should be compatible with one of codes used for UI languages, -// defined in app/l10_util.cc. -const char kDefaultLanguageCode[] = "en-US"; - -} // namespace - AddLanguageComboboxModel::AddLanguageComboboxModel( Profile* profile, const std::vector<std::string>& locale_codes) @@ -85,7 +77,7 @@ LanguageConfigModel::LanguageConfigModel(PrefService* pref_service) void LanguageConfigModel::Init() { // Initialize the maps and vectors. - InitInputMethodIdMapsAndVectors(); + InitInputMethodIdVectors(); preload_engines_.Init( prefs::kLanguagePreloadEngines, pref_service_, this); @@ -99,7 +91,7 @@ size_t LanguageConfigModel::CountNumActiveInputMethods( const std::string& language_code) { int num_selected_active_input_methods = 0; std::vector<std::string> input_method_ids; - input_method::GetInputMethodIdsByLanguageCode( + input_method::GetInputMethodIdsFromLanguageCode( language_code, false /* keyboard_layout_only */, &input_method_ids); for (size_t i = 0; i < input_method_ids.size(); ++i) { if (InputMethodIsActivated(input_method_ids[i])) { @@ -143,15 +135,14 @@ void LanguageConfigModel::UpdateInputMethodPreferences( // Note: Since |new_input_method_ids| is alphabetically sorted and the sort // function below uses stable sort, the relateve order of input methods that // belong to the same language (e.g. "mozc" and "xkb:jp::jpn") is maintained. - input_method::SortInputMethodIdsByNames( - id_to_language_code_map_, &new_input_method_ids); + input_method::SortInputMethodIdsByNames(&new_input_method_ids); preload_engines_.SetValue(UTF8ToWide(JoinString(new_input_method_ids, ','))); } void LanguageConfigModel::DeactivateInputMethodsFor( const std::string& language_code) { for (size_t i = 0; i < num_supported_input_method_ids(); ++i) { - if (GetLanguageCodeFromInputMethodId( + if (input_method::GetLanguageCodeFromInputMethodId( supported_input_method_id_at(i)) == language_code) { // What happens if we disable the input method currently active? @@ -202,32 +193,12 @@ void LanguageConfigModel::GetActiveInputMethodIds( } } -std::string LanguageConfigModel::GetLanguageCodeFromInputMethodId( - const std::string& input_method_id) const { - std::map<std::string, std::string>::const_iterator iter - = id_to_language_code_map_.find(input_method_id); - return (iter == id_to_language_code_map_.end()) ? - // Returning |kDefaultLanguageCode| is not for Chrome OS but for Ubuntu - // where the ibus-xkb-layouts module could be missing. - kDefaultLanguageCode : iter->second; -} - -std::string LanguageConfigModel::GetInputMethodDisplayNameFromId( - const std::string& input_method_id) const { - // |kDefaultDisplayName| is not for Chrome OS. See the comment above. - static const char kDefaultDisplayName[] = "USA"; - std::map<std::string, std::string>::const_iterator iter - = id_to_display_name_map_.find(input_method_id); - return (iter == id_to_display_name_map_.end()) ? - kDefaultDisplayName : iter->second; -} - void LanguageConfigModel::GetInputMethodIdsFromLanguageCode( const std::string& language_code, std::vector<std::string>* input_method_ids) const { DCHECK(input_method_ids); input_method_ids->clear(); - input_method::GetInputMethodIdsByLanguageCode( + input_method::GetInputMethodIdsFromLanguageCode( language_code, false /* keyboard_layout_only */, input_method_ids); // Reorder the input methods. @@ -242,7 +213,7 @@ void LanguageConfigModel::NotifyPrefChanged() { std::set<std::string> language_code_set; for (size_t i = 0; i < input_method_ids.size(); ++i) { const std::string language_code = - GetLanguageCodeFromInputMethodId(input_method_ids[i]); + input_method::GetLanguageCodeFromInputMethodId(input_method_ids[i]); language_code_set.insert(language_code); } @@ -260,7 +231,7 @@ void LanguageConfigModel::Observe(NotificationType type, } } -void LanguageConfigModel::InitInputMethodIdMapsAndVectors() { +void LanguageConfigModel::InitInputMethodIdVectors() { // The two sets are used to build lists without duplication. std::set<std::string> supported_language_code_set; std::set<std::string> supported_input_method_id_set; @@ -274,7 +245,6 @@ void LanguageConfigModel::InitInputMethodIdMapsAndVectors() { const InputMethodDescriptor& input_method = supported_input_methods->at(i); const std::string language_code = input_method::GetLanguageCodeFromDescriptor(input_method); - AddInputMethodToMaps(language_code, input_method); // Add the language code and the input method id to the sets. supported_language_code_set.insert(language_code); supported_input_method_id_set.insert(input_method.id); @@ -294,7 +264,6 @@ void LanguageConfigModel::InitInputMethodIdMapsAndVectors() { // language code and the input method. if (iter != id_to_descriptor_map.end()) { const InputMethodDescriptor& input_method = *(iter->second); - AddInputMethodToMaps(language_code, input_method); // Add the language code and the input method id to the sets. supported_language_code_set.insert(language_code); supported_input_method_id_set.insert(input_method.id); @@ -308,14 +277,4 @@ void LanguageConfigModel::InitInputMethodIdMapsAndVectors() { supported_input_method_id_set.end()); } -void LanguageConfigModel::AddInputMethodToMaps( - const std::string& language_code, - const InputMethodDescriptor& input_method) { - id_to_language_code_map_.insert( - std::make_pair(input_method.id, language_code)); - id_to_display_name_map_.insert( - std::make_pair(input_method.id, - input_method::GetStringUTF8(input_method.display_name))); -} - } // namespace chromeos diff --git a/chrome/browser/chromeos/options/language_config_model.h b/chrome/browser/chromeos/options/language_config_model.h index cb01b65..010f3125 100644 --- a/chrome/browser/chromeos/options/language_config_model.h +++ b/chrome/browser/chromeos/options/language_config_model.h @@ -84,19 +84,6 @@ class LanguageConfigModel : public NotificationObserver { void GetActiveInputMethodIds( std::vector<std::string>* out_input_method_ids); - // Converts an input method ID to a language code of the IME. Returns "Eng" - // when |input_method_id| is unknown. - // Example: "hangul" => "ko" - std::string GetLanguageCodeFromInputMethodId( - const std::string& input_method_id) const; - - // Converts an input method ID to a display name of the IME. Returns - // "USA" (US keyboard) when |input_method_id| is unknown. - // Examples: "pinyin" => "Pinyin" - // "m17n:ar:kbd" => "kbd (m17n)" - std::string GetInputMethodDisplayNameFromId( - const std::string& input_method_id) const; - // Gets the list of input method ids associated with the given language // code. The original contents of |input_method_ids| will be lost. void GetInputMethodIdsFromLanguageCode( @@ -135,18 +122,12 @@ class LanguageConfigModel : public NotificationObserver { private: // Initializes id_to_{code,display_name}_map_ maps, // as well as supported_{language_codes,input_method_ids}_ vectors. - void InitInputMethodIdMapsAndVectors(); - - // Adds the given language code and input method pair to the internal maps. - void AddInputMethodToMaps(const std::string& language_code, - const InputMethodDescriptor& input_method); + void InitInputMethodIdVectors(); PrefService* pref_service_; // The codes of the preferred languages. std::vector<std::string> preferred_language_codes_; StringPrefMember preload_engines_; - std::map<std::string, std::string> id_to_language_code_map_; - std::map<std::string, std::string> id_to_display_name_map_; // List of supported language codes like "en" and "ja". std::vector<std::string> supported_language_codes_; // List of supported IME IDs like "pinyin" and "m17n:ar:kbd". diff --git a/chrome/browser/chromeos/options/language_config_view.cc b/chrome/browser/chromeos/options/language_config_view.cc index e4cb32c..e89b85c 100644 --- a/chrome/browser/chromeos/options/language_config_view.cc +++ b/chrome/browser/chromeos/options/language_config_view.cc @@ -576,8 +576,8 @@ void LanguageConfigView::AddInputMethodSection( for (size_t i = 0; i < input_method_ids.size(); ++i) { const std::string& input_method_id = input_method_ids[i]; - const std::string display_name = model.GetInputMethodDisplayNameFromId( - input_method_id); + const std::string display_name = + input_method::GetInputMethodDisplayNameFromId(input_method_id); layout->StartRow(0, kPerLanguageDoubleColumnSetId); InputMethodCheckbox* checkbox = new InputMethodCheckbox(UTF8ToWide(display_name), |