Move virtualKeyboardPrivate API to src/extensions.
[chromium-blink-merge.git] / chrome / browser / extensions / api / virtual_keyboard_private / chrome_virtual_keyboard_delegate.cc
blobf8617ce9715499c8444daf2c74f7aa47f003ea20
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 "chrome/browser/extensions/api/virtual_keyboard_private/chrome_virtual_keyboard_delegate.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/user_metrics_action.h"
10 #include "base/strings/string16.h"
11 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
12 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/chrome_pages.h"
15 #include "chrome/common/url_constants.h"
16 #include "components/user_manager/user_manager.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/keyboard/keyboard_controller.h"
20 #include "ui/keyboard/keyboard_switches.h"
21 #include "ui/keyboard/keyboard_util.h"
23 namespace {
25 aura::Window* GetKeyboardContainer() {
26 keyboard::KeyboardController* controller =
27 keyboard::KeyboardController::GetInstance();
28 return controller ? controller->GetContainerWindow() : nullptr;
31 } // namespace
33 namespace extensions {
35 bool ChromeVirtualKeyboardDelegate::GetKeyboardConfig(
36 base::DictionaryValue* results) {
37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
38 results->SetString("layout", keyboard::GetKeyboardLayout());
39 results->SetBoolean("a11ymode", keyboard::GetAccessibilityKeyboardEnabled());
40 results->SetBoolean("experimental",
41 keyboard::IsExperimentalInputViewEnabled());
42 return true;
45 bool ChromeVirtualKeyboardDelegate::HideKeyboard() {
46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
47 keyboard::KeyboardController* controller =
48 keyboard::KeyboardController::GetInstance();
49 if (!controller)
50 return false;
52 UMA_HISTOGRAM_ENUMERATION("VirtualKeyboard.KeyboardControlEvent",
53 keyboard::KEYBOARD_CONTROL_HIDE_USER,
54 keyboard::KEYBOARD_CONTROL_MAX);
56 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
57 // would be user generated.
58 controller->HideKeyboard(keyboard::KeyboardController::HIDE_REASON_MANUAL);
59 return true;
62 bool ChromeVirtualKeyboardDelegate::InsertText(const base::string16& text) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
64 keyboard::KeyboardController* controller =
65 keyboard::KeyboardController::GetInstance();
66 if (!controller)
67 return false;
68 aura::Window* window = controller->GetContainerWindow();
69 return window && keyboard::InsertText(text, window);
72 bool ChromeVirtualKeyboardDelegate::OnKeyboardLoaded() {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
74 keyboard::MarkKeyboardLoadFinished();
75 base::UserMetricsAction("VirtualKeyboardLoaded");
76 return true;
79 bool ChromeVirtualKeyboardDelegate::LockKeyboard(bool state) {
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
81 keyboard::KeyboardController* controller =
82 keyboard::KeyboardController::GetInstance();
83 if (!controller)
84 return false;
86 keyboard::KeyboardController::GetInstance()->set_lock_keyboard(state);
87 return true;
90 bool ChromeVirtualKeyboardDelegate::MoveCursor(int swipe_direction,
91 int modifier_flags) {
92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
93 if (!CommandLine::ForCurrentProcess()->HasSwitch(
94 keyboard::switches::kEnableSwipeSelection)) {
95 return false;
97 aura::Window* window = GetKeyboardContainer();
98 return window && keyboard::MoveCursor(
99 swipe_direction, modifier_flags, window->GetHost());
102 bool ChromeVirtualKeyboardDelegate::SendKeyEvent(const std::string& type,
103 int char_value,
104 int key_code,
105 const std::string& key_name,
106 int modifiers) {
107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
108 aura::Window* window = GetKeyboardContainer();
109 return window && keyboard::SendKeyEvent(type,
110 char_value,
111 key_code,
112 key_name,
113 modifiers,
114 window->GetHost());
117 bool ChromeVirtualKeyboardDelegate::ShowLanguageSettings() {
118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
119 content::RecordAction(base::UserMetricsAction("OpenLanguageOptionsDialog"));
120 chrome::ShowSettingsSubPageForProfile(ProfileManager::GetActiveUserProfile(),
121 chrome::kLanguageOptionsSubPage);
122 return true;
125 bool ChromeVirtualKeyboardDelegate::IsLanguageSettingsEnabled() {
126 return (user_manager::UserManager::Get()->IsUserLoggedIn() &&
127 !chromeos::UserAddingScreen::Get()->IsRunning() &&
128 !(chromeos::ScreenLocker::default_screen_locker() &&
129 chromeos::ScreenLocker::default_screen_locker()->locked()));
132 } // namespace extensions