Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / api / virtual_keyboard_private / virtual_keyboard_private_api.cc
blob68d487d3b4026b0b02a6406a5f469baead129737
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 VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
83 VirtualKeyboardDelegate* delegate = GetDelegate(this);
84 if (delegate) {
85 bool lock;
86 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
87 return delegate->LockKeyboard(lock);
89 error_ = kNotYetImplementedError;
90 return false;
93 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
94 VirtualKeyboardDelegate* delegate = GetDelegate(this);
95 if (delegate)
96 return delegate->OnKeyboardLoaded();
97 error_ = kNotYetImplementedError;
98 return false;
101 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
102 VirtualKeyboardDelegate* delegate = GetDelegate(this);
103 if (delegate) {
104 base::DictionaryValue* results = new base::DictionaryValue();
105 if (delegate->GetKeyboardConfig(results)) {
106 SetResult(results);
107 return true;
110 return false;
113 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
114 VirtualKeyboardDelegate* delegate = GetDelegate(this);
115 if (delegate) {
116 if (delegate->IsLanguageSettingsEnabled())
117 return delegate->ShowLanguageSettings();
118 return false;
120 error_ = kNotYetImplementedError;
121 return false;
124 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
125 VirtualKeyboardDelegate* delegate = GetDelegate(this);
126 if (delegate) {
127 scoped_ptr<SetMode::Params> params = SetMode::Params::Create(*args_);
128 EXTENSION_FUNCTION_VALIDATE(params);
129 if (!delegate->SetVirtualKeyboardMode(params->mode)) {
130 error_ = kVirtualKeyboardNotEnabled;
131 return false;
132 } else {
133 return true;
136 error_ = kNotYetImplementedError;
137 return false;
140 bool VirtualKeyboardPrivateSetKeyboardStateFunction::RunSync() {
141 VirtualKeyboardDelegate* delegate = GetDelegate(this);
142 if (delegate) {
143 scoped_ptr<SetRequestedKeyboardState::Params> params =
144 SetRequestedKeyboardState::Params::Create(*args_);
145 EXTENSION_FUNCTION_VALIDATE(params);
146 if (!delegate->SetRequestedKeyboardState(params->state)) {
147 error_ = kVirtualKeyboardNotEnabled;
148 return false;
149 } else {
150 return true;
153 error_ = kNotYetImplementedError;
154 return false;
157 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext* context) {
158 delegate_ =
159 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
162 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
165 static base::LazyInstance<BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>>
166 g_factory = LAZY_INSTANCE_INITIALIZER;
168 // static
169 BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>*
170 VirtualKeyboardAPI::GetFactoryInstance() {
171 return g_factory.Pointer();
174 } // namespace extensions