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/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" | 9 #include "chrome/browser/chromeos/extensions/input_method_event_router.h" |
10 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 10 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
11 #include "chromeos/ime/extension_ime_util.h" | 11 #include "chromeos/ime/extension_ime_util.h" |
| 12 #include "chromeos/ime/input_method_descriptor.h" |
12 #include "chromeos/ime/input_method_manager.h" | 13 #include "chromeos/ime/input_method_manager.h" |
13 #include "extensions/browser/extension_function_registry.h" | 14 #include "extensions/browser/extension_function_registry.h" |
14 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
| 16 #include "extensions/common/value_builder.h" |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 // Prefix, which is used by XKB. | 20 // Prefix, which is used by XKB. |
19 const char kXkbPrefix[] = "xkb:"; | 21 const char kXkbPrefix[] = "xkb:"; |
20 | 22 |
21 } // namespace | 23 } // namespace |
22 | 24 |
23 namespace extensions { | 25 namespace extensions { |
24 | 26 |
25 GetInputMethodFunction::GetInputMethodFunction() { | 27 ExtensionFunction::ResponseAction GetCurrentInputMethodFunction::Run() { |
26 } | |
27 | |
28 GetInputMethodFunction::~GetInputMethodFunction() { | |
29 } | |
30 | |
31 bool GetInputMethodFunction::RunSync() { | |
32 #if !defined(OS_CHROMEOS) | 28 #if !defined(OS_CHROMEOS) |
33 NOTREACHED(); | 29 EXTENSION_FUNCTION_VALIDATE(false); |
34 return false; | |
35 #else | 30 #else |
36 chromeos::input_method::InputMethodManager* manager = | 31 chromeos::input_method::InputMethodManager* manager = |
37 chromeos::input_method::InputMethodManager::Get(); | 32 chromeos::input_method::InputMethodManager::Get(); |
38 const std::string input_method = InputMethodAPI::GetInputMethodForXkb( | 33 return RespondNow(OneArgument( |
39 manager->GetCurrentInputMethod().id()); | 34 new base::StringValue(manager->GetCurrentInputMethod().id()))); |
40 SetResult(base::Value::CreateStringValue(input_method)); | |
41 return true; | |
42 #endif | 35 #endif |
43 } | 36 } |
44 | 37 |
45 StartImeFunction::StartImeFunction() { | 38 ExtensionFunction::ResponseAction SetCurrentInputMethodFunction::Run() { |
46 } | |
47 | |
48 StartImeFunction::~StartImeFunction() { | |
49 } | |
50 | |
51 bool StartImeFunction::RunSync() { | |
52 #if !defined(OS_CHROMEOS) | 39 #if !defined(OS_CHROMEOS) |
53 NOTREACHED(); | 40 EXTENSION_FUNCTION_VALIDATE(false); |
54 return false; | |
55 #else | 41 #else |
56 chromeos::InputMethodEngineInterface* engine = | 42 std::string new_input_method; |
57 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); | 43 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method)); |
58 if (engine) | 44 chromeos::input_method::InputMethodManager* manager = |
59 engine->NotifyImeReady(); | 45 chromeos::input_method::InputMethodManager::Get(); |
60 return true; | 46 const std::vector<std::string>& input_methods = |
| 47 manager->GetActiveInputMethodIds(); |
| 48 for (size_t i = 0; i < input_methods.size(); ++i) { |
| 49 const std::string& input_method = input_methods[i]; |
| 50 if (input_method == new_input_method) { |
| 51 manager->ChangeInputMethod(new_input_method); |
| 52 return RespondNow(NoArguments()); |
| 53 } |
| 54 } |
| 55 return RespondNow(Error("Invalid input method id.")); |
61 #endif | 56 #endif |
62 } | 57 } |
63 | 58 |
| 59 ExtensionFunction::ResponseAction GetInputMethodsFunction::Run() { |
| 60 #if !defined(OS_CHROMEOS) |
| 61 EXTENSION_FUNCTION_VALIDATE(false); |
| 62 #else |
| 63 base::ListValue* output = new base::ListValue(); |
| 64 chromeos::input_method::InputMethodManager* manager = |
| 65 chromeos::input_method::InputMethodManager::Get(); |
| 66 scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods = |
| 67 manager->GetActiveInputMethods(); |
| 68 for (size_t i = 0; i < input_methods->size(); ++i) { |
| 69 const chromeos::input_method::InputMethodDescriptor& input_method = |
| 70 (*input_methods)[i]; |
| 71 base::DictionaryValue* val = new base::DictionaryValue(); |
| 72 val->SetString("id", input_method.id()); |
| 73 val->SetString("name", input_method.name()); |
| 74 val->SetString("indicator", input_method.indicator()); |
| 75 output->Append(val); |
| 76 } |
| 77 return RespondNow(OneArgument(output)); |
| 78 #endif |
| 79 } |
| 80 |
64 // static | 81 // static |
65 const char InputMethodAPI::kOnInputMethodChanged[] = | 82 const char InputMethodAPI::kOnInputMethodChanged[] = |
66 "inputMethodPrivate.onChanged"; | 83 "inputMethodPrivate.onChanged"; |
67 | 84 |
68 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) | 85 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) |
69 : context_(context) { | 86 : context_(context) { |
70 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); | 87 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); |
71 ExtensionFunctionRegistry* registry = | 88 ExtensionFunctionRegistry* registry = |
72 ExtensionFunctionRegistry::GetInstance(); | 89 ExtensionFunctionRegistry::GetInstance(); |
73 registry->RegisterFunction<GetInputMethodFunction>(); | 90 registry->RegisterFunction<GetCurrentInputMethodFunction>(); |
74 registry->RegisterFunction<StartImeFunction>(); | 91 registry->RegisterFunction<SetCurrentInputMethodFunction>(); |
| 92 registry->RegisterFunction<GetInputMethodsFunction>(); |
75 } | 93 } |
76 | 94 |
77 InputMethodAPI::~InputMethodAPI() { | 95 InputMethodAPI::~InputMethodAPI() { |
78 } | 96 } |
79 | 97 |
80 // static | 98 // static |
81 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { | 99 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
82 std::string xkb_prefix = | 100 std::string xkb_prefix = |
83 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); | 101 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); |
84 size_t prefix_length = xkb_prefix.length(); | 102 size_t prefix_length = xkb_prefix.length(); |
(...skipping 18 matching lines...) Expand all Loading... |
103 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > | 121 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > |
104 g_factory = LAZY_INSTANCE_INITIALIZER; | 122 g_factory = LAZY_INSTANCE_INITIALIZER; |
105 | 123 |
106 // static | 124 // static |
107 BrowserContextKeyedAPIFactory<InputMethodAPI>* | 125 BrowserContextKeyedAPIFactory<InputMethodAPI>* |
108 InputMethodAPI::GetFactoryInstance() { | 126 InputMethodAPI::GetFactoryInstance() { |
109 return g_factory.Pointer(); | 127 return g_factory.Pointer(); |
110 } | 128 } |
111 | 129 |
112 } // namespace extensions | 130 } // namespace extensions |
OLD | NEW |