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 "ash/root_window_controller.h"
8 #include "base/command_line.h"
9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/extensions/extension_function_registry.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/user_metrics.h"
15 #include "ui/events/event.h"
16 #include "ui/keyboard/keyboard_controller.h"
17 #include "ui/keyboard/keyboard_switches.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() {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
38 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
40 return keyboard::InsertText(text, ash::Shell::GetPrimaryRootWindow());
42 error_ = kNotYetImplementedError;
46 bool VirtualKeyboardPrivateMoveCursorFunction::RunImpl() {
48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
50 if (!CommandLine::ForCurrentProcess()->HasSwitch(
51 keyboard::switches::kEnableSwipeSelection)) {
57 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
58 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
60 return keyboard::MoveCursor(
63 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
65 error_ = kNotYetImplementedError;
69 bool VirtualKeyboardPrivateSendKeyEventFunction::RunImpl() {
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
73 base::Value* options_value = NULL;
74 base::DictionaryValue* params = NULL;
80 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
81 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(¶ms));
82 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
83 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
84 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
85 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
87 return keyboard::SendKeyEvent(
92 ash::Shell::GetPrimaryRootWindow()->GetDispatcher());
94 error_ = kNotYetImplementedError;
98 bool VirtualKeyboardPrivateHideKeyboardFunction::RunImpl() {
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
102 UMA_HISTOGRAM_ENUMERATION(
103 "VirtualKeyboard.KeyboardControlEvent",
104 keyboard::KEYBOARD_CONTROL_HIDE_USER,
105 keyboard::KEYBOARD_CONTROL_MAX);
107 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
108 // would be user generated.
109 ash::Shell::GetInstance()->keyboard_controller()->HideKeyboard(
110 keyboard::KeyboardController::HIDE_REASON_MANUAL);
114 error_ = kNotYetImplementedError;
118 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunImpl() {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122 keyboard::MarkKeyboardLoadFinished();
124 content::UserMetricsAction("VirtualKeyboardLoaded");
128 error_ = kNotYetImplementedError;
132 InputAPI::InputAPI(Profile* profile) {
135 InputAPI::~InputAPI() {
138 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> >
139 g_factory = LAZY_INSTANCE_INITIALIZER;
142 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
143 return &g_factory.Get();
146 } // namespace extensions