Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: chrome/browser/chromeos/input_method/browser_state_monitor.cc

Issue 11415266: Extract a delegate interface from c/b/input_method to permit decoupling from c/b. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Class comment. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/browser_state_monitor.h" 5 #include "chrome/browser/chromeos/input_method/browser_state_monitor.h"
6 6
7 #include "chrome/browser/browser_process.h" 7 #include "base/logging.h"
8 #include "chrome/browser/chromeos/input_method/input_method_delegate.h"
8 #include "chrome/browser/chromeos/input_method/input_method_util.h" 9 #include "chrome/browser/chromeos/input_method/input_method_util.h"
9 #include "chrome/browser/chromeos/language_preferences.h"
10 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/common/chrome_notification_types.h" 10 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/pref_names.h"
14 #include "content/public/browser/notification_service.h" 11 #include "content/public/browser/notification_service.h"
15 12
16 namespace chromeos { 13 namespace chromeos {
17 namespace input_method { 14 namespace input_method {
18 namespace {
19 15
20 PrefService* GetPrefService() { 16 BrowserStateMonitor::BrowserStateMonitor(InputMethodManager* manager,
21 Profile* profile = ProfileManager::GetDefaultProfile(); 17 InputMethodDelegate* delegate)
22 if (profile)
23 return profile->GetPrefs();
24 return NULL;
25 }
26
27 } // namespace
28
29 BrowserStateMonitor::BrowserStateMonitor(InputMethodManager* manager)
30 : manager_(manager), 18 : manager_(manager),
31 state_(InputMethodManager::STATE_LOGIN_SCREEN), 19 delegate_(delegate),
32 pref_service_(NULL) { 20 state_(InputMethodManager::STATE_LOGIN_SCREEN) {
33 notification_registrar_.Add(this, 21 notification_registrar_.Add(this,
34 chrome::NOTIFICATION_LOGIN_USER_CHANGED, 22 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
35 content::NotificationService::AllSources()); 23 content::NotificationService::AllSources());
36 notification_registrar_.Add(this, 24 notification_registrar_.Add(this,
37 chrome::NOTIFICATION_SESSION_STARTED, 25 chrome::NOTIFICATION_SESSION_STARTED,
38 content::NotificationService::AllSources()); 26 content::NotificationService::AllSources());
39 notification_registrar_.Add(this, 27 notification_registrar_.Add(this,
40 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, 28 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
41 content::NotificationService::AllSources()); 29 content::NotificationService::AllSources());
42 // We should not use ALL_BROWSERS_CLOSING here since logout might be cancelled 30 // We should not use ALL_BROWSERS_CLOSING here since logout might be cancelled
43 // by JavaScript after ALL_BROWSERS_CLOSING is sent (crosbug.com/11055). 31 // by JavaScript after ALL_BROWSERS_CLOSING is sent (crosbug.com/11055).
44 notification_registrar_.Add(this, 32 notification_registrar_.Add(this,
45 chrome::NOTIFICATION_APP_TERMINATING, 33 chrome::NOTIFICATION_APP_TERMINATING,
46 content::NotificationService::AllSources()); 34 content::NotificationService::AllSources());
47 35
48 manager_->SetState(state_); 36 manager_->SetState(state_);
49 manager_->AddObserver(this); 37 manager_->AddObserver(this);
50 } 38 }
51 39
52 BrowserStateMonitor::~BrowserStateMonitor() { 40 BrowserStateMonitor::~BrowserStateMonitor() {
53 manager_->RemoveObserver(this); 41 manager_->RemoveObserver(this);
54 } 42 }
55 43
56 void BrowserStateMonitor::SetPrefServiceForTesting(PrefService* pref_service) {
57 pref_service_ = pref_service;
58 }
59
60 void BrowserStateMonitor::UpdateLocalState(
61 const std::string& current_input_method) {
62 if (!g_browser_process || !g_browser_process->local_state())
63 return;
64 g_browser_process->local_state()->SetString(
65 language_prefs::kPreferredKeyboardLayout,
66 current_input_method);
67 }
68
69 void BrowserStateMonitor::UpdateUserPreferences(
70 const std::string& current_input_method) {
71 PrefService* pref_service = pref_service_ ? pref_service_ : GetPrefService();
72 DCHECK(pref_service);
73
74 // Even though we're DCHECK'ing to catch this on debug builds, we don't
75 // want to crash a release build in case the pref service is no longer
76 // available.
77 if (!pref_service)
78 return;
79
80 const std::string current_input_method_on_pref =
81 pref_service->GetString(prefs::kLanguageCurrentInputMethod);
82 if (current_input_method_on_pref == current_input_method)
83 return;
84
85 pref_service->SetString(prefs::kLanguagePreviousInputMethod,
86 current_input_method_on_pref);
87 pref_service->SetString(prefs::kLanguageCurrentInputMethod,
88 current_input_method);
89 }
90
91 void BrowserStateMonitor::InputMethodChanged(InputMethodManager* manager, 44 void BrowserStateMonitor::InputMethodChanged(InputMethodManager* manager,
92 bool show_message) { 45 bool show_message) {
93 DCHECK_EQ(manager_, manager); 46 DCHECK_EQ(manager_, manager);
94 const std::string current_input_method = 47 const std::string current_input_method =
95 manager->GetCurrentInputMethod().id(); 48 manager->GetCurrentInputMethod().id();
96 // Save the new input method id depending on the current browser state. 49 // Save the new input method id depending on the current browser state.
97 switch (state_) { 50 switch (state_) {
98 case InputMethodManager::STATE_LOGIN_SCREEN: 51 case InputMethodManager::STATE_LOGIN_SCREEN:
99 if (!InputMethodUtil::IsKeyboardLayout(current_input_method)) { 52 if (!InputMethodUtil::IsKeyboardLayout(current_input_method)) {
100 DVLOG(1) << "Only keyboard layouts are supported: " 53 DVLOG(1) << "Only keyboard layouts are supported: "
101 << current_input_method; 54 << current_input_method;
102 return; 55 return;
103 } 56 }
104 UpdateLocalState(current_input_method); 57 delegate_->SetSystemInputMethod(current_input_method);
105 return; 58 return;
106 case InputMethodManager::STATE_BROWSER_SCREEN: 59 case InputMethodManager::STATE_BROWSER_SCREEN:
107 UpdateUserPreferences(current_input_method); 60 delegate_->SetUserInputMethod(current_input_method);
108 return; 61 return;
109 case InputMethodManager::STATE_LOCK_SCREEN: 62 case InputMethodManager::STATE_LOCK_SCREEN:
110 // We use a special set of input methods on the screen. Do not update. 63 // We use a special set of input methods on the screen. Do not update.
111 return; 64 return;
112 case InputMethodManager::STATE_TERMINATING: 65 case InputMethodManager::STATE_TERMINATING:
113 return; 66 return;
114 } 67 }
115 NOTREACHED(); 68 NOTREACHED();
116 } 69 }
117 70
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 135
183 void BrowserStateMonitor::SetState(InputMethodManager::State new_state) { 136 void BrowserStateMonitor::SetState(InputMethodManager::State new_state) {
184 const InputMethodManager::State old_state = state_; 137 const InputMethodManager::State old_state = state_;
185 state_ = new_state; 138 state_ = new_state;
186 if (old_state != state_) 139 if (old_state != state_)
187 manager_->SetState(state_); 140 manager_->SetState(state_);
188 } 141 }
189 142
190 } // namespace input_method 143 } // namespace input_method
191 } // namespace chromeos 144 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698