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/input_method/input_method_persistence.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_persistence.h" |
6 | 6 |
| 7 #include "base/chromeos/chromeos_version.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 11 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
11 #include "chrome/browser/chromeos/language_preferences.h" | 12 #include "chrome/browser/chromeos/language_preferences.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
12 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
15 | 17 |
16 namespace chromeos { | 18 namespace chromeos { |
17 namespace input_method { | 19 namespace input_method { |
18 namespace { | 20 namespace { |
19 | 21 |
20 void PersistSystemInputMethod(const std::string& input_method) { | 22 void PersistSystemInputMethod(const std::string& input_method) { |
21 if (!g_browser_process || !g_browser_process->local_state()) | 23 if (!g_browser_process || !g_browser_process->local_state()) |
22 return; | 24 return; |
23 | 25 |
24 g_browser_process->local_state()->SetString( | 26 g_browser_process->local_state()->SetString( |
25 language_prefs::kPreferredKeyboardLayout, input_method); | 27 language_prefs::kPreferredKeyboardLayout, input_method); |
26 } | 28 } |
27 | 29 |
28 void PersistUserInputMethod(const std::string& input_method) { | 30 // Update user LRU keyboard layout for login screen |
| 31 static void SetUserLRUInputMethod( |
| 32 const std::string& input_method, |
| 33 const chromeos::input_method::InputMethodManager* const manager) { |
| 34 // Skip if it's not a keyboard layout. Drop input methods including |
| 35 // extension ones. |
| 36 if (!InputMethodUtil::IsKeyboardLayout(input_method)) |
| 37 return; |
| 38 |
| 39 PrefService* const local_state = g_browser_process->local_state(); |
| 40 |
| 41 Profile* const profile = ProfileManager::GetDefaultProfile(); |
| 42 |
| 43 if (profile == NULL) |
| 44 return; |
| 45 |
| 46 if (!manager->IsFullLatinKeyboard(input_method)) |
| 47 return; |
| 48 |
| 49 const std::string username = profile->GetProfileName(); |
| 50 if (base::chromeos::IsRunningOnChromeOS() && !username.empty() && |
| 51 !local_state->ReadOnly()) { |
| 52 bool update_succeed = false; |
| 53 { |
| 54 // Updater may have side-effects, therefore we do not replace |
| 55 // entry while updater exists. |
| 56 DictionaryPrefUpdate updater(local_state, prefs::kUsersLRUInputMethod); |
| 57 base::DictionaryValue* const users_lru_input_methods = updater.Get(); |
| 58 if (users_lru_input_methods) { |
| 59 users_lru_input_methods->SetStringWithoutPathExpansion(username, |
| 60 input_method); |
| 61 update_succeed = true; |
| 62 } |
| 63 } |
| 64 if (!update_succeed) { |
| 65 // Somehow key kUsersLRUInputMethod has value of invalid type. |
| 66 // Replace and retry. |
| 67 local_state->Set(prefs::kUsersLRUInputMethod, base::DictionaryValue()); |
| 68 |
| 69 DictionaryPrefUpdate updater(local_state, prefs::kUsersLRUInputMethod); |
| 70 base::DictionaryValue* const users_lru_input_methods = updater.Get(); |
| 71 if (users_lru_input_methods) { |
| 72 users_lru_input_methods->SetStringWithoutPathExpansion(username, |
| 73 input_method); |
| 74 update_succeed = true; |
| 75 } |
| 76 } |
| 77 if (!update_succeed) { |
| 78 DVLOG(1) << "Failed to replace local_state.kUsersLRUInputMethod: '" |
| 79 << prefs::kUsersLRUInputMethod << "' for '" << username << "'"; |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 void PersistUserInputMethod(const std::string& input_method, |
| 85 InputMethodManager* const manager) { |
29 PrefService* user_prefs = NULL; | 86 PrefService* user_prefs = NULL; |
30 Profile* profile = ProfileManager::GetDefaultProfile(); | 87 Profile* profile = ProfileManager::GetDefaultProfile(); |
31 if (profile) | 88 if (profile) |
32 user_prefs = profile->GetPrefs(); | 89 user_prefs = profile->GetPrefs(); |
33 if (!user_prefs) | 90 if (!user_prefs) |
34 return; | 91 return; |
35 | 92 |
| 93 SetUserLRUInputMethod(input_method, manager); |
| 94 |
36 const std::string current_input_method_on_pref = | 95 const std::string current_input_method_on_pref = |
37 user_prefs->GetString(prefs::kLanguageCurrentInputMethod); | 96 user_prefs->GetString(prefs::kLanguageCurrentInputMethod); |
38 if (current_input_method_on_pref == input_method) | 97 if (current_input_method_on_pref == input_method) |
39 return; | 98 return; |
40 | 99 |
41 user_prefs->SetString(prefs::kLanguagePreviousInputMethod, | 100 user_prefs->SetString(prefs::kLanguagePreviousInputMethod, |
42 current_input_method_on_pref); | 101 current_input_method_on_pref); |
43 user_prefs->SetString(prefs::kLanguageCurrentInputMethod, | 102 user_prefs->SetString(prefs::kLanguageCurrentInputMethod, |
44 input_method); | 103 input_method); |
45 } | 104 } |
(...skipping 20 matching lines...) Expand all Loading... |
66 switch (state_) { | 125 switch (state_) { |
67 case InputMethodManager::STATE_LOGIN_SCREEN: | 126 case InputMethodManager::STATE_LOGIN_SCREEN: |
68 if (!InputMethodUtil::IsKeyboardLayout(current_input_method)) { | 127 if (!InputMethodUtil::IsKeyboardLayout(current_input_method)) { |
69 DVLOG(1) << "Only keyboard layouts are supported: " | 128 DVLOG(1) << "Only keyboard layouts are supported: " |
70 << current_input_method; | 129 << current_input_method; |
71 return; | 130 return; |
72 } | 131 } |
73 PersistSystemInputMethod(current_input_method); | 132 PersistSystemInputMethod(current_input_method); |
74 return; | 133 return; |
75 case InputMethodManager::STATE_BROWSER_SCREEN: | 134 case InputMethodManager::STATE_BROWSER_SCREEN: |
76 PersistUserInputMethod(current_input_method); | 135 PersistUserInputMethod(current_input_method, manager); |
77 return; | 136 return; |
78 case InputMethodManager::STATE_LOCK_SCREEN: | 137 case InputMethodManager::STATE_LOCK_SCREEN: |
79 // We use a special set of input methods on the screen. Do not update. | 138 // We use a special set of input methods on the screen. Do not update. |
80 return; | 139 return; |
81 case InputMethodManager::STATE_TERMINATING: | 140 case InputMethodManager::STATE_TERMINATING: |
82 return; | 141 return; |
83 } | 142 } |
84 NOTREACHED(); | 143 NOTREACHED(); |
85 } | 144 } |
86 | 145 |
87 void InputMethodPersistence::InputMethodPropertyChanged( | 146 void InputMethodPersistence::InputMethodPropertyChanged( |
88 InputMethodManager* manager) {} | 147 InputMethodManager* manager) {} |
89 | 148 |
90 void InputMethodPersistence::OnSessionStateChange( | 149 void InputMethodPersistence::OnSessionStateChange( |
91 InputMethodManager::State new_state) { | 150 InputMethodManager::State new_state) { |
92 state_ = new_state; | 151 state_ = new_state; |
93 } | 152 } |
94 | 153 |
95 } // namespace input_method | 154 } // namespace input_method |
96 } // namespace chromeos | 155 } // namespace chromeos |
OLD | NEW |