mirror of
https://github.com/klzgrad/naiveproxy.git
synced 2026-08-18 06:55:34 +03:00
Import chromium-64.0.3282.140
This commit is contained in:
25
base/timer/elapsed_timer.cc
Normal file
25
base/timer/elapsed_timer.cc
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2013 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/timer/elapsed_timer.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
ElapsedTimer::ElapsedTimer() {
|
||||
begin_ = TimeTicks::Now();
|
||||
}
|
||||
|
||||
ElapsedTimer::ElapsedTimer(ElapsedTimer&& other) {
|
||||
begin_ = other.begin_;
|
||||
}
|
||||
|
||||
void ElapsedTimer::operator=(ElapsedTimer&& other) {
|
||||
begin_ = other.begin_;
|
||||
}
|
||||
|
||||
TimeDelta ElapsedTimer::Elapsed() const {
|
||||
return TimeTicks::Now() - begin_;
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
33
base/timer/elapsed_timer.h
Normal file
33
base/timer/elapsed_timer.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2013 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef BASE_TIMER_ELAPSED_TIMER_H_
|
||||
#define BASE_TIMER_ELAPSED_TIMER_H_
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/time/time.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
// A simple wrapper around TimeTicks::Now().
|
||||
class BASE_EXPORT ElapsedTimer {
|
||||
public:
|
||||
ElapsedTimer();
|
||||
ElapsedTimer(ElapsedTimer&& other);
|
||||
|
||||
void operator=(ElapsedTimer&& other);
|
||||
|
||||
// Returns the time elapsed since object construction.
|
||||
TimeDelta Elapsed() const;
|
||||
|
||||
private:
|
||||
TimeTicks begin_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ElapsedTimer);
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif // BASE_TIMER_ELAPSED_TIMER_H_
|
||||
48
base/timer/hi_res_timer_manager.h
Normal file
48
base/timer/hi_res_timer_manager.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef BASE_TIMER_HI_RES_TIMER_MANAGER_H_
|
||||
#define BASE_TIMER_HI_RES_TIMER_MANAGER_H_
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/power_monitor/power_observer.h"
|
||||
#include "base/timer/timer.h"
|
||||
#include "build/build_config.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
// Ensures that the Windows high resolution timer is only used
|
||||
// when not running on battery power.
|
||||
class BASE_EXPORT HighResolutionTimerManager : public base::PowerObserver {
|
||||
public:
|
||||
HighResolutionTimerManager();
|
||||
~HighResolutionTimerManager() override;
|
||||
|
||||
// base::PowerObserver methods.
|
||||
void OnPowerStateChange(bool on_battery_power) override;
|
||||
void OnSuspend() override;
|
||||
void OnResume() override;
|
||||
|
||||
// Returns true if the hi resolution clock could be used right now.
|
||||
bool hi_res_clock_available() const { return hi_res_clock_available_; }
|
||||
|
||||
private:
|
||||
// Enable or disable the faster multimedia timer.
|
||||
void UseHiResClock(bool use);
|
||||
|
||||
bool hi_res_clock_available_;
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// Timer for polling the high resolution timer usage.
|
||||
base::RepeatingTimer timer_;
|
||||
#endif
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager);
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif // BASE_TIMER_HI_RES_TIMER_MANAGER_H_
|
||||
27
base/timer/hi_res_timer_manager_posix.cc
Normal file
27
base/timer/hi_res_timer_manager_posix.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/timer/hi_res_timer_manager.h"
|
||||
|
||||
// On POSIX we don't need to do anything special with the system timer.
|
||||
|
||||
namespace base {
|
||||
|
||||
HighResolutionTimerManager::HighResolutionTimerManager()
|
||||
: hi_res_clock_available_(false) {
|
||||
}
|
||||
|
||||
HighResolutionTimerManager::~HighResolutionTimerManager() = default;
|
||||
|
||||
void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) {
|
||||
}
|
||||
|
||||
void HighResolutionTimerManager::OnSuspend() {}
|
||||
|
||||
void HighResolutionTimerManager::OnResume() {}
|
||||
|
||||
void HighResolutionTimerManager::UseHiResClock(bool use) {
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
70
base/timer/hi_res_timer_manager_win.cc
Normal file
70
base/timer/hi_res_timer_manager_win.cc
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/timer/hi_res_timer_manager.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "base/atomicops.h"
|
||||
#include "base/metrics/histogram_macros.h"
|
||||
#include "base/power_monitor/power_monitor.h"
|
||||
#include "base/task_scheduler/post_task.h"
|
||||
#include "base/time/time.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr TimeDelta kUsageSampleInterval = TimeDelta::FromMinutes(10);
|
||||
|
||||
void ReportHighResolutionTimerUsage() {
|
||||
UMA_HISTOGRAM_PERCENTAGE("Windows.HighResolutionTimerUsage",
|
||||
Time::GetHighResolutionTimerUsage());
|
||||
// Reset usage for the next interval.
|
||||
Time::ResetHighResolutionTimerUsage();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
HighResolutionTimerManager::HighResolutionTimerManager()
|
||||
: hi_res_clock_available_(false) {
|
||||
PowerMonitor* power_monitor = PowerMonitor::Get();
|
||||
DCHECK(power_monitor != NULL);
|
||||
power_monitor->AddObserver(this);
|
||||
UseHiResClock(!power_monitor->IsOnBatteryPower());
|
||||
|
||||
// Start polling the high resolution timer usage.
|
||||
Time::ResetHighResolutionTimerUsage();
|
||||
timer_.Start(FROM_HERE, kUsageSampleInterval,
|
||||
Bind(&ReportHighResolutionTimerUsage));
|
||||
}
|
||||
|
||||
HighResolutionTimerManager::~HighResolutionTimerManager() {
|
||||
PowerMonitor::Get()->RemoveObserver(this);
|
||||
UseHiResClock(false);
|
||||
}
|
||||
|
||||
void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) {
|
||||
UseHiResClock(!on_battery_power);
|
||||
}
|
||||
|
||||
void HighResolutionTimerManager::OnSuspend() {
|
||||
// Stop polling the usage to avoid including the standby time.
|
||||
timer_.Stop();
|
||||
}
|
||||
|
||||
void HighResolutionTimerManager::OnResume() {
|
||||
// Resume polling the usage.
|
||||
Time::ResetHighResolutionTimerUsage();
|
||||
timer_.Reset();
|
||||
}
|
||||
|
||||
void HighResolutionTimerManager::UseHiResClock(bool use) {
|
||||
if (use == hi_res_clock_available_)
|
||||
return;
|
||||
hi_res_clock_available_ = use;
|
||||
Time::EnableHighResolutionTimer(use);
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
59
base/timer/mock_timer.cc
Normal file
59
base/timer/mock_timer.cc
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/timer/mock_timer.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
|
||||
: Timer(retain_user_task, is_repeating),
|
||||
is_running_(false) {
|
||||
}
|
||||
|
||||
MockTimer::MockTimer(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
const base::Closure& user_task,
|
||||
bool is_repeating)
|
||||
: Timer(true, is_repeating), delay_(delay), is_running_(false) {}
|
||||
|
||||
MockTimer::~MockTimer() = default;
|
||||
|
||||
bool MockTimer::IsRunning() const {
|
||||
return is_running_;
|
||||
}
|
||||
|
||||
base::TimeDelta MockTimer::GetCurrentDelay() const {
|
||||
return delay_;
|
||||
}
|
||||
|
||||
void MockTimer::Start(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
const base::Closure& user_task) {
|
||||
delay_ = delay;
|
||||
user_task_ = user_task;
|
||||
Reset();
|
||||
}
|
||||
|
||||
void MockTimer::Stop() {
|
||||
is_running_ = false;
|
||||
if (!retain_user_task())
|
||||
user_task_.Reset();
|
||||
}
|
||||
|
||||
void MockTimer::Reset() {
|
||||
DCHECK(!user_task_.is_null());
|
||||
is_running_ = true;
|
||||
}
|
||||
|
||||
void MockTimer::Fire() {
|
||||
DCHECK(is_running_);
|
||||
base::Closure old_task = user_task_;
|
||||
if (is_repeating())
|
||||
Reset();
|
||||
else
|
||||
Stop();
|
||||
old_task.Run();
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
41
base/timer/mock_timer.h
Normal file
41
base/timer/mock_timer.h
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef BASE_TIMER_MOCK_TIMER_H_
|
||||
#define BASE_TIMER_MOCK_TIMER_H_
|
||||
|
||||
#include "base/timer/timer.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
class BASE_EXPORT MockTimer : public Timer {
|
||||
public:
|
||||
MockTimer(bool retain_user_task, bool is_repeating);
|
||||
MockTimer(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
const base::Closure& user_task,
|
||||
bool is_repeating);
|
||||
~MockTimer() override;
|
||||
|
||||
// base::Timer implementation.
|
||||
bool IsRunning() const override;
|
||||
base::TimeDelta GetCurrentDelay() const override;
|
||||
void Start(const Location& posted_from,
|
||||
base::TimeDelta delay,
|
||||
const base::Closure& user_task) override;
|
||||
void Stop() override;
|
||||
void Reset() override;
|
||||
|
||||
// Testing methods.
|
||||
void Fire();
|
||||
|
||||
private:
|
||||
base::Closure user_task_;
|
||||
TimeDelta delay_;
|
||||
bool is_running_;
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif // BASE_TIMER_MOCK_TIMER_H_
|
||||
266
base/timer/timer.cc
Normal file
266
base/timer/timer.cc
Normal file
@@ -0,0 +1,266 @@
|
||||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/timer/timer.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/threading/platform_thread.h"
|
||||
#include "base/threading/sequenced_task_runner_handle.h"
|
||||
#include "base/time/tick_clock.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
// BaseTimerTaskInternal is a simple delegate for scheduling a callback to Timer
|
||||
// on the current sequence. It also handles the following edge cases:
|
||||
// - deleted by the task runner.
|
||||
// - abandoned (orphaned) by Timer.
|
||||
class BaseTimerTaskInternal {
|
||||
public:
|
||||
explicit BaseTimerTaskInternal(Timer* timer)
|
||||
: timer_(timer) {
|
||||
}
|
||||
|
||||
~BaseTimerTaskInternal() {
|
||||
// This task may be getting cleared because the task runner has been
|
||||
// destructed. If so, don't leave Timer with a dangling pointer
|
||||
// to this.
|
||||
if (timer_)
|
||||
timer_->AbandonAndStop();
|
||||
}
|
||||
|
||||
void Run() {
|
||||
// |timer_| is nullptr if we were abandoned.
|
||||
if (!timer_)
|
||||
return;
|
||||
|
||||
// |this| will be deleted by the task runner, so Timer needs to forget us:
|
||||
timer_->scheduled_task_ = nullptr;
|
||||
|
||||
// Although Timer should not call back into |this|, let's clear |timer_|
|
||||
// first to be pedantic.
|
||||
Timer* timer = timer_;
|
||||
timer_ = nullptr;
|
||||
timer->RunScheduledTask();
|
||||
}
|
||||
|
||||
// The task remains in the queue, but nothing will happen when it runs.
|
||||
void Abandon() { timer_ = nullptr; }
|
||||
|
||||
private:
|
||||
Timer* timer_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(BaseTimerTaskInternal);
|
||||
};
|
||||
|
||||
Timer::Timer(bool retain_user_task, bool is_repeating)
|
||||
: Timer(retain_user_task, is_repeating, nullptr) {}
|
||||
|
||||
Timer::Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock)
|
||||
: scheduled_task_(nullptr),
|
||||
is_repeating_(is_repeating),
|
||||
retain_user_task_(retain_user_task),
|
||||
tick_clock_(tick_clock),
|
||||
is_running_(false) {
|
||||
// It is safe for the timer to be created on a different thread/sequence than
|
||||
// the one from which the timer APIs are called. The first call to the
|
||||
// checker's CalledOnValidSequence() method will re-bind the checker, and
|
||||
// later calls will verify that the same task runner is used.
|
||||
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,
|
||||
TickClock* tick_clock)
|
||||
: scheduled_task_(nullptr),
|
||||
posted_from_(posted_from),
|
||||
delay_(delay),
|
||||
user_task_(user_task),
|
||||
is_repeating_(is_repeating),
|
||||
retain_user_task_(true),
|
||||
tick_clock_(tick_clock),
|
||||
is_running_(false) {
|
||||
// See comment in other constructor.
|
||||
origin_sequence_checker_.DetachFromSequence();
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
AbandonAndStop();
|
||||
}
|
||||
|
||||
bool Timer::IsRunning() const {
|
||||
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
return is_running_;
|
||||
}
|
||||
|
||||
TimeDelta Timer::GetCurrentDelay() const {
|
||||
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
return delay_;
|
||||
}
|
||||
|
||||
void Timer::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
|
||||
// SetTaskRunner() before starting it (CalledOnValidSequence() would undo the
|
||||
// DetachFromSequence() from the constructor). The |!is_running| check kind of
|
||||
// verifies the same thing (and TSAN should catch callers that do it wrong but
|
||||
// somehow evade all debug checks).
|
||||
DCHECK(!is_running_);
|
||||
task_runner_.swap(task_runner);
|
||||
}
|
||||
|
||||
void Timer::Start(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
const base::Closure& user_task) {
|
||||
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
|
||||
posted_from_ = posted_from;
|
||||
delay_ = delay;
|
||||
user_task_ = user_task;
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
void Timer::Stop() {
|
||||
// TODO(gab): Enable this when it's no longer called racily from
|
||||
// RunScheduledTask(): https://crbug.com/587199.
|
||||
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
|
||||
is_running_ = false;
|
||||
|
||||
// It's safe to destroy or restart Timer on another sequence after Stop().
|
||||
origin_sequence_checker_.DetachFromSequence();
|
||||
|
||||
if (!retain_user_task_)
|
||||
user_task_.Reset();
|
||||
// No more member accesses here: |this| could be deleted after freeing
|
||||
// |user_task_|.
|
||||
}
|
||||
|
||||
void Timer::Reset() {
|
||||
DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
DCHECK(!user_task_.is_null());
|
||||
|
||||
// If there's no pending task, start one up and return.
|
||||
if (!scheduled_task_) {
|
||||
PostNewScheduledTask(delay_);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the new |desired_run_time_|.
|
||||
if (delay_ > TimeDelta::FromMicroseconds(0))
|
||||
desired_run_time_ = Now() + delay_;
|
||||
else
|
||||
desired_run_time_ = TimeTicks();
|
||||
|
||||
// We can use the existing scheduled task if it arrives before the new
|
||||
// |desired_run_time_|.
|
||||
if (desired_run_time_ >= scheduled_run_time_) {
|
||||
is_running_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// We can't reuse the |scheduled_task_|, so abandon it and post a new one.
|
||||
AbandonScheduledTask();
|
||||
PostNewScheduledTask(delay_);
|
||||
}
|
||||
|
||||
TimeTicks Timer::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) {
|
||||
// TODO(gab): Enable this when it's no longer called racily from
|
||||
// RunScheduledTask(): https://crbug.com/587199.
|
||||
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
DCHECK(!scheduled_task_);
|
||||
is_running_ = true;
|
||||
scheduled_task_ = new BaseTimerTaskInternal(this);
|
||||
if (delay > TimeDelta::FromMicroseconds(0)) {
|
||||
// TODO(gab): Posting BaseTimerTaskInternal::Run to another sequence makes
|
||||
// this code racy. https://crbug.com/587199
|
||||
GetTaskRunner()->PostDelayedTask(
|
||||
posted_from_,
|
||||
base::BindOnce(&BaseTimerTaskInternal::Run,
|
||||
base::Owned(scheduled_task_)),
|
||||
delay);
|
||||
scheduled_run_time_ = desired_run_time_ = Now() + delay;
|
||||
} else {
|
||||
GetTaskRunner()->PostTask(posted_from_,
|
||||
base::BindOnce(&BaseTimerTaskInternal::Run,
|
||||
base::Owned(scheduled_task_)));
|
||||
scheduled_run_time_ = desired_run_time_ = TimeTicks();
|
||||
}
|
||||
}
|
||||
|
||||
scoped_refptr<SequencedTaskRunner> Timer::GetTaskRunner() {
|
||||
return task_runner_.get() ? task_runner_ : SequencedTaskRunnerHandle::Get();
|
||||
}
|
||||
|
||||
void Timer::AbandonScheduledTask() {
|
||||
// TODO(gab): Enable this when it's no longer called racily from
|
||||
// RunScheduledTask() -> Stop(): https://crbug.com/587199.
|
||||
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
if (scheduled_task_) {
|
||||
scheduled_task_->Abandon();
|
||||
scheduled_task_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::RunScheduledTask() {
|
||||
// TODO(gab): Enable this when it's no longer called racily:
|
||||
// https://crbug.com/587199.
|
||||
// DCHECK(origin_sequence_checker_.CalledOnValidSequence());
|
||||
|
||||
// Task may have been disabled.
|
||||
if (!is_running_)
|
||||
return;
|
||||
|
||||
// First check if we need to delay the task because of a new target time.
|
||||
if (desired_run_time_ > scheduled_run_time_) {
|
||||
// Now() can be expensive, so only call it if we know the user has changed
|
||||
// the |desired_run_time_|.
|
||||
TimeTicks now = Now();
|
||||
// Task runner may have called us late anyway, so only post a continuation
|
||||
// task if the |desired_run_time_| is in the future.
|
||||
if (desired_run_time_ > now) {
|
||||
// Post a new task to span the remaining time.
|
||||
PostNewScheduledTask(desired_run_time_ - now);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Make a local copy of the task to run. The Stop method will reset the
|
||||
// |user_task_| member if |retain_user_task_| is false.
|
||||
base::Closure task = user_task_;
|
||||
|
||||
if (is_repeating_)
|
||||
PostNewScheduledTask(delay_);
|
||||
else
|
||||
Stop();
|
||||
|
||||
task.Run();
|
||||
|
||||
// No more member accesses here: |this| could be deleted at this point.
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
316
base/timer/timer.h
Normal file
316
base/timer/timer.h
Normal file
@@ -0,0 +1,316 @@
|
||||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// 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.
|
||||
// RepeatingTimer on the other hand calls you back periodically with the
|
||||
// prescribed time interval.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Sample RepeatingTimer usage:
|
||||
//
|
||||
// class MyClass {
|
||||
// public:
|
||||
// void StartDoingStuff() {
|
||||
// timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
|
||||
// this, &MyClass::DoStuff);
|
||||
// }
|
||||
// void StopDoingStuff() {
|
||||
// timer_.Stop();
|
||||
// }
|
||||
// private:
|
||||
// void DoStuff() {
|
||||
// // This method is called every second to do stuff.
|
||||
// ...
|
||||
// }
|
||||
// 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.
|
||||
//
|
||||
// These APIs are not thread safe. All methods must be called from the same
|
||||
// sequence (not necessarily the construction sequence), except for the
|
||||
// destructor and SetTaskRunner().
|
||||
// - The destructor may be called from any sequence when the timer is not
|
||||
// running and there is no scheduled task active, i.e. when Start() has never
|
||||
// been called or after AbandonAndStop() has been called.
|
||||
// - SetTaskRunner() may be called from any sequence when the timer is not
|
||||
// running, i.e. when Start() has never been called or Stop() has been called
|
||||
// since the last Start().
|
||||
//
|
||||
// By default, the scheduled tasks will be run on the same sequence that the
|
||||
// Timer was *started on*, but this can be changed *prior* to Start() via
|
||||
// SetTaskRunner().
|
||||
|
||||
#ifndef BASE_TIMER_TIMER_H_
|
||||
#define BASE_TIMER_TIMER_H_
|
||||
|
||||
// IMPORTANT: If you change timer code, make sure that all tests (including
|
||||
// disabled ones) from timer_unittests.cc pass locally. Some are disabled
|
||||
// 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"
|
||||
#include "base/callback.h"
|
||||
#include "base/location.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/sequence_checker_impl.h"
|
||||
#include "base/sequenced_task_runner.h"
|
||||
#include "base/time/time.h"
|
||||
|
||||
namespace base {
|
||||
|
||||
class BaseTimerTaskInternal;
|
||||
class TickClock;
|
||||
|
||||
// TODO(gab): Removing this fwd-decl causes IWYU failures in other headers,
|
||||
// remove it in a follow- up CL.
|
||||
class SingleThreadTaskRunner;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class wraps TaskRunner::PostDelayedTask to manage delayed and repeating
|
||||
// tasks. See meta comment above for thread-safety requirements.
|
||||
//
|
||||
class BASE_EXPORT Timer {
|
||||
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, 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,
|
||||
TickClock* tick_clock);
|
||||
|
||||
virtual ~Timer();
|
||||
|
||||
// Returns true if the timer is running (i.e., not stopped).
|
||||
virtual bool IsRunning() const;
|
||||
|
||||
// Returns the current delay for this timer.
|
||||
virtual TimeDelta GetCurrentDelay() const;
|
||||
|
||||
// Set the task runner on which the task should be scheduled. This method can
|
||||
// only be called before any tasks have been scheduled. If |task_runner| runs
|
||||
// tasks on a different sequence than the sequence owning this Timer,
|
||||
// |user_task_| will be posted to it when the Timer fires (note that this
|
||||
// means |user_task_| can run after ~Timer() and should support that).
|
||||
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner);
|
||||
|
||||
// Start the timer to run at the given |delay| from now. If the timer is
|
||||
// already running, it will be replaced to call the given |user_task|.
|
||||
virtual void Start(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
const base::Closure& user_task);
|
||||
|
||||
// Call this method to stop and cancel the timer. It is a no-op if the timer
|
||||
// is not running.
|
||||
virtual void Stop();
|
||||
|
||||
// Stop running task (if any) and abandon scheduled task (if any).
|
||||
void AbandonAndStop() {
|
||||
AbandonScheduledTask();
|
||||
|
||||
Stop();
|
||||
// No more member accesses here: |this| could be deleted at this point.
|
||||
}
|
||||
|
||||
// Call this method to reset the timer delay. The |user_task_| must be set. If
|
||||
// the timer is not running, this will start it by posting a task.
|
||||
virtual void Reset();
|
||||
|
||||
const base::Closure& user_task() const { return user_task_; }
|
||||
const TimeTicks& desired_run_time() const { return desired_run_time_; }
|
||||
|
||||
protected:
|
||||
// Returns the current tick count.
|
||||
TimeTicks Now() const;
|
||||
|
||||
void set_user_task(const Closure& task) { user_task_ = task; }
|
||||
void set_desired_run_time(TimeTicks desired) { desired_run_time_ = desired; }
|
||||
void set_is_running(bool running) { is_running_ = running; }
|
||||
|
||||
const Location& posted_from() const { return posted_from_; }
|
||||
bool retain_user_task() const { return retain_user_task_; }
|
||||
bool is_repeating() const { return is_repeating_; }
|
||||
bool is_running() const { return is_running_; }
|
||||
|
||||
private:
|
||||
friend class BaseTimerTaskInternal;
|
||||
|
||||
// Allocates a new |scheduled_task_| and posts it on the current sequence with
|
||||
// the given |delay|. |scheduled_task_| must be null. |scheduled_run_time_|
|
||||
// and |desired_run_time_| are reset to Now() + delay.
|
||||
void PostNewScheduledTask(TimeDelta delay);
|
||||
|
||||
// Returns the task runner on which the task should be scheduled. If the
|
||||
// corresponding |task_runner_| field is null, the task runner for the current
|
||||
// sequence is returned.
|
||||
scoped_refptr<SequencedTaskRunner> GetTaskRunner();
|
||||
|
||||
// Disable |scheduled_task_| and abandon it so that it no longer refers back
|
||||
// to this object.
|
||||
void AbandonScheduledTask();
|
||||
|
||||
// Called by BaseTimerTaskInternal when the delayed task fires.
|
||||
void RunScheduledTask();
|
||||
|
||||
// When non-null, the |scheduled_task_| was posted to call RunScheduledTask()
|
||||
// at |scheduled_run_time_|.
|
||||
BaseTimerTaskInternal* scheduled_task_;
|
||||
|
||||
// The task runner on which the task should be scheduled. If it is null, the
|
||||
// task runner for the current sequence will be used.
|
||||
scoped_refptr<SequencedTaskRunner> task_runner_;
|
||||
|
||||
// Location in user code.
|
||||
Location posted_from_;
|
||||
// Delay requested by user.
|
||||
TimeDelta delay_;
|
||||
// |user_task_| is what the user wants to be run at |desired_run_time_|.
|
||||
base::Closure user_task_;
|
||||
|
||||
// The time at which |scheduled_task_| is expected to fire. This time can be a
|
||||
// "zero" TimeTicks if the task must be run immediately.
|
||||
TimeTicks scheduled_run_time_;
|
||||
|
||||
// The desired run time of |user_task_|. The user may update this at any time,
|
||||
// even if their previous request has not run yet. If |desired_run_time_| is
|
||||
// greater than |scheduled_run_time_|, a continuation task will be posted to
|
||||
// wait for the remaining time. This allows us to reuse the pending task so as
|
||||
// not to flood the delayed queues with orphaned tasks when the user code
|
||||
// excessively Stops and Starts the timer. This time can be a "zero" TimeTicks
|
||||
// if the task must be run immediately.
|
||||
TimeTicks desired_run_time_;
|
||||
|
||||
// Timer isn't thread-safe and must only be used on its origin sequence
|
||||
// (sequence on which it was started). Once fully Stop()'ed it may be
|
||||
// destroyed or restarted on another sequence.
|
||||
SequenceChecker origin_sequence_checker_;
|
||||
|
||||
// Repeating timers automatically post the task again before calling the task
|
||||
// callback.
|
||||
const bool is_repeating_;
|
||||
|
||||
// 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_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Timer);
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class is an implementation detail of OneShotTimer and RepeatingTimer.
|
||||
// Please do not use this class directly.
|
||||
class BaseTimerMethodPointer : public Timer {
|
||||
public:
|
||||
// This is here to work around the fact that Timer::Start is "hidden" by the
|
||||
// Start definition below, rather than being overloaded.
|
||||
// TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below
|
||||
// and convert callers to use the base::Closure version in Timer::Start,
|
||||
// see bug 148832.
|
||||
using Timer::Start;
|
||||
|
||||
enum RepeatMode { ONE_SHOT, 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
|
||||
// |reviewer->*method|.
|
||||
template <class Receiver>
|
||||
void Start(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
Receiver* receiver,
|
||||
void (Receiver::*method)()) {
|
||||
Timer::Start(posted_from, delay,
|
||||
base::Bind(method, base::Unretained(receiver)));
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// A simple, one-shot timer. See usage notes at the top of the file.
|
||||
class OneShotTimer : public BaseTimerMethodPointer {
|
||||
public:
|
||||
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() : RepeatingTimer(nullptr) {}
|
||||
explicit RepeatingTimer(TickClock* tick_clock)
|
||||
: BaseTimerMethodPointer(REPEATING, tick_clock) {}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// A Delay timer is like The Button from Lost. Once started, you have to keep
|
||||
// calling Reset otherwise it will call the given method on the sequence it was
|
||||
// initially Reset() from.
|
||||
//
|
||||
// Once created, it is inactive until Reset is called. Once |delay| seconds have
|
||||
// passed since the last call to Reset, the callback is made. Once the callback
|
||||
// has been made, it's inactive until Reset is called again.
|
||||
//
|
||||
// If destroyed, the timeout is canceled and will not occur even if already
|
||||
// inflight.
|
||||
class DelayTimer : protected Timer {
|
||||
public:
|
||||
template <class Receiver>
|
||||
DelayTimer(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
Receiver* receiver,
|
||||
void (Receiver::*method)())
|
||||
: DelayTimer(posted_from, delay, receiver, method, nullptr) {}
|
||||
|
||||
template <class Receiver>
|
||||
DelayTimer(const Location& posted_from,
|
||||
TimeDelta delay,
|
||||
Receiver* receiver,
|
||||
void (Receiver::*method)(),
|
||||
TickClock* tick_clock)
|
||||
: Timer(posted_from,
|
||||
delay,
|
||||
base::Bind(method, base::Unretained(receiver)),
|
||||
false,
|
||||
tick_clock) {}
|
||||
|
||||
using Timer::Reset;
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif // BASE_TIMER_TIMER_H_
|
||||
Reference in New Issue
Block a user