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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: base/memory/memory_pressure_notifier.h
diff --git a/base/memory/memory_pressure_notifier.h b/base/memory/memory_pressure_notifier.h
new file mode 100644
index 0000000000000000000000000000000000000000..a72546141e57b7d5d0c4cc1f3c9d709b8a636406
--- /dev/null
+++ b/base/memory/memory_pressure_notifier.h
@@ -0,0 +1,93 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// MemoryPressureNotifier provides static APIs for handling memory pressure on
+// platforms that have such signals, such as Android.
+// The app will try to discard buffers that aren't deemed essential (individual
+// modules will implement their own policy).
+//
+// Note that the platform would potentially kill the app in order to free memory
+// that other apps may be requesting.
+// The trade-off here is in then terms of how long does it take to free such
+// memory, and later on potentially re-create it, versus being killed and
+// restarted from cold.
+
+#ifndef BASE_MEMORY_PRESSURE_NOTIFIER_H_
+#define BASE_MEMORY_PRESSURE_NOTIFIER_H_
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/singleton.h"
+#include "base/observer_list_threadsafe.h"
+
+namespace base {
+
+// To start listening, create a new instance, passing a callback to a
+// function that takes an MemoryPressureType parameter. To stop listening,
+// simply delete the listener object. The implementation guarantees
+// that the callback will always be called on the thread that created
+// 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.
+//
+// Example:
+//
+// void OnMemoryPressure(MemoryPressureType memory_pressure_type) {
+// ...
+// }
+//
+// // Start listening.
+// MemoryPressureNotifier::Listener* my_listener =
+// 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
+//
+// ...
+//
+// // Stop listening.
+// delete my_listener
+//
+class BASE_EXPORT MemoryPressureNotifier {
+ public:
+ enum MemoryPressureType {
+#define DEFINE_MEMORY_PRESSURE_TYPE(name, value) name = value,
+#include "base/memory/memory_pressure_handler_list.h"
+};
joth 2013/06/05 19:28:58 indent
bulach 2013/06/06 09:28:30 Done.
+ typedef base::Callback<void(MemoryPressureType)> MemoryPressureCallback;
+
+ class Listener {
+ public:
+ explicit Listener(const MemoryPressureCallback& memory_pressure_callback);
+ ~Listener();
+
+ private:
+ friend class MemoryPressureNotifier;
+
+ void Notify(MemoryPressureType memory_pressure_type);
+
+ MemoryPressureCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(Listener);
+ };
+
+ static MemoryPressureNotifier* GetInstance();
+
+ // Internal use only: called when the platform receives a memory pressure
+ // signal.
+ void HandleMemoryPressure(MemoryPressureType memory_pressure_type);
joth 2013/06/05 19:49:48 nit: Handle -> Notify
bulach 2013/06/06 09:28:30 Done.
+
+ private:
+ friend struct DefaultSingletonTraits<MemoryPressureNotifier>;
+
+ MemoryPressureNotifier();
+ ~MemoryPressureNotifier();
+
+ void RegisterListener(Listener* listener);
+ void UnregisterListener(Listener* listener);
+
+ scoped_refptr<ObserverListThreadSafe<Listener> > observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(MemoryPressureNotifier);
+};
+
+} // namespace base
+
+#endif // BASE_MEMORY_PRESSURE_NOTIFIER_H_

Powered by Google App Engine
This is Rietveld 408576698