1 // Copyright 2014 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 "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/string16.h"
9 #include "extensions/browser/api/extensions_api_client.h"
10 #include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_delegate.h"
11 #include "extensions/browser/extension_function_registry.h"
12 #include "extensions/common/api/virtual_keyboard_private.h"
13 #include "ui/events/event.h"
15 namespace SetMode
= extensions::api::virtual_keyboard_private::SetMode
;
16 namespace SetRequestedKeyboardState
=
17 extensions::api::virtual_keyboard_private::SetKeyboardState
;
19 namespace extensions
{
23 const char kNotYetImplementedError
[] =
24 "API is not implemented on this platform.";
25 const char kVirtualKeyboardNotEnabled
[] =
26 "The virtual keyboard is not enabled.";
28 typedef BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
> factory
;
30 VirtualKeyboardDelegate
* GetDelegate(SyncExtensionFunction
* f
) {
31 VirtualKeyboardAPI
* api
= factory::Get(f
->browser_context());
33 return api
? api
->delegate() : nullptr;
38 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
39 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
42 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &text
));
43 return delegate
->InsertText(text
);
45 error_
= kNotYetImplementedError
;
49 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
50 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
52 base::Value
* options_value
= nullptr;
53 base::DictionaryValue
* params
= nullptr;
60 EXTENSION_FUNCTION_VALIDATE(args_
->Get(0, &options_value
));
61 EXTENSION_FUNCTION_VALIDATE(options_value
->GetAsDictionary(¶ms
));
62 EXTENSION_FUNCTION_VALIDATE(params
->GetString("type", &type
));
63 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("charValue", &char_value
));
64 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("keyCode", &key_code
));
65 EXTENSION_FUNCTION_VALIDATE(params
->GetString("keyName", &key_name
));
66 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("modifiers", &modifiers
));
67 return delegate
->SendKeyEvent(
68 type
, char_value
, key_code
, key_name
, modifiers
);
70 error_
= kNotYetImplementedError
;
74 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
75 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
77 return delegate
->HideKeyboard();
78 error_
= kNotYetImplementedError
;
82 bool VirtualKeyboardPrivateSetHotrodKeyboardFunction::RunSync() {
83 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
86 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &enable
));
87 delegate
->SetHotrodKeyboard(enable
);
90 error_
= kNotYetImplementedError
;
94 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
95 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
98 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &lock
));
99 return delegate
->LockKeyboard(lock
);
101 error_
= kNotYetImplementedError
;
105 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
106 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
108 return delegate
->OnKeyboardLoaded();
109 error_
= kNotYetImplementedError
;
113 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
114 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
116 base::DictionaryValue
* results
= new base::DictionaryValue();
117 if (delegate
->GetKeyboardConfig(results
)) {
125 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
126 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
128 if (delegate
->IsLanguageSettingsEnabled())
129 return delegate
->ShowLanguageSettings();
132 error_
= kNotYetImplementedError
;
136 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
137 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
139 scoped_ptr
<SetMode::Params
> params
= SetMode::Params::Create(*args_
);
140 EXTENSION_FUNCTION_VALIDATE(params
);
141 if (!delegate
->SetVirtualKeyboardMode(params
->mode
)) {
142 error_
= kVirtualKeyboardNotEnabled
;
148 error_
= kNotYetImplementedError
;
152 bool VirtualKeyboardPrivateSetKeyboardStateFunction::RunSync() {
153 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
155 scoped_ptr
<SetRequestedKeyboardState::Params
> params
=
156 SetRequestedKeyboardState::Params::Create(*args_
);
157 EXTENSION_FUNCTION_VALIDATE(params
);
158 if (!delegate
->SetRequestedKeyboardState(params
->state
)) {
159 error_
= kVirtualKeyboardNotEnabled
;
165 error_
= kNotYetImplementedError
;
169 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext
* context
) {
171 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
174 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
177 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>>
178 g_factory
= LAZY_INSTANCE_INITIALIZER
;
181 BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>*
182 VirtualKeyboardAPI::GetFactoryInstance() {
183 return g_factory
.Pointer();
186 } // namespace extensions