Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / virtual_keyboard_private / virtual_keyboard_private_api.cc
blobc51790094fc32a0d24968c5a1aa1e0804afc3369
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 {
16 namespace {
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());
25 DCHECK(api);
26 return api ? api->delegate() : nullptr;
29 } // namespace
31 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
32 VirtualKeyboardDelegate* delegate = GetDelegate(this);
33 if (delegate) {
34 base::string16 text;
35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
36 return delegate->InsertText(text);
38 error_ = kNotYetImplementedError;
39 return false;
42 bool VirtualKeyboardPrivateMoveCursorFunction::RunSync() {
43 VirtualKeyboardDelegate* delegate = GetDelegate(this);
44 if (delegate) {
45 int swipe_direction;
46 int modifier_flags;
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;
52 return false;
55 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
56 VirtualKeyboardDelegate* delegate = GetDelegate(this);
57 if (delegate) {
58 base::Value* options_value = nullptr;
59 base::DictionaryValue* params = nullptr;
60 std::string type;
61 int char_value;
62 int key_code;
63 std::string key_name;
64 int modifiers;
66 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
67 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(&params));
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;
77 return false;
80 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
81 VirtualKeyboardDelegate* delegate = GetDelegate(this);
82 if (delegate)
83 return delegate->HideKeyboard();
84 error_ = kNotYetImplementedError;
85 return false;
88 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
89 VirtualKeyboardDelegate* delegate = GetDelegate(this);
90 if (delegate) {
91 bool lock;
92 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
93 return delegate->LockKeyboard(lock);
95 error_ = kNotYetImplementedError;
96 return false;
99 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
100 VirtualKeyboardDelegate* delegate = GetDelegate(this);
101 if (delegate)
102 return delegate->OnKeyboardLoaded();
103 error_ = kNotYetImplementedError;
104 return false;
107 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
108 VirtualKeyboardDelegate* delegate = GetDelegate(this);
109 if (delegate) {
110 base::DictionaryValue* results = new base::DictionaryValue();
111 if (delegate->GetKeyboardConfig(results)) {
112 SetResult(results);
113 return true;
116 return false;
119 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
120 VirtualKeyboardDelegate* delegate = GetDelegate(this);
121 if (delegate) {
122 if (delegate->IsLanguageSettingsEnabled())
123 return delegate->ShowLanguageSettings();
124 return false;
126 error_ = kNotYetImplementedError;
127 return false;
130 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext* context) {
131 delegate_ =
132 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
135 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
138 static base::LazyInstance<BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>>
139 g_factory = LAZY_INSTANCE_INITIALIZER;
141 // static
142 BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>*
143 VirtualKeyboardAPI::GetFactoryInstance() {
144 return g_factory.Pointer();
147 } // namespace extensions