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 so NOTIFICATION_PROFILE_CREATED will be |
| 168 // ignored and all initialization will happen in ctor. |
| 169 registrar_.Add(this, |
| 170 chrome::NOTIFICATION_PROFILE_CREATED, |
| 171 content::NotificationService::AllSources()); |
| 172 #endif |
| 173 } else if (StatusAreaViewChromeos::IsBrowserMode()) { |
170 manager->AddPostLoginPreferenceObserver(this); | 174 manager->AddPostLoginPreferenceObserver(this); |
171 } | 175 } |
172 | 176 |
173 // AddObserver() should be called after AddXXXLoginPreferenceObserver. This is | 177 // AddObserver() should be called after AddXXXLoginPreferenceObserver. This is |
174 // because when the function is called FirstObserverIsAdded might be called | 178 // because when the function is called FirstObserverIsAdded might be called |
175 // back, and FirstObserverIsAdded might then might call ChangeInputMethod() in | 179 // back, and FirstObserverIsAdded might then might call ChangeInputMethod() in |
176 // InputMethodManager. We have to prevent the manager function from calling | 180 // InputMethodManager. We have to prevent the manager function from calling |
177 // callback functions like InputMethodChanged since they touch (yet | 181 // callback functions like InputMethodChanged since they touch (yet |
178 // uninitialized) UI elements. | 182 // uninitialized) UI elements. |
179 manager->AddObserver(this); | 183 manager->AddObserver(this); |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 InputMethodManager* manager, | 418 InputMethodManager* manager, |
415 const input_method::InputMethodDescriptor& current_input_method, | 419 const input_method::InputMethodDescriptor& current_input_method, |
416 size_t num_active_input_methods) { | 420 size_t num_active_input_methods) { |
417 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); | 421 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); |
418 } | 422 } |
419 | 423 |
420 void InputMethodMenu::PreferenceUpdateNeeded( | 424 void InputMethodMenu::PreferenceUpdateNeeded( |
421 InputMethodManager* manager, | 425 InputMethodManager* manager, |
422 const input_method::InputMethodDescriptor& previous_input_method, | 426 const input_method::InputMethodDescriptor& previous_input_method, |
423 const input_method::InputMethodDescriptor& current_input_method) { | 427 const input_method::InputMethodDescriptor& current_input_method) { |
424 if (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE) { | 428 if (StatusAreaViewChromeos::IsBrowserMode()) { |
425 if (pref_service_) { // make sure we're not in unit tests. | 429 if (pref_service_) { // make sure we're not in unit tests. |
426 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. | 430 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. |
427 previous_input_method_pref_.SetValue(previous_input_method.id()); | 431 previous_input_method_pref_.SetValue(previous_input_method.id()); |
428 current_input_method_pref_.SetValue(current_input_method.id()); | 432 current_input_method_pref_.SetValue(current_input_method.id()); |
429 pref_service_->ScheduleSavePersistentPrefs(); | 433 pref_service_->ScheduleSavePersistentPrefs(); |
430 } | 434 } |
431 } else if (screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_VIEWS || | 435 } else if (StatusAreaViewChromeos::IsLoginMode()) { |
432 screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_WEBUI) { | |
433 if (g_browser_process && g_browser_process->local_state()) { | 436 if (g_browser_process && g_browser_process->local_state()) { |
434 g_browser_process->local_state()->SetString( | 437 g_browser_process->local_state()->SetString( |
435 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); | 438 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); |
436 g_browser_process->local_state()->ScheduleSavePersistentPrefs(); | 439 g_browser_process->local_state()->ScheduleSavePersistentPrefs(); |
437 } | 440 } |
438 } | 441 } |
439 } | 442 } |
440 | 443 |
441 void InputMethodMenu::PropertyListChanged( | 444 void InputMethodMenu::PropertyListChanged( |
442 InputMethodManager* manager, | 445 InputMethodManager* manager, |
(...skipping 15 matching lines...) Expand all Loading... |
458 manager->current_input_method(); | 461 manager->current_input_method(); |
459 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); | 462 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); |
460 UpdateUIFromInputMethod(input_method, num_active_input_methods); | 463 UpdateUIFromInputMethod(input_method, num_active_input_methods); |
461 } | 464 } |
462 } | 465 } |
463 | 466 |
464 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { | 467 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { |
465 // NOTICE: Since this function might be called from the constructor of this | 468 // NOTICE: Since this function might be called from the constructor of this |
466 // class, it's better to avoid calling virtual functions. | 469 // class, it's better to avoid calling virtual functions. |
467 | 470 |
468 if (pref_service_ && (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE)) { | 471 if (pref_service_ && (StatusAreaViewChromeos::IsBrowserMode())) { |
469 // Get the input method name in the Preferences file which was in use last | 472 // 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 | 473 // 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 | 474 // preference so that the Control+space hot-key could work fine from the |
472 // beginning. InputMethodChanged() will be called soon and the indicator | 475 // beginning. InputMethodChanged() will be called soon and the indicator |
473 // will be updated. | 476 // will be updated. |
474 const std::string previous_input_method_id = | 477 const std::string previous_input_method_id = |
475 previous_input_method_pref_.GetValue(); | 478 previous_input_method_pref_.GetValue(); |
476 if (!previous_input_method_id.empty()) { | 479 if (!previous_input_method_id.empty()) { |
477 manager->ChangeInputMethod(previous_input_method_id); | 480 manager->ChangeInputMethod(previous_input_method_id); |
478 } | 481 } |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 708 |
706 void InputMethodMenu::Observe(int type, | 709 void InputMethodMenu::Observe(int type, |
707 const content::NotificationSource& source, | 710 const content::NotificationSource& source, |
708 const content::NotificationDetails& details) { | 711 const content::NotificationDetails& details) { |
709 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | 712 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
710 // When a user logs in, we should remove |this| object from the observer | 713 // When a user logs in, we should remove |this| object from the observer |
711 // list so that PreferenceUpdateNeeded() does not update the local state | 714 // list so that PreferenceUpdateNeeded() does not update the local state |
712 // anymore. | 715 // anymore. |
713 RemoveObservers(); | 716 RemoveObservers(); |
714 } | 717 } |
| 718 #if defined(USE_AURA) |
| 719 if (type == chrome::NOTIFICATION_PROFILE_CREATED) { |
| 720 // On Aura status area is not recreated on login for normal user sign in. |
| 721 // NOTIFICATION_LOGIN_USER_CHANGED has been notified early in login process. |
| 722 InitializePrefMembers(); |
| 723 InputMethodManager* manager = InputMethodManager::GetInstance(); |
| 724 manager->AddPostLoginPreferenceObserver(this); |
| 725 manager->AddObserver(this); |
| 726 } |
| 727 #endif |
715 } | 728 } |
716 | 729 |
717 void InputMethodMenu::SetMinimumWidth(int width) { | 730 void InputMethodMenu::SetMinimumWidth(int width) { |
718 // On the OOBE network selection screen, fixed width menu would be preferable. | 731 // On the OOBE network selection screen, fixed width menu would be preferable. |
719 minimum_input_method_menu_width_ = width; | 732 minimum_input_method_menu_width_ = width; |
720 } | 733 } |
721 | 734 |
722 void InputMethodMenu::RemoveObservers() { | 735 void InputMethodMenu::RemoveObservers() { |
723 InputMethodManager* manager = InputMethodManager::GetInstance(); | 736 InputMethodManager* manager = InputMethodManager::GetInstance(); |
724 if (screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_VIEWS || | 737 if (StatusAreaViewChromeos::IsLoginMode()) { |
725 screen_mode_ == StatusAreaViewChromeos::LOGIN_MODE_WEBUI) { | |
726 manager->RemovePreLoginPreferenceObserver(this); | 738 manager->RemovePreLoginPreferenceObserver(this); |
727 } else if (screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE) { | 739 } else if (StatusAreaViewChromeos::IsBrowserMode()) { |
728 manager->RemovePostLoginPreferenceObserver(this); | 740 manager->RemovePostLoginPreferenceObserver(this); |
729 } | 741 } |
730 manager->RemoveObserver(this); | 742 manager->RemoveObserver(this); |
731 } | 743 } |
732 | 744 |
| 745 void InputMethodMenu::InitializePrefMembers() { |
| 746 previous_input_method_pref_.Init( |
| 747 prefs::kLanguagePreviousInputMethod, pref_service_, this); |
| 748 current_input_method_pref_.Init( |
| 749 prefs::kLanguageCurrentInputMethod, pref_service_, this); |
| 750 } |
| 751 |
733 } // namespace chromeos | 752 } // namespace chromeos |
OLD | NEW |