OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/login/lock_screen_utils.h" |
| 6 |
| 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/chromeos/language_preferences.h" |
| 9 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "components/prefs/scoped_user_pref_update.h" |
| 12 #include "components/signin/core/account_id/account_id.h" |
| 13 #include "components/user_manager/known_user.h" |
| 14 #include "ui/base/ime/chromeos/ime_keyboard.h" |
| 15 |
| 16 namespace chromeos { |
| 17 namespace lock_screen_utils { |
| 18 |
| 19 void SetUserInputMethod(const std::string& username, |
| 20 input_method::InputMethodManager::State* ime_state) { |
| 21 bool succeed = false; |
| 22 |
| 23 const std::string input_method = GetUserLastInputMethod(username); |
| 24 |
| 25 EnforcePolicyInputMethods(input_method); |
| 26 |
| 27 if (!input_method.empty()) |
| 28 succeed = SetUserInputMethodImpl(username, input_method, ime_state); |
| 29 |
| 30 // This is also a case when last layout is set only for a few local users, |
| 31 // thus others need to be switched to default locale. |
| 32 // Otherwise they will end up using another user's locale to log in. |
| 33 if (!succeed) { |
| 34 DVLOG(0) << "SetUserInputMethod: failed to set user layout. Switching to " |
| 35 "default."; |
| 36 |
| 37 ime_state->SetInputMethodLoginDefault(); |
| 38 } |
| 39 } |
| 40 |
| 41 std::string GetUserLastInputMethod(const std::string& username) { |
| 42 PrefService* const local_state = g_browser_process->local_state(); |
| 43 const base::DictionaryValue* users_last_input_methods = |
| 44 local_state->GetDictionary(prefs::kUsersLastInputMethod); |
| 45 |
| 46 if (!users_last_input_methods) { |
| 47 DLOG(WARNING) << "GetUserLastInputMethod: no kUsersLastInputMethod"; |
| 48 return std::string(); |
| 49 } |
| 50 |
| 51 std::string input_method; |
| 52 |
| 53 if (!users_last_input_methods->GetStringWithoutPathExpansion(username, |
| 54 &input_method)) { |
| 55 DVLOG(0) << "GetUserLastInputMethod: no input method for this user"; |
| 56 return std::string(); |
| 57 } |
| 58 |
| 59 return input_method; |
| 60 } |
| 61 |
| 62 bool SetUserInputMethodImpl( |
| 63 const std::string& username, |
| 64 const std::string& user_input_method, |
| 65 input_method::InputMethodManager::State* ime_state) { |
| 66 if (!chromeos::input_method::InputMethodManager::Get()->IsLoginKeyboard( |
| 67 user_input_method)) { |
| 68 LOG(WARNING) << "SetUserInputMethod: stored user last input method '" |
| 69 << user_input_method |
| 70 << "' is no longer Full Latin Keyboard Language" |
| 71 << " (entry dropped). Use hardware default instead."; |
| 72 |
| 73 PrefService* const local_state = g_browser_process->local_state(); |
| 74 DictionaryPrefUpdate updater(local_state, prefs::kUsersLastInputMethod); |
| 75 |
| 76 base::DictionaryValue* const users_last_input_methods = updater.Get(); |
| 77 if (users_last_input_methods) |
| 78 users_last_input_methods->SetStringWithoutPathExpansion(username, ""); |
| 79 return false; |
| 80 } |
| 81 if (!base::ContainsValue(ime_state->GetActiveInputMethodIds(), |
| 82 user_input_method)) { |
| 83 if (!ime_state->EnableInputMethod(user_input_method)) { |
| 84 DLOG(ERROR) << "SetUserInputMethod: user input method '" |
| 85 << user_input_method |
| 86 << "' is not enabled and enabling failed (ignored!)."; |
| 87 } |
| 88 } |
| 89 ime_state->ChangeInputMethod(user_input_method, false /* show_message */); |
| 90 |
| 91 return true; |
| 92 } |
| 93 |
| 94 void EnforcePolicyInputMethods(std::string user_input_method) { |
| 95 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get(); |
| 96 const base::ListValue* login_screen_input_methods = nullptr; |
| 97 if (!cros_settings->GetList(chromeos::kDeviceLoginScreenInputMethods, |
| 98 &login_screen_input_methods)) { |
| 99 return; |
| 100 } |
| 101 |
| 102 std::vector<std::string> allowed_input_methods; |
| 103 |
| 104 // Add user's input method first so it is pre-selected. |
| 105 if (!user_input_method.empty()) { |
| 106 allowed_input_methods.push_back(user_input_method); |
| 107 } |
| 108 |
| 109 std::string input_method; |
| 110 for (const auto& input_method_entry : *login_screen_input_methods) { |
| 111 if (input_method_entry.GetAsString(&input_method)) |
| 112 allowed_input_methods.push_back(input_method); |
| 113 } |
| 114 chromeos::input_method::InputMethodManager* imm = |
| 115 chromeos::input_method::InputMethodManager::Get(); |
| 116 imm->GetActiveIMEState()->SetAllowedInputMethods(allowed_input_methods); |
| 117 } |
| 118 |
| 119 void SetKeyboardSettings(const AccountId& account_id) { |
| 120 bool auto_repeat_enabled = language_prefs::kXkbAutoRepeatEnabled; |
| 121 if (user_manager::known_user::GetBooleanPref( |
| 122 account_id, prefs::kLanguageXkbAutoRepeatEnabled, |
| 123 &auto_repeat_enabled) && |
| 124 !auto_repeat_enabled) { |
| 125 input_method::InputMethodManager::Get() |
| 126 ->GetImeKeyboard() |
| 127 ->SetAutoRepeatEnabled(false); |
| 128 return; |
| 129 } |
| 130 |
| 131 int auto_repeat_delay = language_prefs::kXkbAutoRepeatDelayInMs; |
| 132 int auto_repeat_interval = language_prefs::kXkbAutoRepeatIntervalInMs; |
| 133 user_manager::known_user::GetIntegerPref( |
| 134 account_id, prefs::kLanguageXkbAutoRepeatDelay, &auto_repeat_delay); |
| 135 user_manager::known_user::GetIntegerPref( |
| 136 account_id, prefs::kLanguageXkbAutoRepeatInterval, &auto_repeat_interval); |
| 137 input_method::AutoRepeatRate rate; |
| 138 rate.initial_delay_in_ms = auto_repeat_delay; |
| 139 rate.repeat_interval_in_ms = auto_repeat_interval; |
| 140 input_method::InputMethodManager::Get() |
| 141 ->GetImeKeyboard() |
| 142 ->SetAutoRepeatEnabled(true); |
| 143 input_method::InputMethodManager::Get()->GetImeKeyboard()->SetAutoRepeatRate( |
| 144 rate); |
| 145 } |
| 146 |
| 147 } // namespace lock_screen_utils |
| 148 } // namespace chromeos |
OLD | NEW |