OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #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/chromeos/status/status_area_view_chromeos.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/browser/profiles/profile_manager.h" |
19 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
21 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/user_metrics.h" | 24 #include "content/public/browser/user_metrics.h" |
23 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
24 #include "grit/theme_resources.h" | 26 #include "grit/theme_resources.h" |
25 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
26 #include "ui/base/models/simple_menu_model.h" | 28 #include "ui/base/models/simple_menu_model.h" |
27 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
28 #include "ui/views/controls/menu/menu_model_adapter.h" | 30 #include "ui/views/controls/menu/menu_model_adapter.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const size_t kMappingFromIdToIndicatorTextLen = | 120 const size_t kMappingFromIdToIndicatorTextLen = |
119 ARRAYSIZE_UNSAFE(kMappingFromIdToIndicatorText); | 121 ARRAYSIZE_UNSAFE(kMappingFromIdToIndicatorText); |
120 | 122 |
121 // Returns the language name for the given |language_code|. | 123 // Returns the language name for the given |language_code|. |
122 string16 GetLanguageName(const std::string& language_code) { | 124 string16 GetLanguageName(const std::string& language_code) { |
123 const string16 language_name = l10n_util::GetDisplayNameForLocale( | 125 const string16 language_name = l10n_util::GetDisplayNameForLocale( |
124 language_code, g_browser_process->GetApplicationLocale(), true); | 126 language_code, g_browser_process->GetApplicationLocale(), true); |
125 return language_name; | 127 return language_name; |
126 } | 128 } |
127 | 129 |
| 130 PrefService* GetPrefService() { |
| 131 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 132 if (profile) |
| 133 return profile->GetPrefs(); |
| 134 return NULL; |
| 135 } |
| 136 |
128 } // namespace | 137 } // namespace |
129 | 138 |
130 namespace chromeos { | 139 namespace chromeos { |
131 | 140 |
132 using input_method::InputMethodManager; | 141 using input_method::InputMethodManager; |
133 | 142 |
134 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
135 // InputMethodMenu | 144 // InputMethodMenu |
136 | 145 |
137 InputMethodMenu::InputMethodMenu(PrefService* pref_service, | 146 InputMethodMenu::InputMethodMenu(bool for_out_of_box_experience_dialog) |
138 bool for_out_of_box_experience_dialog) | |
139 : initialized_prefs_(false), | 147 : initialized_prefs_(false), |
140 initialized_observers_(false), | 148 initialized_observers_(false), |
141 input_method_descriptors_(InputMethodManager::GetInstance()-> | 149 input_method_descriptors_(InputMethodManager::GetInstance()-> |
142 GetActiveInputMethods()), | 150 GetActiveInputMethods()), |
143 model_(new ui::SimpleMenuModel(NULL)), | 151 model_(new ui::SimpleMenuModel(NULL)), |
144 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_menu_delegate_( | 152 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_menu_delegate_( |
145 new views::MenuModelAdapter(this))), | 153 new views::MenuModelAdapter(this))), |
146 input_method_menu_( | 154 input_method_menu_( |
147 new views::MenuItemView(input_method_menu_delegate_.get())), | 155 new views::MenuItemView(input_method_menu_delegate_.get())), |
148 input_method_menu_runner_(new views::MenuRunner(input_method_menu_)), | 156 input_method_menu_runner_(new views::MenuRunner(input_method_menu_)), |
149 minimum_input_method_menu_width_(0), | 157 minimum_input_method_menu_width_(0), |
150 menu_alignment_(views::MenuItemView::TOPRIGHT), | 158 menu_alignment_(views::MenuItemView::TOPRIGHT), |
151 pref_service_(pref_service), | |
152 for_out_of_box_experience_dialog_(for_out_of_box_experience_dialog) { | 159 for_out_of_box_experience_dialog_(for_out_of_box_experience_dialog) { |
153 DCHECK(input_method_descriptors_.get() && | 160 DCHECK(input_method_descriptors_.get() && |
154 !input_method_descriptors_->empty()); | 161 !input_method_descriptors_->empty()); |
155 | 162 |
156 // Sync current and previous input methods on Chrome prefs with ibus-daemon. | 163 // Sync current and previous input methods on Chrome prefs with ibus-daemon. |
157 if (pref_service_ && StatusAreaViewChromeos::IsBrowserMode()) { | 164 if (StatusAreaViewChromeos::IsBrowserMode()) |
158 InitializePrefMembers(); | 165 InitializePrefMembers(); |
159 } | |
160 | 166 |
161 if (StatusAreaViewChromeos::IsLoginMode()) { | 167 if (StatusAreaViewChromeos::IsLoginMode()) { |
162 registrar_.Add(this, | 168 registrar_.Add(this, |
163 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 169 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
164 content::NotificationService::AllSources()); | 170 content::NotificationService::AllSources()); |
165 #if defined(USE_AURA) | 171 #if defined(USE_AURA) |
166 // On Aura status area is not recreated on sign in. Instead, 2 notifications | 172 // On Aura status area is not recreated on sign in. Instead, 2 notifications |
167 // are sent to Chrome on sign in: NOTIFICATION_LOGIN_USER_CHANGED with | 173 // are sent to Chrome on sign in: NOTIFICATION_LOGIN_USER_CHANGED with |
168 // StatusAreaViewChromeos::IsLoginMode() and NOTIFICATION_SESSION_STARTED | 174 // StatusAreaViewChromeos::IsLoginMode() and NOTIFICATION_SESSION_STARTED |
169 // with StatusAreaViewChromeos::IsBrowserMode(). | 175 // with StatusAreaViewChromeos::IsBrowserMode(). |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 size_t num_active_input_methods) { | 421 size_t num_active_input_methods) { |
416 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); | 422 UpdateUIFromInputMethod(current_input_method, num_active_input_methods); |
417 } | 423 } |
418 | 424 |
419 // TODO(yusukes): Move code for handling preferences to chromeos/input_method/. | 425 // TODO(yusukes): Move code for handling preferences to chromeos/input_method/. |
420 void InputMethodMenu::PreferenceUpdateNeeded( | 426 void InputMethodMenu::PreferenceUpdateNeeded( |
421 InputMethodManager* manager, | 427 InputMethodManager* manager, |
422 const input_method::InputMethodDescriptor& previous_input_method, | 428 const input_method::InputMethodDescriptor& previous_input_method, |
423 const input_method::InputMethodDescriptor& current_input_method) { | 429 const input_method::InputMethodDescriptor& current_input_method) { |
424 if (StatusAreaViewChromeos::IsBrowserMode()) { | 430 if (StatusAreaViewChromeos::IsBrowserMode()) { |
425 if (pref_service_) { // make sure we're not in unit tests. | 431 if (initialized_prefs_) { // make sure we're not in unit tests. |
426 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. | 432 // Sometimes (e.g. initial boot) |previous_input_method.id()| is empty. |
427 previous_input_method_pref_.SetValue(previous_input_method.id()); | 433 previous_input_method_pref_.SetValue(previous_input_method.id()); |
428 current_input_method_pref_.SetValue(current_input_method.id()); | 434 current_input_method_pref_.SetValue(current_input_method.id()); |
429 } | 435 } |
430 } else if (StatusAreaViewChromeos::IsLoginMode()) { | 436 } else if (StatusAreaViewChromeos::IsLoginMode()) { |
431 if (g_browser_process && g_browser_process->local_state()) { | 437 if (g_browser_process && g_browser_process->local_state()) { |
432 g_browser_process->local_state()->SetString( | 438 g_browser_process->local_state()->SetString( |
433 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); | 439 language_prefs::kPreferredKeyboardLayout, current_input_method.id()); |
434 } | 440 } |
435 } | 441 } |
(...skipping 19 matching lines...) Expand all Loading... |
455 manager->current_input_method(); | 461 manager->current_input_method(); |
456 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); | 462 size_t num_active_input_methods = manager->GetNumActiveInputMethods(); |
457 UpdateUIFromInputMethod(input_method, num_active_input_methods); | 463 UpdateUIFromInputMethod(input_method, num_active_input_methods); |
458 } | 464 } |
459 } | 465 } |
460 | 466 |
461 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { | 467 void InputMethodMenu::FirstObserverIsAdded(InputMethodManager* manager) { |
462 // 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 |
463 // class, it's better to avoid calling virtual functions. | 469 // class, it's better to avoid calling virtual functions. |
464 | 470 |
465 if (pref_service_ && (StatusAreaViewChromeos::IsBrowserMode())) { | 471 if (initialized_prefs_ && StatusAreaViewChromeos::IsBrowserMode()) { |
466 // 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 |
467 // 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 |
468 // 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 |
469 // beginning. InputMethodChanged() will be called soon and the indicator | 475 // beginning. InputMethodChanged() will be called soon and the indicator |
470 // will be updated. | 476 // will be updated. |
471 const std::string previous_input_method_id = | 477 const std::string previous_input_method_id = |
472 previous_input_method_pref_.GetValue(); | 478 previous_input_method_pref_.GetValue(); |
473 if (!previous_input_method_id.empty()) { | 479 if (!previous_input_method_id.empty()) { |
474 manager->ChangeInputMethod(previous_input_method_id); | 480 manager->ChangeInputMethod(previous_input_method_id); |
475 } | 481 } |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 manager->RemovePreLoginPreferenceObserver(this); | 741 manager->RemovePreLoginPreferenceObserver(this); |
736 } else if (StatusAreaViewChromeos::IsBrowserMode()) { | 742 } else if (StatusAreaViewChromeos::IsBrowserMode()) { |
737 manager->RemovePostLoginPreferenceObserver(this); | 743 manager->RemovePostLoginPreferenceObserver(this); |
738 } | 744 } |
739 manager->RemoveObserver(this); | 745 manager->RemoveObserver(this); |
740 initialized_observers_ = false; | 746 initialized_observers_ = false; |
741 } | 747 } |
742 | 748 |
743 void InputMethodMenu::InitializePrefMembers() { | 749 void InputMethodMenu::InitializePrefMembers() { |
744 if (!initialized_prefs_) { | 750 if (!initialized_prefs_) { |
745 initialized_prefs_ = true; | 751 PrefService* pref_service = GetPrefService(); |
746 previous_input_method_pref_.Init( | 752 if (pref_service) { |
747 prefs::kLanguagePreviousInputMethod, pref_service_, this); | 753 initialized_prefs_ = true; |
748 current_input_method_pref_.Init( | 754 previous_input_method_pref_.Init( |
749 prefs::kLanguageCurrentInputMethod, pref_service_, this); | 755 prefs::kLanguagePreviousInputMethod, pref_service, this); |
| 756 current_input_method_pref_.Init( |
| 757 prefs::kLanguageCurrentInputMethod, pref_service, this); |
| 758 } |
750 } | 759 } |
751 } | 760 } |
752 | 761 |
753 } // namespace chromeos | 762 } // namespace chromeos |
OLD | NEW |