memory_pressure_monitor.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_
  5. #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/memory/memory_pressure_listener.h"
  9. #include "base/time/time.h"
  10. namespace base {
  11. // TODO(chrisha): Make this a concrete class with per-OS implementations rather
  12. // than an abstract base class.
  13. // Declares the interface for a MemoryPressureMonitor. There are multiple
  14. // OS specific implementations of this class. An instance of the memory
  15. // pressure observer is created at the process level, tracks memory usage, and
  16. // pushes memory state change notifications to the static function
  17. // base::MemoryPressureListener::NotifyMemoryPressure. This is turn notifies
  18. // all MemoryPressureListener instances via a callback.
  19. class BASE_EXPORT MemoryPressureMonitor {
  20. public:
  21. using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
  22. using DispatchCallback =
  23. base::RepeatingCallback<void(MemoryPressureLevel level)>;
  24. MemoryPressureMonitor(const MemoryPressureMonitor&) = delete;
  25. MemoryPressureMonitor& operator=(const MemoryPressureMonitor&) = delete;
  26. virtual ~MemoryPressureMonitor();
  27. // Return the singleton MemoryPressureMonitor.
  28. static MemoryPressureMonitor* Get();
  29. // Returns the currently observed memory pressure.
  30. virtual MemoryPressureLevel GetCurrentPressureLevel() const = 0;
  31. // Sets a notification callback. The default callback invokes
  32. // base::MemoryPressureListener::NotifyMemoryPressure.
  33. virtual void SetDispatchCallback(const DispatchCallback& callback) = 0;
  34. protected:
  35. MemoryPressureMonitor();
  36. };
  37. } // namespace base
  38. #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_