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" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Prefix, which is used by XKB. | 19 // Prefix, which is used by XKB. |
19 const char kXkbPrefix[] = "xkb:"; | 20 const char kXkbPrefix[] = "xkb:"; |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 namespace extensions { | 24 namespace extensions { |
24 | 25 |
25 GetInputMethodFunction::GetInputMethodFunction() { | 26 bool GetCurrentInputMethodFunction::RunSync() { |
26 } | |
27 | |
28 GetInputMethodFunction::~GetInputMethodFunction() { | |
29 } | |
30 | |
31 bool GetInputMethodFunction::RunSync() { | |
32 #if !defined(OS_CHROMEOS) | 27 #if !defined(OS_CHROMEOS) |
33 NOTREACHED(); | 28 NOTREACHED(); |
34 return false; | 29 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 SetResult( |
39 manager->GetCurrentInputMethod().id()); | 34 base::Value::CreateStringValue(manager->GetCurrentInputMethod().id())); |
not at google - send to devlin
2014/06/06 16:36:04
use new base::StringValue not CreateStringValue
Shu Chen
2014/06/07 05:31:09
Done.
| |
40 SetResult(base::Value::CreateStringValue(input_method)); | |
41 return true; | 35 return true; |
42 #endif | 36 #endif |
43 } | 37 } |
38 | |
39 bool SetCurrentInputMethodFunction::RunSync() { | |
40 #if !defined(OS_CHROMEOS) | |
not at google - send to devlin
2014/06/06 16:36:04
better here (and everywhere) is just EXTENSION_FUN
Shu Chen
2014/06/07 05:31:09
Done.
| |
41 NOTREACHED(); | |
42 return false; | |
43 #else | |
44 std::string new_input_method; | |
45 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &new_input_method)); | |
46 chromeos::input_method::InputMethodManager* manager = | |
47 chromeos::input_method::InputMethodManager::Get(); | |
48 const std::vector<std::string>& input_methods = | |
49 manager->GetActiveInputMethodIds(); | |
50 for (size_t i = 0; i < input_methods.size(); ++i) { | |
51 const std::string& input_method = input_methods[i]; | |
52 if (input_method == new_input_method) { | |
53 manager->ChangeInputMethod(new_input_method); | |
54 SetResult(new base::FundamentalValue(true)); | |
not at google - send to devlin
2014/06/06 16:36:04
i.e. just return true here
Shu Chen
2014/06/07 05:31:09
Done.
| |
55 return true; | |
56 } | |
57 } | |
58 SetResult(new base::FundamentalValue(false)); | |
59 return true; | |
not at google - send to devlin
2014/06/06 16:36:04
then here set _error (like "cannot find ime with i
Shu Chen
2014/06/07 05:31:09
Done.
| |
60 #endif | |
61 } | |
62 | |
63 bool GetInputMethodsFunction::RunSync() { | |
64 #if !defined(OS_CHROMEOS) | |
65 NOTREACHED(); | |
66 return false; | |
67 #else | |
68 base::ListValue* output = new base::ListValue(); | |
69 chromeos::input_method::InputMethodManager* manager = | |
70 chromeos::input_method::InputMethodManager::Get(); | |
71 scoped_ptr<chromeos::input_method::InputMethodDescriptors> input_methods = | |
72 manager->GetActiveInputMethods(); | |
73 for (size_t i = 0; i < input_methods->size(); ++i) { | |
74 const chromeos::input_method::InputMethodDescriptor& input_method = | |
75 (*input_methods)[i]; | |
76 base::DictionaryValue* val = new base::DictionaryValue(); | |
77 val->SetString("id", input_method.id()); | |
78 val->SetString("name", input_method.name()); | |
79 val->SetString("indicator", input_method.indicator()); | |
80 output->Append(val); | |
not at google - send to devlin
2014/06/06 16:36:04
what you have here is totally fine. but a fun clas
Shu Chen
2014/06/07 05:31:09
Just found that value_builder.cc is only for test
not at google - send to devlin
2014/06/10 19:48:27
bummer. ok, there's actually no reason that should
| |
81 } | |
82 SetResult(output); | |
83 return true; | |
84 #endif | |
85 } | |
44 | 86 |
45 StartImeFunction::StartImeFunction() { | 87 StartImeFunction::StartImeFunction() { |
46 } | 88 } |
47 | 89 |
48 StartImeFunction::~StartImeFunction() { | 90 StartImeFunction::~StartImeFunction() { |
49 } | 91 } |
50 | 92 |
51 bool StartImeFunction::RunSync() { | 93 bool StartImeFunction::RunSync() { |
52 #if !defined(OS_CHROMEOS) | 94 #if !defined(OS_CHROMEOS) |
53 NOTREACHED(); | 95 NOTREACHED(); |
54 return false; | 96 return false; |
55 #else | 97 #else |
56 chromeos::InputMethodEngineInterface* engine = | 98 chromeos::InputMethodEngineInterface* engine = |
57 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); | 99 InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id()); |
58 if (engine) | 100 if (engine) |
59 engine->NotifyImeReady(); | 101 engine->NotifyImeReady(); |
60 return true; | 102 return true; |
61 #endif | 103 #endif |
62 } | 104 } |
63 | 105 |
64 // static | 106 // static |
65 const char InputMethodAPI::kOnInputMethodChanged[] = | 107 const char InputMethodAPI::kOnInputMethodChanged[] = |
66 "inputMethodPrivate.onChanged"; | 108 "inputMethodPrivate.onChanged"; |
67 | 109 |
68 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) | 110 InputMethodAPI::InputMethodAPI(content::BrowserContext* context) |
69 : context_(context) { | 111 : context_(context) { |
70 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); | 112 EventRouter::Get(context_)->RegisterObserver(this, kOnInputMethodChanged); |
71 ExtensionFunctionRegistry* registry = | 113 ExtensionFunctionRegistry* registry = |
72 ExtensionFunctionRegistry::GetInstance(); | 114 ExtensionFunctionRegistry::GetInstance(); |
73 registry->RegisterFunction<GetInputMethodFunction>(); | 115 registry->RegisterFunction<GetCurrentInputMethodFunction>(); |
116 registry->RegisterFunction<SetCurrentInputMethodFunction>(); | |
117 registry->RegisterFunction<GetInputMethodsFunction>(); | |
74 registry->RegisterFunction<StartImeFunction>(); | 118 registry->RegisterFunction<StartImeFunction>(); |
not at google - send to devlin
2014/06/06 16:36:04
you shouldn't actually need these RegisterFunction
not at google - send to devlin
2014/06/06 16:38:11
never mind, looks like the ime private API doesn't
| |
75 } | 119 } |
76 | 120 |
77 InputMethodAPI::~InputMethodAPI() { | 121 InputMethodAPI::~InputMethodAPI() { |
78 } | 122 } |
79 | 123 |
80 // static | 124 // static |
81 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { | 125 std::string InputMethodAPI::GetInputMethodForXkb(const std::string& xkb_id) { |
82 std::string xkb_prefix = | 126 std::string xkb_prefix = |
83 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); | 127 chromeos::extension_ime_util::GetInputMethodIDByEngineID(kXkbPrefix); |
84 size_t prefix_length = xkb_prefix.length(); | 128 size_t prefix_length = xkb_prefix.length(); |
(...skipping 18 matching lines...) Expand all Loading... | |
103 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > | 147 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputMethodAPI> > |
104 g_factory = LAZY_INSTANCE_INITIALIZER; | 148 g_factory = LAZY_INSTANCE_INITIALIZER; |
105 | 149 |
106 // static | 150 // static |
107 BrowserContextKeyedAPIFactory<InputMethodAPI>* | 151 BrowserContextKeyedAPIFactory<InputMethodAPI>* |
108 InputMethodAPI::GetFactoryInstance() { | 152 InputMethodAPI::GetFactoryInstance() { |
109 return g_factory.Pointer(); | 153 return g_factory.Pointer(); |
110 } | 154 } |
111 | 155 |
112 } // namespace extensions | 156 } // namespace extensions |
OLD | NEW |