#include "chrome/browser/chromeos/extensions/input_method_api.h"
+#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/values.h"
#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
+#include "chrome/browser/chromeos/input_method/input_method_util.h"
#include "chrome/browser/extensions/api/input_ime/input_ime_api.h"
+#include "chromeos/chromeos_switches.h"
#include "chromeos/ime/extension_ime_util.h"
+#include "chromeos/ime/input_method_descriptor.h"
#include "chromeos/ime/input_method_manager.h"
#include "extensions/browser/extension_function_registry.h"
#include "extensions/browser/extension_system.h"
+#include "extensions/common/value_builder.h"
namespace {
namespace extensions {
-GetInputMethodFunction::GetInputMethodFunction() {
-}
-
-GetInputMethodFunction::~GetInputMethodFunction() {
+ExtensionFunction::ResponseAction GetInputMethodConfigFunction::Run() {
+#if !defined(OS_CHROMEOS)
+ EXTENSION_FUNCTION_VALIDATE(false);
+#else
+ base::DictionaryValue* output = new base::DictionaryValue();
+ output->SetBoolean(
+ "isPhysicalKeyboardAutocorrectEnabled",
+ CommandLine::ForCurrentProcess()->HasSwitch(
+ chromeos::switches::kEnablePhysicalKeyboardAutocorrect));
+ return RespondNow(OneArgument(output));
+#endif
}
-bool GetInputMethodFunction::RunImpl() {
+ExtensionFunction::ResponseAction GetCurrentInputMethodFunction::Run() {
#if !defined(OS_CHROMEOS)
- NOTREACHED();
- return false;
+ EXTENSION_FUNCTION_VALIDATE(false);
#else
chromeos::input_method::InputMethodManager* manager =
chromeos::input_method::InputMethodManager::Get();
- const std::string input_method = InputMethodAPI::GetInputMethodForXkb(
- manager->GetCurrentInputMethod().id());
- SetResult(base::Value::CreateStringValue(input_method));
- return true;
+ return RespondNow(OneArgument(new base::StringValue(
+ manager->GetActiveIMEState()->GetCurrentInputMethod().id())));
#endif
}
-StartImeFunction::StartImeFunction() {
-}
-
-StartImeFunction::~StartImeFunction() {
+ExtensionFunction::ResponseAction SetCurrentInputMethodFunction::Run() {
+#if !defined(OS_CHROMEOS)
+ EXTENSION_FUNCTION_VALIDATE(false);
+#else
+ std::string new_input_method;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method));
+ scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state =
+ chromeos::input_method::InputMethodManager::Get()->GetActiveIMEState();
+ const std::vector<std::string>& input_methods =
+ ime_state->GetActiveInputMethodIds();
+ for (size_t i = 0; i < input_methods.size(); ++i) {
+ const std::string& input_method = input_methods[i];
+ if (input_method == new_input_method) {
+ ime_state->ChangeInputMethod(new_input_method, false /* show_message */);
+ return RespondNow(NoArguments());
+ }
+ }
+ return RespondNow(Error("Invalid input method id."));
+#endif
}
-bool StartImeFunction::RunImpl() {
+ExtensionFunction::ResponseAction GetInputMethodsFunction::Run() {
#if !defined(OS_CHROMEOS)
- NOTREACHED();
- return false;
+ EXTENSION_FUNCTION_VALIDATE(false);
#else
- chromeos::InputMethodEngineInterface* engine =
- InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
- if (engine)
- engine->NotifyImeReady();
- return true;
+ base::ListValue* output = new base::ListValue();
+ chromeos::input_method::InputMethodManager* manager =
+ chromeos::input_method::InputMethodManager::Get();
+ chromeos::input_method::InputMethodUtil* util = manager->GetInputMethodUtil();
+ scoped_refptr<chromeos::input_method::InputMethodManager::State> ime_state =
+ manager->GetActiveIMEState();
+ scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods =
+ ime_state->GetActiveInputMethods();
+ for (size_t i = 0; i < input_methods->size(); ++i) {
+ const chromeos::input_method::InputMethodDescriptor& input_method =
+ (*input_methods)[i];
+ base::DictionaryValue* val = new base::DictionaryValue();
+ val->SetString("id", input_method.id());
+ val->SetString("name", util->GetInputMethodLongName(input_method));
+ val->SetString("indicator", util->GetInputMethodShortName(input_method));
+ output->Append(val);
+ }
+ return RespondNow(OneArgument(output));
#endif
}
InputMethodAPI::InputMethodAPI(content::BrowserContext* context)
: context_(context) {
- ExtensionSystem::Get(context_)->event_router()->RegisterObserver(
- this, kOnInputMethodChanged);
+ EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged);
ExtensionFunctionRegistry* registry =
ExtensionFunctionRegistry::GetInstance();
- registry->RegisterFunction<GetInputMethodFunction>();
- registry->RegisterFunction<StartImeFunction>();
+ registry->RegisterFunction<GetInputMethodConfigFunction>();
+ registry->RegisterFunction<GetCurrentInputMethodFunction>();
+ registry->RegisterFunction<SetCurrentInputMethodFunction>();
+ registry->RegisterFunction<GetInputMethodsFunction>();
}
InputMethodAPI::~InputMethodAPI() {
// static
std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) {
std::string xkb_prefix =
- chromeos::extension_ime_util::GetInputMethodIDByKeyboardLayout(
- kXkbPrefix);
+ chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix);
size_t prefix_length = xkb_prefix.length();
DCHECK(xkb_id.substr(0, prefix_length) == xkb_prefix);
return xkb_id.substr(prefix_length);
void InputMethodAPI::Shutdown() {
// UnregisterObserver may have already been called in OnListenerAdded,
// but it is safe to call it more than once.
- ExtensionSystem::Get(context_)->event_router()->UnregisterObserver(this);
+ EventRouter::Get(context_)->UnregisterObserver(this);
}
void InputMethodAPI::OnListenerAdded(
DCHECK(!input_method_event_router_.get());
input_method_event_router_.reset(
new chromeos::ExtensionInputMethodEventRouter(context_));
- ExtensionSystem::Get(context_)->event_router()->UnregisterObserver(this);
+ EventRouter::Get(context_)->UnregisterObserver(this);
}
static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> >