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/extensions/input_method_api.h" | 5 #include "chrome/browser/chromeos/extensions/input_method_api.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/extensions/dictionary_event_router.h" |
10 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 11 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
11 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 12 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
12 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 13 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
13 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 14 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 15 #include "chrome/browser/spellchecker/spellcheck_service.h" |
15 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
16 #include "extensions/browser/extension_function_registry.h" | 17 #include "extensions/browser/extension_function_registry.h" |
17 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
18 #include "ui/base/ime/chromeos/extension_ime_util.h" | 19 #include "ui/base/ime/chromeos/extension_ime_util.h" |
19 #include "ui/base/ime/chromeos/input_method_descriptor.h" | 20 #include "ui/base/ime/chromeos/input_method_descriptor.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // - Already in the dictionary. | 150 // - Already in the dictionary. |
150 // - Not a UTF8 string. | 151 // - Not a UTF8 string. |
151 // - Longer than 99 bytes (MAX_CUSTOM_DICTIONARY_WORD_BYTES). | 152 // - Longer than 99 bytes (MAX_CUSTOM_DICTIONARY_WORD_BYTES). |
152 // - Leading/trailing whitespace. | 153 // - Leading/trailing whitespace. |
153 // - Empty. | 154 // - Empty. |
154 return RespondNow(Error("Unable to add invalid word to dictionary.")); | 155 return RespondNow(Error("Unable to add invalid word to dictionary.")); |
155 #endif | 156 #endif |
156 } | 157 } |
157 | 158 |
158 // static | 159 // static |
| 160 const char InputMethodAPI::kOnDictionaryChanged[] = |
| 161 "inputMethodPrivate.onDictionaryChanged"; |
| 162 |
| 163 // static |
| 164 const char InputMethodAPI::kOnDictionaryLoaded[] = |
| 165 "inputMethodPrivate.onDictionaryLoaded"; |
| 166 |
| 167 // static |
159 const char InputMethodAPI::kOnInputMethodChanged[] = | 168 const char InputMethodAPI::kOnInputMethodChanged[] = |
160 "inputMethodPrivate.onChanged"; | 169 "inputMethodPrivate.onChanged"; |
161 | 170 |
162 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) | 171 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) |
163 : context_(context) { | 172 : context_(context) { |
164 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); | 173 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); |
| 174 EventRouter::Get(context_)->RegisterObserver(this, kOnDictionaryChanged); |
| 175 EventRouter::Get(context_)->RegisterObserver(this, kOnDictionaryLoaded); |
165 ExtensionFunctionRegistry* registry = | 176 ExtensionFunctionRegistry* registry = |
166 ExtensionFunctionRegistry::GetInstance(); | 177 ExtensionFunctionRegistry::GetInstance(); |
167 registry->RegisterFunction<GetInputMethodConfigFunction>(); | 178 registry->RegisterFunction<GetInputMethodConfigFunction>(); |
168 registry->RegisterFunction<GetCurrentInputMethodFunction>(); | 179 registry->RegisterFunction<GetCurrentInputMethodFunction>(); |
169 registry->RegisterFunction<SetCurrentInputMethodFunction>(); | 180 registry->RegisterFunction<SetCurrentInputMethodFunction>(); |
170 registry->RegisterFunction<GetInputMethodsFunction>(); | 181 registry->RegisterFunction<GetInputMethodsFunction>(); |
171 registry->RegisterFunction<FetchAllDictionaryWordsFunction>(); | 182 registry->RegisterFunction<FetchAllDictionaryWordsFunction>(); |
172 registry->RegisterFunction<AddWordToDictionaryFunction>(); | 183 registry->RegisterFunction<AddWordToDictionaryFunction>(); |
173 } | 184 } |
174 | 185 |
175 InputMethodAPI::~InputMethodAPI() { | 186 InputMethodAPI::~InputMethodAPI() { |
176 } | 187 } |
177 | 188 |
178 // static | 189 // static |
179 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { | 190 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
180 std::string xkb_prefix = | 191 std::string xkb_prefix = |
181 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); | 192 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); |
182 size_t prefix_length = xkb_prefix.length(); | 193 size_t prefix_length = xkb_prefix.length(); |
183 DCHECK(xkb_id.substr(0, prefix_length) == xkb_prefix); | 194 DCHECK(xkb_id.substr(0, prefix_length) == xkb_prefix); |
184 return xkb_id.substr(prefix_length); | 195 return xkb_id.substr(prefix_length); |
185 } | 196 } |
186 | 197 |
187 void InputMethodAPI::Shutdown() { | 198 void InputMethodAPI::Shutdown() { |
188 // UnregisterObserver may have already been called in OnListenerAdded, | |
189 // but it is safe to call it more than once. | |
190 EventRouter::Get(context_)->UnregisterObserver(this); | 199 EventRouter::Get(context_)->UnregisterObserver(this); |
191 } | 200 } |
192 | 201 |
193 void InputMethodAPI::OnListenerAdded( | 202 void InputMethodAPI::OnListenerAdded( |
194 const extensions::EventListenerInfo& details) { | 203 const extensions::EventListenerInfo& details) { |
195 DCHECK(!input_method_event_router_.get()); | 204 if (details.event_name == kOnInputMethodChanged) { |
196 input_method_event_router_.reset( | 205 if (!input_method_event_router_.get()) { |
197 new chromeos::ExtensionInputMethodEventRouter(context_)); | 206 input_method_event_router_.reset( |
198 EventRouter::Get(context_)->UnregisterObserver(this); | 207 new chromeos::ExtensionInputMethodEventRouter(context_)); |
| 208 } |
| 209 } else if (details.event_name == kOnDictionaryChanged || |
| 210 details.event_name == kOnDictionaryLoaded) { |
| 211 if (!dictionary_event_router_.get()) { |
| 212 dictionary_event_router_.reset( |
| 213 new chromeos::ExtensionDictionaryEventRouter(context_)); |
| 214 } |
| 215 if (details.event_name == kOnDictionaryLoaded) { |
| 216 dictionary_event_router_->DispatchLoadedEventIfLoaded(); |
| 217 } |
| 218 } |
199 } | 219 } |
200 | 220 |
201 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > | 221 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > |
202 g_factory = LAZY_INSTANCE_INITIALIZER; | 222 g_factory = LAZY_INSTANCE_INITIALIZER; |
203 | 223 |
204 // static | 224 // static |
205 BrowserContextKeyedAPIFactory<InputMethodAPI>* | 225 BrowserContextKeyedAPIFactory<InputMethodAPI>* |
206 InputMethodAPI::GetFactoryInstance() { | 226 InputMethodAPI::GetFactoryInstance() { |
207 return g_factory.Pointer(); | 227 return g_factory.Pointer(); |
208 } | 228 } |
209 | 229 |
210 } // namespace extensions | 230 } // namespace extensions |
OLD | NEW |