#include "base/memory/post_delayed_memory_reduction_task.h"
#include "base/timer/timer.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/pre_freeze_background_memory_trimmer.h"
#endif
namespace base {
void PostDelayedMemoryReductionTask(
scoped_refptr<SequencedTaskRunner> task_runner,
const Location& from_here,
OnceClosure task,
base::TimeDelta delay) { … }
void PostDelayedMemoryReductionTask(
scoped_refptr<SequencedTaskRunner> task_runner,
const Location& from_here,
OnceCallback<void(MemoryReductionTaskContext)> task,
base::TimeDelta delay) { … }
class OneShotDelayedBackgroundTimer::TimerImpl final
: public OneShotDelayedBackgroundTimer::OneShotDelayedBackgroundTimerImpl { … };
#if BUILDFLAG(IS_ANDROID)
class OneShotDelayedBackgroundTimer::TaskImpl final
: public OneShotDelayedBackgroundTimer::OneShotDelayedBackgroundTimerImpl {
public:
~TaskImpl() override = default;
void Start(const Location& from_here,
TimeDelta delay,
OnceCallback<void(MemoryReductionTaskContext)> task) override {
this->StartInternal(
from_here, delay,
BindOnce(
[](TaskImpl* timer,
OnceCallback<void(MemoryReductionTaskContext)> task,
MemoryReductionTaskContext in_pre_freeze) {
std::move(task).Run(in_pre_freeze);
timer->task_ = nullptr;
},
base::Unretained(this), std::move(task)));
}
void StartInternal(const Location& from_here,
TimeDelta delay,
OnceCallback<void(MemoryReductionTaskContext)> task) {
if (IsRunning()) {
Stop();
}
DCHECK(GetTaskRunner()->RunsTasksInCurrentSequence());
base::AutoLock locker(
android::PreFreezeBackgroundMemoryTrimmer::Instance().lock_);
task_ = android::PreFreezeBackgroundMemoryTrimmer::Instance()
.PostDelayedBackgroundTaskModernHelper(
GetTaskRunner(), from_here, std::move(task), delay);
}
void Stop() override {
if (IsRunning()) {
task_.ExtractAsDangling()->CancelTask();
}
}
bool IsRunning() const override { return task_ != nullptr; }
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override {
task_runner_ = task_runner;
}
private:
scoped_refptr<SequencedTaskRunner> GetTaskRunner() {
return task_runner_ ? task_runner_
: SequencedTaskRunner::GetCurrentDefault();
}
raw_ptr<android::PreFreezeBackgroundMemoryTrimmer::BackgroundTask> task_ =
nullptr;
scoped_refptr<SequencedTaskRunner> task_runner_ = nullptr;
};
#endif
OneShotDelayedBackgroundTimer::OneShotDelayedBackgroundTimer() { … }
OneShotDelayedBackgroundTimer::~OneShotDelayedBackgroundTimer() { … }
void OneShotDelayedBackgroundTimer::Stop() { … }
bool OneShotDelayedBackgroundTimer::IsRunning() const { … }
void OneShotDelayedBackgroundTimer::SetTaskRunner(
scoped_refptr<SequencedTaskRunner> task_runner) { … }
void OneShotDelayedBackgroundTimer::Start(
const Location& from_here,
TimeDelta delay,
OnceCallback<void(MemoryReductionTaskContext)> task) { … }
}