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 "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 9 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
| 10 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 10 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
| 11 #include "chromeos/ime/extension_ime_util.h" | 11 #include "chromeos/ime/extension_ime_util.h" |
| 12 #include "chromeos/ime/input_method_descriptor.h" | |
| 12 #include "chromeos/ime/input_method_manager.h" | 13 #include "chromeos/ime/input_method_manager.h" |
| 13 #include "extensions/browser/extension_function_registry.h" | 14 #include "extensions/browser/extension_function_registry.h" |
| 14 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
| 16 #include "extensions/common/value_builder.h" | |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // Prefix, which is used by XKB. | 20 // Prefix, which is used by XKB. |
| 19 const char kXkbPrefix[] = "xkb:"; | 21 const char kXkbPrefix[] = "xkb:"; |
| 20 | 22 |
| 21 } // namespace | 23 } // namespace |
| 22 | 24 |
| 23 namespace extensions { | 25 namespace extensions { |
| 24 | 26 |
| 25 GetInputMethodFunction::GetInputMethodFunction() { | 27 ExtensionFunction::ResponseAction GetCurrentInputMethodFunction::Run() { |
| 26 } | |
| 27 | |
| 28 GetInputMethodFunction::~GetInputMethodFunction() { | |
| 29 } | |
| 30 | |
| 31 bool GetInputMethodFunction::RunSync() { | |
| 32 #if !defined(OS_CHROMEOS) | 28 #if !defined(OS_CHROMEOS) |
| 33 NOTREACHED(); | 29 EXTENSION_FUNCTION_VALIDATE(false); |
| 34 return false; | |
| 35 #else | 30 #else |
| 36 chromeos::input_method::InputMethodManager* manager = | 31 chromeos::input_method::InputMethodManager* manager = |
| 37 chromeos::input_method::InputMethodManager::Get(); | 32 chromeos::input_method::InputMethodManager::Get(); |
| 38 const std::string input_method = InputMethodAPI::GetInputMethodForXkb( | 33 return RespondNow(OneArgument( |
| 39 manager->GetCurrentInputMethod().id()); | 34 new base::StringValue(manager->GetCurrentInputMethod().id()))); |
| 40 SetResult(base::Value::CreateStringValue(input_method)); | |
| 41 return true; | |
| 42 #endif | 35 #endif |
| 43 } | 36 } |
| 44 | 37 |
| 38 ExtensionFunction::ResponseAction SetCurrentInputMethodFunction::Run() { | |
| 39 #if !defined(OS_CHROMEOS) | |
| 40 EXTENSION_FUNCTION_VALIDATE(false); | |
| 41 #else | |
| 42 std::string new_input_method; | |
| 43 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method)); | |
| 44 chromeos::input_method::InputMethodManager* manager = | |
| 45 chromeos::input_method::InputMethodManager::Get(); | |
| 46 const std::vector<std::string>& input_methods = | |
| 47 manager->GetActiveInputMethodIds(); | |
| 48 for (size_t i = 0; i < input_methods.size(); ++i) { | |
| 49 const std::string& input_method = input_methods[i]; | |
| 50 if (input_method == new_input_method) { | |
| 51 manager->ChangeInputMethod(new_input_method); | |
| 52 return RespondNow(NoArguments()); | |
| 53 } | |
| 54 } | |
| 55 return RespondNow(Error("Invalid input method id.")); | |
| 56 #endif | |
| 57 } | |
| 58 | |
| 59 ExtensionFunction::ResponseAction GetInputMethodsFunction::Run() { | |
| 60 #if !defined(OS_CHROMEOS) | |
| 61 EXTENSION_FUNCTION_VALIDATE(false); | |
| 62 #else | |
| 63 base::ListValue* output = new base::ListValue(); | |
| 64 chromeos::input_method::InputMethodManager* manager = | |
| 65 chromeos::input_method::InputMethodManager::Get(); | |
| 66 scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods = | |
| 67 manager->GetActiveInputMethods(); | |
| 68 for (size_t i = 0; i < input_methods->size(); ++i) { | |
| 69 const chromeos::input_method::InputMethodDescriptor& input_method = | |
| 70 (*input_methods)[i]; | |
| 71 base::DictionaryValue* val = new base::DictionaryValue(); | |
| 72 val->SetString("id", input_method.id()); | |
| 73 val->SetString("name", input_method.name()); | |
| 74 val->SetString("indicator", input_method.indicator()); | |
| 75 output->Append(val); | |
| 76 } | |
| 77 return RespondNow(OneArgument(output)); | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 45 StartImeFunction::StartImeFunction() { | 81 StartImeFunction::StartImeFunction() { |
| 46 } | 82 } |
| 47 | 83 |
| 48 StartImeFunction::~StartImeFunction() { | 84 StartImeFunction::~StartImeFunction() { |
| 49 } | 85 } |
| 50 | 86 |
| 51 bool StartImeFunction::RunSync() { | 87 ExtensionFunction::ResponseAction StartImeFunction::Run() { |
|
Seigo Nonaka
2014/06/11 03:18:04
I guess this function is no longer necessary.
This
Shu Chen
2014/06/11 03:31:15
Done.
| |
| 52 #if !defined(OS_CHROMEOS) | 88 #if !defined(OS_CHROMEOS) |
| 53 NOTREACHED(); | 89 EXTENSION_FUNCTION_VALIDATE(false); |
|
Seigo Nonaka
2014/06/11 03:18:04
I think NOTREACHED is still fine since this is Pri
Shu Chen
2014/06/11 03:31:15
The return value changed. EXTENSION_FUNCTION_VALID
Seigo Nonaka
2014/06/11 05:53:36
Oh, I didn't know NOTREACHED does not always cause
| |
| 54 return false; | |
| 55 #else | 90 #else |
| 56 chromeos::InputMethodEngineInterface* engine = | 91 chromeos::InputMethodEngineInterface* engine = |
| 57 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); | 92 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); |
| 58 if (engine) | 93 if (engine) |
| 59 engine->NotifyImeReady(); | 94 engine->NotifyImeReady(); |
| 60 return true; | 95 return RespondNow(NoArguments()); |
| 61 #endif | 96 #endif |
| 62 } | 97 } |
| 63 | 98 |
| 64 // static | 99 // static |
| 65 const char InputMethodAPI::kOnInputMethodChanged[] = | 100 const char InputMethodAPI::kOnInputMethodChanged[] = |
| 66 "inputMethodPrivate.onChanged"; | 101 "inputMethodPrivate.onChanged"; |
| 67 | 102 |
| 68 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) | 103 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) |
| 69 : context_(context) { | 104 : context_(context) { |
| 70 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); | 105 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); |
| 71 ExtensionFunctionRegistry* registry = | 106 ExtensionFunctionRegistry* registry = |
| 72 ExtensionFunctionRegistry::GetInstance(); | 107 ExtensionFunctionRegistry::GetInstance(); |
| 73 registry->RegisterFunction<GetInputMethodFunction>(); | 108 registry->RegisterFunction<GetCurrentInputMethodFunction>(); |
| 109 registry->RegisterFunction<SetCurrentInputMethodFunction>(); | |
| 110 registry->RegisterFunction<GetInputMethodsFunction>(); | |
| 74 registry->RegisterFunction<StartImeFunction>(); | 111 registry->RegisterFunction<StartImeFunction>(); |
| 75 } | 112 } |
| 76 | 113 |
| 77 InputMethodAPI::~InputMethodAPI() { | 114 InputMethodAPI::~InputMethodAPI() { |
| 78 } | 115 } |
| 79 | 116 |
| 80 // static | 117 // static |
| 81 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { | 118 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
| 82 std::string xkb_prefix = | 119 std::string xkb_prefix = |
| 83 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); | 120 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 103 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > | 140 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > |
| 104 g_factory = LAZY_INSTANCE_INITIALIZER; | 141 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 105 | 142 |
| 106 // static | 143 // static |
| 107 BrowserContextKeyedAPIFactory<InputMethodAPI>* | 144 BrowserContextKeyedAPIFactory<InputMethodAPI>* |
| 108 InputMethodAPI::GetFactoryInstance() { | 145 InputMethodAPI::GetFactoryInstance() { |
| 109 return g_factory.Pointer(); | 146 return g_factory.Pointer(); |
| 110 } | 147 } |
| 111 | 148 |
| 112 } // namespace extensions | 149 } // namespace extensions |
| OLD | NEW |