lock_on_leave_controller.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2021 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_HUMAN_PRESENCE_LOCK_ON_LEAVE_CONTROLLER_H_
  5. #define ASH_SYSTEM_HUMAN_PRESENCE_LOCK_ON_LEAVE_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/system/human_presence/human_presence_orientation_controller.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/scoped_observation.h"
  10. #include "chromeos/ash/components/dbus/hps/hps_service.pb.h"
  11. #include "chromeos/ash/components/dbus/human_presence/human_presence_dbus_client.h"
  12. namespace ash {
  13. // Helper class for HumanPresenceDBusClient, responsible for
  14. // enabling/disabling the DBus service via the client and is responsible for
  15. // maintaining state between restarts.
  16. class ASH_EXPORT LockOnLeaveController
  17. : public HumanPresenceOrientationController::Observer,
  18. HumanPresenceDBusClient::Observer {
  19. public:
  20. // The state of lock on leave inside DBus service that is configured. It is
  21. // set as kUnknown in this class on initialization. And is set to either
  22. // kEnable or kDisable when EnableLockOnLeave() or DisableLockOnLeave() is
  23. // called.
  24. enum class ConfiguredLockOnLeaveState {
  25. kUnknown,
  26. kEnabled,
  27. kDisabled,
  28. };
  29. LockOnLeaveController();
  30. LockOnLeaveController(const LockOnLeaveController&) = delete;
  31. LockOnLeaveController& operator=(const LockOnLeaveController&) = delete;
  32. ~LockOnLeaveController() override;
  33. // Enables the LockOnLeave feature inside HumanPresenceDBusClient; and it only
  34. // sends the method call if LockOnLeave is not enabled yet.
  35. void EnableLockOnLeave();
  36. // Disables the LockOnLeave feature inside HumanPresenceDBusClient if it is
  37. // currently enabled.
  38. void DisableLockOnLeave();
  39. // HumanPresenceOrientationObserver:
  40. void OnOrientationChanged(bool suitable_for_human_presence) override;
  41. // HumanPresenceDBusClient::Observer:
  42. void OnHpsSenseChanged(const hps::HpsResultProto&) override;
  43. void OnHpsNotifyChanged(const hps::HpsResultProto&) override;
  44. // Re-enables LockOnLeave on human presence service restart if it was enabled
  45. // before.
  46. void OnRestart() override;
  47. void OnShutdown() override;
  48. private:
  49. // Called when the human presence service is available.
  50. void OnServiceAvailable(bool service_available);
  51. // May disable/enable lock-on-leave based on current state.
  52. void ReconfigViaDbus();
  53. // Indicates whether the human presence service is available; it is set inside
  54. // OnServiceAvailable and set to false OnShutdown.
  55. bool service_available_ = false;
  56. // Records requested lock-on-leave enable state from client.
  57. bool want_lock_on_leave_ = false;
  58. // Whether the device is in physical orientation where our models are
  59. // accurate.
  60. bool suitable_for_human_presence_ = false;
  61. // Current configured state of LockOnLeave.
  62. ConfiguredLockOnLeaveState configured_state_ =
  63. ConfiguredLockOnLeaveState::kUnknown;
  64. base::ScopedObservation<HumanPresenceDBusClient,
  65. HumanPresenceDBusClient::Observer>
  66. human_presence_observation_{this};
  67. base::ScopedObservation<HumanPresenceOrientationController,
  68. HumanPresenceOrientationController::Observer>
  69. orientation_observation_{this};
  70. base::WeakPtrFactory<LockOnLeaveController> weak_ptr_factory_{this};
  71. };
  72. } // namespace ash
  73. #endif // ASH_SYSTEM_HUMAN_PRESENCE_LOCK_ON_LEAVE_CONTROLLER_H_