Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "chrome/browser/chromeos/extensions/dictionary_event_router.h" | 16 #include "chrome/browser/chromeos/extensions/dictionary_event_router.h" |
| 17 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 17 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
| 18 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 18 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 19 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 19 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
| 20 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 20 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 21 #include "chrome/browser/spellchecker/spellcheck_service.h" | 21 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 22 #include "chrome/browser/sync/profile_sync_service_factory.h" | 22 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 23 #include "chrome/common/extensions/api/input_method_private.h" | |
| 23 #include "chromeos/chromeos_switches.h" | 24 #include "chromeos/chromeos_switches.h" |
| 24 #include "components/browser_sync/browser/profile_sync_service.h" | 25 #include "components/browser_sync/browser/profile_sync_service.h" |
| 25 #include "extensions/browser/extension_function_registry.h" | 26 #include "extensions/browser/extension_function_registry.h" |
| 26 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
| 27 #include "ui/base/ime/chromeos/extension_ime_util.h" | 28 #include "ui/base/ime/chromeos/extension_ime_util.h" |
| 28 #include "ui/base/ime/chromeos/input_method_descriptor.h" | 29 #include "ui/base/ime/chromeos/input_method_descriptor.h" |
| 29 #include "ui/base/ime/chromeos/input_method_manager.h" | 30 #include "ui/base/ime/chromeos/input_method_manager.h" |
| 30 #include "ui/keyboard/keyboard_util.h" | 31 #include "ui/keyboard/keyboard_util.h" |
| 31 | 32 |
| 33 namespace OnChanged = extensions::api::input_method_private::OnChanged; | |
| 34 namespace OnDictionaryChanged = | |
| 35 extensions::api::input_method_private::OnDictionaryChanged; | |
| 36 namespace OnDictionaryLoaded = | |
| 37 extensions::api::input_method_private::OnDictionaryLoaded; | |
| 38 | |
| 32 namespace { | 39 namespace { |
| 33 | 40 |
| 34 // Prefix, which is used by XKB. | 41 // Prefix, which is used by XKB. |
| 35 const char kXkbPrefix[] = "xkb:"; | 42 const char kXkbPrefix[] = "xkb:"; |
| 36 | 43 |
| 37 } // namespace | 44 } // namespace |
| 38 | 45 |
| 39 namespace extensions { | 46 namespace extensions { |
| 40 | 47 |
| 41 ExtensionFunction::ResponseAction GetInputMethodConfigFunction::Run() { | 48 ExtensionFunction::ResponseAction |
| 49 InputMethodPrivateGetInputMethodConfigFunction::Run() { | |
| 42 #if !defined(OS_CHROMEOS) | 50 #if !defined(OS_CHROMEOS) |
| 43 EXTENSION_FUNCTION_VALIDATE(false); | 51 EXTENSION_FUNCTION_VALIDATE(false); |
| 44 #else | 52 #else |
| 45 base::DictionaryValue* output = new base::DictionaryValue(); | 53 base::DictionaryValue* output = new base::DictionaryValue(); |
| 46 output->SetBoolean( | 54 output->SetBoolean( |
| 47 "isPhysicalKeyboardAutocorrectEnabled", | 55 "isPhysicalKeyboardAutocorrectEnabled", |
| 48 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 56 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 49 chromeos::switches::kDisablePhysicalKeyboardAutocorrect)); | 57 chromeos::switches::kDisablePhysicalKeyboardAutocorrect)); |
| 50 return RespondNow(OneArgument(output)); | 58 return RespondNow(OneArgument(output)); |
| 51 #endif | 59 #endif |
| 52 } | 60 } |
| 53 | 61 |
| 54 ExtensionFunction::ResponseAction GetCurrentInputMethodFunction::Run() { | 62 ExtensionFunction::ResponseAction |
| 63 InputMethodPrivateGetCurrentInputMethodFunction::Run() { | |
| 55 #if !defined(OS_CHROMEOS) | 64 #if !defined(OS_CHROMEOS) |
| 56 EXTENSION_FUNCTION_VALIDATE(false); | 65 EXTENSION_FUNCTION_VALIDATE(false); |
| 57 #else | 66 #else |
| 58 chromeos::input_method::InputMethodManager* manager = | 67 chromeos::input_method::InputMethodManager* manager = |
| 59 chromeos::input_method::InputMethodManager::Get(); | 68 chromeos::input_method::InputMethodManager::Get(); |
| 60 return RespondNow(OneArgument(new base::StringValue( | 69 return RespondNow(OneArgument(new base::StringValue( |
| 61 manager->GetActiveIMEState()->GetCurrentInputMethod().id()))); | 70 manager->GetActiveIMEState()->GetCurrentInputMethod().id()))); |
| 62 #endif | 71 #endif |
| 63 } | 72 } |
| 64 | 73 |
| 65 ExtensionFunction::ResponseAction SetCurrentInputMethodFunction::Run() { | 74 ExtensionFunction::ResponseAction |
| 75 InputMethodPrivateSetCurrentInputMethodFunction::Run() { | |
| 66 #if !defined(OS_CHROMEOS) | 76 #if !defined(OS_CHROMEOS) |
| 67 EXTENSION_FUNCTION_VALIDATE(false); | 77 EXTENSION_FUNCTION_VALIDATE(false); |
| 68 #else | 78 #else |
| 69 std::string new_input_method; | 79 std::string new_input_method; |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method)); | 80 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method)); |
| 71 scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state = | 81 scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state = |
| 72 chromeos::input_method::InputMethodManager::Get()->GetActiveIMEState(); | 82 chromeos::input_method::InputMethodManager::Get()->GetActiveIMEState(); |
| 73 const std::vector<std::string>& input_methods = | 83 const std::vector<std::string>& input_methods = |
| 74 ime_state->GetActiveInputMethodIds(); | 84 ime_state->GetActiveInputMethodIds(); |
| 75 for (size_t i = 0; i < input_methods.size(); ++i) { | 85 for (size_t i = 0; i < input_methods.size(); ++i) { |
| 76 const std::string& input_method = input_methods[i]; | 86 const std::string& input_method = input_methods[i]; |
| 77 if (input_method == new_input_method) { | 87 if (input_method == new_input_method) { |
| 78 ime_state->ChangeInputMethod(new_input_method, false /* show_message */); | 88 ime_state->ChangeInputMethod(new_input_method, false /* show_message */); |
| 79 return RespondNow(NoArguments()); | 89 return RespondNow(NoArguments()); |
| 80 } | 90 } |
| 81 } | 91 } |
| 82 return RespondNow(Error("Invalid input method id.")); | 92 return RespondNow(Error("Invalid input method id.")); |
| 83 #endif | 93 #endif |
| 84 } | 94 } |
| 85 | 95 |
| 86 ExtensionFunction::ResponseAction GetInputMethodsFunction::Run() { | 96 ExtensionFunction::ResponseAction |
| 97 InputMethodPrivateGetInputMethodsFunction::Run() { | |
| 87 #if !defined(OS_CHROMEOS) | 98 #if !defined(OS_CHROMEOS) |
| 88 EXTENSION_FUNCTION_VALIDATE(false); | 99 EXTENSION_FUNCTION_VALIDATE(false); |
| 89 #else | 100 #else |
| 90 base::ListValue* output = new base::ListValue(); | 101 base::ListValue* output = new base::ListValue(); |
| 91 chromeos::input_method::InputMethodManager* manager = | 102 chromeos::input_method::InputMethodManager* manager = |
| 92 chromeos::input_method::InputMethodManager::Get(); | 103 chromeos::input_method::InputMethodManager::Get(); |
| 93 chromeos::input_method::InputMethodUtil* util = manager->GetInputMethodUtil(); | 104 chromeos::input_method::InputMethodUtil* util = manager->GetInputMethodUtil(); |
| 94 scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state = | 105 scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state = |
| 95 manager->GetActiveIMEState(); | 106 manager->GetActiveIMEState(); |
| 96 scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods = | 107 scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods = |
| 97 ime_state->GetActiveInputMethods(); | 108 ime_state->GetActiveInputMethods(); |
| 98 for (size_t i = 0; i < input_methods->size(); ++i) { | 109 for (size_t i = 0; i < input_methods->size(); ++i) { |
| 99 const chromeos::input_method::InputMethodDescriptor& input_method = | 110 const chromeos::input_method::InputMethodDescriptor& input_method = |
| 100 (*input_methods)[i]; | 111 (*input_methods)[i]; |
| 101 base::DictionaryValue* val = new base::DictionaryValue(); | 112 base::DictionaryValue* val = new base::DictionaryValue(); |
| 102 val->SetString("id", input_method.id()); | 113 val->SetString("id", input_method.id()); |
| 103 val->SetString("name", util->GetInputMethodLongName(input_method)); | 114 val->SetString("name", util->GetInputMethodLongName(input_method)); |
| 104 val->SetString("indicator", util->GetInputMethodShortName(input_method)); | 115 val->SetString("indicator", util->GetInputMethodShortName(input_method)); |
| 105 output->Append(val); | 116 output->Append(val); |
| 106 } | 117 } |
| 107 return RespondNow(OneArgument(output)); | 118 return RespondNow(OneArgument(output)); |
| 108 #endif | 119 #endif |
| 109 } | 120 } |
| 110 | 121 |
| 111 ExtensionFunction::ResponseAction FetchAllDictionaryWordsFunction::Run() { | 122 ExtensionFunction::ResponseAction |
| 123 InputMethodPrivateFetchAllDictionaryWordsFunction::Run() { | |
| 112 #if !defined(OS_CHROMEOS) | 124 #if !defined(OS_CHROMEOS) |
| 113 EXTENSION_FUNCTION_VALIDATE(false); | 125 EXTENSION_FUNCTION_VALIDATE(false); |
| 114 #else | 126 #else |
| 115 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( | 127 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( |
| 116 context_); | 128 context_); |
| 117 if (!spellcheck) { | 129 if (!spellcheck) { |
| 118 return RespondNow(Error("Spellcheck service not available.")); | 130 return RespondNow(Error("Spellcheck service not available.")); |
| 119 } | 131 } |
| 120 SpellcheckCustomDictionary* dictionary = spellcheck->GetCustomDictionary(); | 132 SpellcheckCustomDictionary* dictionary = spellcheck->GetCustomDictionary(); |
| 121 if (!dictionary->IsLoaded()) { | 133 if (!dictionary->IsLoaded()) { |
| 122 return RespondNow(Error("Custom dictionary not loaded yet.")); | 134 return RespondNow(Error("Custom dictionary not loaded yet.")); |
| 123 } | 135 } |
| 124 | 136 |
| 125 const std::set<std::string>& words = dictionary->GetWords(); | 137 const std::set<std::string>& words = dictionary->GetWords(); |
| 126 base::ListValue* output = new base::ListValue(); | 138 base::ListValue* output = new base::ListValue(); |
| 127 for (auto it = words.begin(); it != words.end(); ++it) { | 139 for (auto it = words.begin(); it != words.end(); ++it) { |
| 128 output->AppendString(*it); | 140 output->AppendString(*it); |
| 129 } | 141 } |
| 130 return RespondNow(OneArgument(output)); | 142 return RespondNow(OneArgument(output)); |
| 131 #endif | 143 #endif |
| 132 } | 144 } |
| 133 | 145 |
| 134 ExtensionFunction::ResponseAction AddWordToDictionaryFunction::Run() { | 146 ExtensionFunction::ResponseAction |
| 147 InputMethodPrivateAddWordToDictionaryFunction::Run() { | |
| 135 #if !defined(OS_CHROMEOS) | 148 #if !defined(OS_CHROMEOS) |
| 136 EXTENSION_FUNCTION_VALIDATE(false); | 149 EXTENSION_FUNCTION_VALIDATE(false); |
| 137 #else | 150 #else |
| 138 std::string word; | 151 std::string word; |
| 139 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &word)); | 152 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &word)); |
|
Shu Chen
2016/01/14 03:49:56
Pls use the generated classes for parameters.
Azure Wei
2016/01/14 04:17:04
Done.
| |
| 140 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( | 153 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( |
| 141 context_); | 154 context_); |
| 142 if (!spellcheck) { | 155 if (!spellcheck) { |
| 143 return RespondNow(Error("Spellcheck service not available.")); | 156 return RespondNow(Error("Spellcheck service not available.")); |
| 144 } | 157 } |
| 145 SpellcheckCustomDictionary* dictionary = spellcheck->GetCustomDictionary(); | 158 SpellcheckCustomDictionary* dictionary = spellcheck->GetCustomDictionary(); |
| 146 if (!dictionary->IsLoaded()) { | 159 if (!dictionary->IsLoaded()) { |
| 147 return RespondNow(Error("Custom dictionary not loaded yet.")); | 160 return RespondNow(Error("Custom dictionary not loaded yet.")); |
| 148 } | 161 } |
| 149 | 162 |
| 150 if (dictionary->AddWord(word)) | 163 if (dictionary->AddWord(word)) |
| 151 return RespondNow(NoArguments()); | 164 return RespondNow(NoArguments()); |
| 152 // Invalid words: | 165 // Invalid words: |
| 153 // - Already in the dictionary. | 166 // - Already in the dictionary. |
| 154 // - Not a UTF8 string. | 167 // - Not a UTF8 string. |
| 155 // - Longer than 99 bytes (MAX_CUSTOM_DICTIONARY_WORD_BYTES). | 168 // - Longer than 99 bytes (MAX_CUSTOM_DICTIONARY_WORD_BYTES). |
| 156 // - Leading/trailing whitespace. | 169 // - Leading/trailing whitespace. |
| 157 // - Empty. | 170 // - Empty. |
| 158 return RespondNow(Error("Unable to add invalid word to dictionary.")); | 171 return RespondNow(Error("Unable to add invalid word to dictionary.")); |
| 159 #endif | 172 #endif |
| 160 } | 173 } |
| 161 | 174 |
| 162 ExtensionFunction::ResponseAction GetEncryptSyncEnabledFunction::Run() { | 175 ExtensionFunction::ResponseAction |
| 176 InputMethodPrivateGetEncryptSyncEnabledFunction::Run() { | |
| 163 #if !defined(OS_CHROMEOS) | 177 #if !defined(OS_CHROMEOS) |
| 164 EXTENSION_FUNCTION_VALIDATE(false); | 178 EXTENSION_FUNCTION_VALIDATE(false); |
| 165 #else | 179 #else |
| 166 ProfileSyncService* profile_sync_service = | 180 ProfileSyncService* profile_sync_service = |
| 167 ProfileSyncServiceFactory::GetForProfile( | 181 ProfileSyncServiceFactory::GetForProfile( |
| 168 Profile::FromBrowserContext(browser_context())); | 182 Profile::FromBrowserContext(browser_context())); |
| 169 if (!profile_sync_service) | 183 if (!profile_sync_service) |
| 170 return RespondNow(Error("Sync service is not ready for current profile.")); | 184 return RespondNow(Error("Sync service is not ready for current profile.")); |
| 171 scoped_ptr<base::Value> ret(new base::FundamentalValue( | 185 scoped_ptr<base::Value> ret(new base::FundamentalValue( |
| 172 profile_sync_service->IsEncryptEverythingEnabled())); | 186 profile_sync_service->IsEncryptEverythingEnabled())); |
| 173 return RespondNow(OneArgument(std::move(ret))); | 187 return RespondNow(OneArgument(std::move(ret))); |
| 174 #endif | 188 #endif |
| 175 } | 189 } |
| 176 | 190 |
| 177 // static | |
| 178 const char InputMethodAPI::kOnDictionaryChanged[] = | |
| 179 "inputMethodPrivate.onDictionaryChanged"; | |
| 180 | |
| 181 // static | |
| 182 const char InputMethodAPI::kOnDictionaryLoaded[] = | |
| 183 "inputMethodPrivate.onDictionaryLoaded"; | |
| 184 | |
| 185 // static | |
| 186 const char InputMethodAPI::kOnInputMethodChanged[] = | |
| 187 "inputMethodPrivate.onChanged"; | |
| 188 | |
| 189 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) | 191 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) |
| 190 : context_(context) { | 192 : context_(context) { |
| 191 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); | |
| 192 EventRouter::Get(context_)->RegisterObserver(this, kOnDictionaryChanged); | |
| 193 EventRouter::Get(context_)->RegisterObserver(this, kOnDictionaryLoaded); | |
| 194 ExtensionFunctionRegistry* registry = | |
| 195 ExtensionFunctionRegistry::GetInstance(); | |
| 196 registry->RegisterFunction<GetInputMethodConfigFunction>(); | |
| 197 registry->RegisterFunction<GetCurrentInputMethodFunction>(); | |
| 198 registry->RegisterFunction<SetCurrentInputMethodFunction>(); | |
| 199 registry->RegisterFunction<GetInputMethodsFunction>(); | |
| 200 registry->RegisterFunction<FetchAllDictionaryWordsFunction>(); | |
| 201 registry->RegisterFunction<AddWordToDictionaryFunction>(); | |
| 202 registry->RegisterFunction<GetEncryptSyncEnabledFunction>(); | |
| 203 } | 193 } |
| 204 | 194 |
| 205 InputMethodAPI::~InputMethodAPI() { | 195 InputMethodAPI::~InputMethodAPI() { |
| 206 } | 196 } |
| 207 | 197 |
| 208 // static | 198 // static |
| 209 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { | 199 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
| 210 std::string xkb_prefix = | 200 std::string xkb_prefix = |
| 211 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); | 201 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); |
| 212 size_t prefix_length = xkb_prefix.length(); | 202 size_t prefix_length = xkb_prefix.length(); |
| 213 DCHECK(xkb_id.substr(0, prefix_length) == xkb_prefix); | 203 DCHECK(xkb_id.substr(0, prefix_length) == xkb_prefix); |
| 214 return xkb_id.substr(prefix_length); | 204 return xkb_id.substr(prefix_length); |
| 215 } | 205 } |
| 216 | 206 |
| 217 void InputMethodAPI::Shutdown() { | 207 void InputMethodAPI::Shutdown() { |
| 218 EventRouter::Get(context_)->UnregisterObserver(this); | 208 EventRouter::Get(context_)->UnregisterObserver(this); |
| 219 } | 209 } |
| 220 | 210 |
| 221 void InputMethodAPI::OnListenerAdded( | 211 void InputMethodAPI::OnListenerAdded( |
| 222 const extensions::EventListenerInfo& details) { | 212 const extensions::EventListenerInfo& details) { |
| 223 if (details.event_name == kOnInputMethodChanged) { | 213 if (details.event_name == OnChanged::kEventName && |
| 224 if (!input_method_event_router_.get()) { | 214 !input_method_event_router_.get()) { |
| 225 input_method_event_router_.reset( | 215 input_method_event_router_.reset( |
| 226 new chromeos::ExtensionInputMethodEventRouter(context_)); | 216 new chromeos::ExtensionInputMethodEventRouter(context_)); |
| 227 } | 217 } else if (details.event_name == OnDictionaryChanged::kEventName || |
| 228 } else if (details.event_name == kOnDictionaryChanged || | 218 details.event_name == OnDictionaryLoaded::kEventName) { |
| 229 details.event_name == kOnDictionaryLoaded) { | |
| 230 if (!dictionary_event_router_.get()) { | 219 if (!dictionary_event_router_.get()) { |
| 231 dictionary_event_router_.reset( | 220 dictionary_event_router_.reset( |
| 232 new chromeos::ExtensionDictionaryEventRouter(context_)); | 221 new chromeos::ExtensionDictionaryEventRouter(context_)); |
| 233 } | 222 } |
| 234 if (details.event_name == kOnDictionaryLoaded) { | 223 if (details.event_name == OnDictionaryLoaded::kEventName) { |
| 235 dictionary_event_router_->DispatchLoadedEventIfLoaded(); | 224 dictionary_event_router_->DispatchLoadedEventIfLoaded(); |
| 236 } | 225 } |
| 237 } | 226 } |
| 238 } | 227 } |
| 239 | 228 |
| 240 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > | 229 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > |
| 241 g_factory = LAZY_INSTANCE_INITIALIZER; | 230 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 242 | 231 |
| 243 // static | 232 // static |
| 244 BrowserContextKeyedAPIFactory<InputMethodAPI>* | 233 BrowserContextKeyedAPIFactory<InputMethodAPI>* |
| 245 InputMethodAPI::GetFactoryInstance() { | 234 InputMethodAPI::GetFactoryInstance() { |
| 246 return g_factory.Pointer(); | 235 return g_factory.Pointer(); |
| 247 } | 236 } |
| 248 | 237 |
| 249 } // namespace extensions | 238 } // namespace extensions |
| OLD | NEW |