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

Unified Diff: base/memory/memory_pressure_handler.h

Issue 15995014: Adds MemoryPressureListener. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Hooks up with MemoryPurger 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_handler.h
diff --git a/base/memory/memory_pressure_handler.h b/base/memory/memory_pressure_handler.h
new file mode 100644
index 0000000000000000000000000000000000000000..e3a0f77d6e9f442ed384bc71c54800b3cbf1d074
--- /dev/null
+++ b/base/memory/memory_pressure_handler.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.
+
+// MemoryPressureHandler provides static APIs for handling memory pressure on
+// platforms that have such signals, such as Android.
+// Chrome will try to discard buffers that aren't deemed essential (individual
+// modules will implement their own policy).
+//
+// Note that the platform would potentially kill chrome 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_HANDLER_H_
+#define BASE_MEMORY_PRESSURE_HANDLER_H_
joth 2013/06/05 16:51:23 nit: don't like the Handler name as it doesn't han
bulach 2013/06/05 19:02:11 Done.
+
+#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.
+//
+// Example:
+//
+// void OnMemoryPressure(MemoryPressureType memory_pressure_type) {
+// ...
+// }
+//
+// // Start listening.
+// MemoryPressureHandler::Listener* my_listener =
+// new MemoryPressureHandler::Listener(base::Bind(&OnMemoryPressure));
+//
+// ...
+//
+// // Stop listening.
+// delete my_listener
+//
+class BASE_EXPORT MemoryPressureHandler {
+ public:
+ enum MemoryPressureType {
+#define DEFINE_MEMORY_PRESSURE_TYPE(name, value) name = value,
+#include "base/memory/memory_pressure_handler_list.h"
+};
+ typedef base::Callback<void(MemoryPressureType)> MemoryPressureCallback;
+
+ class Listener {
+ public:
+ explicit Listener(const MemoryPressureCallback& memory_pressure_callback);
+ ~Listener();
+
+ private:
+ friend class MemoryPressureHandler;
+
+ void Notify(MemoryPressureType memory_pressure_type);
+
+ MemoryPressureCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(Listener);
+ };
+
+ static MemoryPressureHandler* GetInstance();
+
+ // Internal use only: called when the platform receives a memory pressure
+ // signal.
+ void HandleMemoryPressure(MemoryPressureType memory_pressure_type);
+
+ private:
+ friend struct DefaultSingletonTraits<MemoryPressureHandler>;
+
+ MemoryPressureHandler();
+ ~MemoryPressureHandler();
+
+ void RegisterListener(Listener* listener);
+ void UnregisterListener(Listener* listener);
+
+ scoped_refptr<ObserverListThreadSafe<Listener> > observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(MemoryPressureHandler);
+};
+
+} // namespace base
+
+#endif // BASE_MEMORY_PRESSURE_HANDLER_H_

Powered by Google App Engine
This is Rietveld 408576698