From ce50a96c9a161fb74d943e4f44ccc6582860680e Mon Sep 17 00:00:00 2001 From: "yusukes@google.com" Date: Thu, 8 Apr 2010 09:35:40 +0000 Subject: chromeos_language cleanup (Chrome part). - Stopped using deprecated APIs: GetSupportedLanguages, GetActiveLanguages, ChangeLanguage, and SetLanguageActivated. - Use new APIs insted: GetSupportedInputMethods, GetActiveInputMethods, ChangeInputMethod, and SetInputMethodActivated - Use "InputMethodDescriptor(s)" instead of "InputLanguage(List)" - Stopped using the word "IME" as much as possible. Use "input method" instead. - Removed all LANGUAGE_CATEGORY_XXXs. This change depends on the cros DEPS roll: http://codereview.chromium.org/1593014 BUG=crosbug.com/2403 TEST=ran browser_tests Review URL: http://codereview.chromium.org/1540019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43937 0039d316-1c4b-4281-b951-d872f2087c98 --- .../chromeos/cros/cros_in_process_browser_test.cc | 15 +-- .../chromeos/cros/cros_in_process_browser_test.h | 1 - chrome/browser/chromeos/cros/language_library.cc | 108 ++++++++++------ chrome/browser/chromeos/cros/language_library.h | 105 +++++++--------- .../browser/chromeos/cros/mock_language_library.h | 14 +-- chrome/browser/chromeos/language_preferences.h | 6 +- .../chromeos/options/language_config_view.cc | 48 +++---- .../chromeos/options/language_config_view.h | 8 +- chrome/browser/chromeos/preferences.cc | 27 ++-- .../chromeos/status/language_menu_button.cc | 139 ++++++++++----------- .../browser/chromeos/status/language_menu_button.h | 16 +-- 11 files changed, 250 insertions(+), 237 deletions(-) (limited to 'chrome/browser/chromeos') diff --git a/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc b/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc index 1c910b0..bdd3db4 100644 --- a/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc +++ b/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc @@ -16,9 +16,6 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/gmock/include/gmock/gmock.h" -static const char kDefaultXKBId[] = "USA"; -static const char kDefaultXKBDisplayName[] = "US"; - namespace chromeos { using ::testing::AnyNumber; using ::testing::InvokeWithoutArgs; @@ -100,13 +97,13 @@ void CrosInProcessBrowserTest::SetLanguageLibraryStatusAreaExpectations() { EXPECT_CALL(*mock_language_library_, AddObserver(_)) .Times(1) .RetiresOnSaturation(); - EXPECT_CALL(*mock_language_library_, GetActiveLanguages()) + EXPECT_CALL(*mock_language_library_, GetActiveInputMethods()) .Times(2) // Don't use WillRepeatedly since the fucntion should be evaluated twice. - .WillOnce(Return(CreateFallbackInputLanguageList())) - .WillOnce(Return(CreateFallbackInputLanguageList())) + .WillOnce(Return(CreateFallbackInputMethodDescriptors())) + .WillOnce(Return(CreateFallbackInputMethodDescriptors())) .RetiresOnSaturation(); - EXPECT_CALL(*mock_language_library_, SetLanguageActivated(_, _, _)) + EXPECT_CALL(*mock_language_library_, SetInputMethodActivated(_, _)) .Times(1) .WillOnce((Return(true))) .RetiresOnSaturation(); @@ -114,10 +111,6 @@ void CrosInProcessBrowserTest::SetLanguageLibraryStatusAreaExpectations() { .Times(1) .WillOnce((ReturnRef(ime_properties_))) .RetiresOnSaturation(); - EXPECT_CALL(*mock_language_library_, current_language()) - .Times(1) - .WillOnce((ReturnRef(language_))) - .RetiresOnSaturation(); EXPECT_CALL(*mock_language_library_, SetImeConfig(_, _, _)) .Times(2) .WillRepeatedly((Return(true))) diff --git a/chrome/browser/chromeos/cros/cros_in_process_browser_test.h b/chrome/browser/chromeos/cros/cros_in_process_browser_test.h index cbc102a..6e01bc2 100644 --- a/chrome/browser/chromeos/cros/cros_in_process_browser_test.h +++ b/chrome/browser/chromeos/cros/cros_in_process_browser_test.h @@ -74,7 +74,6 @@ class CrosInProcessBrowserTest : public InProcessBrowserTest { MockSynapticsLibrary* mock_synaptics_library_; ImePropertyList ime_properties_; - InputLanguage language_; private: DISALLOW_COPY_AND_ASSIGN(CrosInProcessBrowserTest); diff --git a/chrome/browser/chromeos/cros/language_library.cc b/chrome/browser/chromeos/cros/language_library.cc index fc2667e..b70ab60 100644 --- a/chrome/browser/chromeos/cros/language_library.cc +++ b/chrome/browser/chromeos/cros/language_library.cc @@ -65,7 +65,9 @@ bool LanguageLibrary::IsKeyboardLayout( return StartsWithASCII(input_method_id, "xkb:", case_insensitive); } -LanguageLibraryImpl::LanguageLibraryImpl() : language_status_connection_(NULL) { +LanguageLibraryImpl::LanguageLibraryImpl() + : language_status_connection_(NULL), + current_input_method_("", "", "") { } LanguageLibraryImpl::~LanguageLibraryImpl() { @@ -85,26 +87,55 @@ void LanguageLibraryImpl::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } -chromeos::InputLanguageList* LanguageLibraryImpl::GetActiveLanguages() { - chromeos::InputLanguageList* result = NULL; +namespace { + +// Removes "US" input method. Older (v18 and earlier) cros might add the dummy +// input method to |input_methods|, but we don't need it anymore. +// TODO(yusukes): Remove this function when we finish migrating to v20. +void RemoveDummyInputMethod(chromeos::InputMethodDescriptors* input_methods) { + chromeos::InputMethodDescriptors::iterator iter; + for (iter = input_methods->begin(); iter != input_methods->end(); ++iter) { + if ((iter->id == "USA") && (input_methods->size() > 1)) { + input_methods->erase(iter); + return; + } + } +} + +} // namespace + +chromeos::InputMethodDescriptors* LanguageLibraryImpl::GetActiveInputMethods() { + chromeos::InputMethodDescriptors* result = NULL; if (EnsureLoadedAndStarted()) { - result = chromeos::GetActiveLanguages(language_status_connection_); + result = chromeos::GetActiveInputMethods(language_status_connection_); + } + if (!result) { + result = CreateFallbackInputMethodDescriptors(); } - return result ? result : CreateFallbackInputLanguageList(); + // TODO(yusukes): Remove this hack. + RemoveDummyInputMethod(result); + return result; } -chromeos::InputLanguageList* LanguageLibraryImpl::GetSupportedLanguages() { - chromeos::InputLanguageList* result = NULL; +chromeos::InputMethodDescriptors* +LanguageLibraryImpl::GetSupportedInputMethods() { + chromeos::InputMethodDescriptors* result = NULL; if (EnsureLoadedAndStarted()) { - result = chromeos::GetSupportedLanguages(language_status_connection_); + result = chromeos::GetSupportedInputMethods(language_status_connection_); + } + if (!result) { + result = CreateFallbackInputMethodDescriptors(); } - return result ? result : CreateFallbackInputLanguageList(); + // TODO(yusukes): Remove this hack. + RemoveDummyInputMethod(result); + return result; } -void LanguageLibraryImpl::ChangeLanguage( - LanguageCategory category, const std::string& id) { +void LanguageLibraryImpl::ChangeInputMethod( + const std::string& input_method_id) { if (EnsureLoadedAndStarted()) { - chromeos::ChangeLanguage(language_status_connection_, category, id.c_str()); + chromeos::ChangeInputMethod( + language_status_connection_, input_method_id.c_str()); } } @@ -117,23 +148,23 @@ void LanguageLibraryImpl::SetImePropertyActivated(const std::string& key, } } -bool LanguageLibraryImpl::SetLanguageActivated( - LanguageCategory category, const std::string& id, bool activated) { +bool LanguageLibraryImpl::SetInputMethodActivated( + const std::string& input_method_id, bool activated) { bool success = false; if (EnsureLoadedAndStarted()) { - success = chromeos::SetLanguageActivated(language_status_connection_, - category, id.c_str(), activated); + success = chromeos::SetInputMethodActivated(language_status_connection_, + input_method_id.c_str(), + activated); } return success; } -bool LanguageLibraryImpl::LanguageIsActivated( - LanguageCategory category, const std::string& id) { - scoped_ptr active_language_list( - CrosLibrary::Get()->GetLanguageLibrary()->GetActiveLanguages()); - for (size_t i = 0; i < active_language_list->size(); ++i) { - if (active_language_list->at(i).category == category && - active_language_list->at(i).id == id) { +bool LanguageLibraryImpl::InputMethodIsActivated( + const std::string& input_method_id) { + scoped_ptr active_input_method_descriptors( + CrosLibrary::Get()->GetLanguageLibrary()->GetActiveInputMethods()); + for (size_t i = 0; i < active_input_method_descriptors->size(); ++i) { + if (active_input_method_descriptors->at(i).id == input_method_id) { return true; } } @@ -161,11 +192,11 @@ bool LanguageLibraryImpl::SetImeConfig( } // static -void LanguageLibraryImpl::LanguageChangedHandler( - void* object, const chromeos::InputLanguage& current_language) { +void LanguageLibraryImpl::InputMethodChangedHandler( + void* object, const chromeos::InputMethodDescriptor& current_input_method) { LanguageLibraryImpl* language_library = static_cast(object); - language_library->UpdateCurrentLanguage(current_language); + language_library->UpdateCurrentInputMethod(current_input_method); } // static @@ -194,7 +225,7 @@ bool LanguageLibraryImpl::EnsureStarted() { chromeos::DisconnectLanguageStatus(language_status_connection_); } chromeos::LanguageStatusMonitorFunctions monitor_functions; - monitor_functions.current_language = &LanguageChangedHandler; + monitor_functions.current_language = &InputMethodChangedHandler; monitor_functions.register_ime_properties = &RegisterPropertiesHandler; monitor_functions.update_ime_property = &UpdatePropertyHandler; language_status_connection_ @@ -207,26 +238,26 @@ bool LanguageLibraryImpl::EnsureLoadedAndStarted() { EnsureStarted(); } -void LanguageLibraryImpl::UpdateCurrentLanguage( - const chromeos::InputLanguage& current_language) { +void LanguageLibraryImpl::UpdateCurrentInputMethod( + const chromeos::InputMethodDescriptor& current_input_method) { // Make sure we run on UI thread. if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { - DLOG(INFO) << "UpdateCurrentLanguage (Background thread)"; + DLOG(INFO) << "UpdateCurrentInputMethod (Background thread)"; ChromeThread::PostTask( ChromeThread::UI, FROM_HERE, - // NewRunnableMethod() copies |current_language| by value. + // NewRunnableMethod() copies |current_input_method| by value. NewRunnableMethod( - this, &LanguageLibraryImpl::UpdateCurrentLanguage, - current_language)); + this, &LanguageLibraryImpl::UpdateCurrentInputMethod, + current_input_method)); return; } - DLOG(INFO) << "UpdateCurrentLanguage (UI thread)"; + DLOG(INFO) << "UpdateCurrentInputMethod (UI thread)"; const char kDefaultLayout[] = "us"; - if (IsKeyboardLayout(current_language.id)) { + if (IsKeyboardLayout(current_input_method.id)) { // If the new input method is a keyboard layout, switch the keyboard. std::vector portions; - SplitString(current_language.id, ':', &portions); + SplitString(current_input_method.id, ':', &portions); const std::string keyboard_layout = (portions.size() > 1 && !portions[1].empty() ? portions[1] : kDefaultLayout); @@ -237,8 +268,9 @@ void LanguageLibraryImpl::UpdateCurrentLanguage( // keyboard, such as a Japanese keyboard? We need to rework this. chromeos::SetCurrentKeyboardLayoutByName(kDefaultLayout); } - current_language_ = current_language; - FOR_EACH_OBSERVER(Observer, observers_, LanguageChanged(this)); + + current_input_method_ = current_input_method; + FOR_EACH_OBSERVER(Observer, observers_, InputMethodChanged(this)); } void LanguageLibraryImpl::RegisterProperties(const ImePropertyList& prop_list) { diff --git a/chrome/browser/chromeos/cros/language_library.h b/chrome/browser/chromeos/cros/language_library.h index 08a4111..853283a 100644 --- a/chrome/browser/chromeos/cros/language_library.h +++ b/chrome/browser/chromeos/cros/language_library.h @@ -21,7 +21,7 @@ class LanguageLibrary { class Observer { public: virtual ~Observer() = 0; - virtual void LanguageChanged(LanguageLibrary* obj) = 0; + virtual void InputMethodChanged(LanguageLibrary* obj) = 0; virtual void ImePropertiesChanged(LanguageLibrary* obj) = 0; }; virtual ~LanguageLibrary() {} @@ -29,27 +29,20 @@ class LanguageLibrary { virtual void AddObserver(Observer* observer) = 0; virtual void RemoveObserver(Observer* observer) = 0; - // Returns the list of IMEs and keyboard layouts we can select - // (i.e. active). If the cros library is not found or IBus/DBus daemon - // is not alive, this function returns a fallback language list (and - // never returns NULL). - virtual InputLanguageList* GetActiveLanguages() = 0; - - // Returns the list of IMEs and keyboard layouts we support, including - // ones not active. If the cros library is not found or IBus/DBus - // daemon is not alive, this function returns a fallback language list - // (and never returns NULL). - virtual InputLanguageList* GetSupportedLanguages() = 0; - - // Changes the current IME engine to |id| and enable IME (when |category| - // is LANGUAGE_CATEGORY_IME). Changes the current XKB layout to |id| and - // disable IME (when |category| is LANGUAGE_CATEGORY_XKB). |id| is a unique - // identifier of a IME engine or XKB layout. Please check chromeos_language.h - // in src third_party/cros/ for details. - virtual void ChangeLanguage(LanguageCategory category, - const std::string& id) = 0; - - // Sets whether the IME property specified by |key| is activated. If + // Returns the list of input methods we can select (i.e. active). If the cros + // library is not found or IBus/DBus daemon is not alive, this function + // returns a fallback input method list (and never returns NULL). + virtual InputMethodDescriptors* GetActiveInputMethods() = 0; + + // Returns the list of input methods we support, including ones not active. + // If the cros library is not found or IBus/DBus daemon is not alive, this + // function returns a fallback input method list (and never returns NULL). + virtual InputMethodDescriptors* GetSupportedInputMethods() = 0; + + // Changes the current input method to |input_method_id|. + virtual void ChangeInputMethod(const std::string& input_method_id) = 0; + + // Sets whether the input method property specified by |key| is activated. If // |activated| is true, activates the property. If |activate| is false, // deactivates the property. Examples of keys: // - "InputMode.Katakana" @@ -59,16 +52,14 @@ class LanguageLibrary { virtual void SetImePropertyActivated(const std::string& key, bool activated) = 0; - // Sets whether the language specified by |category| and |id| is - // activated. If |activated| is true, activates the language. If - // |activate| is false, deactivates the language. - virtual bool SetLanguageActivated(LanguageCategory category, - const std::string& id, - bool activated) = 0; + // Sets whether the input method specified by |input_method_id| is + // activated. If |activated| is true, activates the input method. If + // |activate| is false, deactivates the input method. + virtual bool SetInputMethodActivated(const std::string& input_method_id, + bool activated) = 0; - // Returns true if the language specified by |category| and |id| is active. - virtual bool LanguageIsActivated(LanguageCategory category, - const std::string& id) = 0; + // Returns true if the input method specified by |input_method_id| is active. + virtual bool InputMethodIsActivated(const std::string& input_method_id) = 0; // Get a configuration of ibus-daemon or IBus engines and stores it on // |out_value|. Returns true if |out_value| is successfully updated. @@ -86,7 +77,7 @@ class LanguageLibrary { const char* config_name, const ImeConfigValue& value) = 0; - virtual const InputLanguage& current_language() const = 0; + virtual const InputMethodDescriptor& current_input_method() const = 0; virtual const ImePropertyList& current_ime_properties() const = 0; @@ -111,24 +102,22 @@ class LanguageLibraryImpl : public LanguageLibrary { // LanguageLibrary overrides. virtual void AddObserver(Observer* observer); virtual void RemoveObserver(Observer* observer); - virtual InputLanguageList* GetActiveLanguages(); - virtual InputLanguageList* GetSupportedLanguages(); - virtual void ChangeLanguage(LanguageCategory category, const std::string& id); + virtual InputMethodDescriptors* GetActiveInputMethods(); + virtual InputMethodDescriptors* GetSupportedInputMethods(); + virtual void ChangeInputMethod(const std::string& input_method_id); virtual void SetImePropertyActivated(const std::string& key, bool activated); - virtual bool SetLanguageActivated(LanguageCategory category, - const std::string& id, - bool activated); - virtual bool LanguageIsActivated(LanguageCategory category, - const std::string& id); + virtual bool SetInputMethodActivated(const std::string& input_method_id, + bool activated); + virtual bool InputMethodIsActivated(const std::string& input_method_id); virtual bool GetImeConfig( const char* section, const char* config_name, ImeConfigValue* out_value); virtual bool SetImeConfig(const char* section, const char* config_name, const ImeConfigValue& value); - virtual const InputLanguage& current_language() const { - return current_language_; + virtual const InputMethodDescriptor& current_input_method() const { + return current_input_method_; } virtual const ImePropertyList& current_ime_properties() const { @@ -136,19 +125,20 @@ class LanguageLibraryImpl : public LanguageLibrary { } private: - // This method is called when there's a change in language status. - static void LanguageChangedHandler( - void* object, const InputLanguage& current_language); + // This method is called when there's a change in input method status. + static void InputMethodChangedHandler( + void* object, const InputMethodDescriptor& current_input_method); - // This method is called when an IME engine sends "RegisterProperties" signal. + // This method is called when an input method sends "RegisterProperties" + // signal. static void RegisterPropertiesHandler( void* object, const ImePropertyList& prop_list); - // This method is called when an IME engine sends "UpdateProperty" signal. + // This method is called when an input method sends "UpdateProperty" signal. static void UpdatePropertyHandler( void* object, const ImePropertyList& prop_list); - // Ensures that the monitoring of language changes is started. Starts + // Ensures that the monitoring of input method changes is started. Starts // the monitoring if necessary. Returns true if the monitoring has been // successfully started. bool EnsureStarted(); @@ -158,26 +148,27 @@ class LanguageLibraryImpl : public LanguageLibrary { // necessary. Returns true if the two conditions are both met. bool EnsureLoadedAndStarted(); - // Called by the handler to update the language status. + // Called by the handler to update the input method status. // This will notify all the Observers. - void UpdateCurrentLanguage(const InputLanguage& current_language); + void UpdateCurrentInputMethod( + const InputMethodDescriptor& current_input_method); - // Called by the handler to register IME properties. + // Called by the handler to register input method properties. void RegisterProperties(const ImePropertyList& prop_list); - // Called by the handler to update IME properties. + // Called by the handler to update input method properties. void UpdateProperty(const ImePropertyList& prop_list); - // A reference to the language api, to allow callbacks when the language + // A reference to the language api, to allow callbacks when the input method // status changes. LanguageStatusConnection* language_status_connection_; ObserverList observers_; - // The language (IME or XKB layout) which currently selected. - InputLanguage current_language_; + // The input method which is currently selected. + InputMethodDescriptor current_input_method_; - // The IME properties which the current IME engine uses. The list might be - // empty when no IME is used. + // The input method properties which the current input method uses. The list + // might be empty when no input method is used. ImePropertyList current_ime_properties_; DISALLOW_COPY_AND_ASSIGN(LanguageLibraryImpl); diff --git a/chrome/browser/chromeos/cros/mock_language_library.h b/chrome/browser/chromeos/cros/mock_language_library.h index bcc5f61..cdc974e 100644 --- a/chrome/browser/chromeos/cros/mock_language_library.h +++ b/chrome/browser/chromeos/cros/mock_language_library.h @@ -20,18 +20,16 @@ class MockLanguageLibrary : public LanguageLibrary { MOCK_METHOD1(AddObserver, void(Observer*)); MOCK_METHOD1(RemoveObserver, void(Observer*)); - MOCK_METHOD0(GetActiveLanguages, InputLanguageList*(void)); - MOCK_METHOD0(GetSupportedLanguages, InputLanguageList*(void)); - MOCK_METHOD2(ChangeLanguage, void(LanguageCategory, const std::string&)); + MOCK_METHOD0(GetActiveInputMethods, InputMethodDescriptors*(void)); + MOCK_METHOD0(GetSupportedInputMethods, InputMethodDescriptors*(void)); + MOCK_METHOD1(ChangeInputMethod, void(const std::string&)); MOCK_METHOD2(SetImePropertyActivated, void(const std::string&, bool)); - MOCK_METHOD3(SetLanguageActivated, - bool(LanguageCategory, const std::string&, bool)); - MOCK_METHOD2(LanguageIsActivated, - bool(LanguageCategory, const std::string&)); + MOCK_METHOD2(SetInputMethodActivated, bool(const std::string&, bool)); + MOCK_METHOD1(InputMethodIsActivated, bool(const std::string&)); MOCK_METHOD3(GetImeConfig, bool(const char*, const char*, ImeConfigValue*)); MOCK_METHOD3(SetImeConfig, bool(const char*, const char*, const ImeConfigValue&)); - MOCK_CONST_METHOD0(current_language, const InputLanguage&(void)); + MOCK_CONST_METHOD0(current_input_method, const InputMethodDescriptor&(void)); MOCK_CONST_METHOD0(current_ime_properties, const ImePropertyList&(void)); }; diff --git a/chrome/browser/chromeos/language_preferences.h b/chrome/browser/chromeos/language_preferences.h index a98986b..78c90cd 100644 --- a/chrome/browser/chromeos/language_preferences.h +++ b/chrome/browser/chromeos/language_preferences.h @@ -18,13 +18,11 @@ const struct HangulKeyboardNameIDPair { { L"Sebeolsik 390", L"39" }, { L"Sebeolsik No-shift", L"3s" }, { L"Sebeolsik 2 set", L"32" }, - // TODO(yusukes): Use generated_resources.grd IDs for |keyboard_name|. + // TODO(yusukes): Use generated_resources.grd IDs for |keyboard_name|. Ask + // jshin first. }; // For ibus-daemon -// TODO(yusukes): replace "USA" with "xkb:..." when ibus-xkb-layout gets ready. -const wchar_t kDefaultPreloadEngine[] = L"USA"; - // For Simplified Chinese IME (ibus-pinyin) // For Traditional Chinese IME (ibus-chewing) // For Japanese IME (ibus-google-japanese-input) diff --git a/chrome/browser/chromeos/options/language_config_view.cc b/chrome/browser/chromeos/options/language_config_view.cc index bcfbc46..4d4c2ce 100644 --- a/chrome/browser/chromeos/options/language_config_view.cc +++ b/chrome/browser/chromeos/options/language_config_view.cc @@ -35,6 +35,8 @@ using views::GridLayout; namespace { +const char kDefaultLanguageCode[] = "eng"; + // Creates the LanguageHangulConfigView. The function is used to create // the object via a function pointer. See also InitInputMethodConfigViewMap(). views::DialogDelegate* CreateLanguageHangulConfigView(Profile* profile) { @@ -230,11 +232,7 @@ void LanguageConfigView::ButtonPressed( InputMethodRadioButton* radio_button = static_cast(sender); const std::string& input_method_id = radio_button->input_method_id(); - if (input_method_id == "USA") { - // For the time being, we don't allow users to disable "USA" layout. - // TODO(yusukes): remove this hack when XKB switcher gets ready. - radio_button->SetChecked(true); - } else if (radio_button->checked()) { + if (radio_button->checked()) { // Deactivate all input methods first, then activate one that checked. DeactivateInputMethodsFor(GetLanguageCodeFromId(input_method_id)); SetInputMethodActivated(input_method_id, true); @@ -373,8 +371,7 @@ void LanguageConfigView::OnSelectionChanged() { const int row = preferred_language_table_->GetFirstSelectedRow(); const std::string& language_code = preferred_language_codes_[row]; // TODO(satorux): For now, don't allow users to remove English. - // TODO(yusukes): "eng" should be changed to "xkb:en" or something like that. - if (language_code == "eng") { + if (language_code == kDefaultLanguageCode) { remove_language_button_->SetEnabled(false); } else { remove_language_button_->SetEnabled(true); @@ -419,7 +416,7 @@ void LanguageConfigView::InitControlLayout() { preload_engines_.Init( prefs::kLanguagePreloadEngines, profile()->GetPrefs(), this); // TODO(yusukes): It might be safer to call GetActiveLanguages() cros API - // here and compare the result and preload_engines_GetValue(). If there's + // here and compare the result and preload_engines_.GetValue(). If there's // a discrepancy between IBus setting and Chrome prefs, we can resolve it // by calling preload_engines_SetValue() here. @@ -468,13 +465,11 @@ void LanguageConfigView::InitInputMethodConfigViewMap() { } void LanguageConfigView::InitInputMethodIdMaps() { - // TODO(satorux): Use GetSupportedInputMethods() instead. // GetSupportedLanguages() never return NULL. - scoped_ptr supported_input_methods( - CrosLibrary::Get()->GetLanguageLibrary()->GetSupportedLanguages()); + scoped_ptr supported_input_methods( + CrosLibrary::Get()->GetLanguageLibrary()->GetSupportedInputMethods()); for (size_t i = 0; i < supported_input_methods->size(); ++i) { - // TODO(satorux): Use InputMethodDescriptor instead. - const InputLanguage& input_method = supported_input_methods->at(i); + const InputMethodDescriptor& input_method = supported_input_methods->at(i); // Normalize the language code as some engines return three-letter // codes like "jpn" wheres some other engines return two-letter codes // like "ja". @@ -592,9 +587,9 @@ void LanguageConfigView::DeactivateInputMethodsFor( } // Switch back to the US English. - // TODO(yusukes): what if "USA" is not active? - CrosLibrary::Get()->GetLanguageLibrary()->ChangeLanguage( - chromeos::LANGUAGE_CATEGORY_XKB, "USA"); + // TODO(yusukes): what if the fallback input method is not active? + CrosLibrary::Get()->GetLanguageLibrary()->ChangeInputMethod( + kFallbackInputMethodId); } views::DialogDelegate* LanguageConfigView::CreateInputMethodConfigureView( @@ -622,17 +617,18 @@ void LanguageConfigView::SetInputMethodActivated( std::vector input_method_ids; GetActiveInputMethodIds(&input_method_ids); - std::set id_set(input_method_ids.begin(), - input_method_ids.end()); + std::set input_method_id_set(input_method_ids.begin(), + input_method_ids.end()); if (activated) { // Add |id| if it's not already added. - id_set.insert(input_method_id); + input_method_id_set.insert(input_method_id); } else { - id_set.erase(input_method_id); + input_method_id_set.erase(input_method_id); } // Update Chrome's preference. - std::vector new_input_method_ids(id_set.begin(), id_set.end()); + std::vector new_input_method_ids(input_method_id_set.begin(), + input_method_id_set.end()); preload_engines_.SetValue(UTF8ToWide(JoinString(new_input_method_ids, ','))); } @@ -680,14 +676,20 @@ std::string LanguageConfigView::GetLanguageCodeFromId( const std::string& input_method_id) const { std::map::const_iterator iter = id_to_language_code_map_.find(input_method_id); - return (iter == id_to_language_code_map_.end()) ? "" : iter->second; + 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 LanguageConfigView::GetDisplayNameFromId( const std::string& input_method_id) const { + // |kDefaultDisplayName| is not for Chrome OS. See the comment above. + static const char kDefaultDisplayName[] = "English"; std::map::const_iterator iter = id_to_display_name_map_.find(input_method_id); - return (iter == id_to_display_name_map_.end()) ? "" : iter->second; + return (iter == id_to_display_name_map_.end()) ? + kDefaultDisplayName : iter->second; } void LanguageConfigView::NotifyPrefChanged() { diff --git a/chrome/browser/chromeos/options/language_config_view.h b/chrome/browser/chromeos/options/language_config_view.h index ac3c170..54e2c7f 100644 --- a/chrome/browser/chromeos/options/language_config_view.h +++ b/chrome/browser/chromeos/options/language_config_view.h @@ -121,13 +121,13 @@ class LanguageConfigView : public TableModel, void GetSupportedInputMethodIds( std::vector* out_input_method_ids) const; - // Converts an input method ID to a language code of the IME. Returns "" when - // |input_method_id| is unknown. + // 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 GetLanguageCodeFromId(const std::string& input_method_id) const; - // Converts an input method ID to a display name of the IME. Returns "" when - // |input_method_id| is unknown. + // Converts an input method ID to a display name of the IME. Returns "English" + // when |input_method_id| is unknown. // Examples: "pinyin" => "Pinyin" // "m17n:ar:kbd" => "kbd (m17n)" std::string GetDisplayNameFromId(const std::string& input_method_id) const; diff --git a/chrome/browser/chromeos/preferences.cc b/chrome/browser/chromeos/preferences.cc index 6009748..d245f29 100644 --- a/chrome/browser/chromeos/preferences.cc +++ b/chrome/browser/chromeos/preferences.cc @@ -37,7 +37,7 @@ void Preferences::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterIntegerPref(prefs::kTouchpadSensitivity, 5); prefs->RegisterBooleanPref(prefs::kLanguageUseGlobalEngine, true); prefs->RegisterStringPref(prefs::kLanguagePreloadEngines, - kDefaultPreloadEngine); + UTF8ToWide(kFallbackInputMethodId)); // EN layout prefs->RegisterStringPref(prefs::kLanguageHangulKeyboard, kHangulKeyboardNameIDPairs[0].keyboard_id); } @@ -122,24 +122,25 @@ void Preferences::SetPreloadEngines(const std::wstring& value) { // could accept the comma separated |value| as-is. LanguageLibrary* library = CrosLibrary::Get()->GetLanguageLibrary(); - std::vector engine_ids; - SplitString(value, L',', &engine_ids); + std::vector input_method_ids; + SplitString(value, L',', &input_method_ids); LOG(INFO) << "Setting preload_engines to '" << value << "'"; // Activate languages in |value|. - for (size_t i = 0; i < engine_ids.size(); ++i) { - library->SetLanguageActivated( - LANGUAGE_CATEGORY_IME, WideToUTF8(engine_ids[i]), true); + for (size_t i = 0; i < input_method_ids.size(); ++i) { + library->SetInputMethodActivated(WideToUTF8(input_method_ids[i]), true); } // Deactivate languages that are currently active, but are not in |value|. - const std::set id_set(engine_ids.begin(), engine_ids.end()); - scoped_ptr active_engines(library->GetActiveLanguages()); - for (size_t i = 0; i < active_engines->size(); ++i) { - const InputLanguage& active_engine = active_engines->at(i); - if (id_set.count(UTF8ToWide(active_engine.id)) == 0) { - library->SetLanguageActivated( - active_engine.category, active_engine.id, false); + const std::set input_method_id_set(input_method_ids.begin(), + input_method_ids.end()); + scoped_ptr active_input_methods( + library->GetActiveInputMethods()); + for (size_t i = 0; i < active_input_methods->size(); ++i) { + const InputMethodDescriptor& active_input_method + = active_input_methods->at(i); + if (input_method_id_set.count(UTF8ToWide(active_input_method.id)) == 0) { + library->SetInputMethodActivated(active_input_method.id, false); } } } diff --git a/chrome/browser/chromeos/status/language_menu_button.cc b/chrome/browser/chromeos/status/language_menu_button.cc index 85b3d9a..8a36c26 100644 --- a/chrome/browser/chromeos/status/language_menu_button.cc +++ b/chrome/browser/chromeos/status/language_menu_button.cc @@ -19,17 +19,16 @@ // The language menu consists of 3 parts (in this order): // -// (1) XKB layout names and IME languages names. The size of the list is -// always >= 1. -// (2) IME properties. This list might be empty. +// (1) input method names. The size of the list is always >= 1. +// (2) input method properties. This list might be empty. // (3) "Configure IME..." button. // // Example of the menu (Japanese): // // ============================== (border of the popup window) -// [ ] US (|index| in the following functions is 0) -// [*] Anthy -// [ ] PinYin +// [ ] English (|index| in the following functions is 0) +// [*] Japanese +// [ ] Chinese (Simplified) // ------------------------------ (separator) // [*] Hiragana (index = 5, The property has 2 radio groups) // [ ] Katakana @@ -43,9 +42,9 @@ // Example of the menu (Simplified Chinese): // // ============================== (border of the popup window) -// [ ] US -// [ ] Anthy -// [*] PinYin +// [ ] English +// [ ] Japanese +// [*] Chinese (Simplified) // ------------------------------ (separator) // Switch to full letter mode (The property has 2 command buttons) // Switch to half punctuation mode @@ -58,59 +57,58 @@ namespace { // Constants to specify the type of items in |model_|. enum { - COMMAND_ID_LANGUAGES = 0, // US, Anthy, PinYin, ... + COMMAND_ID_INPUT_METHODS = 0, // US, Anthy, PinYin, ... COMMAND_ID_IME_PROPERTIES, // Hiragana, Katakana, ... COMMAND_ID_CONFIGURE_IME, // The "Configure IME..." button. }; // A group ID for IME properties starts from 0. We use the huge value for the -// XKB/IME language list to avoid conflict. +// input method list to avoid conflict. const int kRadioGroupLanguage = 1 << 16; const int kRadioGroupNone = -1; const size_t kMaxLanguageNameLen = 3; const wchar_t kSpacer[] = L"MMMMMMM"; -// Returns the language name for the given |language|. Instead of input +// Returns the language name for the given |input_method|. Instead of input // method names like "Pinyin" and "Anthy", we'll show language names like // "Chinese (Simplified)" and "Japanese". -std::wstring GetLanguageName(const chromeos::InputLanguage& language) { - std::string language_code = language.language_code; - if (language.id == "pinyin") { +std::wstring GetLanguageName( + const chromeos::InputMethodDescriptor& input_method) { + std::string language_code = input_method.language_code; + if (input_method.id == "pinyin") { // The pinyin input method returns "zh_CN" as language_code, but // l10n_util expects "zh-CN". language_code = "zh-CN"; - } else if (language.id == "chewing") { + } else if (input_method.id == "chewing") { // Likewise, the chewing input method returns "zh" as language_code, // which is ambiguous. We use zh-TW instead. language_code = "zh-TW"; } else if (language_code == "t") { // "t" is used by input methods that do not associate with a // particular language. Returns the display name as-is. - return UTF8ToWide(language.display_name); + return UTF8ToWide(input_method.display_name); } const string16 language_name = l10n_util::GetDisplayNameForLocale( - language_code, - g_browser_process->GetApplicationLocale(), - true); + language_code, g_browser_process->GetApplicationLocale(), true); // TODO(satorux): We should add input method names if multiple input // methods are available for one input language. return UTF16ToWide(language_name); } -// Converts chromeos::InputLanguage object into human readable string. Returns -// a string for the drop-down menu if |for_menu| is true. Otherwise, returns a -// string for the status area. +// Converts chromeos::InputMethodDescriptor object into human readable string. +// Returns a string for the drop-down menu if |for_menu| is true. Otherwise, +// returns a string for the status area. std::wstring FormatInputLanguage( - const chromeos::InputLanguage& language, bool for_menu) { - std::wstring formatted = GetLanguageName(language); + const chromeos::InputMethodDescriptor& input_method, bool for_menu) { + std::wstring formatted = GetLanguageName(input_method); if (formatted.empty()) { - formatted = UTF8ToWide(language.id); + formatted = UTF8ToWide(input_method.id); } if (!for_menu) { // For status area. Trim the string. formatted = formatted.substr(0, kMaxLanguageNameLen); - // TODO(yusukes): How can we ensure that the trimmed string does not - // overflow the area? + // TODO(yusukes): For the menu, we should use two-letter language code like + // "EN", "JA". } return formatted; } @@ -124,21 +122,23 @@ namespace chromeos { LanguageMenuButton::LanguageMenuButton(StatusAreaHost* host) : MenuButton(NULL, std::wstring(), this, false), - language_list_(CrosLibrary::Get()->GetLanguageLibrary()-> - GetActiveLanguages()), + input_method_descriptors_(CrosLibrary::Get()->GetLanguageLibrary()-> + GetActiveInputMethods()), model_(NULL), // Be aware that the constructor of |language_menu_| calls GetItemCount() // in this class. Therefore, GetItemCount() have to return 0 when // |model_| is NULL. ALLOW_THIS_IN_INITIALIZER_LIST(language_menu_(this)), host_(host) { - DCHECK(language_list_.get() && !language_list_->empty()); + DCHECK(input_method_descriptors_.get() && + !input_method_descriptors_->empty()); // Update the model RebuildModel(); // Grab the real estate. UpdateIcon(kSpacer); - // Display the default XKB name (usually "US"). - const std::wstring name = FormatInputLanguage(language_list_->at(0), false); + // Display the default input method name. + const std::wstring name + = FormatInputLanguage(input_method_descriptors_->at(0), false); UpdateIcon(name); CrosLibrary::Get()->GetLanguageLibrary()->AddObserver(this); } @@ -167,12 +167,13 @@ bool LanguageMenuButton::GetAcceleratorAt( bool LanguageMenuButton::IsItemCheckedAt(int index) const { DCHECK_GE(index, 0); - DCHECK(language_list_.get()); + DCHECK(input_method_descriptors_.get()); - if (IndexIsInLanguageList(index)) { - const InputLanguage& language = language_list_->at(index); - return language == CrosLibrary::Get()->GetLanguageLibrary()-> - current_language(); + if (IndexIsInInputMethodList(index)) { + const InputMethodDescriptor& input_method + = input_method_descriptors_->at(index); + return input_method == CrosLibrary::Get()->GetLanguageLibrary()-> + current_input_method(); } if (GetPropertyIndex(index, &index)) { @@ -188,7 +189,7 @@ bool LanguageMenuButton::IsItemCheckedAt(int index) const { int LanguageMenuButton::GetGroupIdAt(int index) const { DCHECK_GE(index, 0); - if (IndexIsInLanguageList(index)) { + if (IndexIsInInputMethodList(index)) { return kRadioGroupLanguage; } @@ -202,7 +203,7 @@ int LanguageMenuButton::GetGroupIdAt(int index) const { } bool LanguageMenuButton::HasIcons() const { - // We don't support IME nor keyboard icons on Chrome OS. + // We don't support icons on Chrome OS. return false; } @@ -211,8 +212,8 @@ bool LanguageMenuButton::GetIconAt(int index, SkBitmap* icon) const { } bool LanguageMenuButton::IsEnabledAt(int index) const { - // Just return true so all IMEs, XKB layouts, and IME properties could be - // clicked. + // Just return true so all input method names and input method propertie names + // could be clicked. return true; } @@ -245,7 +246,7 @@ menus::MenuModel::ItemType LanguageMenuButton::GetTypeAt(int index) const { return menus::MenuModel::TYPE_COMMAND; // "Configure IME" } - if (IndexIsInLanguageList(index)) { + if (IndexIsInInputMethodList(index)) { return menus::MenuModel::TYPE_RADIO; } @@ -263,15 +264,15 @@ menus::MenuModel::ItemType LanguageMenuButton::GetTypeAt(int index) const { string16 LanguageMenuButton::GetLabelAt(int index) const { DCHECK_GE(index, 0); - DCHECK(language_list_.get()); + DCHECK(input_method_descriptors_.get()); if (IndexPointsToConfigureImeMenuItem(index)) { return l10n_util::GetStringUTF16(IDS_STATUSBAR_IME_CONFIGURE); } std::wstring name; - if (IndexIsInLanguageList(index)) { - name = FormatInputLanguage(language_list_->at(index), true); + if (IndexIsInInputMethodList(index)) { + name = FormatInputLanguage(input_method_descriptors_->at(index), true); } else if (GetPropertyIndex(index, &index)) { const ImePropertyList& property_list = CrosLibrary::Get()->GetLanguageLibrary()->current_ime_properties(); @@ -284,18 +285,19 @@ string16 LanguageMenuButton::GetLabelAt(int index) const { void LanguageMenuButton::ActivatedAt(int index) { DCHECK_GE(index, 0); - DCHECK(language_list_.get()); + DCHECK(input_method_descriptors_.get()); if (IndexPointsToConfigureImeMenuItem(index)) { host_->OpenButtonOptions(this); return; } - if (IndexIsInLanguageList(index)) { - // Inter-IME switching or IME-XKB switching. - const InputLanguage& language = language_list_->at(index); - CrosLibrary::Get()->GetLanguageLibrary()->ChangeLanguage(language.category, - language.id); + if (IndexIsInInputMethodList(index)) { + // Inter-IME switching. + const InputMethodDescriptor& input_method + = input_method_descriptors_->at(index); + CrosLibrary::Get()->GetLanguageLibrary()->ChangeInputMethod( + input_method.id); return; } @@ -334,8 +336,8 @@ void LanguageMenuButton::ActivatedAt(int index) { // LanguageMenuButton, views::ViewMenuDelegate implementation: void LanguageMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { - language_list_.reset(CrosLibrary::Get()->GetLanguageLibrary()-> - GetActiveLanguages()); + input_method_descriptors_.reset(CrosLibrary::Get()->GetLanguageLibrary()-> + GetActiveInputMethods()); RebuildModel(); language_menu_.Rebuild(); language_menu_.UpdateStates(); @@ -345,12 +347,14 @@ void LanguageMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { //////////////////////////////////////////////////////////////////////////////// // LanguageLibrary::Observer implementation: -void LanguageMenuButton::LanguageChanged(LanguageLibrary* obj) { - const std::wstring name = FormatInputLanguage(obj->current_language(), false); +void LanguageMenuButton::InputMethodChanged(LanguageLibrary* obj) { + const std::wstring name = FormatInputLanguage( + obj->current_input_method(), false); UpdateIcon(name); - // This is necessary to remove IME properties when the current language is - // switched to XKB. + // This is necessary to remove input method properties when the current + // language is switched to XKB. + // TODO(yusukes): remove this call? RebuildModel(); } @@ -375,22 +379,19 @@ void LanguageMenuButton::RebuildModel() { // Indicates if separator's needed before each section. bool need_separator = false; - if (!language_list_->empty()) { + if (!input_method_descriptors_->empty()) { // We "abuse" the command_id and group_id arguments of AddRadioItem method. // A COMMAND_ID_XXX enum value is passed as command_id, and array index of - // |language_list_| or |property_list| is passed as group_id. - for (size_t i = 0; i < language_list_->size(); ++i) { - model_->AddRadioItem(COMMAND_ID_LANGUAGES, dummy_label, i); + // |input_method_descriptors_| or |property_list| is passed as group_id. + for (size_t i = 0; i < input_method_descriptors_->size(); ++i) { + model_->AddRadioItem(COMMAND_ID_INPUT_METHODS, dummy_label, i); } need_separator = true; } const ImePropertyList& property_list = CrosLibrary::Get()->GetLanguageLibrary()->current_ime_properties(); - const InputLanguage& current_language - = CrosLibrary::Get()->GetLanguageLibrary()->current_language(); - if ((!property_list.empty()) && - (current_language.category == chromeos::LANGUAGE_CATEGORY_IME)) { + if (!property_list.empty()) { if (need_separator) model_->AddSeparator(); for (size_t i = 0; i < property_list.size(); ++i) { @@ -408,12 +409,12 @@ void LanguageMenuButton::RebuildModel() { } } -bool LanguageMenuButton::IndexIsInLanguageList(int index) const { +bool LanguageMenuButton::IndexIsInInputMethodList(int index) const { DCHECK_GE(index, 0); DCHECK(model_.get()); return ((model_->GetTypeAt(index) == menus::MenuModel::TYPE_RADIO) && - (model_->GetCommandIdAt(index) == COMMAND_ID_LANGUAGES)); + (model_->GetCommandIdAt(index) == COMMAND_ID_INPUT_METHODS)); } bool LanguageMenuButton::GetPropertyIndex( @@ -438,6 +439,4 @@ bool LanguageMenuButton::IndexPointsToConfigureImeMenuItem(int index) const { (model_->GetCommandIdAt(index) == COMMAND_ID_CONFIGURE_IME)); } -// TODO(yusukes): Register and handle hotkeys for IME and XKB switching? - } // namespace chromeos diff --git a/chrome/browser/chromeos/status/language_menu_button.h b/chrome/browser/chromeos/status/language_menu_button.h index 3c0a88b..b51e513 100644 --- a/chrome/browser/chromeos/status/language_menu_button.h +++ b/chrome/browser/chromeos/status/language_menu_button.h @@ -46,7 +46,7 @@ class LanguageMenuButton : public views::MenuButton, virtual void MenuWillShow(); // LanguageLibrary::Observer implementation. - virtual void LanguageChanged(LanguageLibrary* obj); + virtual void InputMethodChanged(LanguageLibrary* obj); virtual void ImePropertiesChanged(LanguageLibrary* obj); private: @@ -56,13 +56,13 @@ class LanguageMenuButton : public views::MenuButton, // Update the status area with |name|. void UpdateIcon(const std::wstring& name); - // Rebuilds |model_|. This function should be called whenever |language_list_| - // is updated, or ImePropertiesChanged() is called. + // Rebuilds |model_|. This function should be called whenever + // |input_method_descriptors_| is updated, or ImePropertiesChanged() is + // called. void RebuildModel(); - // Returns true if the zero-origin |index| points to one of the input - // languages. - bool IndexIsInLanguageList(int index) const; + // Returns true if the zero-origin |index| points to one of the input methods. + bool IndexIsInInputMethodList(int index) const; // Returns true if the zero-origin |index| points to one of the IME // properties. When returning true, |property_index| is updated so that @@ -73,8 +73,8 @@ class LanguageMenuButton : public views::MenuButton, // item. bool IndexPointsToConfigureImeMenuItem(int index) const; - // The current language list. - scoped_ptr language_list_; + // The current input method list. + scoped_ptr input_method_descriptors_; // We borrow menus::SimpleMenuModel implementation to maintain the current // content of the pop-up menu. The menus::MenuModel is implemented using this -- cgit v1.1