chromium/chrome/browser/performance_manager/policies/urgent_page_discarding_policy.cc

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

#include "chrome/browser/performance_manager/policies/urgent_page_discarding_policy.h"

#include <memory>

#include "base/feature_list.h"
#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"
#include "chrome/browser/performance_manager/policies/policy_features.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chrome/browser/lacros/lacros_memory_pressure_evaluator.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chromeos/ash/components/memory/pressure/system_memory_pressure_evaluator.h"
#endif

namespace performance_manager::policies {

namespace {

#if BUILDFLAG(IS_CHROMEOS)
std::optional<memory_pressure::ReclaimTarget> GetReclaimTarget() {
  std::optional<memory_pressure::ReclaimTarget> reclaim_target = std::nullopt;
#if BUILDFLAG(IS_CHROMEOS_LACROS)
  auto* evaluator = LacrosMemoryPressureEvaluator::Get();
#elif BUILDFLAG(IS_CHROMEOS_ASH)
  auto* evaluator = ash::memory::SystemMemoryPressureEvaluator::Get();
#endif
  if (evaluator) {
    reclaim_target = evaluator->GetCachedReclaimTarget();
  }
  return reclaim_target;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

UrgentPageDiscardingPolicy::UrgentPageDiscardingPolicy() = default;
UrgentPageDiscardingPolicy::~UrgentPageDiscardingPolicy() = default;

void UrgentPageDiscardingPolicy::OnPassedToGraph(Graph* graph) {}

void UrgentPageDiscardingPolicy::OnTakenFromGraph(Graph* graph) {}

#if BUILDFLAG(IS_CHROMEOS)
void UrgentPageDiscardingPolicy::OnReclaimTarget(
    std::optional<memory_pressure::ReclaimTarget> reclaim_target) {
  PageDiscardingHelper::GetFromGraph(GetOwningGraph())
      ->DiscardMultiplePages(
          reclaim_target, true,
          base::BindOnce(
              [](UrgentPageDiscardingPolicy* policy, bool success_unused) {
                DCHECK(policy->handling_memory_pressure_notification_);
                policy->handling_memory_pressure_notification_ = false;
              },
              base::Unretained(this)),
          PageDiscardingHelper::DiscardReason::URGENT);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void UrgentPageDiscardingPolicy::OnMemoryPressure(
    base::MemoryPressureListener::MemoryPressureLevel new_level) {}

}  // namespace performance_manager::policies