blob: 9b72e5f87af96669a149c7a7601da04e1d9dad14 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2017 The Chromium Authors
Qiang Xuf1400a362017-09-14 02:28:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/touch/touch_devices_controller.h"
6
Qiang Xu6b7b1842018-03-12 19:00:087#include <utility>
8
Henrique Ferreirof9d1cb22021-07-13 01:32:479#include "ash/constants/ash_pref_names.h"
Henrique Ferreirof3fbcea22021-02-05 23:12:1910#include "ash/constants/ash_switches.h"
James Cookbd0b7792017-11-17 03:24:2611#include "ash/root_window_controller.h"
Xiyuan Xiae7b19542019-05-06 23:05:1812#include "ash/session/session_controller_impl.h"
Qiang Xuf1400a362017-09-14 02:28:3013#include "ash/shell.h"
14#include "ash/shell_delegate.h"
Ana Salazareb7c25712020-02-03 20:27:2615#include "base/command_line.h"
Avi Drissman4de6dab2023-01-06 23:17:3516#include "base/functional/bind.h"
Qiang Xu7e7dd6b2018-03-06 05:27:5117#include "base/metrics/histogram_macros.h"
18#include "components/pref_registry/pref_registry_syncable.h"
Qiang Xuf1400a362017-09-14 02:28:3019#include "components/prefs/pref_change_registrar.h"
20#include "components/prefs/pref_registry_simple.h"
21#include "components/prefs/pref_service.h"
Scott Violetde79e422019-06-13 04:01:2522#include "ui/ozone/public/input_controller.h"
23#include "ui/ozone/public/ozone_platform.h"
Qiang Xuf1400a362017-09-14 02:28:3024
25namespace ash {
26
27namespace {
28
James Cookbd0b7792017-11-17 03:24:2629PrefService* GetActivePrefService() {
30 return Shell::Get()->session_controller()->GetActivePrefService();
31}
32
Qiang Xuf1400a362017-09-14 02:28:3033} // namespace
34
35// static
Ana Salazareb7c25712020-02-03 20:27:2636void TouchDevicesController::RegisterProfilePrefs(PrefRegistrySimple* registry,
37 bool for_test) {
Qiang Xue534b672018-04-06 22:19:5338 registry->RegisterBooleanPref(
39 prefs::kTapDraggingEnabled, false,
James Cookbf73f082019-10-23 03:19:0440 user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
James Cook277f6432019-10-18 22:39:3341 registry->RegisterBooleanPref(prefs::kTouchpadEnabled, true);
42 registry->RegisterBooleanPref(prefs::kTouchscreenEnabled, true);
Ana Salazareb7c25712020-02-03 20:27:2643 if (for_test) {
44 registry->RegisterBooleanPref(
45 prefs::kNaturalScroll,
46 base::CommandLine::ForCurrentProcess()->HasSwitch(
Henrique Ferreiro93dd33b2022-01-18 16:06:4547 switches::kNaturalScrollDefault),
Ana Salazareb7c25712020-02-03 20:27:2648 user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
49 }
Qiang Xuf1400a362017-09-14 02:28:3050}
51
52TouchDevicesController::TouchDevicesController() {
53 Shell::Get()->session_controller()->AddObserver(this);
54}
55
56TouchDevicesController::~TouchDevicesController() {
57 Shell::Get()->session_controller()->RemoveObserver(this);
58}
59
60void TouchDevicesController::ToggleTouchpad() {
James Cookbd0b7792017-11-17 03:24:2661 PrefService* prefs = GetActivePrefService();
Qiang Xuf1400a362017-09-14 02:28:3062 if (!prefs)
63 return;
64 const bool touchpad_enabled = prefs->GetBoolean(prefs::kTouchpadEnabled);
65 prefs->SetBoolean(prefs::kTouchpadEnabled, !touchpad_enabled);
66}
67
Jun Mukaic82ae4e72018-04-03 03:08:1368bool TouchDevicesController::GetTouchpadEnabled(
69 TouchDeviceEnabledSource source) const {
70 if (source == TouchDeviceEnabledSource::GLOBAL)
71 return global_touchpad_enabled_;
72
73 PrefService* prefs = GetActivePrefService();
74 return prefs && prefs->GetBoolean(prefs::kTouchpadEnabled);
75}
76
77void TouchDevicesController::SetTouchpadEnabled(
78 bool enabled,
79 TouchDeviceEnabledSource source) {
80 if (source == TouchDeviceEnabledSource::GLOBAL) {
81 global_touchpad_enabled_ = enabled;
82 UpdateTouchpadEnabled();
83 return;
84 }
85
86 PrefService* prefs = GetActivePrefService();
87 if (!prefs)
88 return;
89 prefs->SetBoolean(prefs::kTouchpadEnabled, enabled);
90}
91
Qiang Xuf1400a362017-09-14 02:28:3092bool TouchDevicesController::GetTouchscreenEnabled(
Jun Mukaic82ae4e72018-04-03 03:08:1393 TouchDeviceEnabledSource source) const {
94 if (source == TouchDeviceEnabledSource::GLOBAL)
Qiang Xuf1400a362017-09-14 02:28:3095 return global_touchscreen_enabled_;
96
James Cookbd0b7792017-11-17 03:24:2697 PrefService* prefs = GetActivePrefService();
Qiang Xuf1400a362017-09-14 02:28:3098 return prefs && prefs->GetBoolean(prefs::kTouchscreenEnabled);
99}
100
101void TouchDevicesController::SetTouchscreenEnabled(
102 bool enabled,
Jun Mukaic82ae4e72018-04-03 03:08:13103 TouchDeviceEnabledSource source) {
104 if (source == TouchDeviceEnabledSource::GLOBAL) {
Qiang Xuf1400a362017-09-14 02:28:30105 global_touchscreen_enabled_ = enabled;
106 // Explicitly call |UpdateTouchscreenEnabled()| to update the actual
107 // touchscreen state from multiple sources.
108 UpdateTouchscreenEnabled();
109 return;
110 }
111
James Cookbd0b7792017-11-17 03:24:26112 PrefService* prefs = GetActivePrefService();
Qiang Xuf1400a362017-09-14 02:28:30113 if (!prefs)
114 return;
115 prefs->SetBoolean(prefs::kTouchscreenEnabled, enabled);
116}
117
Qiang Xu7e7dd6b2018-03-06 05:27:51118void TouchDevicesController::OnUserSessionAdded(const AccountId& account_id) {
Qiang Xu6b7b1842018-03-12 19:00:08119 uma_record_callback_ = base::BindOnce([](PrefService* prefs) {
120 UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Started",
121 prefs->GetBoolean(prefs::kTapDraggingEnabled));
122 });
Qiang Xu7e7dd6b2018-03-06 05:27:51123}
124
Qiang Xuf1400a362017-09-14 02:28:30125void TouchDevicesController::OnSigninScreenPrefServiceInitialized(
126 PrefService* prefs) {
127 ObservePrefs(prefs);
128}
129
130void TouchDevicesController::OnActiveUserPrefServiceChanged(
131 PrefService* prefs) {
Qiang Xu6b7b1842018-03-12 19:00:08132 if (uma_record_callback_)
133 std::move(uma_record_callback_).Run(prefs);
Qiang Xuf1400a362017-09-14 02:28:30134 ObservePrefs(prefs);
Qiang Xuf1400a362017-09-14 02:28:30135}
136
137void TouchDevicesController::ObservePrefs(PrefService* prefs) {
138 // Watch for pref updates.
139 pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
140 pref_change_registrar_->Init(prefs);
141 pref_change_registrar_->Add(
Qiang Xu7e7dd6b2018-03-06 05:27:51142 prefs::kTapDraggingEnabled,
Qiang Xudadd3ad62018-03-07 17:22:45143 base::BindRepeating(&TouchDevicesController::UpdateTapDraggingEnabled,
144 base::Unretained(this)));
Qiang Xu7e7dd6b2018-03-06 05:27:51145 pref_change_registrar_->Add(
Qiang Xuf1400a362017-09-14 02:28:30146 prefs::kTouchpadEnabled,
Qiang Xudadd3ad62018-03-07 17:22:45147 base::BindRepeating(&TouchDevicesController::UpdateTouchpadEnabled,
148 base::Unretained(this)));
Qiang Xuf1400a362017-09-14 02:28:30149 pref_change_registrar_->Add(
150 prefs::kTouchscreenEnabled,
Qiang Xudadd3ad62018-03-07 17:22:45151 base::BindRepeating(&TouchDevicesController::UpdateTouchscreenEnabled,
152 base::Unretained(this)));
Qiang Xuf1400a362017-09-14 02:28:30153 // Load current state.
Qiang Xu7e7dd6b2018-03-06 05:27:51154 UpdateTapDraggingEnabled();
Qiang Xuf1400a362017-09-14 02:28:30155 UpdateTouchpadEnabled();
156 UpdateTouchscreenEnabled();
157}
158
Qiang Xu7e7dd6b2018-03-06 05:27:51159void TouchDevicesController::UpdateTapDraggingEnabled() {
160 PrefService* prefs = GetActivePrefService();
161 const bool enabled = prefs->GetBoolean(prefs::kTapDraggingEnabled);
162
163 if (tap_dragging_enabled_ == enabled)
164 return;
165
166 tap_dragging_enabled_ = enabled;
167
168 UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Changed", enabled);
169
Scott Violetde79e422019-06-13 04:01:25170 ui::OzonePlatform::GetInstance()->GetInputController()->SetTapDragging(
David Padlipskyc5afc052023-03-02 23:13:37171 absl::nullopt, enabled);
Qiang Xu7e7dd6b2018-03-06 05:27:51172}
173
Qiang Xuf1400a362017-09-14 02:28:30174void TouchDevicesController::UpdateTouchpadEnabled() {
Avery Musbach8502842e2020-10-05 18:20:21175 ui::OzonePlatform::GetInstance()
176 ->GetInputController()
177 ->SetInternalTouchpadEnabled(
178 GetTouchpadEnabled(TouchDeviceEnabledSource::GLOBAL) &&
179 GetTouchpadEnabled(TouchDeviceEnabledSource::USER_PREF));
Qiang Xuf1400a362017-09-14 02:28:30180}
181
182void TouchDevicesController::UpdateTouchscreenEnabled() {
Scott Violetde79e422019-06-13 04:01:25183 ui::OzonePlatform::GetInstance()
184 ->GetInputController()
185 ->SetTouchscreensEnabled(
186 GetTouchscreenEnabled(TouchDeviceEnabledSource::GLOBAL) &&
187 GetTouchscreenEnabled(TouchDeviceEnabledSource::USER_PREF));
Qiang Xuf1400a362017-09-14 02:28:30188}
189
190} // namespace ash