Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / virtual_keyboard_private / virtual_keyboard_private_api.cc
blob2d945e9697de4fb0cad39fcd2443b89be0f853bd
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 VirtualKeyboardPrivateMoveCursorFunction::RunSync() {
48 VirtualKeyboardDelegate* delegate = GetDelegate(this);
49 if (delegate) {
50 int swipe_direction;
51 int modifier_flags;
52 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
53 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
54 return delegate->MoveCursor(swipe_direction, modifier_flags);
56 error_ = kNotYetImplementedError;
57 return false;
60 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
61 VirtualKeyboardDelegate* delegate = GetDelegate(this);
62 if (delegate) {
63 base::Value* options_value = nullptr;
64 base::DictionaryValue* params = nullptr;
65 std::string type;
66 int char_value;
67 int key_code;
68 std::string key_name;
69 int modifiers;
71 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
72 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(&params));
73 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
74 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
75 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
76 EXTENSION_FUNCTION_VALIDATE(params->GetString("keyName", &key_name));
77 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
78 return delegate->SendKeyEvent(
79 type, char_value, key_code, key_name, modifiers);
81 error_ = kNotYetImplementedError;
82 return false;
85 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
86 VirtualKeyboardDelegate* delegate = GetDelegate(this);
87 if (delegate)
88 return delegate->HideKeyboard();
89 error_ = kNotYetImplementedError;
90 return false;
93 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
94 VirtualKeyboardDelegate* delegate = GetDelegate(this);
95 if (delegate) {
96 bool lock;
97 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
98 return delegate->LockKeyboard(lock);
100 error_ = kNotYetImplementedError;
101 return false;
104 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
105 VirtualKeyboardDelegate* delegate = GetDelegate(this);
106 if (delegate)
107 return delegate->OnKeyboardLoaded();
108 error_ = kNotYetImplementedError;
109 return false;
112 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
113 VirtualKeyboardDelegate* delegate = GetDelegate(this);
114 if (delegate) {
115 base::DictionaryValue* results = new base::DictionaryValue();
116 if (delegate->GetKeyboardConfig(results)) {
117 SetResult(results);
118 return true;
121 return false;
124 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
125 VirtualKeyboardDelegate* delegate = GetDelegate(this);
126 if (delegate) {
127 if (delegate->IsLanguageSettingsEnabled())
128 return delegate->ShowLanguageSettings();
129 return false;
131 error_ = kNotYetImplementedError;
132 return false;
135 bool VirtualKeyboardPrivateSetModeFunction::RunSync() {
136 VirtualKeyboardDelegate* delegate = GetDelegate(this);
137 if (delegate) {
138 scoped_ptr<SetMode::Params> params = SetMode::Params::Create(*args_);
139 EXTENSION_FUNCTION_VALIDATE(params);
140 if (!delegate->SetVirtualKeyboardMode(params->mode)) {
141 error_ = kVirtualKeyboardNotEnabled;
142 return false;
143 } else {
144 return true;
147 error_ = kNotYetImplementedError;
148 return false;
151 VirtualKeyboardAPI::VirtualKeyboardAPI(content::BrowserContext* context) {
152 delegate_ =
153 extensions::ExtensionsAPIClient::Get()->CreateVirtualKeyboardDelegate();
156 VirtualKeyboardAPI::~VirtualKeyboardAPI() {
159 static base::LazyInstance<BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>>
160 g_factory = LAZY_INSTANCE_INITIALIZER;
162 // static
163 BrowserContextKeyedAPIFactory<VirtualKeyboardAPI>*
164 VirtualKeyboardAPI::GetFactoryInstance() {
165 return g_factory.Pointer();
168 } // namespace extensions