bluetooth_power_controller.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef ASH_SYSTEM_BLUETOOTH_BLUETOOTH_POWER_CONTROLLER_H_
  5. #define ASH_SYSTEM_BLUETOOTH_BLUETOOTH_POWER_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/session/session_observer.h"
  8. #include "base/containers/queue.h"
  9. #include "components/prefs/pref_change_registrar.h"
  10. #include "components/user_manager/user_type.h"
  11. #include "device/bluetooth/bluetooth_adapter.h"
  12. #include "device/bluetooth/chromeos/bluetooth_utils.h"
  13. class PrefRegistrySimple;
  14. class PrefService;
  15. namespace ash {
  16. // Listens to changes of bluetooth power preferences and apply them to the
  17. // device. Also initializes the bluetooth power during system startup
  18. // and user session startup.
  19. //
  20. // This should be the only entity controlling bluetooth power. All other code
  21. // outside ash that wants to control bluetooth power should set user pref
  22. // setting instead.
  23. class ASH_EXPORT BluetoothPowerController
  24. : public SessionObserver,
  25. public device::BluetoothAdapter::Observer {
  26. public:
  27. explicit BluetoothPowerController(PrefService* local_state);
  28. BluetoothPowerController(const BluetoothPowerController&) = delete;
  29. BluetoothPowerController& operator=(const BluetoothPowerController&) = delete;
  30. ~BluetoothPowerController() override;
  31. // Changes the bluetooth power setting to |enabled|.
  32. void SetBluetoothEnabled(bool enabled);
  33. static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
  34. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  35. // Sets the primary user's bluetooth power pref. Setting the pref will also
  36. // trigger the change of the bluetooth power. This method can only be called
  37. // when the primary user is the active user, otherwise the operation is
  38. // ignored.
  39. void SetPrimaryUserBluetoothPowerSetting(bool enabled);
  40. // Called when BluetoothAdapterFactory::GetAdapter is ready to pass the
  41. // adapter pointer.
  42. void InitializeOnAdapterReady(
  43. scoped_refptr<device::BluetoothAdapter> adapter);
  44. // SessionObserver:
  45. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  46. // BluetoothAdapter::Observer:
  47. void AdapterPresentChanged(device::BluetoothAdapter* adapter,
  48. bool present) override;
  49. device::BluetoothAdapter* bluetooth_adapter_for_test() {
  50. return bluetooth_adapter_.get();
  51. }
  52. private:
  53. friend class BluetoothPowerControllerTest;
  54. void StartWatchingActiveUserPrefsChanges();
  55. void StartWatchingLocalStatePrefsChanges();
  56. void StopWatchingActiveUserPrefsChanges();
  57. void OnBluetoothPowerActiveUserPrefChanged();
  58. void OnBluetoothPowerLocalStatePrefChanged();
  59. // At primary user session startup, apply the user's bluetooth power setting
  60. // or set the default if the user doesn't have the setting yet.
  61. void ApplyBluetoothPrimaryUserPref();
  62. // At login screen startup, apply the local state bluetooth power setting
  63. // or set the default if the device doesn't have the setting yet.
  64. void ApplyBluetoothLocalStatePref();
  65. // Sets the bluetooth power, may defer the operation if bluetooth adapter
  66. // is not yet ready.
  67. void SetBluetoothPower(bool enabled);
  68. // Sets the bluetooth power given the ready adapter.
  69. void SetBluetoothPowerOnAdapterReady();
  70. // Called by dbus:: on completion of the D-Bus method call to set power state
  71. // on the device.
  72. void OnSetBluetoothPower(device::PoweredStateOperation power_operation,
  73. bool success);
  74. using BluetoothTask = base::OnceClosure;
  75. // If adapter is ready run the task right now, otherwise add the task
  76. // to the queue which will be executed when bluetooth adapter is ready.
  77. void RunBluetoothTaskWhenAdapterReady(BluetoothTask task);
  78. // Triggers running all pending bluetooth adapter-dependent tasks.
  79. void TriggerRunPendingBluetoothTasks();
  80. // Runs the next pending bluetooth task. This will trigger another
  81. // RunNextPendingBluetoothTask when the current task is finished. It will stop
  82. // executing the next task when the adapter becomes not present or the queue
  83. // has been empty.
  84. void RunNextPendingBluetoothTask();
  85. // Sets the pref value based on current bluetooth power state. May defer
  86. // the operation if bluetooth adapter is not ready yet.
  87. void SavePrefValue(PrefService* prefs, const char* pref_name);
  88. // Sets the pref value based on current bluetooth power state, given the ready
  89. // bluetooth adapter.
  90. void SavePrefValueOnAdapterReady(PrefService* prefs, const char* pref_name);
  91. // Decides whether to apply bluetooth setting based on user type.
  92. // Returns true if the user type represents a human individual, currently this
  93. // includes: regular, child, supervised, or active directory. The other types
  94. // do not represent human account so those account should follow system-wide
  95. // bluetooth setting instead.
  96. bool ShouldApplyUserBluetoothSetting(user_manager::UserType user_type) const;
  97. // Remembers whether we have ever applied the primary user's bluetooth
  98. // setting. If this variable is true, we will ignore any active user change
  99. // event since we know that the primary user's bluetooth setting has been
  100. // applied.
  101. bool is_primary_user_bluetooth_applied_ = false;
  102. PrefService* active_user_pref_service_ = nullptr;
  103. PrefService* local_state_ = nullptr;
  104. // Contains pending tasks which depend on the availability of bluetooth
  105. // adapter.
  106. base::queue<BluetoothTask> pending_bluetooth_tasks_;
  107. // The registrar used to watch prefs changes in the above
  108. // |active_user_pref_service_| from outside ash.
  109. // NOTE: Prefs are how Chrome communicates changes to the bluetooth power
  110. // settings controlled by this class from the WebUI settings.
  111. std::unique_ptr<PrefChangeRegistrar> active_user_pref_change_registrar_;
  112. std::unique_ptr<PrefChangeRegistrar> local_state_pref_change_registrar_;
  113. // True indicates that pending_bluetooth_tasks_ is being executed and
  114. // waiting for complete callback.
  115. bool pending_tasks_busy_ = false;
  116. // If not empty this indicates the pending target bluetooth power to be set.
  117. // This needs to be tracked so that we can combine multiple pending power
  118. // change requests.
  119. absl::optional<bool> pending_bluetooth_power_target_;
  120. scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
  121. base::WeakPtrFactory<BluetoothPowerController> weak_ptr_factory_{this};
  122. };
  123. } // namespace ash
  124. #endif // ASH_SYSTEM_BLUETOOTH_BLUETOOTH_POWER_CONTROLLER_H_