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