input_method_api.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/extensions/input_method_api.h" 6 7#include "base/lazy_instance.h" 8#include "base/values.h" 9#include "chrome/browser/chromeos/extensions/input_method_event_router.h" 10#include "chrome/browser/extensions/api/input_ime/input_ime_api.h" 11#include "chrome/browser/extensions/event_names.h" 12#include "chrome/browser/extensions/extension_function_registry.h" 13#include "chrome/browser/extensions/extension_system.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chromeos/ime/input_method_manager.h" 16 17namespace { 18 19// Prefix, which is used by XKB. 20const char kXkbPrefix[] = "xkb:"; 21 22} // namespace 23 24namespace extensions { 25 26GetInputMethodFunction::GetInputMethodFunction() { 27} 28 29GetInputMethodFunction::~GetInputMethodFunction() { 30} 31 32bool GetInputMethodFunction::RunImpl() { 33#if !defined(OS_CHROMEOS) 34 NOTREACHED(); 35 return false; 36#else 37 chromeos::input_method::InputMethodManager* manager = 38 chromeos::input_method::InputMethodManager::Get(); 39 const std::string input_method = InputMethodAPI::GetInputMethodForXkb( 40 manager->GetCurrentInputMethod().id()); 41 SetResult(Value::CreateStringValue(input_method)); 42 return true; 43#endif 44} 45 46StartImeFunction::StartImeFunction() { 47} 48 49StartImeFunction::~StartImeFunction() { 50} 51 52bool StartImeFunction::RunImpl() { 53#if !defined(OS_CHROMEOS) 54 NOTREACHED(); 55 return false; 56#else 57 chromeos::InputMethodEngine* engine = 58 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); 59 if (engine) 60 engine->StartIme(); 61 return true; 62#endif 63} 64 65InputMethodAPI::InputMethodAPI(Profile* profile) 66 : profile_(profile) { 67 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 68 this, event_names::kOnInputMethodChanged); 69 ExtensionFunctionRegistry* registry = 70 ExtensionFunctionRegistry::GetInstance(); 71 registry->RegisterFunction<GetInputMethodFunction>(); 72 registry->RegisterFunction<StartImeFunction>(); 73} 74 75InputMethodAPI::~InputMethodAPI() { 76} 77 78// static 79std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { 80 size_t prefix_length = std::string(kXkbPrefix).length(); 81 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); 82 return xkb_id.substr(prefix_length); 83} 84 85void InputMethodAPI::Shutdown() { 86 // UnregisterObserver may have already been called in OnListenerAdded, 87 // but it is safe to call it more than once. 88 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 89} 90 91void InputMethodAPI::OnListenerAdded( 92 const extensions::EventListenerInfo& details) { 93 DCHECK(!input_method_event_router_.get()); 94 input_method_event_router_.reset( 95 new chromeos::ExtensionInputMethodEventRouter()); 96 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 97} 98 99static base::LazyInstance<ProfileKeyedAPIFactory<InputMethodAPI> > 100 g_factory = LAZY_INSTANCE_INITIALIZER; 101 102// static 103ProfileKeyedAPIFactory<InputMethodAPI>* InputMethodAPI::GetFactoryInstance() { 104 return &g_factory.Get(); 105} 106 107} // namespace extensions 108