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 VirtualKeyboardPrivateMoveCursorFunction::RunSync() {
48 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
52 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(0, &swipe_direction
));
53 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(1, &modifier_flags
));
54 return delegate
->MoveCursor(swipe_direction
, modifier_flags
);
56 error_
= kNotYetImplementedError
;
60 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
61 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
63 base::Value
* options_value
= nullptr;
64 base::DictionaryValue
* params
= nullptr;
71 EXTENSION_FUNCTION_VALIDATE(args_
->Get(0, &options_value
));
72 EXTENSION_FUNCTION_VALIDATE(options_value
->GetAsDictionary(¶ms
));
73 EXTENSION_FUNCTION_VALIDATE(params
->GetString("type", &type
));
74 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("charValue", &char_value
));
75 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("keyCode", &key_code
));
76 EXTENSION_FUNCTION_VALIDATE(params
->GetString("keyName", &key_name
));
77 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("modifiers", &modifiers
));
78 return delegate
->SendKeyEvent(
79 type
, char_value
, key_code
, key_name
, modifiers
);
81 error_
= kNotYetImplementedError
;
85 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
86 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
88 return delegate
->HideKeyboard();
89 error_
= kNotYetImplementedError
;
93 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
94 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
97 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &lock
));
98 return delegate
->LockKeyboard(lock
);
100 error_
= kNotYetImplementedError
;
104 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
105 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
107 return delegate
->OnKeyboardLoaded();
108 error_
= kNotYetImplementedError
;
112 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
113 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
115 base::DictionaryValue
* results
= new base::DictionaryValue();
116 if (delegate
->GetKeyboardConfig(results
)) {
124 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
125 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
127 if (delegate
->IsLanguageSettingsEnabled())
128 return delegate
->ShowLanguageSettings();
131 error_
= kNotYetImplementedError
;
135 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
136 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
138 scoped_ptr
<SetMode::Params
> params
= SetMode::Params::Create(*args_
);
139 EXTENSION_FUNCTION_VALIDATE(params
);
140 if (!delegate
->SetVirtualKeyboardMode(params
->mode
)) {
141 error_
= kVirtualKeyboardNotEnabled
;
147 error_
= kNotYetImplementedError
;
151 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext
* context
) {
153 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
156 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
159 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>>
160 g_factory
= LAZY_INSTANCE_INITIALIZER
;
163 BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>*
164 VirtualKeyboardAPI::GetFactoryInstance() {
165 return g_factory
.Pointer();
168 } // namespace extensions