1 // Copyright 2012 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 "chrome/browser/extensions/api/input/input.h"
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string16.h"
11 #include "chrome/browser/extensions/extension_function_registry.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/user_metrics.h"
14 #include "ui/events/event.h"
15 #include "ui/keyboard/keyboard_controller.h"
16 #include "ui/keyboard/keyboard_switches.h"
19 #include "ash/root_window_controller.h"
20 #include "ash/shell.h"
21 #include "ui/keyboard/keyboard_util.h"
26 const char kNotYetImplementedError[] =
27 "API is not implemented on this platform.";
31 namespace extensions {
33 bool VirtualKeyboardPrivateInsertTextFunction::RunImpl() {
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
37 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
39 return keyboard::InsertText(text, ash::Shell::GetPrimaryRootWindow());
41 error_ = kNotYetImplementedError;
45 bool VirtualKeyboardPrivateMoveCursorFunction::RunImpl() {
46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
48 if (!CommandLine::ForCurrentProcess()->HasSwitch(
49 keyboard::switches::kEnableSwipeSelection)) {
55 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
56 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
58 return keyboard::MoveCursor(
61 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
63 error_ = kNotYetImplementedError;
67 bool VirtualKeyboardPrivateSendKeyEventFunction::RunImpl() {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
70 base::Value* options_value = NULL;
71 base::DictionaryValue* params = NULL;
78 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
79 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(¶ms));
80 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
81 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
82 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
83 EXTENSION_FUNCTION_VALIDATE(params->GetString("keyName", &key_name));
84 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
86 return keyboard::SendKeyEvent(
92 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
94 error_ = kNotYetImplementedError;
98 bool VirtualKeyboardPrivateHideKeyboardFunction::RunImpl() {
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101 UMA_HISTOGRAM_ENUMERATION(
102 "VirtualKeyboard.KeyboardControlEvent",
103 keyboard::KEYBOARD_CONTROL_HIDE_USER,
104 keyboard::KEYBOARD_CONTROL_MAX);
106 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
107 // would be user generated.
108 ash::Shell::GetInstance()->keyboard_controller()->HideKeyboard(
109 keyboard::KeyboardController::HIDE_REASON_MANUAL);
113 error_ = kNotYetImplementedError;
117 bool VirtualKeyboardPrivateLockKeyboardFunction::RunImpl() {
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
123 ash::Shell::GetInstance()->keyboard_controller()->set_lock_keyboard(lock);
127 error_ = kNotYetImplementedError;
131 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunImpl() {
132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
134 keyboard::MarkKeyboardLoadFinished();
136 base::UserMetricsAction("VirtualKeyboardLoaded");
140 error_ = kNotYetImplementedError;
144 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunImpl() {
145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
147 base::DictionaryValue* results = new base::DictionaryValue();
148 results->SetString("layout", keyboard::GetKeyboardLayout());
149 results->SetBoolean("a11ymode", keyboard::GetAccessibilityKeyboardEnabled());
153 error_ = kNotYetImplementedError;
157 InputAPI::InputAPI(Profile* profile) {
160 InputAPI::~InputAPI() {
163 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> >
164 g_factory = LAZY_INSTANCE_INITIALIZER;
167 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
168 return g_factory.Pointer();
171 } // namespace extensions