Linux Sandbox: make sure Credentials test pass on TSAN
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blob3ecc0bc271c1f3164438eebbd4b1c4ef2de29217
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 "ash/virtual_keyboard_controller.h"
7 #include <vector>
9 #include "ash/shell.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "ash_switches.h"
12 #include "base/command_line.h"
13 #include "base/strings/string_util.h"
14 #include "ui/events/devices/device_data_manager.h"
15 #include "ui/events/devices/input_device.h"
16 #include "ui/events/devices/keyboard_device.h"
17 #include "ui/events/devices/touchscreen_device.h"
18 #include "ui/gfx/x/x11_types.h"
19 #include "ui/keyboard/keyboard_switches.h"
20 #include "ui/keyboard/keyboard_util.h"
22 namespace ash {
23 namespace {
25 // Checks whether smart deployment is enabled.
26 bool IsSmartVirtualKeyboardEnabled() {
27 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
28 keyboard::switches::kEnableVirtualKeyboard)) {
29 return false;
31 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
32 keyboard::switches::kDisableSmartVirtualKeyboard);
35 } // namespace
37 VirtualKeyboardController::VirtualKeyboardController()
38 : has_external_keyboard_(false),
39 has_internal_keyboard_(false),
40 has_touchscreen_(false),
41 ignore_external_keyboard_(false) {
42 Shell::GetInstance()->AddShellObserver(this);
43 ui::DeviceDataManager::GetInstance()->AddObserver(this);
44 UpdateDevices();
47 VirtualKeyboardController::~VirtualKeyboardController() {
48 Shell::GetInstance()->RemoveShellObserver(this);
49 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
52 void VirtualKeyboardController::OnMaximizeModeStarted() {
53 if (!IsSmartVirtualKeyboardEnabled())
54 SetKeyboardEnabled(true);
57 void VirtualKeyboardController::OnMaximizeModeEnded() {
58 if (!IsSmartVirtualKeyboardEnabled())
59 SetKeyboardEnabled(false);
62 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
63 UpdateDevices();
66 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
67 UpdateDevices();
70 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
71 ignore_external_keyboard_ = !ignore_external_keyboard_;
72 UpdateKeyboardEnabled();
75 void VirtualKeyboardController::UpdateDevices() {
76 ui::DeviceDataManager* device_data_manager =
77 ui::DeviceDataManager::GetInstance();
79 // Checks for touchscreens.
80 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
82 // Checks for keyboards.
83 has_external_keyboard_ = false;
84 has_internal_keyboard_ = false;
85 for (const ui::KeyboardDevice& device :
86 device_data_manager->keyboard_devices()) {
87 if (has_internal_keyboard_ && has_external_keyboard_)
88 break;
89 ui::InputDeviceType type = device.type;
90 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
91 has_internal_keyboard_ = true;
92 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
93 has_external_keyboard_ = true;
95 // Update keyboard state.
96 UpdateKeyboardEnabled();
99 void VirtualKeyboardController::UpdateKeyboardEnabled() {
100 if (!IsSmartVirtualKeyboardEnabled()) {
101 SetKeyboardEnabled(Shell::GetInstance()
102 ->maximize_mode_controller()
103 ->IsMaximizeModeWindowManagerEnabled());
104 return;
106 SetKeyboardEnabled(!has_internal_keyboard_ && has_touchscreen_ &&
107 (!has_external_keyboard_ || ignore_external_keyboard_));
108 ash::Shell::GetInstance()
109 ->system_tray_notifier()
110 ->NotifyVirtualKeyboardSuppressionChanged(!has_internal_keyboard_ &&
111 has_touchscreen_ &&
112 has_external_keyboard_);
115 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
116 keyboard::SetTouchKeyboardEnabled(enabled);
117 if (enabled) {
118 Shell::GetInstance()->CreateKeyboard();
119 } else {
120 if (!keyboard::IsKeyboardEnabled())
121 Shell::GetInstance()->DeactivateKeyboard();
125 } // namespace ash