| 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/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 8 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
| 9 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 9 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 10 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | 10 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/event_names.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 | 14 |
| 15 namespace { |
| 16 |
| 17 // Prefix, which is used by XKB. |
| 18 const char kXkbPrefix[] = "xkb:"; |
| 19 |
| 20 } // namespace |
| 21 |
| 15 namespace extensions { | 22 namespace extensions { |
| 16 | 23 |
| 17 GetInputMethodFunction::GetInputMethodFunction() { | 24 GetInputMethodFunction::GetInputMethodFunction() { |
| 18 } | 25 } |
| 19 | 26 |
| 20 GetInputMethodFunction::~GetInputMethodFunction() { | 27 GetInputMethodFunction::~GetInputMethodFunction() { |
| 21 } | 28 } |
| 22 | 29 |
| 23 bool GetInputMethodFunction::RunImpl() { | 30 bool GetInputMethodFunction::RunImpl() { |
| 24 #if !defined(OS_CHROMEOS) | 31 #if !defined(OS_CHROMEOS) |
| 25 NOTREACHED(); | 32 NOTREACHED(); |
| 26 return false; | 33 return false; |
| 27 #else | 34 #else |
| 28 chromeos::ExtensionInputMethodEventRouter* router = | |
| 29 extensions::ExtensionSystem::Get(profile_)->extension_service()-> | |
| 30 input_method_event_router(); | |
| 31 chromeos::input_method::InputMethodManager* manager = | 35 chromeos::input_method::InputMethodManager* manager = |
| 32 chromeos::input_method::GetInputMethodManager(); | 36 chromeos::input_method::GetInputMethodManager(); |
| 33 const std::string input_method = | 37 const std::string input_method = InputMethodAPI::GetInputMethodForXkb( |
| 34 router->GetInputMethodForXkb(manager->GetCurrentInputMethod().id()); | 38 manager->GetCurrentInputMethod().id()); |
| 35 SetResult(Value::CreateStringValue(input_method)); | 39 SetResult(Value::CreateStringValue(input_method)); |
| 36 return true; | 40 return true; |
| 37 #endif | 41 #endif |
| 38 } | 42 } |
| 39 | 43 |
| 44 InputMethodAPI::InputMethodAPI(Profile* profile) |
| 45 : profile_(profile) { |
| 46 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 47 this, event_names::kOnInputMethodChanged); |
| 48 } |
| 49 |
| 50 InputMethodAPI::~InputMethodAPI() { |
| 51 } |
| 52 |
| 53 // static |
| 54 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
| 55 size_t prefix_length = std::string(kXkbPrefix).length(); |
| 56 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); |
| 57 return xkb_id.substr(prefix_length); |
| 58 } |
| 59 |
| 60 void InputMethodAPI::Shutdown() { |
| 61 // UnregisterObserver may have already been called in OnListenerAdded, |
| 62 // but it is safe to call it more than once. |
| 63 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 64 } |
| 65 |
| 66 void InputMethodAPI::OnListenerAdded( |
| 67 const extensions::EventListenerInfo& details) { |
| 68 DCHECK(!input_method_event_router_.get()); |
| 69 input_method_event_router_.reset( |
| 70 new chromeos::ExtensionInputMethodEventRouter()); |
| 71 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 72 } |
| 73 |
| 40 } // namespace extensions | 74 } // namespace extensions |
| OLD | NEW |