native_timer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 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 CHROMEOS_DBUS_POWER_NATIVE_TIMER_H_
  5. #define CHROMEOS_DBUS_POWER_NATIVE_TIMER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/files/file_descriptor_watcher_posix.h"
  10. #include "base/files/scoped_file.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/threading/sequenced_task_runner_handle.h"
  15. #include "base/threading/thread.h"
  16. #include "base/time/time.h"
  17. #include "base/timer/timer.h"
  18. #include "chromeos/dbus/power/power_manager_client.h"
  19. namespace chromeos {
  20. using OnStartNativeTimerCallback = base::OnceCallback<void(bool)>;
  21. // Sets timers that can also wake up the device from suspend by making D-Bus
  22. // calls to the power daemon.
  23. class COMPONENT_EXPORT(DBUS_POWER) NativeTimer {
  24. public:
  25. explicit NativeTimer(const std::string& tag);
  26. NativeTimer(const NativeTimer&) = delete;
  27. NativeTimer& operator=(const NativeTimer&) = delete;
  28. ~NativeTimer();
  29. // Starts a timer to expire at |absolute_expiration_time|. Runs
  30. // |timer_expiration_callback| on timer expiration. Runs |result_callback|
  31. // with the result of the start operation. If starting the timer failed then
  32. // |timer_expiration_callback| will not be called.
  33. //
  34. // Consecutive |Start| calls override the previous |Start| call.
  35. void Start(base::TimeTicks absolute_expiration_time,
  36. base::OnceClosure timer_expiration_callback,
  37. OnStartNativeTimerCallback result_callback);
  38. private:
  39. struct StartTimerParams;
  40. // D-Bus callback for a create timer D-Bus call.
  41. void OnCreateTimer(base::ScopedFD expiration_fd,
  42. absl::optional<std::vector<int32_t>> timer_ids);
  43. // D-Bus callback for a start timer D-Bus call.
  44. void OnStartTimer(base::OnceClosure timer_expiration_callback,
  45. OnStartNativeTimerCallback result_callback,
  46. bool result);
  47. // Callback for timer expiration.
  48. void OnExpiration();
  49. // Resets the |expiration_fd_watcher_| and cancels any inflight callbacks.
  50. void ResetState();
  51. // Calls the result callback for a pending |Start| operation with false iff
  52. // |result| is false. Else, schedules a timer using the D-Bus API and calls
  53. // the result callback for a pending |Start| operation with true. Resets
  54. // |in_flight_start_timer_params_| in all cases.
  55. void ProcessAndResetInFlightStartParams(bool result);
  56. // Stores the parameters for |Start| when timer is not yet created i.e.
  57. // |timer_id_| is uninitialized. Since |Start| calls override each other at
  58. // any point only the latest |Start| call's parameters are stored in this.
  59. std::unique_ptr<StartTimerParams> in_flight_start_timer_params_;
  60. // Timer id returned by the power daemon, to be used as a handle for the timer
  61. // APIs.
  62. PowerManagerClient::TimerId timer_id_;
  63. // Tag associated with the D-Bus API. Cached for deleting the timer in the
  64. // destructor.
  65. std::string tag_;
  66. // File descriptor that will be written to when a Chrome OS alarm fires.
  67. base::ScopedFD expiration_fd_;
  68. // Callback to run when the timer expires.
  69. base::OnceClosure timer_expiration_callback_;
  70. // Watches |expiration_fd_| for an event.
  71. std::unique_ptr<base::FileDescriptorWatcher::Controller>
  72. expiration_fd_watcher_;
  73. SEQUENCE_CHECKER(sequence_checker_);
  74. base::WeakPtrFactory<NativeTimer> weak_factory_{this};
  75. };
  76. } // namespace chromeos
  77. #endif // CHROMEOS_DBUS_POWER_NATIVE_TIMER_H_