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"
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
;
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
) {
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
) {
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
;
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
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());
94 bool ChromeVirtualKeyboardDelegate::HideKeyboard() {
95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
96 keyboard::KeyboardController
* controller
=
97 keyboard::KeyboardController::GetInstance();
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
);
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");
123 void ChromeVirtualKeyboardDelegate::SetHotrodKeyboard(bool enable
) {
124 if (keyboard::GetHotrodKeyboardEnabled() == enable
)
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();
142 keyboard::KeyboardController::GetInstance()->set_lock_keyboard(state
);
146 bool ChromeVirtualKeyboardDelegate::SendKeyEvent(const std::string
& type
,
149 const std::string
& key_name
,
151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
152 aura::Window
* window
= GetKeyboardContainer();
153 return window
&& keyboard::SendKeyEvent(type
,
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
);
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();
177 controller
->SetKeyboardMode(keyboard_mode
);
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
)
190 ash::Shell::GetInstance()->CreateKeyboard();
192 ash::Shell::GetInstance()->DeactivateKeyboard();
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