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 "ui/events/event.h"
14 namespace extensions
{
18 const char kNotYetImplementedError
[] =
19 "API is not implemented on this platform.";
21 typedef BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
> factory
;
23 VirtualKeyboardDelegate
* GetDelegate(SyncExtensionFunction
* f
) {
24 VirtualKeyboardAPI
* api
= factory::Get(f
->browser_context());
26 return api
? api
->delegate() : nullptr;
31 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
32 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
35 EXTENSION_FUNCTION_VALIDATE(args_
->GetString(0, &text
));
36 return delegate
->InsertText(text
);
38 error_
= kNotYetImplementedError
;
42 bool VirtualKeyboardPrivateMoveCursorFunction::RunSync() {
43 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
47 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(0, &swipe_direction
));
48 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(1, &modifier_flags
));
49 return delegate
->MoveCursor(swipe_direction
, modifier_flags
);
51 error_
= kNotYetImplementedError
;
55 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
56 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
58 base::Value
* options_value
= nullptr;
59 base::DictionaryValue
* params
= nullptr;
66 EXTENSION_FUNCTION_VALIDATE(args_
->Get(0, &options_value
));
67 EXTENSION_FUNCTION_VALIDATE(options_value
->GetAsDictionary(¶ms
));
68 EXTENSION_FUNCTION_VALIDATE(params
->GetString("type", &type
));
69 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("charValue", &char_value
));
70 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("keyCode", &key_code
));
71 EXTENSION_FUNCTION_VALIDATE(params
->GetString("keyName", &key_name
));
72 EXTENSION_FUNCTION_VALIDATE(params
->GetInteger("modifiers", &modifiers
));
73 return delegate
->SendKeyEvent(
74 type
, char_value
, key_code
, key_name
, modifiers
);
76 error_
= kNotYetImplementedError
;
80 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
81 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
83 return delegate
->HideKeyboard();
84 error_
= kNotYetImplementedError
;
88 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
89 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
92 EXTENSION_FUNCTION_VALIDATE(args_
->GetBoolean(0, &lock
));
93 return delegate
->LockKeyboard(lock
);
95 error_
= kNotYetImplementedError
;
99 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
100 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
102 return delegate
->OnKeyboardLoaded();
103 error_
= kNotYetImplementedError
;
107 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
108 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
110 base::DictionaryValue
* results
= new base::DictionaryValue();
111 if (delegate
->GetKeyboardConfig(results
)) {
119 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
120 VirtualKeyboardDelegate
* delegate
= GetDelegate(this);
122 if (delegate
->IsLanguageSettingsEnabled())
123 return delegate
->ShowLanguageSettings();
126 error_
= kNotYetImplementedError
;
130 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext
* context
) {
132 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
135 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
138 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>>
139 g_factory
= LAZY_INSTANCE_INITIALIZER
;
142 BrowserContextKeyedAPIFactory
<VirtualKeyboardAPI
>*
143 VirtualKeyboardAPI::GetFactoryInstance() {
144 return g_factory
.Pointer();
147 } // namespace extensions