Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2207)

Unified Diff: base/timer/timer.h

Issue 2484023002: Use a TickClock instead of TimeTicks::Now() in Timer. (Closed)
Patch Set: Added unit tests and removed Chrome OS changes. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/timer/timer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer.h
diff --git a/base/timer/timer.h b/base/timer/timer.h
index 661829b513d8281f99bb63741b91f9c026e6ff39..50aedbd4cec28e97c408b2acd8b896c70526732c 100644
--- a/base/timer/timer.h
+++ b/base/timer/timer.h
@@ -49,6 +49,8 @@
// because they're flaky on the buildbot, but when you run them locally you
// should be able to tell the difference.
+#include <memory>
+
#include "base/base_export.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
@@ -61,6 +63,7 @@ namespace base {
class BaseTimerTaskInternal;
class SingleThreadTaskRunner;
+class TickClock;
//-----------------------------------------------------------------------------
// This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating
@@ -71,14 +74,23 @@ class BASE_EXPORT Timer {
public:
// Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must
// be called later to set task info. |retain_user_task| determines whether the
- // user_task is retained or reset when it runs or stops.
+ // user_task is retained or reset when it runs or stops. If |tick_clock| is
+ // provided, it is used instead of TimeTicks::Now() to get TimeTicks when
+ // scheduling tasks.
Timer(bool retain_user_task, bool is_repeating);
+ Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock);
- // Construct a timer with retained task info.
+ // Construct a timer with retained task info. If |tick_clock| is provided, it
+ // is used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks.
Timer(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating);
+ Timer(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const base::Closure& user_task,
+ bool is_repeating,
+ TickClock* tick_clock);
virtual ~Timer();
@@ -111,6 +123,9 @@ class BASE_EXPORT Timer {
const TimeTicks& desired_run_time() const { return desired_run_time_; }
protected:
+ // Returns the current tick count.
+ TimeTicks Now() const;
+
// Used to initiate a new delayed task. This has the side-effect of disabling
// scheduled_task_ if it is non-null.
void SetTaskInfo(const tracked_objects::Location& posted_from,
@@ -191,6 +206,9 @@ class BASE_EXPORT Timer {
// If true, hold on to the user_task_ closure object for reuse.
const bool retain_user_task_;
+ // The tick clock used to calculate the run time for scheduled tasks.
+ TickClock* const tick_clock_;
+
// If true, user_task_ is scheduled to run sometime in the future.
bool is_running_;
@@ -210,8 +228,8 @@ class BaseTimerMethodPointer : public Timer {
using Timer::Start;
enum RepeatMode { ONE_SHOT, REPEATING };
- BaseTimerMethodPointer(RepeatMode mode)
- : Timer(mode == REPEATING, mode == REPEATING) {}
+ BaseTimerMethodPointer(RepeatMode mode, TickClock* tick_clock)
+ : Timer(mode == REPEATING, mode == REPEATING, tick_clock) {}
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call a task formed from
@@ -230,14 +248,18 @@ class BaseTimerMethodPointer : public Timer {
// A simple, one-shot timer. See usage notes at the top of the file.
class OneShotTimer : public BaseTimerMethodPointer {
public:
- OneShotTimer() : BaseTimerMethodPointer(ONE_SHOT) {}
+ OneShotTimer() : OneShotTimer(nullptr) {}
+ explicit OneShotTimer(TickClock* tick_clock)
+ : BaseTimerMethodPointer(ONE_SHOT, tick_clock) {}
};
//-----------------------------------------------------------------------------
// A simple, repeating timer. See usage notes at the top of the file.
class RepeatingTimer : public BaseTimerMethodPointer {
public:
- RepeatingTimer() : BaseTimerMethodPointer(REPEATING) {}
+ RepeatingTimer() : RepeatingTimer(nullptr) {}
+ explicit RepeatingTimer(TickClock* tick_clock)
+ : BaseTimerMethodPointer(REPEATING, tick_clock) {}
};
//-----------------------------------------------------------------------------
@@ -258,10 +280,19 @@ class DelayTimer : protected Timer {
TimeDelta delay,
Receiver* receiver,
void (Receiver::*method)())
+ : DelayTimer(posted_from, delay, receiver, method, nullptr) {}
+
+ template <class Receiver>
+ DelayTimer(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ Receiver* receiver,
+ void (Receiver::*method)(),
+ TickClock* tick_clock)
: Timer(posted_from,
delay,
base::Bind(method, base::Unretained(receiver)),
- false) {}
+ false,
+ tick_clock) {}
void Reset() override;
};
« no previous file with comments | « no previous file | base/timer/timer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698