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 VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
83 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
86 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &lock
));
87 return delegate
->LockKeyboard(lock
);
89 error_
= kNotYetImplementedError
;
93 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
94 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
96 return delegate
->OnKeyboardLoaded();
97 error_
= kNotYetImplementedError
;
101 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
102 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
104 base::DictionaryValue
* results
= new base::DictionaryValue();
105 if (delegate
->GetKeyboardConfig(results
)) {
113 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
114 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
116 if (delegate
->IsLanguageSettingsEnabled())
117 return delegate
->ShowLanguageSettings();
120 error_
= kNotYetImplementedError
;
124 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
125 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
127 scoped_ptr
<SetMode::Params
> params
= SetMode::Params::Create(*args_
);
128 EXTENSION_FUNCTION_VALIDATE(params
);
129 if (!delegate
->SetVirtualKeyboardMode(params
->mode
)) {
130 error_
= kVirtualKeyboardNotEnabled
;
136 error_
= kNotYetImplementedError
;
140 bool VirtualKeyboardPrivateSetKeyboardStateFunction::RunSync() {
141 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
143 scoped_ptr
<SetRequestedKeyboardState::Params
> params
=
144 SetRequestedKeyboardState::Params::Create(*args_
);
145 EXTENSION_FUNCTION_VALIDATE(params
);
146 if (!delegate
->SetRequestedKeyboardState(params
->state
)) {
147 error_
= kVirtualKeyboardNotEnabled
;
153 error_
= kNotYetImplementedError
;
157 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext
* context
) {
159 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
162 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
165 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>>
166 g_factory
= LAZY_INSTANCE_INITIALIZER
;
169 BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>*
170 VirtualKeyboardAPI::GetFactoryInstance() {
171 return g_factory
.Pointer();
174 } // namespace extensions