touch_devices_controller.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2017 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.
  4. #include "ash/touch/touch_devices_controller.h"
  5. #include <utility>
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/constants/ash_switches.h"
  8. #include "ash/root_window_controller.h"
  9. #include "ash/session/session_controller_impl.h"
  10. #include "ash/shell.h"
  11. #include "ash/shell_delegate.h"
  12. #include "base/bind.h"
  13. #include "base/command_line.h"
  14. #include "base/metrics/histogram_macros.h"
  15. #include "components/pref_registry/pref_registry_syncable.h"
  16. #include "components/prefs/pref_change_registrar.h"
  17. #include "components/prefs/pref_registry_simple.h"
  18. #include "components/prefs/pref_service.h"
  19. #include "ui/ozone/public/input_controller.h"
  20. #include "ui/ozone/public/ozone_platform.h"
  21. namespace ash {
  22. namespace {
  23. PrefService* GetActivePrefService() {
  24. return Shell::Get()->session_controller()->GetActivePrefService();
  25. }
  26. } // namespace
  27. // static
  28. void TouchDevicesController::RegisterProfilePrefs(PrefRegistrySimple* registry,
  29. bool for_test) {
  30. registry->RegisterBooleanPref(
  31. prefs::kTapDraggingEnabled, false,
  32. user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
  33. registry->RegisterBooleanPref(prefs::kTouchpadEnabled, true);
  34. registry->RegisterBooleanPref(prefs::kTouchscreenEnabled, true);
  35. if (for_test) {
  36. registry->RegisterBooleanPref(
  37. prefs::kNaturalScroll,
  38. base::CommandLine::ForCurrentProcess()->HasSwitch(
  39. switches::kNaturalScrollDefault),
  40. user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
  41. }
  42. }
  43. TouchDevicesController::TouchDevicesController() {
  44. Shell::Get()->session_controller()->AddObserver(this);
  45. }
  46. TouchDevicesController::~TouchDevicesController() {
  47. Shell::Get()->session_controller()->RemoveObserver(this);
  48. }
  49. void TouchDevicesController::ToggleTouchpad() {
  50. PrefService* prefs = GetActivePrefService();
  51. if (!prefs)
  52. return;
  53. const bool touchpad_enabled = prefs->GetBoolean(prefs::kTouchpadEnabled);
  54. prefs->SetBoolean(prefs::kTouchpadEnabled, !touchpad_enabled);
  55. }
  56. bool TouchDevicesController::GetTouchpadEnabled(
  57. TouchDeviceEnabledSource source) const {
  58. if (source == TouchDeviceEnabledSource::GLOBAL)
  59. return global_touchpad_enabled_;
  60. PrefService* prefs = GetActivePrefService();
  61. return prefs && prefs->GetBoolean(prefs::kTouchpadEnabled);
  62. }
  63. void TouchDevicesController::SetTouchpadEnabled(
  64. bool enabled,
  65. TouchDeviceEnabledSource source) {
  66. if (source == TouchDeviceEnabledSource::GLOBAL) {
  67. global_touchpad_enabled_ = enabled;
  68. UpdateTouchpadEnabled();
  69. return;
  70. }
  71. PrefService* prefs = GetActivePrefService();
  72. if (!prefs)
  73. return;
  74. prefs->SetBoolean(prefs::kTouchpadEnabled, enabled);
  75. }
  76. bool TouchDevicesController::GetTouchscreenEnabled(
  77. TouchDeviceEnabledSource source) const {
  78. if (source == TouchDeviceEnabledSource::GLOBAL)
  79. return global_touchscreen_enabled_;
  80. PrefService* prefs = GetActivePrefService();
  81. return prefs && prefs->GetBoolean(prefs::kTouchscreenEnabled);
  82. }
  83. void TouchDevicesController::SetTouchscreenEnabled(
  84. bool enabled,
  85. TouchDeviceEnabledSource source) {
  86. if (source == TouchDeviceEnabledSource::GLOBAL) {
  87. global_touchscreen_enabled_ = enabled;
  88. // Explicitly call |UpdateTouchscreenEnabled()| to update the actual
  89. // touchscreen state from multiple sources.
  90. UpdateTouchscreenEnabled();
  91. return;
  92. }
  93. PrefService* prefs = GetActivePrefService();
  94. if (!prefs)
  95. return;
  96. prefs->SetBoolean(prefs::kTouchscreenEnabled, enabled);
  97. }
  98. void TouchDevicesController::OnUserSessionAdded(const AccountId& account_id) {
  99. uma_record_callback_ = base::BindOnce([](PrefService* prefs) {
  100. UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Started",
  101. prefs->GetBoolean(prefs::kTapDraggingEnabled));
  102. });
  103. }
  104. void TouchDevicesController::OnSigninScreenPrefServiceInitialized(
  105. PrefService* prefs) {
  106. ObservePrefs(prefs);
  107. }
  108. void TouchDevicesController::OnActiveUserPrefServiceChanged(
  109. PrefService* prefs) {
  110. if (uma_record_callback_)
  111. std::move(uma_record_callback_).Run(prefs);
  112. ObservePrefs(prefs);
  113. }
  114. void TouchDevicesController::ObservePrefs(PrefService* prefs) {
  115. // Watch for pref updates.
  116. pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
  117. pref_change_registrar_->Init(prefs);
  118. pref_change_registrar_->Add(
  119. prefs::kTapDraggingEnabled,
  120. base::BindRepeating(&TouchDevicesController::UpdateTapDraggingEnabled,
  121. base::Unretained(this)));
  122. pref_change_registrar_->Add(
  123. prefs::kTouchpadEnabled,
  124. base::BindRepeating(&TouchDevicesController::UpdateTouchpadEnabled,
  125. base::Unretained(this)));
  126. pref_change_registrar_->Add(
  127. prefs::kTouchscreenEnabled,
  128. base::BindRepeating(&TouchDevicesController::UpdateTouchscreenEnabled,
  129. base::Unretained(this)));
  130. // Load current state.
  131. UpdateTapDraggingEnabled();
  132. UpdateTouchpadEnabled();
  133. UpdateTouchscreenEnabled();
  134. }
  135. void TouchDevicesController::UpdateTapDraggingEnabled() {
  136. PrefService* prefs = GetActivePrefService();
  137. const bool enabled = prefs->GetBoolean(prefs::kTapDraggingEnabled);
  138. if (tap_dragging_enabled_ == enabled)
  139. return;
  140. tap_dragging_enabled_ = enabled;
  141. UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Changed", enabled);
  142. ui::OzonePlatform::GetInstance()->GetInputController()->SetTapDragging(
  143. enabled);
  144. }
  145. void TouchDevicesController::UpdateTouchpadEnabled() {
  146. ui::OzonePlatform::GetInstance()
  147. ->GetInputController()
  148. ->SetInternalTouchpadEnabled(
  149. GetTouchpadEnabled(TouchDeviceEnabledSource::GLOBAL) &&
  150. GetTouchpadEnabled(TouchDeviceEnabledSource::USER_PREF));
  151. }
  152. void TouchDevicesController::UpdateTouchscreenEnabled() {
  153. ui::OzonePlatform::GetInstance()
  154. ->GetInputController()
  155. ->SetTouchscreensEnabled(
  156. GetTouchscreenEnabled(TouchDeviceEnabledSource::GLOBAL) &&
  157. GetTouchscreenEnabled(TouchDeviceEnabledSource::USER_PREF));
  158. }
  159. } // namespace ash