• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_TIME_TIME_OVERRIDE_H_
6 #define BASE_TIME_TIME_OVERRIDE_H_
7 
8 #include "base/base_export.h"
9 #include "base/time/time.h"
10 
11 namespace base {
12 
13 using TimeNowFunction = decltype(&Time::Now);
14 using TimeTicksNowFunction = decltype(&TimeTicks::Now);
15 using ThreadTicksNowFunction = decltype(&ThreadTicks::Now);
16 
17 // Time overrides should be used with extreme caution. Discuss with //base/time
18 // OWNERS before adding a new one.
19 namespace subtle {
20 
21 // Override the return value of Time::Now and Time::NowFromSystemTime /
22 // TimeTicks::Now / ThreadTicks::Now to emulate time, e.g. for tests or to
23 // modify progression of time. Note that the override should be set while
24 // single-threaded and before the first call to Now() to avoid threading issues
25 // and inconsistencies in returned values. Nested overrides are not allowed.
26 class BASE_EXPORT ScopedTimeClockOverrides {
27  public:
28   // Pass |nullptr| for any override if it shouldn't be overriden.
29   ScopedTimeClockOverrides(TimeNowFunction time_override,
30                            TimeTicksNowFunction time_ticks_override,
31                            ThreadTicksNowFunction thread_ticks_override);
32 
33   // Restores the platform default Now() functions.
34   ~ScopedTimeClockOverrides();
35 
36  private:
37 #if DCHECK_IS_ON()
38   static bool overrides_active_;
39 #endif
40 
41   DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedTimeClockOverrides);
42 };
43 
44 // These methods return the platform default Time::Now / TimeTicks::Now /
45 // ThreadTicks::Now values even while an override is in place. These methods
46 // should only be used in places where emulated time should be disregarded. For
47 // example, they can be used to implement test timeouts for tests that may
48 // override time.
49 BASE_EXPORT Time TimeNowIgnoringOverride();
50 BASE_EXPORT Time TimeNowFromSystemTimeIgnoringOverride();
51 BASE_EXPORT TimeTicks TimeTicksNowIgnoringOverride();
52 BASE_EXPORT ThreadTicks ThreadTicksNowIgnoringOverride();
53 
54 }  // namespace subtle
55 
56 namespace internal {
57 
58 // These function pointers are used by platform-independent implementations of
59 // the Now() methods and ScopedTimeClockOverrides. They are set to point to the
60 // respective NowIgnoringOverride functions by default, but can also be set by
61 // platform-specific code to select a default implementation at runtime, thereby
62 // avoiding the indirection via the NowIgnoringOverride functions. Note that the
63 // pointers can be overridden and later reset to the NowIgnoringOverride
64 // functions by ScopedTimeClockOverrides.
65 extern TimeNowFunction g_time_now_function;
66 extern TimeNowFunction g_time_now_from_system_time_function;
67 extern TimeTicksNowFunction g_time_ticks_now_function;
68 extern ThreadTicksNowFunction g_thread_ticks_now_function;
69 
70 }  // namespace internal
71 
72 }  // namespace base
73 
74 #endif  // BASE_TIME_TIME_OVERRIDE_H_
75