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.
5 #include "chrome/browser/chromeos/extensions/input_method_api.h"
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"
19 // Prefix, which is used by XKB.
20 const char kXkbPrefix[] = "xkb:";
24 namespace extensions {
26 GetInputMethodFunction::GetInputMethodFunction() {
29 GetInputMethodFunction::~GetInputMethodFunction() {
32 bool GetInputMethodFunction::RunImpl() {
33 #if !defined(OS_CHROMEOS)
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));
46 StartImeFunction::StartImeFunction() {
49 StartImeFunction::~StartImeFunction() {
52 bool StartImeFunction::RunImpl() {
53 #if !defined(OS_CHROMEOS)
57 chromeos::InputMethodEngine* engine =
58 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
65 InputMethodAPI::InputMethodAPI(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>();
75 InputMethodAPI::~InputMethodAPI() {
79 std::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);
85 void 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);
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());
96 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
99 static base::LazyInstance<ProfileKeyedAPIFactory<InputMethodAPI> >
100 g_factory = LAZY_INSTANCE_INITIALIZER;
103 ProfileKeyedAPIFactory<InputMethodAPI>* InputMethodAPI::GetFactoryInstance() {
104 return &g_factory.Get();
107 } // namespace extensions