• 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 #include "base/time/time_override.h"
6 
7 namespace base {
8 namespace subtle {
9 
10 #if DCHECK_IS_ON()
11 // static
12 bool ScopedTimeClockOverrides::overrides_active_ = false;
13 #endif
14 
ScopedTimeClockOverrides(TimeNowFunction time_override,TimeTicksNowFunction time_ticks_override,ThreadTicksNowFunction thread_ticks_override)15 ScopedTimeClockOverrides::ScopedTimeClockOverrides(
16     TimeNowFunction time_override,
17     TimeTicksNowFunction time_ticks_override,
18     ThreadTicksNowFunction thread_ticks_override) {
19 #if DCHECK_IS_ON()
20   DCHECK(!overrides_active_);
21   overrides_active_ = true;
22 #endif
23   if (time_override) {
24     internal::g_time_now_function = time_override;
25     internal::g_time_now_from_system_time_function = time_override;
26   }
27   if (time_ticks_override)
28     internal::g_time_ticks_now_function = time_ticks_override;
29   if (thread_ticks_override)
30     internal::g_thread_ticks_now_function = thread_ticks_override;
31 }
32 
~ScopedTimeClockOverrides()33 ScopedTimeClockOverrides::~ScopedTimeClockOverrides() {
34   internal::g_time_now_function = &TimeNowIgnoringOverride;
35   internal::g_time_now_from_system_time_function =
36       &TimeNowFromSystemTimeIgnoringOverride;
37   internal::g_time_ticks_now_function = &TimeTicksNowIgnoringOverride;
38   internal::g_thread_ticks_now_function = &ThreadTicksNowIgnoringOverride;
39 #if DCHECK_IS_ON()
40   overrides_active_ = false;
41 #endif
42 }
43 
44 }  // namespace subtle
45 }  // namespace base
46