• 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/task/sequence_manager/lazy_now.h"
6 
7 #include "base/time/tick_clock.h"
8 
9 namespace base {
10 namespace sequence_manager {
11 
LazyNow(TimeTicks now)12 LazyNow::LazyNow(TimeTicks now) : tick_clock_(nullptr), now_(now) {}
13 
LazyNow(const TickClock * tick_clock)14 LazyNow::LazyNow(const TickClock* tick_clock)
15     : tick_clock_(tick_clock), now_() {
16   DCHECK(tick_clock);
17 }
18 
LazyNow(LazyNow && move_from)19 LazyNow::LazyNow(LazyNow&& move_from) noexcept
20     : tick_clock_(move_from.tick_clock_), now_(move_from.now_) {
21   move_from.tick_clock_ = nullptr;
22   move_from.now_ = nullopt;
23 }
24 
Now()25 TimeTicks LazyNow::Now() {
26   // It looks tempting to avoid using Optional and to rely on is_null() instead,
27   // but in some test environments clock intentionally starts from zero.
28   if (!now_) {
29     DCHECK(tick_clock_);  // It can fire only on use after std::move.
30     now_ = tick_clock_->NowTicks();
31   }
32   return *now_;
33 }
34 
35 }  // namespace sequence_manager
36 }  // namespace base
37