xref: /aosp_15_r20/external/cronet/base/task/sequence_manager/time_domain.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 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_TASK_SEQUENCE_MANAGER_TIME_DOMAIN_H_
6 #define BASE_TASK_SEQUENCE_MANAGER_TIME_DOMAIN_H_
7 
8 #include <optional>
9 
10 #include "base/base_export.h"
11 #include "base/check.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/task/common/lazy_now.h"
14 #include "base/task/sequence_manager/tasks.h"
15 #include "base/time/tick_clock.h"
16 #include "base/values.h"
17 
18 namespace base {
19 namespace sequence_manager {
20 
21 class SequenceManager;
22 
23 namespace internal {
24 class SequenceManagerImpl;
25 }  // namespace internal
26 
27 // TimeDomain allows subclasses to enable clock overriding
28 // (e.g. auto-advancing virtual time, throttled clock, etc).
29 class BASE_EXPORT TimeDomain : public TickClock {
30  public:
31   TimeDomain(const TimeDomain&) = delete;
32   TimeDomain& operator=(const TimeDomain&) = delete;
33   ~TimeDomain() override = default;
34 
35   // Invoked when the thread reaches idle. Gives an opportunity to a virtual
36   // time domain impl to fast-forward time and return true to indicate that
37   // there's more work to run. If RunLoop::QuitWhenIdle has been called then
38   // `quit_when_idle_requested` will be true.
39   virtual bool MaybeFastForwardToWakeUp(std::optional<WakeUp> next_wake_up,
40                                         bool quit_when_idle_requested) = 0;
41 
42   // Debug info.
43   Value::Dict AsValue() const;
44 
45  protected:
46   TimeDomain() = default;
47 
48   virtual const char* GetName() const = 0;
49 
50   // Tells SequenceManager that internal policy might have changed to
51   // re-evaluate MaybeFastForwardToWakeUp().
52   void NotifyPolicyChanged();
53 
54   // Called when the TimeDomain is assigned to a SequenceManagerImpl.
55   // `sequence_manager` is expected to be valid for the duration of TimeDomain's
56   // existence. TODO(scheduler-dev): Pass SequenceManager in the constructor.
57   void OnAssignedToSequenceManager(
58       internal::SequenceManagerImpl* sequence_manager);
59 
60  private:
61   friend class internal::SequenceManagerImpl;
62 
63   raw_ptr<internal::SequenceManagerImpl, DanglingUntriaged> sequence_manager_ =
64       nullptr;
65 };
66 
67 }  // namespace sequence_manager
68 }  // namespace base
69 
70 #endif  // BASE_TASK_SEQUENCE_MANAGER_TIME_DOMAIN_H_
71