xref: /aosp_15_r20/external/cronet/base/timer/hi_res_timer_manager.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TIMER_HI_RES_TIMER_MANAGER_H_
6 #define BASE_TIMER_HI_RES_TIMER_MANAGER_H_
7 
8 #include "base/base_export.h"
9 #include "base/power_monitor/power_observer.h"
10 #include "base/timer/timer.h"
11 #include "build/build_config.h"
12 
13 namespace base {
14 
15 // Ensures that the Windows high resolution timer is only used
16 // when not running on battery power.
17 class BASE_EXPORT HighResolutionTimerManager
18     : public base::PowerSuspendObserver,
19       public base::PowerStateObserver {
20  public:
21   HighResolutionTimerManager();
22 
23   HighResolutionTimerManager(const HighResolutionTimerManager&) = delete;
24   HighResolutionTimerManager& operator=(const HighResolutionTimerManager&) =
25       delete;
26 
27   ~HighResolutionTimerManager() override;
28 
29   // base::PowerStateObserver methods.
30   void OnPowerStateChange(bool on_battery_power) override;
31   // base::PowerSuspendObserver methods.
32   void OnSuspend() override;
33   void OnResume() override;
34 
35   // Returns true if the hi resolution clock could be used right now.
hi_res_clock_available()36   bool hi_res_clock_available() const { return hi_res_clock_available_; }
37 
38  private:
39   // Enable or disable the faster multimedia timer.
40   void UseHiResClock(bool use);
41 
42   bool hi_res_clock_available_;
43 
44 #if BUILDFLAG(IS_WIN)
45   // Timer for polling the high resolution timer usage.
46   base::RepeatingTimer timer_;
47 #endif
48 };
49 
50 }  // namespace base
51 
52 #endif  // BASE_TIMER_HI_RES_TIMER_MANAGER_H_
53