backlights_forced_off_setter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_POWER_BACKLIGHTS_FORCED_OFF_SETTER_H_
  5. #define ASH_SYSTEM_POWER_BACKLIGHTS_FORCED_OFF_SETTER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/screen_backlight.h"
  9. #include "ash/public/cpp/screen_backlight_observer.h"
  10. #include "ash/public/cpp/screen_backlight_type.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/scoped_observation.h"
  14. #include "chromeos/dbus/power/power_manager_client.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace ash {
  17. class ScopedBacklightsForcedOff;
  18. // BacklightsForcedOffSetter manages multiple requests to force the backlights
  19. // off and coalesces them into SetBacklightsForcedOff D-Bus calls to powerd.
  20. class ASH_EXPORT BacklightsForcedOffSetter
  21. : public chromeos::PowerManagerClient::Observer,
  22. public ScreenBacklight {
  23. public:
  24. BacklightsForcedOffSetter();
  25. BacklightsForcedOffSetter(const BacklightsForcedOffSetter&) = delete;
  26. BacklightsForcedOffSetter& operator=(const BacklightsForcedOffSetter&) =
  27. delete;
  28. ~BacklightsForcedOffSetter() override;
  29. bool backlights_forced_off() const {
  30. return backlights_forced_off_.value_or(false);
  31. }
  32. // ScreenBacklight:
  33. void AddObserver(ScreenBacklightObserver* observer) override;
  34. void RemoveObserver(ScreenBacklightObserver* observer) override;
  35. ScreenBacklightState GetScreenBacklightState() const override;
  36. // Forces the backlights off. The backlights will be kept in the forced-off
  37. // state until all requests have been destroyed.
  38. std::unique_ptr<ScopedBacklightsForcedOff> ForceBacklightsOff();
  39. // Overridden from chromeos::PowerManagerClient::Observer:
  40. void ScreenBrightnessChanged(
  41. const power_manager::BacklightBrightnessChange& change) override;
  42. void PowerManagerRestarted() override;
  43. // Resets internal state for tests.
  44. // Note: This will silently cancel all active backlights forced off requests.
  45. void ResetForTest();
  46. private:
  47. // Sets |disable_touchscreen_while_screen_off_| depending on the state of the
  48. // current command line,
  49. void InitDisableTouchscreenWhileScreenOff();
  50. // Sends a request to powerd to get the backlights forced off state so that
  51. // |backlights_forced_off_| can be initialized.
  52. void GetInitialBacklightsForcedOff();
  53. // Callback for |GetInitialBacklightsForcedOff()|.
  54. void OnGotInitialBacklightsForcedOff(absl::optional<bool> is_forced_off);
  55. // Removes a force backlights off request from the list of active ones, which
  56. // effectively cancels the request. This is passed to every
  57. // ScopedBacklightsForcedOff created by |ForceBacklightsOff| as its
  58. // cancellation callback.
  59. void OnScopedBacklightsForcedOffDestroyed();
  60. // Updates the power manager's backlights-forced-off state.
  61. void SetBacklightsForcedOff(bool forced_off);
  62. // Enables or disables the touchscreen by updating the global touchscreen
  63. // enabled status. The touchscreen is disabled when backlights are forced off
  64. // or |screen_backlight_state_| is OFF_AUTO.
  65. void UpdateTouchscreenStatus();
  66. // Controls whether the touchscreen is disabled when the screen is turned off
  67. // due to user inactivity.
  68. bool disable_touchscreen_while_screen_off_ = true;
  69. // Current forced-off state of backlights.
  70. absl::optional<bool> backlights_forced_off_;
  71. // Current screen state.
  72. ScreenBacklightState screen_backlight_state_ = ScreenBacklightState::ON;
  73. // Number of active backlights forced off requests.
  74. int active_backlights_forced_off_count_ = 0;
  75. base::ObserverList<ScreenBacklightObserver>::Unchecked observers_;
  76. base::ScopedObservation<chromeos::PowerManagerClient,
  77. chromeos::PowerManagerClient::Observer>
  78. power_manager_observation_{this};
  79. base::WeakPtrFactory<BacklightsForcedOffSetter> weak_ptr_factory_{this};
  80. };
  81. } // namespace ash
  82. #endif // ASH_SYSTEM_POWER_BACKLIGHTS_FORCED_OFF_SETTER_H_