Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_persistence.cc
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.
4
5 #include "chrome/browser/chromeos/input_method/input_method_persistence.h"
6
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"
18
19 namespace chromeos {
20 namespace input_method {
21 namespace {
22
23 void PersistSystemInputMethod(const std::string& input_method) {
24   if (!g_browser_process || !g_browser_process->local_state())
25     return;
26
27   g_browser_process->local_state()->SetString(
28         language_prefs::kPreferredKeyboardLayout, input_method);
29 }
30
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,
35     Profile* profile) {
36   // Skip if it's not a keyboard layout. Drop input methods including
37   // extension ones.
38   if (!manager->IsLoginKeyboard(input_method))
39     return;
40
41   PrefService* const local_state = g_browser_process->local_state();
42
43   if (profile == NULL)
44     return;
45
46   const std::string username = profile->GetProfileName();
47   if (base::SysInfo::IsRunningOnChromeOS() && !username.empty() &&
48       !local_state->ReadOnly()) {
49     bool update_succeed = false;
50     {
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,
57                                                                input_method);
58         update_succeed = true;
59       }
60     }
61     if (!update_succeed) {
62       // Somehow key kUsersLRUInputMethod has value of invalid type.
63       // Replace and retry.
64       local_state->Set(prefs::kUsersLRUInputMethod, base::DictionaryValue());
65
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,
70                                                                input_method);
71         update_succeed = true;
72       }
73     }
74     if (!update_succeed) {
75       DVLOG(1) << "Failed to replace local_state.kUsersLRUInputMethod: '"
76                << prefs::kUsersLRUInputMethod << "' for '" << username << "'";
77     }
78   }
79 }
80
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)
90     return;
91
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();
97   if (profile)
98     user_prefs = profile->GetPrefs();
99   if (!user_prefs)
100     return;
101   SetUserLRUInputMethod(input_method, manager, profile);
102
103   const std::string current_input_method_on_pref =
104       user_prefs->GetString(prefs::kLanguageCurrentInputMethod);
105   if (current_input_method_on_pref == input_method)
106     return;
107
108   user_prefs->SetString(prefs::kLanguagePreviousInputMethod,
109                         current_input_method_on_pref);
110   user_prefs->SetString(prefs::kLanguageCurrentInputMethod,
111                         input_method);
112 }
113
114 }  // namespace
115
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);
121 }
122
123 InputMethodPersistence::~InputMethodPersistence() {
124   input_method_manager_->RemoveObserver(this);
125 }
126
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.
133   switch (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;
138         return;
139       }
140       PersistSystemInputMethod(current_input_method);
141       return;
142     case InputMethodManager::STATE_BROWSER_SCREEN:
143       PersistUserInputMethod(current_input_method, manager);
144       return;
145     case InputMethodManager::STATE_LOCK_SCREEN:
146       // We use a special set of input methods on the screen. Do not update.
147       return;
148     case InputMethodManager::STATE_TERMINATING:
149       return;
150   }
151   NOTREACHED();
152 }
153
154 void InputMethodPersistence::InputMethodPropertyChanged(
155     InputMethodManager* manager) {}
156
157 void InputMethodPersistence::OnSessionStateChange(
158     InputMethodManager::State new_state) {
159   state_ = new_state;
160 }
161
162 }  // namespace input_method
163 }  // namespace chromeos