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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/input_method/input_method_persistence.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/sys_info.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/input_method/input_method_util.h"
13 #include "chrome/browser/chromeos/language_preferences.h"
14 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/common/pref_names.h"
20 namespace input_method
{
23 void PersistSystemInputMethod(const std::string
& input_method
) {
24 if (!g_browser_process
|| !g_browser_process
->local_state())
27 g_browser_process
->local_state()->SetString(
28 language_prefs::kPreferredKeyboardLayout
, input_method
);
31 // Update user LRU keyboard layout for login screen
32 static void SetUserLRUInputMethod(
33 const std::string
& input_method
,
34 const chromeos::input_method::InputMethodManager
* const manager
,
36 // Skip if it's not a keyboard layout. Drop input methods including
38 if (!manager
->IsLoginKeyboard(input_method
))
41 PrefService
* const local_state
= g_browser_process
->local_state();
46 const std::string username
= profile
->GetProfileName();
47 if (base::SysInfo::IsRunningOnChromeOS() && !username
.empty() &&
48 !local_state
->ReadOnly()) {
49 bool update_succeed
= false;
51 // Updater may have side-effects, therefore we do not replace
52 // entry while updater exists.
53 DictionaryPrefUpdate
updater(local_state
, prefs::kUsersLRUInputMethod
);
54 base::DictionaryValue
* const users_lru_input_methods
= updater
.Get();
55 if (users_lru_input_methods
) {
56 users_lru_input_methods
->SetStringWithoutPathExpansion(username
,
58 update_succeed
= true;
61 if (!update_succeed
) {
62 // Somehow key kUsersLRUInputMethod has value of invalid type.
64 local_state
->Set(prefs::kUsersLRUInputMethod
, base::DictionaryValue());
66 DictionaryPrefUpdate
updater(local_state
, prefs::kUsersLRUInputMethod
);
67 base::DictionaryValue
* const users_lru_input_methods
= updater
.Get();
68 if (users_lru_input_methods
) {
69 users_lru_input_methods
->SetStringWithoutPathExpansion(username
,
71 update_succeed
= true;
74 if (!update_succeed
) {
75 DVLOG(1) << "Failed to replace local_state.kUsersLRUInputMethod: '"
76 << prefs::kUsersLRUInputMethod
<< "' for '" << username
<< "'";
81 void PersistUserInputMethod(const std::string
& input_method
,
82 InputMethodManager
* const manager
) {
83 // TODO(nkostylev): This feature is currently broken in various ways (see for
84 // example crbug.com/328541). Furthermore it is planned to combine all IME
85 // settings of all users in a session, which means that the user might not
86 // have the used IME setting installed. We therefore do not persist the IME
87 // in a multi profile session. This should be fixed in M34.
88 if (UserManager::IsInitialized() &&
89 UserManager::Get()->GetLoggedInUsers().size() > 1)
92 PrefService
* user_prefs
= NULL
;
93 // Persist the method on a per user basis. Note that the keyboard settings are
94 // stored per user desktop and a visiting window will use the same input
95 // method as the desktop it is on (and not of the owner of the window).
96 Profile
* profile
= ProfileManager::GetActiveUserProfile();
98 user_prefs
= profile
->GetPrefs();
101 SetUserLRUInputMethod(input_method
, manager
, profile
);
103 const std::string current_input_method_on_pref
=
104 user_prefs
->GetString(prefs::kLanguageCurrentInputMethod
);
105 if (current_input_method_on_pref
== input_method
)
108 user_prefs
->SetString(prefs::kLanguagePreviousInputMethod
,
109 current_input_method_on_pref
);
110 user_prefs
->SetString(prefs::kLanguageCurrentInputMethod
,
116 InputMethodPersistence::InputMethodPersistence(
117 InputMethodManager
* input_method_manager
)
118 : input_method_manager_(input_method_manager
),
119 state_(InputMethodManager::STATE_LOGIN_SCREEN
) {
120 input_method_manager_
->AddObserver(this);
123 InputMethodPersistence::~InputMethodPersistence() {
124 input_method_manager_
->RemoveObserver(this);
127 void InputMethodPersistence::InputMethodChanged(
128 InputMethodManager
* manager
, bool show_message
) {
129 DCHECK_EQ(input_method_manager_
, manager
);
130 const std::string current_input_method
=
131 manager
->GetCurrentInputMethod().id();
132 // Save the new input method id depending on the current browser state.
134 case InputMethodManager::STATE_LOGIN_SCREEN
:
135 if (!manager
->IsLoginKeyboard(current_input_method
)) {
136 DVLOG(1) << "Only keyboard layouts are supported: "
137 << current_input_method
;
140 PersistSystemInputMethod(current_input_method
);
142 case InputMethodManager::STATE_BROWSER_SCREEN
:
143 PersistUserInputMethod(current_input_method
, manager
);
145 case InputMethodManager::STATE_LOCK_SCREEN
:
146 // We use a special set of input methods on the screen. Do not update.
148 case InputMethodManager::STATE_TERMINATING
:
154 void InputMethodPersistence::InputMethodPropertyChanged(
155 InputMethodManager
* manager
) {}
157 void InputMethodPersistence::OnSessionStateChange(
158 InputMethodManager::State new_state
) {
162 } // namespace input_method
163 } // namespace chromeos