Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 2 | // 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 Xu | 6b7b184 | 2018-03-12 19:00:08 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Henrique Ferreiro | f9d1cb2 | 2021-07-13 01:32:47 | [diff] [blame] | 9 | #include "ash/constants/ash_pref_names.h" |
Henrique Ferreiro | f3fbcea2 | 2021-02-05 23:12:19 | [diff] [blame] | 10 | #include "ash/constants/ash_switches.h" |
James Cook | bd0b779 | 2017-11-17 03:24:26 | [diff] [blame] | 11 | #include "ash/root_window_controller.h" |
Xiyuan Xia | e7b1954 | 2019-05-06 23:05:18 | [diff] [blame] | 12 | #include "ash/session/session_controller_impl.h" |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 13 | #include "ash/shell.h" |
| 14 | #include "ash/shell_delegate.h" |
Ana Salazar | eb7c2571 | 2020-02-03 20:27:26 | [diff] [blame] | 15 | #include "base/command_line.h" |
Avi Drissman | 4de6dab | 2023-01-06 23:17:35 | [diff] [blame] | 16 | #include "base/functional/bind.h" |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 17 | #include "base/metrics/histogram_macros.h" |
| 18 | #include "components/pref_registry/pref_registry_syncable.h" |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 19 | #include "components/prefs/pref_change_registrar.h" |
| 20 | #include "components/prefs/pref_registry_simple.h" |
| 21 | #include "components/prefs/pref_service.h" |
Scott Violet | de79e42 | 2019-06-13 04:01:25 | [diff] [blame] | 22 | #include "ui/ozone/public/input_controller.h" |
| 23 | #include "ui/ozone/public/ozone_platform.h" |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 24 | |
| 25 | namespace ash { |
| 26 | |
| 27 | namespace { |
| 28 | |
James Cook | bd0b779 | 2017-11-17 03:24:26 | [diff] [blame] | 29 | PrefService* GetActivePrefService() { |
| 30 | return Shell::Get()->session_controller()->GetActivePrefService(); |
| 31 | } |
| 32 | |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 33 | } // namespace |
| 34 | |
| 35 | // static |
Ana Salazar | eb7c2571 | 2020-02-03 20:27:26 | [diff] [blame] | 36 | void TouchDevicesController::RegisterProfilePrefs(PrefRegistrySimple* registry, |
| 37 | bool for_test) { |
Qiang Xu | e534b67 | 2018-04-06 22:19:53 | [diff] [blame] | 38 | registry->RegisterBooleanPref( |
| 39 | prefs::kTapDraggingEnabled, false, |
James Cook | bf73f08 | 2019-10-23 03:19:04 | [diff] [blame] | 40 | user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF); |
James Cook | 277f643 | 2019-10-18 22:39:33 | [diff] [blame] | 41 | registry->RegisterBooleanPref(prefs::kTouchpadEnabled, true); |
| 42 | registry->RegisterBooleanPref(prefs::kTouchscreenEnabled, true); |
Ana Salazar | eb7c2571 | 2020-02-03 20:27:26 | [diff] [blame] | 43 | if (for_test) { |
| 44 | registry->RegisterBooleanPref( |
| 45 | prefs::kNaturalScroll, |
| 46 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
Henrique Ferreiro | 93dd33b | 2022-01-18 16:06:45 | [diff] [blame] | 47 | switches::kNaturalScrollDefault), |
Ana Salazar | eb7c2571 | 2020-02-03 20:27:26 | [diff] [blame] | 48 | user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF); |
| 49 | } |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TouchDevicesController::TouchDevicesController() { |
| 53 | Shell::Get()->session_controller()->AddObserver(this); |
| 54 | } |
| 55 | |
| 56 | TouchDevicesController::~TouchDevicesController() { |
| 57 | Shell::Get()->session_controller()->RemoveObserver(this); |
| 58 | } |
| 59 | |
| 60 | void TouchDevicesController::ToggleTouchpad() { |
James Cook | bd0b779 | 2017-11-17 03:24:26 | [diff] [blame] | 61 | PrefService* prefs = GetActivePrefService(); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 62 | if (!prefs) |
| 63 | return; |
| 64 | const bool touchpad_enabled = prefs->GetBoolean(prefs::kTouchpadEnabled); |
| 65 | prefs->SetBoolean(prefs::kTouchpadEnabled, !touchpad_enabled); |
| 66 | } |
| 67 | |
Jun Mukai | c82ae4e7 | 2018-04-03 03:08:13 | [diff] [blame] | 68 | bool 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 | |
| 77 | void 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 Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 92 | bool TouchDevicesController::GetTouchscreenEnabled( |
Jun Mukai | c82ae4e7 | 2018-04-03 03:08:13 | [diff] [blame] | 93 | TouchDeviceEnabledSource source) const { |
| 94 | if (source == TouchDeviceEnabledSource::GLOBAL) |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 95 | return global_touchscreen_enabled_; |
| 96 | |
James Cook | bd0b779 | 2017-11-17 03:24:26 | [diff] [blame] | 97 | PrefService* prefs = GetActivePrefService(); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 98 | return prefs && prefs->GetBoolean(prefs::kTouchscreenEnabled); |
| 99 | } |
| 100 | |
| 101 | void TouchDevicesController::SetTouchscreenEnabled( |
| 102 | bool enabled, |
Jun Mukai | c82ae4e7 | 2018-04-03 03:08:13 | [diff] [blame] | 103 | TouchDeviceEnabledSource source) { |
| 104 | if (source == TouchDeviceEnabledSource::GLOBAL) { |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 105 | 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 Cook | bd0b779 | 2017-11-17 03:24:26 | [diff] [blame] | 112 | PrefService* prefs = GetActivePrefService(); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 113 | if (!prefs) |
| 114 | return; |
| 115 | prefs->SetBoolean(prefs::kTouchscreenEnabled, enabled); |
| 116 | } |
| 117 | |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 118 | void TouchDevicesController::OnUserSessionAdded(const AccountId& account_id) { |
Qiang Xu | 6b7b184 | 2018-03-12 19:00:08 | [diff] [blame] | 119 | uma_record_callback_ = base::BindOnce([](PrefService* prefs) { |
| 120 | UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Started", |
| 121 | prefs->GetBoolean(prefs::kTapDraggingEnabled)); |
| 122 | }); |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 123 | } |
| 124 | |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 125 | void TouchDevicesController::OnSigninScreenPrefServiceInitialized( |
| 126 | PrefService* prefs) { |
| 127 | ObservePrefs(prefs); |
| 128 | } |
| 129 | |
| 130 | void TouchDevicesController::OnActiveUserPrefServiceChanged( |
| 131 | PrefService* prefs) { |
Qiang Xu | 6b7b184 | 2018-03-12 19:00:08 | [diff] [blame] | 132 | if (uma_record_callback_) |
| 133 | std::move(uma_record_callback_).Run(prefs); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 134 | ObservePrefs(prefs); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void 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 Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 142 | prefs::kTapDraggingEnabled, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 143 | base::BindRepeating(&TouchDevicesController::UpdateTapDraggingEnabled, |
| 144 | base::Unretained(this))); |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 145 | pref_change_registrar_->Add( |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 146 | prefs::kTouchpadEnabled, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 147 | base::BindRepeating(&TouchDevicesController::UpdateTouchpadEnabled, |
| 148 | base::Unretained(this))); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 149 | pref_change_registrar_->Add( |
| 150 | prefs::kTouchscreenEnabled, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 151 | base::BindRepeating(&TouchDevicesController::UpdateTouchscreenEnabled, |
| 152 | base::Unretained(this))); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 153 | // Load current state. |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 154 | UpdateTapDraggingEnabled(); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 155 | UpdateTouchpadEnabled(); |
| 156 | UpdateTouchscreenEnabled(); |
| 157 | } |
| 158 | |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 159 | void 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 Violet | de79e42 | 2019-06-13 04:01:25 | [diff] [blame] | 170 | ui::OzonePlatform::GetInstance()->GetInputController()->SetTapDragging( |
David Padlipsky | c5afc05 | 2023-03-02 23:13:37 | [diff] [blame] | 171 | absl::nullopt, enabled); |
Qiang Xu | 7e7dd6b | 2018-03-06 05:27:51 | [diff] [blame] | 172 | } |
| 173 | |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 174 | void TouchDevicesController::UpdateTouchpadEnabled() { |
Avery Musbach | 8502842e | 2020-10-05 18:20:21 | [diff] [blame] | 175 | ui::OzonePlatform::GetInstance() |
| 176 | ->GetInputController() |
| 177 | ->SetInternalTouchpadEnabled( |
| 178 | GetTouchpadEnabled(TouchDeviceEnabledSource::GLOBAL) && |
| 179 | GetTouchpadEnabled(TouchDeviceEnabledSource::USER_PREF)); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void TouchDevicesController::UpdateTouchscreenEnabled() { |
Scott Violet | de79e42 | 2019-06-13 04:01:25 | [diff] [blame] | 183 | ui::OzonePlatform::GetInstance() |
| 184 | ->GetInputController() |
| 185 | ->SetTouchscreensEnabled( |
| 186 | GetTouchscreenEnabled(TouchDeviceEnabledSource::GLOBAL) && |
| 187 | GetTouchscreenEnabled(TouchDeviceEnabledSource::USER_PREF)); |
Qiang Xu | f1400a36 | 2017-09-14 02:28:30 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace ash |