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