Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / api / virtual_keyboard_private / chrome_virtual_keyboard_delegate.cc
blob943f58d01485afdbc8f905564342097f3e386ee1
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 <string>
9 #include "ash/shell.h"
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/user_metrics_action.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
15 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/user_manager/user_manager.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "extensions/common/api/virtual_keyboard_private.h"
22 #include "ui/aura/window_tree_host.h"
23 #include "ui/keyboard/keyboard_controller.h"
24 #include "ui/keyboard/keyboard_switches.h"
25 #include "ui/keyboard/keyboard_util.h"
27 namespace keyboard_api = extensions::api::virtual_keyboard_private;
29 namespace {
31 aura::Window* GetKeyboardContainer() {
32 keyboard::KeyboardController* controller =
33 keyboard::KeyboardController::GetInstance();
34 return controller ? controller->GetContainerWindow() : nullptr;
37 std::string GenerateFeatureFlag(std::string feature, bool enabled) {
38 return feature + (enabled ? "-enabled" : "-disabled");
41 keyboard::KeyboardMode getKeyboardModeEnum(keyboard_api::KeyboardMode mode) {
42 switch (mode) {
43 case keyboard_api::KEYBOARD_MODE_NONE:
44 return keyboard::NONE;
45 case keyboard_api::KEYBOARD_MODE_FULL_WIDTH:
46 return keyboard::FULL_WIDTH;
47 case keyboard_api::KEYBOARD_MODE_FLOATING:
48 return keyboard::FLOATING;
50 return keyboard::NONE;
53 keyboard::KeyboardState getKeyboardStateEnum(
54 keyboard_api::KeyboardState state) {
55 switch (state) {
56 case keyboard_api::KEYBOARD_STATE_ENABLED:
57 return keyboard::KEYBOARD_STATE_ENABLED;
58 case keyboard_api::KEYBOARD_STATE_DISABLED:
59 return keyboard::KEYBOARD_STATE_DISABLED;
60 case keyboard_api::KEYBOARD_STATE_AUTO:
61 case keyboard_api::KEYBOARD_STATE_NONE:
62 return keyboard::KEYBOARD_STATE_AUTO;
64 return keyboard::KEYBOARD_STATE_AUTO;
67 } // namespace
69 namespace extensions {
71 bool ChromeVirtualKeyboardDelegate::GetKeyboardConfig(
72 base::DictionaryValue* results) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
74 results->SetString("layout", keyboard::GetKeyboardLayout());
75 // TODO(bshe): Consolidate a11y, hotrod and normal mode into a mode enum. See
76 // crbug.com/529474.
77 results->SetBoolean("a11ymode", keyboard::GetAccessibilityKeyboardEnabled());
78 results->SetBoolean("hotrodmode", keyboard::GetHotrodKeyboardEnabled());
79 scoped_ptr<base::ListValue> features(new base::ListValue());
80 features->AppendString(GenerateFeatureFlag(
81 "floatingvirtualkeyboard", keyboard::IsFloatingVirtualKeyboardEnabled()));
82 features->AppendString(
83 GenerateFeatureFlag("gesturetyping", keyboard::IsGestureTypingEnabled()));
84 features->AppendString(GenerateFeatureFlag(
85 "gestureediting", keyboard::IsGestureEditingEnabled()));
86 features->AppendString(
87 GenerateFeatureFlag("voiceinput", keyboard::IsVoiceInputEnabled()));
88 features->AppendString(GenerateFeatureFlag("experimental",
89 keyboard::IsExperimentalInputViewEnabled()));
90 results->Set("features", features.Pass());
91 return true;
94 bool ChromeVirtualKeyboardDelegate::HideKeyboard() {
95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
96 keyboard::KeyboardController* controller =
97 keyboard::KeyboardController::GetInstance();
98 if (!controller)
99 return false;
101 UMA_HISTOGRAM_ENUMERATION("VirtualKeyboard.KeyboardControlEvent",
102 keyboard::KEYBOARD_CONTROL_HIDE_USER,
103 keyboard::KEYBOARD_CONTROL_MAX);
105 // Pass HIDE_REASON_MANUAL since calls to HideKeyboard as part of this API
106 // would be user generated.
107 controller->HideKeyboard(keyboard::KeyboardController::HIDE_REASON_MANUAL);
108 return true;
111 bool ChromeVirtualKeyboardDelegate::InsertText(const base::string16& text) {
112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
113 return keyboard::InsertText(text);
116 bool ChromeVirtualKeyboardDelegate::OnKeyboardLoaded() {
117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
118 keyboard::MarkKeyboardLoadFinished();
119 base::UserMetricsAction("VirtualKeyboardLoaded");
120 return true;
123 void ChromeVirtualKeyboardDelegate::SetHotrodKeyboard(bool enable) {
124 if (keyboard::GetHotrodKeyboardEnabled() == enable)
125 return;
127 keyboard::SetHotrodKeyboardEnabled(enable);
128 // This reloads virtual keyboard even if it exists. This ensures virtual
129 // keyboard gets the correct state of the hotrod keyboard through
130 // chrome.virtualKeyboardPrivate.getKeyboardConfig.
131 if (enable && keyboard::IsKeyboardEnabled())
132 ash::Shell::GetInstance()->CreateKeyboard();
135 bool ChromeVirtualKeyboardDelegate::LockKeyboard(bool state) {
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
137 keyboard::KeyboardController* controller =
138 keyboard::KeyboardController::GetInstance();
139 if (!controller)
140 return false;
142 keyboard::KeyboardController::GetInstance()->set_lock_keyboard(state);
143 return true;
146 bool ChromeVirtualKeyboardDelegate::SendKeyEvent(const std::string& type,
147 int char_value,
148 int key_code,
149 const std::string& key_name,
150 int modifiers) {
151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
152 aura::Window* window = GetKeyboardContainer();
153 return window && keyboard::SendKeyEvent(type,
154 char_value,
155 key_code,
156 key_name,
157 modifiers,
158 window->GetHost());
161 bool ChromeVirtualKeyboardDelegate::ShowLanguageSettings() {
162 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
163 content::RecordAction(base::UserMetricsAction("OpenLanguageOptionsDialog"));
164 chrome::ShowSettingsSubPageForProfile(ProfileManager::GetActiveUserProfile(),
165 chrome::kLanguageOptionsSubPage);
166 return true;
169 bool ChromeVirtualKeyboardDelegate::SetVirtualKeyboardMode(int mode_enum) {
170 keyboard::KeyboardMode keyboard_mode =
171 getKeyboardModeEnum(static_cast<keyboard_api::KeyboardMode>(mode_enum));
172 keyboard::KeyboardController* controller =
173 keyboard::KeyboardController::GetInstance();
174 if (!controller)
175 return false;
177 controller->SetKeyboardMode(keyboard_mode);
178 return true;
181 bool ChromeVirtualKeyboardDelegate::SetRequestedKeyboardState(int state_enum) {
182 keyboard::KeyboardState keyboard_state = getKeyboardStateEnum(
183 static_cast<keyboard_api::KeyboardState>(state_enum));
184 bool was_enabled = keyboard::IsKeyboardEnabled();
185 keyboard::SetRequestedKeyboardState(keyboard_state);
186 bool is_enabled = keyboard::IsKeyboardEnabled();
187 if (was_enabled == is_enabled)
188 return true;
189 if (is_enabled) {
190 ash::Shell::GetInstance()->CreateKeyboard();
191 } else {
192 ash::Shell::GetInstance()->DeactivateKeyboard();
194 return true;
197 bool ChromeVirtualKeyboardDelegate::IsLanguageSettingsEnabled() {
198 return (user_manager::UserManager::Get()->IsUserLoggedIn() &&
199 !chromeos::UserAddingScreen::Get()->IsRunning() &&
200 !(chromeos::ScreenLocker::default_screen_locker() &&
201 chromeos::ScreenLocker::default_screen_locker()->locked()));
204 } // namespace extensions