Commit d93bb086 authored by tzik's avatar tzik Committed by Commit Bot

Rename base::Timer to base::TimerBase

This CL updates all remaining user of base::Timer to use its subclasses,
and renames base::Timer to base::TimerBase to ensure there's no user
anymore.

Bug: 850247
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:linux_vr;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I73d70cbbe338e17f5d7de34acd1c961ce05c4305
Reviewed-on: https://chromium-review.googlesource.com/1124200
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576447}
parent 08752cc7
......@@ -308,7 +308,7 @@ void ImportantFileWriter::ClearPendingWrite() {
serializer_ = nullptr;
}
void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) {
void ImportantFileWriter::SetTimerForTesting(OneShotTimer* timer_override) {
timer_override_ = timer_override;
}
......
......@@ -114,14 +114,13 @@ class BASE_EXPORT ImportantFileWriter {
}
// Overrides the timer to use for scheduling writes with |timer_override|.
void SetTimerForTesting(Timer* timer_override);
void SetTimerForTesting(OneShotTimer* timer_override);
private:
const Timer& timer() const {
return timer_override_ ? const_cast<const Timer&>(*timer_override_)
: timer_;
const OneShotTimer& timer() const {
return timer_override_ ? *timer_override_ : timer_;
}
Timer& timer() { return timer_override_ ? *timer_override_ : timer_; }
OneShotTimer& timer() { return timer_override_ ? *timer_override_ : timer_; }
void ClearPendingWrite();
......@@ -139,7 +138,7 @@ class BASE_EXPORT ImportantFileWriter {
OneShotTimer timer_;
// An override for |timer_| used for testing.
Timer* timer_override_ = nullptr;
OneShotTimer* timer_override_ = nullptr;
// Serializer which will provide the data to be saved.
DataSerializer* serializer_;
......
......@@ -12,7 +12,7 @@ namespace base {
class TestSimpleTaskRunner;
// A mock implementation of base::Timer which requires being explicitly
// A mock implementation of base::OneShotTimer which requires being explicitly
// Fire()'d.
// Prefer using ScopedTaskEnvironment::MOCK_TIME + FastForward*() to this when
// possible.
......
......@@ -16,6 +16,7 @@
#include "base/time/tick_clock.h"
namespace base {
namespace internal {
// BaseTimerTaskInternal is a simple delegate for scheduling a callback to Timer
// on the current sequence. It also handles the following edge cases:
......@@ -23,9 +24,7 @@ namespace base {
// - abandoned (orphaned) by Timer.
class BaseTimerTaskInternal {
public:
explicit BaseTimerTaskInternal(Timer* timer)
: timer_(timer) {
}
explicit BaseTimerTaskInternal(TimerBase* timer) : timer_(timer) {}
~BaseTimerTaskInternal() {
// This task may be getting cleared because the task runner has been
......@@ -45,7 +44,7 @@ class BaseTimerTaskInternal {
// Although Timer should not call back into |this|, let's clear |timer_|
// first to be pedantic.
Timer* timer = timer_;
TimerBase* timer = timer_;
timer_ = nullptr;
timer->RunScheduledTask();
}
......@@ -54,17 +53,17 @@ class BaseTimerTaskInternal {
void Abandon() { timer_ = nullptr; }
private:
Timer* timer_;
TimerBase* timer_;
DISALLOW_COPY_AND_ASSIGN(BaseTimerTaskInternal);
};
Timer::Timer(bool retain_user_task, bool is_repeating)
: Timer(retain_user_task, is_repeating, nullptr) {}
TimerBase::TimerBase(bool retain_user_task, bool is_repeating)
: TimerBase(retain_user_task, is_repeating, nullptr) {}
Timer::Timer(bool retain_user_task,
bool is_repeating,
const TickClock* tick_clock)
TimerBase::TimerBase(bool retain_user_task,
bool is_repeating,
const TickClock* tick_clock)
: scheduled_task_(nullptr),
is_repeating_(is_repeating),
retain_user_task_(retain_user_task),
......@@ -77,17 +76,17 @@ Timer::Timer(bool retain_user_task,
origin_sequence_checker_.DetachFromSequence();
}
Timer::Timer(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating)
: Timer(posted_from, delay, user_task, is_repeating, nullptr) {}
Timer::Timer(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating,
const TickClock* tick_clock)
TimerBase::TimerBase(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating)
: TimerBase(posted_from, delay, user_task, is_repeating, nullptr) {}
TimerBase::TimerBase(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating,
const TickClock* tick_clock)
: scheduled_task_(nullptr),
posted_from_(posted_from),
delay_(delay),
......@@ -100,22 +99,22 @@ Timer::Timer(const Location& posted_from,
origin_sequence_checker_.DetachFromSequence();
}
Timer::~Timer() {
TimerBase::~TimerBase() {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
AbandonAndStop();
}
bool Timer::IsRunning() const {
bool TimerBase::IsRunning() const {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
return is_running_;
}
TimeDelta Timer::GetCurrentDelay() const {
TimeDelta TimerBase::GetCurrentDelay() const {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
return delay_;
}
void Timer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
void TimerBase::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
// Do not allow changing the task runner when the Timer is running.
// Don't check for |origin_sequence_checker_.CalledOnValidSequence()| here to
// allow the use case of constructing the Timer and immediatetly invoking
......@@ -127,9 +126,9 @@ void Timer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
task_runner_.swap(task_runner);
}
void Timer::Start(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
void TimerBase::Start(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
posted_from_ = posted_from;
......@@ -139,7 +138,7 @@ void Timer::Start(const Location& posted_from,
Reset();
}
void Timer::Stop() {
void TimerBase::Stop() {
// TODO(gab): Enable this when it's no longer called racily from
// RunScheduledTask(): https://crbug.com/587199.
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
......@@ -155,7 +154,7 @@ void Timer::Stop() {
// |user_task_|.
}
void Timer::Reset() {
void TimerBase::Reset() {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
DCHECK(!user_task_.is_null());
......@@ -183,14 +182,14 @@ void Timer::Reset() {
PostNewScheduledTask(delay_);
}
TimeTicks Timer::Now() const {
TimeTicks TimerBase::Now() const {
// TODO(gab): Enable this when it's no longer called racily from
// RunScheduledTask(): https://crbug.com/587199.
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now();
}
void Timer::PostNewScheduledTask(TimeDelta delay) {
void TimerBase::PostNewScheduledTask(TimeDelta delay) {
// TODO(gab): Enable this when it's no longer called racily from
// RunScheduledTask(): https://crbug.com/587199.
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
......@@ -214,11 +213,11 @@ void Timer::PostNewScheduledTask(TimeDelta delay) {
}
}
scoped_refptr<SequencedTaskRunner> Timer::GetTaskRunner() {
scoped_refptr<SequencedTaskRunner> TimerBase::GetTaskRunner() {
return task_runner_.get() ? task_runner_ : SequencedTaskRunnerHandle::Get();
}
void Timer::AbandonScheduledTask() {
void TimerBase::AbandonScheduledTask() {
// TODO(gab): Enable this when it's no longer called racily from
// RunScheduledTask() -> Stop(): https://crbug.com/587199.
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
......@@ -228,7 +227,7 @@ void Timer::AbandonScheduledTask() {
}
}
void Timer::RunScheduledTask() {
void TimerBase::RunScheduledTask() {
// TODO(gab): Enable this when it's no longer called racily:
// https://crbug.com/587199.
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
......@@ -265,6 +264,8 @@ void Timer::RunScheduledTask() {
// No more member accesses here: |this| could be deleted at this point.
}
} // namespace internal
void OneShotTimer::FireNow() {
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
DCHECK(!task_runner_) << "FireNow() is incompatible with SetTaskRunner()";
......
......@@ -2,16 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// OneShotTimer and RepeatingTimer provide a simple timer API. As the names
// suggest, OneShotTimer calls you back once after a time delay expires.
// OneShotTimer, RepeatingTimer and RetainingOneShotTimer provide a simple timer
// API. As the names suggest, OneShotTimer calls you back once after a time
// delay expires.
// RepeatingTimer on the other hand calls you back periodically with the
// prescribed time interval.
// RetainingOneShotTimer doesn't repeat the task itself like OneShotTimer, but
// retains the given task after the time out. You can restart it with Reset
// again without giving new task to Start.
//
// OneShotTimer and RepeatingTimer both cancel the timer when they go out of
// scope, which makes it easy to ensure that you do not get called when your
// object has gone out of scope. Just instantiate a OneShotTimer or
// RepeatingTimer as a member variable of the class for which you wish to
// receive timer events.
// All of OneShotTimer, RepeatingTimer and RetainingOneShotTimer cancel the
// timer when they go out of scope, which makes it easy to ensure that you do
// not get called when your object has gone out of scope. Just instantiate a
// timer as a member variable of the class for which you wish to receive timer
// events.
//
// Sample RepeatingTimer usage:
//
......@@ -32,12 +36,11 @@
// base::RepeatingTimer timer_;
// };
//
// Both OneShotTimer and RepeatingTimer also support a Reset method, which
// allows you to easily defer the timer event until the timer delay passes once
// again. So, in the above example, if 0.5 seconds have already passed,
// calling Reset on |timer_| would postpone DoStuff by another 1 second. In
// other words, Reset is shorthand for calling Stop and then Start again with
// the same arguments.
// Timers also support a Reset method, which allows you to easily defer the
// timer event until the timer delay passes once again. So, in the above
// example, if 0.5 seconds have already passed, calling Reset on |timer_|
// would postpone DoStuff by another 1 second. In other words, Reset is
// shorthand for calling Stop and then Start again with the same arguments.
//
// These APIs are not thread safe. All methods must be called from the same
// sequence (not necessarily the construction sequence), except for the
......@@ -75,35 +78,42 @@
namespace base {
class BaseTimerTaskInternal;
class TickClock;
namespace internal {
class BaseTimerTaskInternal;
//-----------------------------------------------------------------------------
// This class wraps TaskRunner::PostDelayedTask to manage delayed and repeating
// tasks. See meta comment above for thread-safety requirements.
// Do not use this class directly. Use one of OneShotTimer, RepeatingTimer or
// RetainingOneShotTimer.
//
class BASE_EXPORT Timer {
class BASE_EXPORT TimerBase {
public:
// Construct a timer in repeating or one-shot mode. Start 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. 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, const TickClock* tick_clock);
TimerBase(bool retain_user_task, bool is_repeating);
TimerBase(bool retain_user_task,
bool is_repeating,
const TickClock* tick_clock);
// 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 Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating);
Timer(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating,
const TickClock* tick_clock);
virtual ~Timer();
TimerBase(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating);
TimerBase(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating,
const TickClock* tick_clock);
virtual ~TimerBase();
// Returns true if the timer is running (i.e., not stopped).
bool IsRunning() const;
......@@ -231,16 +241,18 @@ class BASE_EXPORT Timer {
// If true, |user_task_| is scheduled to run sometime in the future.
bool is_running_;
DISALLOW_COPY_AND_ASSIGN(Timer);
DISALLOW_COPY_AND_ASSIGN(TimerBase);
};
} // namespace internal
//-----------------------------------------------------------------------------
// A simple, one-shot timer. See usage notes at the top of the file.
class BASE_EXPORT OneShotTimer : public Timer {
class BASE_EXPORT OneShotTimer : public internal::TimerBase {
public:
OneShotTimer() : OneShotTimer(nullptr) {}
explicit OneShotTimer(const TickClock* tick_clock)
: Timer(false, false, tick_clock) {}
: internal::TimerBase(false, false, tick_clock) {}
// Run the scheduled task immediately, and stop the timer. The timer needs to
// be running.
......@@ -249,41 +261,49 @@ class BASE_EXPORT OneShotTimer : public Timer {
//-----------------------------------------------------------------------------
// A simple, repeating timer. See usage notes at the top of the file.
class RepeatingTimer : public Timer {
class RepeatingTimer : public internal::TimerBase {
public:
RepeatingTimer() : RepeatingTimer(nullptr) {}
explicit RepeatingTimer(const TickClock* tick_clock)
: Timer(true, true, tick_clock) {}
: internal::TimerBase(true, true, tick_clock) {}
RepeatingTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task)
: Timer(posted_from, delay, std::move(user_task), true) {}
: internal::TimerBase(posted_from, delay, std::move(user_task), true) {}
RepeatingTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task,
const TickClock* tick_clock)
: Timer(posted_from, delay, std::move(user_task), true, tick_clock) {}
: internal::TimerBase(posted_from,
delay,
std::move(user_task),
true,
tick_clock) {}
};
//-----------------------------------------------------------------------------
// A simple, one-shot timer with the retained user task. See usage notes at the
// top of the file.
class RetainingOneShotTimer : public Timer {
class RetainingOneShotTimer : public internal::TimerBase {
public:
RetainingOneShotTimer() : RetainingOneShotTimer(nullptr) {}
explicit RetainingOneShotTimer(const TickClock* tick_clock)
: Timer(true, false, tick_clock) {}
: internal::TimerBase(true, false, tick_clock) {}
RetainingOneShotTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task)
: Timer(posted_from, delay, std::move(user_task), false) {}
: internal::TimerBase(posted_from, delay, std::move(user_task), false) {}
RetainingOneShotTimer(const Location& posted_from,
TimeDelta delay,
RepeatingClosure user_task,
const TickClock* tick_clock)
: Timer(posted_from, delay, std::move(user_task), false, tick_clock) {}
: internal::TimerBase(posted_from,
delay,
std::move(user_task),
false,
tick_clock) {}
};
//-----------------------------------------------------------------------------
......
......@@ -603,7 +603,7 @@ void TimerTestCallback() {
TEST(TimerTest, NonRepeatIsRunning) {
{
MessageLoop loop;
Timer timer(false, false);
OneShotTimer timer;
EXPECT_FALSE(timer.IsRunning());
timer.Start(FROM_HERE, TimeDelta::FromDays(1), Bind(&TimerTestCallback));
EXPECT_TRUE(timer.IsRunning());
......@@ -613,7 +613,7 @@ TEST(TimerTest, NonRepeatIsRunning) {
}
{
Timer timer(true, false);
RetainingOneShotTimer timer;
MessageLoop loop;
EXPECT_FALSE(timer.IsRunning());
timer.Start(FROM_HERE, TimeDelta::FromDays(1), Bind(&TimerTestCallback));
......@@ -627,7 +627,7 @@ TEST(TimerTest, NonRepeatIsRunning) {
}
TEST(TimerTest, NonRepeatMessageLoopDeath) {
Timer timer(false, false);
OneShotTimer timer;
{
MessageLoop loop;
EXPECT_FALSE(timer.IsRunning());
......@@ -640,8 +640,8 @@ TEST(TimerTest, NonRepeatMessageLoopDeath) {
TEST(TimerTest, RetainRepeatIsRunning) {
MessageLoop loop;
Timer timer(FROM_HERE, TimeDelta::FromDays(1), Bind(&TimerTestCallback),
true);
RepeatingTimer timer(FROM_HERE, TimeDelta::FromDays(1),
Bind(&TimerTestCallback));
EXPECT_FALSE(timer.IsRunning());
timer.Reset();
EXPECT_TRUE(timer.IsRunning());
......@@ -653,8 +653,8 @@ TEST(TimerTest, RetainRepeatIsRunning) {
TEST(TimerTest, RetainNonRepeatIsRunning) {
MessageLoop loop;
Timer timer(FROM_HERE, TimeDelta::FromDays(1), Bind(&TimerTestCallback),
false);
RetainingOneShotTimer timer(FROM_HERE, TimeDelta::FromDays(1),
Bind(&TimerTestCallback));
EXPECT_FALSE(timer.IsRunning());
timer.Reset();
EXPECT_TRUE(timer.IsRunning());
......@@ -692,7 +692,7 @@ TEST(TimerTest, ContinuationStopStart) {
{
ClearAllCallbackHappened();
MessageLoop loop;
Timer timer(false, false);
OneShotTimer timer;
timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
Bind(&SetCallbackHappened1));
timer.Stop();
......@@ -708,7 +708,7 @@ TEST(TimerTest, ContinuationReset) {
{
ClearAllCallbackHappened();
MessageLoop loop;
Timer timer(false, false);
OneShotTimer timer;
timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
Bind(&SetCallbackHappened1));
timer.Reset();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment