forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_devices_controller.cc
159 lines (131 loc) · 5.25 KB
/
touch_devices_controller.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/touch/touch_devices_controller.h"
#include "ash/public/cpp/ash_pref_names.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "services/ui/public/cpp/input_devices/input_device_controller_client.h"
namespace ash {
namespace {
void OnSetTouchpadEnabledDone(bool succeeded) {
// Don't log here, |succeeded| is only true if there is a touchpad *and* the
// value changed. In other words |succeeded| is false when not on device or
// the value was already at the value specified. Neither of these are
// interesting failures.
}
ui::InputDeviceControllerClient* GetInputDeviceControllerClient() {
return Shell::Get()->shell_delegate()->GetInputDeviceControllerClient();
}
PrefService* GetActivePrefService() {
return Shell::Get()->session_controller()->GetActivePrefService();
}
} // namespace
// static
void TouchDevicesController::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kTouchpadEnabled, PrefRegistry::PUBLIC);
registry->RegisterBooleanPref(prefs::kTouchscreenEnabled,
PrefRegistry::PUBLIC);
// Chrome doesn't use this, so it isn't marked PUBLIC. It's also not SYNCABLE
// so it isn't observed for changes.
registry->RegisterBooleanPref(prefs::kTouchHudProjectionEnabled, false);
}
TouchDevicesController::TouchDevicesController() {
Shell::Get()->session_controller()->AddObserver(this);
}
TouchDevicesController::~TouchDevicesController() {
Shell::Get()->session_controller()->RemoveObserver(this);
}
void TouchDevicesController::ToggleTouchpad() {
PrefService* prefs = GetActivePrefService();
if (!prefs)
return;
const bool touchpad_enabled = prefs->GetBoolean(prefs::kTouchpadEnabled);
prefs->SetBoolean(prefs::kTouchpadEnabled, !touchpad_enabled);
}
bool TouchDevicesController::GetTouchscreenEnabled(
TouchscreenEnabledSource source) const {
if (source == TouchscreenEnabledSource::GLOBAL)
return global_touchscreen_enabled_;
PrefService* prefs = GetActivePrefService();
return prefs && prefs->GetBoolean(prefs::kTouchscreenEnabled);
}
void TouchDevicesController::SetTouchscreenEnabled(
bool enabled,
TouchscreenEnabledSource source) {
if (source == TouchscreenEnabledSource::GLOBAL) {
global_touchscreen_enabled_ = enabled;
// Explicitly call |UpdateTouchscreenEnabled()| to update the actual
// touchscreen state from multiple sources.
UpdateTouchscreenEnabled();
return;
}
PrefService* prefs = GetActivePrefService();
if (!prefs)
return;
prefs->SetBoolean(prefs::kTouchscreenEnabled, enabled);
}
bool TouchDevicesController::IsTouchHudProjectionEnabled() const {
PrefService* prefs = GetActivePrefService();
// Touch HUD isn't used before login, when |prefs| would be null.
return prefs && prefs->GetBoolean(prefs::kTouchHudProjectionEnabled);
}
void TouchDevicesController::SetTouchHudProjectionEnabled(bool enabled) {
PrefService* prefs = GetActivePrefService();
// Touch HUD isn't used before login, when |prefs| would be null.
if (!prefs)
return;
prefs->SetBoolean(prefs::kTouchHudProjectionEnabled, enabled);
for (RootWindowController* root : Shell::GetAllRootWindowControllers())
root->SetTouchHudProjectionEnabled(enabled);
}
void TouchDevicesController::OnSigninScreenPrefServiceInitialized(
PrefService* prefs) {
ObservePrefs(prefs);
}
void TouchDevicesController::OnActiveUserPrefServiceChanged(
PrefService* prefs) {
ObservePrefs(prefs);
SetTouchHudProjectionEnabled(IsTouchHudProjectionEnabled());
}
void TouchDevicesController::ObservePrefs(PrefService* prefs) {
// Watch for pref updates.
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(
prefs::kTouchpadEnabled,
base::Bind(&TouchDevicesController::UpdateTouchpadEnabled,
base::Unretained(this)));
pref_change_registrar_->Add(
prefs::kTouchscreenEnabled,
base::Bind(&TouchDevicesController::UpdateTouchscreenEnabled,
base::Unretained(this)));
// Load current state.
UpdateTouchpadEnabled();
UpdateTouchscreenEnabled();
}
void TouchDevicesController::UpdateTouchpadEnabled() {
if (!GetInputDeviceControllerClient())
return; // Happens in tests.
PrefService* prefs =
Shell::Get()->session_controller()->GetActivePrefService();
if (!prefs)
return;
GetInputDeviceControllerClient()->SetInternalTouchpadEnabled(
prefs->GetBoolean(prefs::kTouchpadEnabled),
base::BindOnce(&OnSetTouchpadEnabledDone));
}
void TouchDevicesController::UpdateTouchscreenEnabled() {
if (!GetInputDeviceControllerClient())
return; // Happens in tests.
GetInputDeviceControllerClient()->SetTouchscreensEnabled(
GetTouchscreenEnabled(TouchscreenEnabledSource::GLOBAL) &&
GetTouchscreenEnabled(TouchscreenEnabledSource::USER_PREF));
}
} // namespace ash