1 // Copyright 2015 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ 6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ 7 8 #include "base/base_export.h" 9 #include "base/callback.h" 10 #include "base/macros.h" 11 #include "base/memory/memory_pressure_listener.h" 12 13 namespace base { 14 15 // TODO(chrisha): Make this a concrete class with per-OS implementations rather 16 // than an abstract base class. 17 18 // Declares the interface for a MemoryPressureMonitor. There are multiple 19 // OS specific implementations of this class. An instance of the memory 20 // pressure observer is created at the process level, tracks memory usage, and 21 // pushes memory state change notifications to the static function 22 // base::MemoryPressureListener::NotifyMemoryPressure. This is turn notifies 23 // all MemoryPressureListener instances via a callback. 24 class BASE_EXPORT MemoryPressureMonitor { 25 public: 26 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; 27 using DispatchCallback = base::Callback<void(MemoryPressureLevel level)>; 28 29 virtual ~MemoryPressureMonitor(); 30 31 // Return the singleton MemoryPressureMonitor. 32 static MemoryPressureMonitor* Get(); 33 34 // Record memory pressure UMA statistic. A tick is 5 seconds. 35 static void RecordMemoryPressure(MemoryPressureLevel level, int ticks); 36 37 // Returns the currently observed memory pressure. 38 virtual MemoryPressureLevel GetCurrentPressureLevel() = 0; 39 40 // Sets a notification callback. The default callback invokes 41 // base::MemoryPressureListener::NotifyMemoryPressure. 42 virtual void SetDispatchCallback(const DispatchCallback& callback) = 0; 43 44 protected: 45 MemoryPressureMonitor(); 46 47 private: 48 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); 49 }; 50 51 } // namespace base 52 53 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ 54