Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/chromeos/status/input_method_menu.h" | 5 #include "chrome/browser/chromeos/status/input_method_menu.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 15 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 16 #include "chrome/browser/chromeos/language_preferences.h" | 16 #include "chrome/browser/chromeos/language_preferences.h" |
| 17 #include "chrome/browser/chromeos/status/status_area_view_chromeos.h" | |
| 17 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "content/browser/user_metrics.h" | 21 #include "content/browser/user_metrics.h" |
| 21 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 22 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 23 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/models/simple_menu_model.h" | 26 #include "ui/base/models/simple_menu_model.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } // namespace | 126 } // namespace |
| 126 | 127 |
| 127 namespace chromeos { | 128 namespace chromeos { |
| 128 | 129 |
| 129 using input_method::InputMethodManager; | 130 using input_method::InputMethodManager; |
| 130 | 131 |
| 131 //////////////////////////////////////////////////////////////////////////////// | 132 //////////////////////////////////////////////////////////////////////////////// |
| 132 // InputMethodMenu | 133 // InputMethodMenu |
| 133 | 134 |
| 134 InputMethodMenu::InputMethodMenu(PrefService* pref_service, | 135 InputMethodMenu::InputMethodMenu(PrefService* pref_service, |
| 135 StatusAreaViewChromeos::ScreenMode screen_mode, | |
| 136 bool for_out_of_box_experience_dialog) | 136 bool for_out_of_box_experience_dialog) |
| 137 : input_method_descriptors_(InputMethodManager::GetInstance()-> | 137 : input_method_descriptors_(InputMethodManager::GetInstance()-> |
| 138 GetActiveInputMethods()), | 138 GetActiveInputMethods()), |
| 139 model_(new ui::SimpleMenuModel(NULL)), | 139 model_(new ui::SimpleMenuModel(NULL)), |
| 140 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_menu_delegate_( | 140 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_menu_delegate_( |
| 141 new views::MenuModelAdapter(this))), | 141 new views::MenuModelAdapter(this))), |
| 142 input_method_menu_( | 142 input_method_menu_( |
| 143 new views::MenuItemView(input_method_menu_delegate_.get())), | 143 new views::MenuItemView(input_method_menu_delegate_.get())), |
| 144 input_method_menu_runner_(new views::MenuRunner(input_method_menu_)), | 144 input_method_menu_runner_(new views::MenuRunner(input_method_menu_)), |
| 145 minimum_input_method_menu_width_(0), | 145 minimum_input_method_menu_width_(0), |
| 146 menu_alignment_(views::MenuItemView::TOPRIGHT), | 146 menu_alignment_(views::MenuItemView::TOPRIGHT), |
| 147 pref_service_(pref_service), | 147 pref_service_(pref_service), |
| 148 screen_mode_(screen_mode), | |
| 149 for_out_of_box_experience_dialog_(for_out_of_box_experience_dialog) { | 148 for_out_of_box_experience_dialog_(for_out_of_box_experience_dialog) { |
| 150 DCHECK(input_method_descriptors_.get() && | 149 DCHECK(input_method_descriptors_.get() && |
| 151 !input_method_descriptors_->empty()); | 150 !input_method_descriptors_->empty()); |
| 152 | 151 |
| 153 // Sync current and previous input methods on Chrome prefs with ibus-daemon. | 152 // Sync current and previous input methods on Chrome prefs with ibus-daemon. |
| 154 if (pref_service_ && (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE)) { | 153 if (pref_service_ && StatusAreaViewChromeos::IsBrowserMode()) { |
| 155 previous_input_method_pref_.Init( | 154 InitializePrefMembers(); |
| 156 prefs::kLanguagePreviousInputMethod, pref_service, this); | |
| 157 current_input_method_pref_.Init( | |
| 158 prefs::kLanguageCurrentInputMethod, pref_service, this); | |
| 159 } | 155 } |
| 160 | 156 |
| 161 InputMethodManager* manager = InputMethodManager::GetInstance(); | 157 InputMethodManager* manager = InputMethodManager::GetInstance(); |
| 162 if (screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_VIEWS || | 158 if (StatusAreaViewChromeos::IsLoginMode()) { |
| 163 screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_WEBUI) { | |
| 164 // This button is for the login screen. | 159 // This button is for the login screen. |
| 165 manager->AddPreLoginPreferenceObserver(this); | 160 manager->AddPreLoginPreferenceObserver(this); |
| 166 registrar_.Add(this, | 161 registrar_.Add(this, |
| 167 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 162 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 168 content::NotificationService::AllSources()); | 163 content::NotificationService::AllSources()); |
| 169 } else if (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE) { | 164 #if defined(USE_AURA) |
| 165 // On Aura status area is not recreated on sign in. | |
| 166 // In case of Chrome crash, Chrome will be reloaded but IsLoginMode() will | |
| 167 // return false at this point. | |
| 168 registrar_.Add(this, | |
| 169 chrome::NOTIFICATION_PROFILE_CREATED, | |
| 170 content::NotificationService::AllSources()); | |
|
stevenjb
2011/11/21 18:10:16
nit: Alignment
Nikita (slow)
2011/11/22 09:01:28
Done.
| |
| 171 #endif | |
| 172 } else if (StatusAreaViewChromeos::IsBrowserMode()) { | |
| 170 manager->AddPostLoginPreferenceObserver(this); | 173 manager->AddPostLoginPreferenceObserver(this); |
| 171 } | 174 } |
| 172 | 175 |
| 173 // AddObserver() should be called after AddXXXLoginPreferenceObserver. This is | 176 // AddObserver() should be called after AddXXXLoginPreferenceObserver. This is |
| 174 // because when the function is called FirstObserverIsAdded might be called | 177 // because when the function is called FirstObserverIsAdded might be called |
| 175 // back, and FirstObserverIsAdded might then might call ChangeInputMethod() in | 178 // back, and FirstObserverIsAdded might then might call ChangeInputMethod() in |
| 176 // InputMethodManager. We have to prevent the manager function from calling | 179 // InputMethodManager. We have to prevent the manager function from calling |
| 177 // callback functions like InputMethodChanged since they touch (yet | 180 // callback functions like InputMethodChanged since they touch (yet |
| 178 // uninitialized) UI elements. | 181 // uninitialized) UI elements. |
| 179 manager->AddObserver(this); | 182 manager->AddObserver(this); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 InputMethodManager* manager, | 417 InputMethodManager* manager, |
| 415 const input_method::InputMethodDescriptor& current_input_method, | 418 const input_method::InputMethodDescriptor& current_input_method, |
| 416 size_t num_active_input_methods) { | 419 size_t num_active_input_methods) { |
| 417 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); | 420 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); |
| 418 } | 421 } |
| 419 | 422 |
| 420 void InputMethodMenu::PreferenceUpdateNeeded( | 423 void InputMethodMenu::PreferenceUpdateNeeded( |
| 421 InputMethodManager* manager, | 424 InputMethodManager* manager, |
| 422 const input_method::InputMethodDescriptor& previous_input_method, | 425 const input_method::InputMethodDescriptor& previous_input_method, |
| 423 const input_method::InputMethodDescriptor& current_input_method) { | 426 const input_method::InputMethodDescriptor& current_input_method) { |
| 424 if (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE) { | 427 if (StatusAreaViewChromeos::IsBrowserMode()) { |
| 425 if (pref_service_) { // make sure we're not in unit tests. | 428 if (pref_service_) { // make sure we're not in unit tests. |
| 426 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. | 429 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. |
| 427 previous_input_method_pref_.SetValue(previous_input_method.id()); | 430 previous_input_method_pref_.SetValue(previous_input_method.id()); |
| 428 current_input_method_pref_.SetValue(current_input_method.id()); | 431 current_input_method_pref_.SetValue(current_input_method.id()); |
| 429 pref_service_->ScheduleSavePersistentPrefs(); | 432 pref_service_->ScheduleSavePersistentPrefs(); |
| 430 } | 433 } |
| 431 } else if (screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_VIEWS || | 434 } else if (StatusAreaViewChromeos::IsLoginMode()) { |
| 432 screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_WEBUI) { | |
| 433 if (g_browser_process && g_browser_process->local_state()) { | 435 if (g_browser_process && g_browser_process->local_state()) { |
| 434 g_browser_process->local_state()->SetString( | 436 g_browser_process->local_state()->SetString( |
| 435 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); | 437 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); |
| 436 g_browser_process->local_state()->ScheduleSavePersistentPrefs(); | 438 g_browser_process->local_state()->ScheduleSavePersistentPrefs(); |
| 437 } | 439 } |
| 438 } | 440 } |
| 439 } | 441 } |
| 440 | 442 |
| 441 void InputMethodMenu::PropertyListChanged( | 443 void InputMethodMenu::PropertyListChanged( |
| 442 InputMethodManager* manager, | 444 InputMethodManager* manager, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 458 manager->current_input_method(); | 460 manager->current_input_method(); |
| 459 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); | 461 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); |
| 460 UpdateUIFromInputMethod(input_method, num_active_input_methods); | 462 UpdateUIFromInputMethod(input_method, num_active_input_methods); |
| 461 } | 463 } |
| 462 } | 464 } |
| 463 | 465 |
| 464 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { | 466 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { |
| 465 // NOTICE: Since this function might be called from the constructor of this | 467 // NOTICE: Since this function might be called from the constructor of this |
| 466 // class, it's better to avoid calling virtual functions. | 468 // class, it's better to avoid calling virtual functions. |
| 467 | 469 |
| 468 if (pref_service_ && (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE)) { | 470 if (pref_service_ && (StatusAreaViewChromeos::IsBrowserMode())) { |
| 469 // Get the input method name in the Preferences file which was in use last | 471 // Get the input method name in the Preferences file which was in use last |
| 470 // time, and switch to the method. We remember two input method names in the | 472 // time, and switch to the method. We remember two input method names in the |
| 471 // preference so that the Control+space hot-key could work fine from the | 473 // preference so that the Control+space hot-key could work fine from the |
| 472 // beginning. InputMethodChanged() will be called soon and the indicator | 474 // beginning. InputMethodChanged() will be called soon and the indicator |
| 473 // will be updated. | 475 // will be updated. |
| 474 const std::string previous_input_method_id = | 476 const std::string previous_input_method_id = |
| 475 previous_input_method_pref_.GetValue(); | 477 previous_input_method_pref_.GetValue(); |
| 476 if (!previous_input_method_id.empty()) { | 478 if (!previous_input_method_id.empty()) { |
| 477 manager->ChangeInputMethod(previous_input_method_id); | 479 manager->ChangeInputMethod(previous_input_method_id); |
| 478 } | 480 } |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 705 | 707 |
| 706 void InputMethodMenu::Observe(int type, | 708 void InputMethodMenu::Observe(int type, |
| 707 const content::NotificationSource& source, | 709 const content::NotificationSource& source, |
| 708 const content::NotificationDetails& details) { | 710 const content::NotificationDetails& details) { |
| 709 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | 711 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
| 710 // When a user logs in, we should remove |this| object from the observer | 712 // When a user logs in, we should remove |this| object from the observer |
| 711 // list so that PreferenceUpdateNeeded() does not update the local state | 713 // list so that PreferenceUpdateNeeded() does not update the local state |
| 712 // anymore. | 714 // anymore. |
| 713 RemoveObservers(); | 715 RemoveObservers(); |
| 714 } | 716 } |
| 717 #if defined(USE_AURA) | |
| 718 if (type == chrome::NOTIFICATION_PROFILE_CREATED) { | |
| 719 // On Aura status area is not recreated on login for normal user sign in. | |
| 720 // NOTIFICATION_LOGIN_USER_CHANGED has been notified early in login process. | |
| 721 InitializePrefMembers(); | |
| 722 InputMethodManager* manager = InputMethodManager::GetInstance(); | |
| 723 manager->AddPostLoginPreferenceObserver(this); | |
| 724 manager->AddObserver(this); | |
| 725 } | |
| 726 #endif | |
| 715 } | 727 } |
| 716 | 728 |
| 717 void InputMethodMenu::SetMinimumWidth(int width) { | 729 void InputMethodMenu::SetMinimumWidth(int width) { |
| 718 // On the OOBE network selection screen, fixed width menu would be preferable. | 730 // On the OOBE network selection screen, fixed width menu would be preferable. |
| 719 minimum_input_method_menu_width_ = width; | 731 minimum_input_method_menu_width_ = width; |
| 720 } | 732 } |
| 721 | 733 |
| 722 void InputMethodMenu::RemoveObservers() { | 734 void InputMethodMenu::RemoveObservers() { |
| 723 InputMethodManager* manager = InputMethodManager::GetInstance(); | 735 InputMethodManager* manager = InputMethodManager::GetInstance(); |
| 724 if (screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_VIEWS || | 736 if (StatusAreaViewChromeos::IsLoginMode()) { |
| 725 screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_WEBUI) { | |
| 726 manager->RemovePreLoginPreferenceObserver(this); | 737 manager->RemovePreLoginPreferenceObserver(this); |
| 727 } else if (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE) { | 738 } else if (StatusAreaViewChromeos::IsBrowserMode()) { |
| 728 manager->RemovePostLoginPreferenceObserver(this); | 739 manager->RemovePostLoginPreferenceObserver(this); |
| 729 } | 740 } |
| 730 manager->RemoveObserver(this); | 741 manager->RemoveObserver(this); |
| 731 } | 742 } |
| 732 | 743 |
| 744 void InputMethodMenu::InitializePrefMembers() { | |
| 745 previous_input_method_pref_.Init( | |
| 746 prefs::kLanguagePreviousInputMethod, pref_service_, this); | |
| 747 current_input_method_pref_.Init( | |
| 748 prefs::kLanguageCurrentInputMethod, pref_service_, this); | |
| 749 } | |
| 750 | |
| 733 } // namespace chromeos | 751 } // namespace chromeos |
| OLD | NEW |