Index: components/memory_coordinator/browser/memory_coordinator.cc |
diff --git a/components/memory_coordinator/browser/memory_coordinator.cc b/components/memory_coordinator/browser/memory_coordinator.cc |
index 634ebf809054e6098c15a500d690643f0cd4fccf..b4e714ab731798c73d0b27712b3ae1a1442356a6 100644 |
--- a/components/memory_coordinator/browser/memory_coordinator.cc |
+++ b/components/memory_coordinator/browser/memory_coordinator.cc |
@@ -4,6 +4,8 @@ |
#include "components/memory_coordinator/browser/memory_coordinator.h" |
+#include "base/memory/memory_pressure_listener.h" |
+ |
namespace memory_coordinator { |
// The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom |
@@ -31,9 +33,23 @@ class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { |
DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); |
}; |
-MemoryCoordinator::MemoryCoordinator() {} |
+MemoryCoordinator::MemoryCoordinator() |
+ : pressure_level_dispatcher_( |
+ base::Bind(&MemoryCoordinator::OnMemoryPressure, |
+ base::Unretained(this))) { |
+ auto* monitor = base::MemoryPressureMonitor::Get(); |
+ if (monitor) { |
+ monitor->SetDispatchCallback(pressure_level_dispatcher_); |
+ } |
+} |
-MemoryCoordinator::~MemoryCoordinator() {} |
+MemoryCoordinator::~MemoryCoordinator() { |
+ auto* monitor = base::MemoryPressureMonitor::Get(); |
+ if (monitor) { |
+ monitor->SetDispatchCallback( |
+ base::Bind(&base::MemoryPressureListener::NotifyMemoryPressure)); |
+ } |
+} |
void MemoryCoordinator::CreateHandle( |
int render_process_id, |
@@ -53,4 +69,19 @@ void MemoryCoordinator::OnConnectionError(int render_process_id) { |
children_.erase(render_process_id); |
} |
+void MemoryCoordinator::OnMemoryPressure( |
+ base::MemoryPressureMonitor::MemoryPressureLevel level) { |
+ // TODO(bashi): The current implementation just translates memory pressure |
+ // levels to memory coordinator states. The logic will be replaced with |
+ // the priority tracker. |
+ if (level == base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) { |
+ clients()->Notify(FROM_HERE, &MemoryCoordinatorClient::OnMemoryStateChange, |
+ mojom::MemoryState::THROTTLED); |
+ } else if (level == |
+ base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
+ clients()->Notify(FROM_HERE, &MemoryCoordinatorClient::OnMemoryStateChange, |
+ mojom::MemoryState::SUSPENDED); |
+ } |
+} |
+ |
} // namespace memory_coordinator |