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

Side by Side Diff: chrome/browser/chromeos/cros/language_library.cc

Issue 1142005: Mocks for all libcros elements (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 months 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/cros/language_library.h" 5 #include "chrome/browser/chromeos/cros/language_library.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "chrome/browser/chrome_thread.h" 9 #include "chrome/browser/chrome_thread.h"
10 #include "chrome/browser/chromeos/cros/cros_library.h" 10 #include "chrome/browser/chromeos/cros/cros_library.h"
11 11
12 // Allows InvokeLater without adding refcounting. This class is a Singleton and 12 // Allows InvokeLater without adding refcounting. This class is a Singleton and
13 // won't be deleted until it's last InvokeLater is run. 13 // won't be deleted until it's last InvokeLater is run.
14 template <> 14 template <>
15 struct RunnableMethodTraits<chromeos::LanguageLibrary> { 15 struct RunnableMethodTraits<chromeos::LanguageLibraryImpl> {
16 void RetainCallee(chromeos::LanguageLibrary* obj) {} 16 void RetainCallee(chromeos::LanguageLibraryImpl* obj) {}
17 void ReleaseCallee(chromeos::LanguageLibrary* obj) {} 17 void ReleaseCallee(chromeos::LanguageLibraryImpl* obj) {}
18 }; 18 };
19 19
20 namespace { 20 namespace {
21 21
22 // Finds a property which has |new_prop.key| from |prop_list|, and replaces the 22 // Finds a property which has |new_prop.key| from |prop_list|, and replaces the
23 // property with |new_prop|. Returns true if such a property is found. 23 // property with |new_prop|. Returns true if such a property is found.
24 bool FindAndUpdateProperty(const chromeos::ImeProperty& new_prop, 24 bool FindAndUpdateProperty(const chromeos::ImeProperty& new_prop,
25 chromeos::ImePropertyList* prop_list) { 25 chromeos::ImePropertyList* prop_list) {
26 for (size_t i = 0; i < prop_list->size(); ++i) { 26 for (size_t i = 0; i < prop_list->size(); ++i) {
27 chromeos::ImeProperty& prop = prop_list->at(i); 27 chromeos::ImeProperty& prop = prop_list->at(i);
28 if (prop.key == new_prop.key) { 28 if (prop.key == new_prop.key) {
29 const int saved_id = prop.selection_item_id; 29 const int saved_id = prop.selection_item_id;
30 // Update the list except the radio id. As written in chromeos_language.h, 30 // Update the list except the radio id. As written in chromeos_language.h,
31 // |prop.selection_item_id| is dummy. 31 // |prop.selection_item_id| is dummy.
32 prop = new_prop; 32 prop = new_prop;
33 prop.selection_item_id = saved_id; 33 prop.selection_item_id = saved_id;
34 return true; 34 return true;
35 } 35 }
36 } 36 }
37 return false; 37 return false;
38 } 38 }
39 39
40 } // namespace 40 } // namespace
41 41
42 namespace chromeos { 42 namespace chromeos {
43 43
44 LanguageLibrary::LanguageLibrary() : language_status_connection_(NULL) { 44 LanguageLibraryImpl::LanguageLibraryImpl() : language_status_connection_(NULL) {
45 } 45 }
46 46
47 LanguageLibrary::~LanguageLibrary() { 47 LanguageLibraryImpl::~LanguageLibraryImpl() {
48 if (EnsureLoadedAndStarted()) { 48 if (language_status_connection_) {
49 chromeos::DisconnectLanguageStatus(language_status_connection_); 49 chromeos::DisconnectLanguageStatus(language_status_connection_);
50 } 50 }
51 } 51 }
52 52
53 LanguageLibrary::Observer::~Observer() { 53 LanguageLibraryImpl::Observer::~Observer() {
54 } 54 }
55 55
56 // static 56 void LanguageLibraryImpl::AddObserver(Observer* observer) {
57 LanguageLibrary* LanguageLibrary::Get() {
58 return Singleton<LanguageLibrary>::get();
59 }
60
61 void LanguageLibrary::AddObserver(Observer* observer) {
62 observers_.AddObserver(observer); 57 observers_.AddObserver(observer);
63 } 58 }
64 59
65 void LanguageLibrary::RemoveObserver(Observer* observer) { 60 void LanguageLibraryImpl::RemoveObserver(Observer* observer) {
66 observers_.RemoveObserver(observer); 61 observers_.RemoveObserver(observer);
67 } 62 }
68 63
69 chromeos::InputLanguageList* LanguageLibrary::GetActiveLanguages() { 64 chromeos::InputLanguageList* LanguageLibraryImpl::GetActiveLanguages() {
70 chromeos::InputLanguageList* result = NULL; 65 chromeos::InputLanguageList* result = NULL;
71 if (EnsureLoadedAndStarted()) { 66 if (EnsureLoadedAndStarted()) {
72 result = chromeos::GetActiveLanguages(language_status_connection_); 67 result = chromeos::GetActiveLanguages(language_status_connection_);
73 } 68 }
74 return result ? result : CreateFallbackInputLanguageList(); 69 return result ? result : CreateFallbackInputLanguageList();
75 } 70 }
76 71
77 chromeos::InputLanguageList* LanguageLibrary::GetSupportedLanguages() { 72 chromeos::InputLanguageList* LanguageLibraryImpl::GetSupportedLanguages() {
78 chromeos::InputLanguageList* result = NULL; 73 chromeos::InputLanguageList* result = NULL;
79 if (EnsureLoadedAndStarted()) { 74 if (EnsureLoadedAndStarted()) {
80 result = chromeos::GetSupportedLanguages(language_status_connection_); 75 result = chromeos::GetSupportedLanguages(language_status_connection_);
81 } 76 }
82 return result ? result : CreateFallbackInputLanguageList(); 77 return result ? result : CreateFallbackInputLanguageList();
83 } 78 }
84 79
85 void LanguageLibrary::ChangeLanguage( 80 void LanguageLibraryImpl::ChangeLanguage(
86 LanguageCategory category, const std::string& id) { 81 LanguageCategory category, const std::string& id) {
87 if (EnsureLoadedAndStarted()) { 82 if (EnsureLoadedAndStarted()) {
88 chromeos::ChangeLanguage(language_status_connection_, category, id.c_str()); 83 chromeos::ChangeLanguage(language_status_connection_, category, id.c_str());
89 } 84 }
90 } 85 }
91 86
92 void LanguageLibrary::ActivateImeProperty(const std::string& key) { 87 void LanguageLibraryImpl::ActivateImeProperty(const std::string& key) {
93 DCHECK(!key.empty()); 88 DCHECK(!key.empty());
94 if (EnsureLoadedAndStarted()) { 89 if (EnsureLoadedAndStarted()) {
95 chromeos::ActivateImeProperty( 90 chromeos::ActivateImeProperty(
96 language_status_connection_, key.c_str()); 91 language_status_connection_, key.c_str());
97 } 92 }
98 } 93 }
99 94
100 void LanguageLibrary::DeactivateImeProperty(const std::string& key) { 95 void LanguageLibraryImpl::DeactivateImeProperty(const std::string& key) {
101 DCHECK(!key.empty()); 96 DCHECK(!key.empty());
102 if (EnsureLoadedAndStarted()) { 97 if (EnsureLoadedAndStarted()) {
103 chromeos::DeactivateImeProperty( 98 chromeos::DeactivateImeProperty(
104 language_status_connection_, key.c_str()); 99 language_status_connection_, key.c_str());
105 } 100 }
106 } 101 }
107 102
108 bool LanguageLibrary::ActivateLanguage( 103 bool LanguageLibraryImpl::ActivateLanguage(
109 LanguageCategory category, const std::string& id) { 104 LanguageCategory category, const std::string& id) {
110 bool success = false; 105 bool success = false;
111 if (EnsureLoadedAndStarted()) { 106 if (EnsureLoadedAndStarted()) {
112 success = chromeos::ActivateLanguage(language_status_connection_, 107 success = chromeos::ActivateLanguage(language_status_connection_,
113 category, id.c_str()); 108 category, id.c_str());
114 } 109 }
115 return success; 110 return success;
116 } 111 }
117 112
118 bool LanguageLibrary::DeactivateLanguage( 113 bool LanguageLibraryImpl::DeactivateLanguage(
119 LanguageCategory category, const std::string& id) { 114 LanguageCategory category, const std::string& id) {
120 bool success = false; 115 bool success = false;
121 if (EnsureLoadedAndStarted()) { 116 if (EnsureLoadedAndStarted()) {
122 success = chromeos::DeactivateLanguage(language_status_connection_, 117 success = chromeos::DeactivateLanguage(language_status_connection_,
123 category, id.c_str()); 118 category, id.c_str());
124 } 119 }
125 return success; 120 return success;
126 } 121 }
127 122
128 bool LanguageLibrary::GetImeConfig( 123 bool LanguageLibraryImpl::GetImeConfig(
129 const char* section, const char* config_name, ImeConfigValue* out_value) { 124 const char* section, const char* config_name, ImeConfigValue* out_value) {
130 bool success = false; 125 bool success = false;
131 if (EnsureLoadedAndStarted()) { 126 if (EnsureLoadedAndStarted()) {
132 success = chromeos::GetImeConfig( 127 success = chromeos::GetImeConfig(
133 language_status_connection_, section, config_name, out_value); 128 language_status_connection_, section, config_name, out_value);
134 } 129 }
135 return success; 130 return success;
136 } 131 }
137 132
138 bool LanguageLibrary::SetImeConfig( 133 bool LanguageLibraryImpl::SetImeConfig(
139 const char* section, const char* config_name, const ImeConfigValue& value) { 134 const char* section, const char* config_name, const ImeConfigValue& value) {
140 bool success = false; 135 bool success = false;
141 if (EnsureLoadedAndStarted()) { 136 if (EnsureLoadedAndStarted()) {
142 success = chromeos::SetImeConfig( 137 success = chromeos::SetImeConfig(
143 language_status_connection_, section, config_name, value); 138 language_status_connection_, section, config_name, value);
144 } 139 }
145 return success; 140 return success;
146 } 141 }
147 142
148 // static 143 // static
149 void LanguageLibrary::LanguageChangedHandler( 144 void LanguageLibraryImpl::LanguageChangedHandler(
150 void* object, const chromeos::InputLanguage& current_language) { 145 void* object, const chromeos::InputLanguage& current_language) {
151 LanguageLibrary* language_library = static_cast<LanguageLibrary*>(object); 146 LanguageLibraryImpl* language_library =
147 static_cast<LanguageLibraryImpl*>(object);
152 language_library->UpdateCurrentLanguage(current_language); 148 language_library->UpdateCurrentLanguage(current_language);
153 } 149 }
154 150
155 // static 151 // static
156 void LanguageLibrary::RegisterPropertiesHandler( 152 void LanguageLibraryImpl::RegisterPropertiesHandler(
157 void* object, const ImePropertyList& prop_list) { 153 void* object, const ImePropertyList& prop_list) {
158 LanguageLibrary* language_library = static_cast<LanguageLibrary*>(object); 154 LanguageLibraryImpl* language_library =
155 static_cast<LanguageLibraryImpl*>(object);
159 language_library->RegisterProperties(prop_list); 156 language_library->RegisterProperties(prop_list);
160 } 157 }
161 158
162 // static 159 // static
163 void LanguageLibrary::UpdatePropertyHandler( 160 void LanguageLibraryImpl::UpdatePropertyHandler(
164 void* object, const ImePropertyList& prop_list) { 161 void* object, const ImePropertyList& prop_list) {
165 LanguageLibrary* language_library = static_cast<LanguageLibrary*>(object); 162 LanguageLibraryImpl* language_library =
163 static_cast<LanguageLibraryImpl*>(object);
166 language_library->UpdateProperty(prop_list); 164 language_library->UpdateProperty(prop_list);
167 } 165 }
168 166
169 bool LanguageLibrary::EnsureStarted() { 167 bool LanguageLibraryImpl::EnsureStarted() {
170 if (language_status_connection_) { 168 if (language_status_connection_) {
171 if (chromeos::LanguageStatusConnectionIsAlive( 169 if (chromeos::LanguageStatusConnectionIsAlive(
172 language_status_connection_)) { 170 language_status_connection_)) {
173 return true; 171 return true;
174 } 172 }
175 DLOG(WARNING) << "IBus/XKB connection is closed. Trying to reconnect..."; 173 DLOG(WARNING) << "IBus/XKB connection is closed. Trying to reconnect...";
176 chromeos::DisconnectLanguageStatus(language_status_connection_); 174 chromeos::DisconnectLanguageStatus(language_status_connection_);
177 } 175 }
178 chromeos::LanguageStatusMonitorFunctions monitor_functions; 176 chromeos::LanguageStatusMonitorFunctions monitor_functions;
179 monitor_functions.current_language = &LanguageChangedHandler; 177 monitor_functions.current_language = &LanguageChangedHandler;
180 monitor_functions.register_ime_properties = &RegisterPropertiesHandler; 178 monitor_functions.register_ime_properties = &RegisterPropertiesHandler;
181 monitor_functions.update_ime_property = &UpdatePropertyHandler; 179 monitor_functions.update_ime_property = &UpdatePropertyHandler;
182 language_status_connection_ 180 language_status_connection_
183 = chromeos::MonitorLanguageStatus(monitor_functions, this); 181 = chromeos::MonitorLanguageStatus(monitor_functions, this);
184 return language_status_connection_ != NULL; 182 return language_status_connection_ != NULL;
185 } 183 }
186 184
187 bool LanguageLibrary::EnsureLoadedAndStarted() { 185 bool LanguageLibraryImpl::EnsureLoadedAndStarted() {
188 return CrosLibrary::EnsureLoaded() && EnsureStarted(); 186 return CrosLibrary::Get()->EnsureLoaded() &&
187 EnsureStarted();
189 } 188 }
190 189
191 void LanguageLibrary::UpdateCurrentLanguage( 190 void LanguageLibraryImpl::UpdateCurrentLanguage(
192 const chromeos::InputLanguage& current_language) { 191 const chromeos::InputLanguage& current_language) {
193 // Make sure we run on UI thread. 192 // Make sure we run on UI thread.
194 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { 193 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
195 DLOG(INFO) << "UpdateCurrentLanguage (Background thread)"; 194 DLOG(INFO) << "UpdateCurrentLanguage (Background thread)";
196 ChromeThread::PostTask( 195 ChromeThread::PostTask(
197 ChromeThread::UI, FROM_HERE, 196 ChromeThread::UI, FROM_HERE,
198 // NewRunnableMethod() copies |current_language| by value. 197 // NewRunnableMethod() copies |current_language| by value.
199 NewRunnableMethod( 198 NewRunnableMethod(
200 this, &LanguageLibrary::UpdateCurrentLanguage, current_language)); 199 this, &LanguageLibraryImpl::UpdateCurrentLanguage,
200 current_language));
201 return; 201 return;
202 } 202 }
203 203
204 DLOG(INFO) << "UpdateCurrentLanguage (UI thread)"; 204 DLOG(INFO) << "UpdateCurrentLanguage (UI thread)";
205 current_language_ = current_language; 205 current_language_ = current_language;
206 FOR_EACH_OBSERVER(Observer, observers_, LanguageChanged(this)); 206 FOR_EACH_OBSERVER(Observer, observers_, LanguageChanged(this));
207 } 207 }
208 208
209 void LanguageLibrary::RegisterProperties(const ImePropertyList& prop_list) { 209 void LanguageLibraryImpl::RegisterProperties(const ImePropertyList& prop_list) {
210 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { 210 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
211 ChromeThread::PostTask( 211 ChromeThread::PostTask(
212 ChromeThread::UI, FROM_HERE, 212 ChromeThread::UI, FROM_HERE,
213 NewRunnableMethod( 213 NewRunnableMethod(
214 this, &LanguageLibrary::RegisterProperties, prop_list)); 214 this, &LanguageLibraryImpl::RegisterProperties, prop_list));
215 return; 215 return;
216 } 216 }
217 217
218 // |prop_list| might be empty. This means "clear all properties." 218 // |prop_list| might be empty. This means "clear all properties."
219 current_ime_properties_ = prop_list; 219 current_ime_properties_ = prop_list;
220 FOR_EACH_OBSERVER(Observer, observers_, ImePropertiesChanged(this)); 220 FOR_EACH_OBSERVER(Observer, observers_, ImePropertiesChanged(this));
221 } 221 }
222 222
223 void LanguageLibrary::UpdateProperty(const ImePropertyList& prop_list) { 223 void LanguageLibraryImpl::UpdateProperty(const ImePropertyList& prop_list) {
224 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { 224 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
225 ChromeThread::PostTask( 225 ChromeThread::PostTask(
226 ChromeThread::UI, FROM_HERE, 226 ChromeThread::UI, FROM_HERE,
227 NewRunnableMethod( 227 NewRunnableMethod(
228 this, &LanguageLibrary::UpdateProperty, prop_list)); 228 this, &LanguageLibraryImpl::UpdateProperty, prop_list));
229 return; 229 return;
230 } 230 }
231 231
232 for (size_t i = 0; i < prop_list.size(); ++i) { 232 for (size_t i = 0; i < prop_list.size(); ++i) {
233 FindAndUpdateProperty(prop_list[i], &current_ime_properties_); 233 FindAndUpdateProperty(prop_list[i], &current_ime_properties_);
234 } 234 }
235 FOR_EACH_OBSERVER(Observer, observers_, ImePropertiesChanged(this)); 235 FOR_EACH_OBSERVER(Observer, observers_, ImePropertiesChanged(this));
236 } 236 }
237 237
238 } // namespace chromeos 238 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/language_library.h ('k') | chrome/browser/chromeos/cros/login_library.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698