Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / virtual_keyboard_private / virtual_keyboard_private_api.cc
blob763e23da5e40e675bf8245df111229e27cd6691a
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 {
19 namespace {
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());
30 DCHECK(api);
31 return api ? api->delegate() : nullptr;
34 } // namespace
36 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
37 VirtualKeyboardDelegate* delegate = GetDelegate(this);
38 if (delegate) {
39 base::string16 text;
40 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
41 return delegate->InsertText(text);
43 error_ = kNotYetImplementedError;
44 return false;
47 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
48 VirtualKeyboardDelegate* delegate = GetDelegate(this);
49 if (delegate) {
50 base::Value* options_value = nullptr;
51 base::DictionaryValue* params = nullptr;
52 std::string type;
53 int char_value;
54 int key_code;
55 std::string key_name;
56 int modifiers;
58 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
59 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(&params));
60 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
61 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
62 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
63 EXTENSION_FUNCTION_VALIDATE(params->GetString("keyName", &key_name));
64 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
65 return delegate->SendKeyEvent(
66 type, char_value, key_code, key_name, modifiers);
68 error_ = kNotYetImplementedError;
69 return false;
72 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
73 VirtualKeyboardDelegate* delegate = GetDelegate(this);
74 if (delegate)
75 return delegate->HideKeyboard();
76 error_ = kNotYetImplementedError;
77 return false;
80 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
81 VirtualKeyboardDelegate* delegate = GetDelegate(this);
82 if (delegate) {
83 bool lock;
84 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
85 return delegate->LockKeyboard(lock);
87 error_ = kNotYetImplementedError;
88 return false;
91 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
92 VirtualKeyboardDelegate* delegate = GetDelegate(this);
93 if (delegate)
94 return delegate->OnKeyboardLoaded();
95 error_ = kNotYetImplementedError;
96 return false;
99 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
100 VirtualKeyboardDelegate* delegate = GetDelegate(this);
101 if (delegate) {
102 base::DictionaryValue* results = new base::DictionaryValue();
103 if (delegate->GetKeyboardConfig(results)) {
104 SetResult(results);
105 return true;
108 return false;
111 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
112 VirtualKeyboardDelegate* delegate = GetDelegate(this);
113 if (delegate) {
114 if (delegate->IsLanguageSettingsEnabled())
115 return delegate->ShowLanguageSettings();
116 return false;
118 error_ = kNotYetImplementedError;
119 return false;
122 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
123 VirtualKeyboardDelegate* delegate = GetDelegate(this);
124 if (delegate) {
125 scoped_ptr<SetMode::Params> params = SetMode::Params::Create(*args_);
126 EXTENSION_FUNCTION_VALIDATE(params);
127 if (!delegate->SetVirtualKeyboardMode(params->mode)) {
128 error_ = kVirtualKeyboardNotEnabled;
129 return false;
130 } else {
131 return true;
134 error_ = kNotYetImplementedError;
135 return false;
138 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext* context) {
139 delegate_ =
140 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
143 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
146 static base::LazyInstance<BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>>
147 g_factory = LAZY_INSTANCE_INITIALIZER;
149 // static
150 BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>*
151 VirtualKeyboardAPI::GetFactoryInstance() {
152 return g_factory.Pointer();
155 } // namespace extensions