1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183

base / memory / post_delayed_memory_reduction_task.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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) {
#if BUILDFLAG(IS_ANDROID)
  android::PreFreezeBackgroundMemoryTrimmer::PostDelayedBackgroundTask(
      std::move(task_runner), from_here, std::move(task), delay);
#else
  task_runner->PostDelayedTask(from_here, std::move(task), delay);
#endif
}

void PostDelayedMemoryReductionTask(
    scoped_refptr<SequencedTaskRunner> task_runner,
    const Location& from_here,
    OnceCallback<void(MemoryReductionTaskContext)> task,
    base::TimeDelta delay) {
#if BUILDFLAG(IS_ANDROID)
  android::PreFreezeBackgroundMemoryTrimmer::PostDelayedBackgroundTask(
      std::move(task_runner), from_here, std::move(task), delay);
#else
  task_runner->PostDelayedTask(
      from_here,
      BindOnce(std::move(task), MemoryReductionTaskContext::kDelayExpired),
      delay);
#endif  // BUILDFLAG(IS_ANDROID)
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                OneShotDelayedBackgroundTimer::TimerImpl                   *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// This implementation is just a small wrapper around a |base::OneShotTimer|.
class OneShotDelayedBackgroundTimer::TimerImpl final
    : public OneShotDelayedBackgroundTimer::OneShotDelayedBackgroundTimerImpl {
 public:
  ~TimerImpl() override = default;
  void Start(const Location& from_here,
             TimeDelta delay,
             OnceCallback<void(MemoryReductionTaskContext)> task) override {
    timer_.Start(
        from_here, delay,
        BindOnce(std::move(task), MemoryReductionTaskContext::kDelayExpired));
  }
  void Stop() override { timer_.Stop(); }
  bool IsRunning() const override { return timer_.IsRunning(); }
  void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override {
    timer_.SetTaskRunner(std::move(task_runner));
  }

 private:
  OneShotTimer timer_;
};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                                TaskImpl                                   *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#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)| is safe here because destroying this
            // will cancel the task. We do not need to worry about race
            // conditions here because destruction should always happen on the
            // same thread that the task is started on.
            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() {
    // This matches the semantics of |OneShotTimer::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() {
#if BUILDFLAG(IS_ANDROID)
  if (android::PreFreezeBackgroundMemoryTrimmer::ShouldUseModernTrim()) {
    impl_ = std::make_unique<TaskImpl>();
  } else {
    impl_ = std::make_unique<TimerImpl>();
  }
#else
  impl_ = std::make_unique<TimerImpl>();
#endif
}

OneShotDelayedBackgroundTimer::~OneShotDelayedBackgroundTimer() {
  Stop();
}

void OneShotDelayedBackgroundTimer::Stop() {
  impl_->Stop();
}

bool OneShotDelayedBackgroundTimer::IsRunning() const {
  return impl_->IsRunning();
}

void OneShotDelayedBackgroundTimer::SetTaskRunner(
    scoped_refptr<SequencedTaskRunner> task_runner) {
  impl_->SetTaskRunner(std::move(task_runner));
}

void OneShotDelayedBackgroundTimer::Start(
    const Location& from_here,
    TimeDelta delay,
    OnceCallback<void(MemoryReductionTaskContext)> task) {
#if BUILDFLAG(IS_ANDROID)
  android::PreFreezeBackgroundMemoryTrimmer::
      RegisterPrivateMemoryFootprintMetric();
#endif
  impl_->Start(from_here, delay, std::move(task));
}

}  // namespace base