time_override.h 3.0 KB

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