Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / virtual_keyboard_private / virtual_keyboard_private_api.cc
blobcb85a9f98f52a45dd7c97a6db44b3e5be48234de
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 {
21 namespace {
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());
32 DCHECK(api);
33 return api ? api->delegate() : nullptr;
36 } // namespace
38 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
39 VirtualKeyboardDelegate* delegate = GetDelegate(this);
40 if (delegate) {
41 base::string16 text;
42 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
43 return delegate->InsertText(text);
45 error_ = kNotYetImplementedError;
46 return false;
49 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
50 VirtualKeyboardDelegate* delegate = GetDelegate(this);
51 if (delegate) {
52 base::Value* options_value = nullptr;
53 base::DictionaryValue* params = nullptr;
54 std::string type;
55 int char_value;
56 int key_code;
57 std::string key_name;
58 int modifiers;
60 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
61 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(&params));
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;
71 return false;
74 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
75 VirtualKeyboardDelegate* delegate = GetDelegate(this);
76 if (delegate)
77 return delegate->HideKeyboard();
78 error_ = kNotYetImplementedError;
79 return false;
82 bool VirtualKeyboardPrivateSetHotrodKeyboardFunction::RunSync() {
83 VirtualKeyboardDelegate* delegate = GetDelegate(this);
84 if (delegate) {
85 bool enable;
86 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &enable));
87 delegate->SetHotrodKeyboard(enable);
88 return true;
90 error_ = kNotYetImplementedError;
91 return false;
94 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
95 VirtualKeyboardDelegate* delegate = GetDelegate(this);
96 if (delegate) {
97 bool lock;
98 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
99 return delegate->LockKeyboard(lock);
101 error_ = kNotYetImplementedError;
102 return false;
105 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
106 VirtualKeyboardDelegate* delegate = GetDelegate(this);
107 if (delegate)
108 return delegate->OnKeyboardLoaded();
109 error_ = kNotYetImplementedError;
110 return false;
113 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
114 VirtualKeyboardDelegate* delegate = GetDelegate(this);
115 if (delegate) {
116 base::DictionaryValue* results = new base::DictionaryValue();
117 if (delegate->GetKeyboardConfig(results)) {
118 SetResult(results);
119 return true;
122 return false;
125 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
126 VirtualKeyboardDelegate* delegate = GetDelegate(this);
127 if (delegate) {
128 if (delegate->IsLanguageSettingsEnabled())
129 return delegate->ShowLanguageSettings();
130 return false;
132 error_ = kNotYetImplementedError;
133 return false;
136 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
137 VirtualKeyboardDelegate* delegate = GetDelegate(this);
138 if (delegate) {
139 scoped_ptr<SetMode::Params> params = SetMode::Params::Create(*args_);
140 EXTENSION_FUNCTION_VALIDATE(params);
141 if (!delegate->SetVirtualKeyboardMode(params->mode)) {
142 error_ = kVirtualKeyboardNotEnabled;
143 return false;
144 } else {
145 return true;
148 error_ = kNotYetImplementedError;
149 return false;
152 bool VirtualKeyboardPrivateSetKeyboardStateFunction::RunSync() {
153 VirtualKeyboardDelegate* delegate = GetDelegate(this);
154 if (delegate) {
155 scoped_ptr<SetRequestedKeyboardState::Params> params =
156 SetRequestedKeyboardState::Params::Create(*args_);
157 EXTENSION_FUNCTION_VALIDATE(params);
158 if (!delegate->SetRequestedKeyboardState(params->state)) {
159 error_ = kVirtualKeyboardNotEnabled;
160 return false;
161 } else {
162 return true;
165 error_ = kNotYetImplementedError;
166 return false;
169 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext* context) {
170 delegate_ =
171 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
174 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
177 static base::LazyInstance<BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>>
178 g_factory = LAZY_INSTANCE_INITIALIZER;
180 // static
181 BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>*
182 VirtualKeyboardAPI::GetFactoryInstance() {
183 return g_factory.Pointer();
186 } // namespace extensions