Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / athena / resource_manager / memory_pressure_notifier.h
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.
4
5 #ifndef ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
6 #define ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_
7
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"
13
14 namespace athena {
15
16 class MemoryPressureNotifierImpl;
17 class ResourceManagerDelegate;
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // MemoryPressureObserver
21 //
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 {
26  public:
27   MemoryPressureObserver() {}
28   virtual ~MemoryPressureObserver() {}
29
30   // The observer.
31   virtual void OnMemoryPressure(ResourceManager::MemoryPressure pressure) = 0;
32
33   // OS system interface functions. The delegate remains owned by the Observer.
34   virtual ResourceManagerDelegate* GetDelegate() = 0;
35 };
36
37
38 ////////////////////////////////////////////////////////////////////////////////
39 // MemoryPressureNotifier
40 //
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.
44 //
45 // The observer will use 3 different fill levels: 50% full, 75% full and 90%
46 // full.
47 class ATHENA_EXPORT MemoryPressureNotifier {
48  public:
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();
53
54   // Stop observing the memory fill level.
55   // May be safely called if StartObserving has not been called.
56   void StopObserving();
57
58  private:
59   // Starts observing the memory fill level.
60   // Calls to StartObserving should always be matched with calls to
61   // StopObserving.
62   void StartObserving();
63
64   // The function which gets periodically be called to check any changes in the
65   // memory pressure.
66   void CheckMemoryPressure();
67
68   // Converts free percent of memory into a memory pressure value.
69   ResourceManager::MemoryPressure GetMemoryPressureLevelFromFillLevel(
70       int memory_fill_level);
71
72   base::RepeatingTimer<MemoryPressureNotifier> timer_;
73
74   // The listener which needs to be informed about memory pressure.
75   MemoryPressureObserver* listener_;
76
77   // Our current memory pressure.
78   ResourceManager::MemoryPressure current_pressure_;
79
80   DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifier);
81 };
82
83 }  // namespace athena
84
85 #endif  // ATHENA_RESOURCE_MANAGER_MEMORY_PRESSURE_NOTIFIER_H_