1 // Copyright 2014 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.
5 #ifndef ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
6 #define ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
8 #include "athena/athena_export.h"
9 #include "athena/resource_manager/public/resource_manager.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/timer/timer.h"
16 class MemoryPressureNotifierImpl
;
17 class ResourceManagerDelegate
;
19 ////////////////////////////////////////////////////////////////////////////////
20 // MemoryPressureObserver
22 // This observer gets informed once about a |MEMORY_PRESSURE_LOW|. When the
23 // pressure exceeds, the observer will get polled until |MEMORY_PRESSURE_LOW| is
24 // reached again to counter memory consumption.
25 class MemoryPressureObserver
{
27 MemoryPressureObserver() {}
28 virtual ~MemoryPressureObserver() {}
31 virtual void OnMemoryPressure(ResourceManager::MemoryPressure pressure
) = 0;
33 // OS system interface functions. The delegate remains owned by the Observer.
34 virtual ResourceManagerDelegate
* GetDelegate() = 0;
38 ////////////////////////////////////////////////////////////////////////////////
39 // MemoryPressureNotifier
41 // Class to handle the observation of our free memory. It notifies the owner of
42 // memory fill level changes, so that it can take action to reduce memory by
43 // reducing active activities.
45 // The observer will use 3 different fill levels: 50% full, 75% full and 90%
47 class ATHENA_EXPORT MemoryPressureNotifier
{
49 // The creator gets the |listener| object. Note that the ownership of the
50 // listener object remains with the creator.
51 explicit MemoryPressureNotifier(MemoryPressureObserver
* listener
);
52 ~MemoryPressureNotifier();
54 // Stop observing the memory fill level.
55 // May be safely called if StartObserving has not been called.
59 // Starts observing the memory fill level.
60 // Calls to StartObserving should always be matched with calls to
62 void StartObserving();
64 // The function which gets periodically be called to check any changes in the
66 void CheckMemoryPressure();
68 // Converts free percent of memory into a memory pressure value.
69 ResourceManager::MemoryPressure
GetMemoryPressureLevelFromFillLevel(
70 int memory_fill_level
);
72 base::RepeatingTimer
<MemoryPressureNotifier
> timer_
;
74 // The listener which needs to be informed about memory pressure.
75 MemoryPressureObserver
* listener_
;
77 // Our current memory pressure.
78 ResourceManager::MemoryPressure current_pressure_
;
80 DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifier
);
85 #endif // ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_