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::core_api::virtual_keyboard_private::SetMode
;
17 namespace extensions
{
21 const char kNotYetImplementedError
[] =
22 "API is not implemented on this platform.";
23 const char kVirtualKeyboardNotEnabled
[] =
24 "The virtual keyboard is not enabled.";
26 typedef BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
> factory
;
28 VirtualKeyboardDelegate
* GetDelegate(SyncExtensionFunction
* f
) {
29 VirtualKeyboardAPI
* api
= factory::Get(f
->browser_context());
31 return api
? api
->delegate() : nullptr;
36 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
37 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
40 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &text
));
41 return delegate
->InsertText(text
);
43 error_
= kNotYetImplementedError
;
47 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
48 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
50 base::Value
* options_value
= nullptr;
51 base::DictionaryValue
* params
= nullptr;
58 EXTENSION_FUNCTION_VALIDATE(args_
->Get(0, &options_value
));
59 EXTENSION_FUNCTION_VALIDATE(options_value
->GetAsDictionary(¶ms
));
60 EXTENSION_FUNCTION_VALIDATE(params
->GetString("type", &type
));
61 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("charValue", &char_value
));
62 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("keyCode", &key_code
));
63 EXTENSION_FUNCTION_VALIDATE(params
->GetString("keyName", &key_name
));
64 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("modifiers", &modifiers
));
65 return delegate
->SendKeyEvent(
66 type
, char_value
, key_code
, key_name
, modifiers
);
68 error_
= kNotYetImplementedError
;
72 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
73 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
75 return delegate
->HideKeyboard();
76 error_
= kNotYetImplementedError
;
80 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
81 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
84 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &lock
));
85 return delegate
->LockKeyboard(lock
);
87 error_
= kNotYetImplementedError
;
91 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
92 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
94 return delegate
->OnKeyboardLoaded();
95 error_
= kNotYetImplementedError
;
99 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
100 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
102 base::DictionaryValue
* results
= new base::DictionaryValue();
103 if (delegate
->GetKeyboardConfig(results
)) {
111 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
112 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
114 if (delegate
->IsLanguageSettingsEnabled())
115 return delegate
->ShowLanguageSettings();
118 error_
= kNotYetImplementedError
;
122 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
123 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
125 scoped_ptr
<SetMode::Params
> params
= SetMode::Params::Create(*args_
);
126 EXTENSION_FUNCTION_VALIDATE(params
);
127 if (!delegate
->SetVirtualKeyboardMode(params
->mode
)) {
128 error_
= kVirtualKeyboardNotEnabled
;
134 error_
= kNotYetImplementedError
;
138 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext
* context
) {
140 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
143 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
146 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>>
147 g_factory
= LAZY_INSTANCE_INITIALIZER
;
150 BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>*
151 VirtualKeyboardAPI::GetFactoryInstance() {
152 return g_factory
.Pointer();
155 } // namespace extensions