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/profiles/profile_manager.h"
12 #include "chrome/browser/ui/chrome_pages.h"
13 #include "chrome/common/url_constants.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "extensions/browser/extension_function_registry.h"
17 #include "ui/events/event.h"
18 #include "ui/keyboard/keyboard_controller.h"
19 #include "ui/keyboard/keyboard_switches.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
23 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
24 #include "components/user_manager/user_manager.h"
28 #include "ash/root_window_controller.h"
29 #include "ash/shell.h"
30 #include "ui/aura/window_tree_host.h"
31 #include "ui/keyboard/keyboard_util.h"
37 const char kNotYetImplementedError[] =
38 "API is not implemented on this platform.";
43 namespace extensions {
45 bool VirtualKeyboardPrivateInsertTextFunction::RunSync() {
46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
49 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
51 return keyboard::InsertText(text, ash::Shell::GetPrimaryRootWindow());
53 error_ = kNotYetImplementedError;
58 bool VirtualKeyboardPrivateMoveCursorFunction::RunSync() {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
61 if (!CommandLine::ForCurrentProcess()->HasSwitch(
62 keyboard::switches::kEnableSwipeSelection)) {
68 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
69 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
71 return keyboard::MoveCursor(
74 ash::Shell::GetPrimaryRootWindow()->GetHost());
76 error_ = kNotYetImplementedError;
81 bool VirtualKeyboardPrivateSendKeyEventFunction::RunSync() {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
84 base::Value* options_value = NULL;
85 base::DictionaryValue* params = NULL;
92 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &options_value));
93 EXTENSION_FUNCTION_VALIDATE(options_value->GetAsDictionary(¶ms));
94 EXTENSION_FUNCTION_VALIDATE(params->GetString("type", &type));
95 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("charValue", &char_value));
96 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("keyCode", &key_code));
97 EXTENSION_FUNCTION_VALIDATE(params->GetString("keyName", &key_name));
98 EXTENSION_FUNCTION_VALIDATE(params->GetInteger("modifiers", &modifiers));
100 return keyboard::SendKeyEvent(
106 ash::Shell::GetPrimaryRootWindow()->GetHost());
108 error_ = kNotYetImplementedError;
113 bool VirtualKeyboardPrivateHideKeyboardFunction::RunSync() {
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
116 UMA_HISTOGRAM_ENUMERATION(
117 "VirtualKeyboard.KeyboardControlEvent",
118 keyboard::KEYBOARD_CONTROL_HIDE_USER,
119 keyboard::KEYBOARD_CONTROL_MAX);
121 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
122 // would be user generated.
123 keyboard::KeyboardController::GetInstance()->HideKeyboard(
124 keyboard::KeyboardController::HIDE_REASON_MANUAL);
128 error_ = kNotYetImplementedError;
133 bool VirtualKeyboardPrivateLockKeyboardFunction::RunSync() {
134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
137 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &lock));
138 keyboard::KeyboardController::GetInstance()->set_lock_keyboard(lock);
141 error_ = kNotYetImplementedError;
146 bool VirtualKeyboardPrivateKeyboardLoadedFunction::RunSync() {
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
149 keyboard::MarkKeyboardLoadFinished();
150 base::UserMetricsAction("VirtualKeyboardLoaded");
153 error_ = kNotYetImplementedError;
158 bool VirtualKeyboardPrivateGetKeyboardConfigFunction::RunSync() {
159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
161 base::DictionaryValue* results = new base::DictionaryValue();
162 results->SetString("layout", keyboard::GetKeyboardLayout());
163 results->SetBoolean("a11ymode", keyboard::GetAccessibilityKeyboardEnabled());
164 results->SetBoolean("experimental",
165 keyboard::IsExperimentalInputViewEnabled());
169 error_ = kNotYetImplementedError;
174 bool VirtualKeyboardPrivateOpenSettingsFunction::RunSync() {
175 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
177 #if defined(OS_CHROMEOS)
178 // Do not try to open language options page if user is not logged in or
180 if (!user_manager::UserManager::Get()->IsUserLoggedIn() ||
181 chromeos::UserAddingScreen::Get()->IsRunning() ||
182 (chromeos::ScreenLocker::default_screen_locker() &&
183 chromeos::ScreenLocker::default_screen_locker()->locked()))
185 #endif // OS_CHROMEOS
187 content::RecordAction(base::UserMetricsAction("OpenLanguageOptionsDialog"));
188 chrome::ShowSettingsSubPageForProfile(
189 ProfileManager::GetActiveUserProfile(), chrome::kLanguageOptionsSubPage);
192 error_ = kNotYetImplementedError;
197 InputAPI::InputAPI(content::BrowserContext* context) {}
199 InputAPI::~InputAPI() {
202 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputAPI> > g_factory =
203 LAZY_INSTANCE_INITIALIZER;
206 BrowserContextKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
207 return g_factory.Pointer();
210 } // namespace extensions