Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: base/memory/memory_pressure_notifier.h

Issue 15995014: Adds MemoryPressureListener. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Joth's comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 // MemoryPressureNotifier provides static APIs for handling memory pressure on
6 // platforms that have such signals, such as Android.
7 // The app will try to discard buffers that aren't deemed essential (individual
8 // modules will implement their own policy).
9 //
10 // Note that the platform would potentially kill the app in order to free memory
11 // that other apps may be requesting.
12 // The trade-off here is in then terms of how long does it take to free such
13 // memory, and later on potentially re-create it, versus being killed and
14 // restarted from cold.
15
16 #ifndef BASE_MEMORY_PRESSURE_NOTIFIER_H_
17 #define BASE_MEMORY_PRESSURE_NOTIFIER_H_
18
19 #include "base/base_export.h"
20 #include "base/basictypes.h"
21 #include "base/memory/ref_counted.h"
22 #include "base/memory/singleton.h"
23 #include "base/observer_list_threadsafe.h"
24
25 namespace base {
26
27 // To start listening, create a new instance, passing a callback to a
28 // function that takes an MemoryPressureType parameter. To stop listening,
29 // simply delete the listener object. The implementation guarantees
30 // that the callback will always be called on the thread that created
31 // the listener.
joth 2013/06/05 19:49:48 I'd like to add some note that if this is the same
bulach 2013/06/06 09:28:30 added almost verbatim.
32 //
33 // Example:
34 //
35 // void OnMemoryPressure(MemoryPressureType memory_pressure_type) {
36 // ...
37 // }
38 //
39 // // Start listening.
40 // MemoryPressureNotifier::Listener* my_listener =
41 // new MemoryPressureNotifier::Listener(base::Bind(&OnMemoryPressure));
joth 2013/06/05 19:49:48 oh right! I forgot the pattern used in ActivitySta
bulach 2013/06/06 09:28:30 since this is based on the existing "ActivityStatu
42 //
43 // ...
44 //
45 // // Stop listening.
46 // delete my_listener
47 //
48 class BASE_EXPORT MemoryPressureNotifier {
49 public:
50 enum MemoryPressureType {
51 #define DEFINE_MEMORY_PRESSURE_TYPE(name, value) name = value,
52 #include "base/memory/memory_pressure_handler_list.h"
53 };
joth 2013/06/05 19:28:58 indent
bulach 2013/06/06 09:28:30 Done.
54 typedef base::Callback<void(MemoryPressureType)> MemoryPressureCallback;
55
56 class Listener {
57 public:
58 explicit Listener(const MemoryPressureCallback& memory_pressure_callback);
59 ~Listener();
60
61 private:
62 friend class MemoryPressureNotifier;
63
64 void Notify(MemoryPressureType memory_pressure_type);
65
66 MemoryPressureCallback callback_;
67
68 DISALLOW_COPY_AND_ASSIGN(Listener);
69 };
70
71 static MemoryPressureNotifier* GetInstance();
72
73 // Internal use only: called when the platform receives a memory pressure
74 // signal.
75 void HandleMemoryPressure(MemoryPressureType memory_pressure_type);
joth 2013/06/05 19:49:48 nit: Handle -> Notify
bulach 2013/06/06 09:28:30 Done.
76
77 private:
78 friend struct DefaultSingletonTraits<MemoryPressureNotifier>;
79
80 MemoryPressureNotifier();
81 ~MemoryPressureNotifier();
82
83 void RegisterListener(Listener* listener);
84 void UnregisterListener(Listener* listener);
85
86 scoped_refptr<ObserverListThreadSafe<Listener> > observers_;
87
88 DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifier);
89 };
90
91 } // namespace base
92
93 #endif // BASE_MEMORY_PRESSURE_NOTIFIER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698