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

Unified Diff: base/memory/memory_pressure_notifier.cc

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.cc
diff --git a/base/memory/memory_pressure_notifier.cc b/base/memory/memory_pressure_notifier.cc
new file mode 100644
index 0000000000000000000000000000000000000000..96c6cb7aeec0a041909cc9c886c6ba5dbdc5596f
--- /dev/null
+++ b/base/memory/memory_pressure_notifier.cc
@@ -0,0 +1,52 @@
+// 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.
+
+#include "base/memory/memory_pressure_notifier.h"
+
+#include "base/memory/singleton.h"
+
+namespace base {
+
+MemoryPressureNotifier::Listener::Listener(
+ const MemoryPressureNotifier::MemoryPressureCallback& callback)
+ : callback_(callback) {
+ MemoryPressureNotifier::GetInstance()->RegisterListener(this);
+}
+
+MemoryPressureNotifier::Listener::~Listener() {
+ MemoryPressureNotifier::GetInstance()->UnregisterListener(this);
+}
+
+void MemoryPressureNotifier::Listener::Notify(
+ MemoryPressureType memory_pressure_type) {
+ callback_.Run(memory_pressure_type);
+}
+
+// static
+MemoryPressureNotifier* MemoryPressureNotifier::GetInstance() {
+ return Singleton<MemoryPressureNotifier,
+ LeakySingletonTraits<MemoryPressureNotifier> >::get();
+}
+
+MemoryPressureNotifier::MemoryPressureNotifier()
+ : observers_(new ObserverListThreadSafe<Listener>()) {
+}
+
+MemoryPressureNotifier::~MemoryPressureNotifier() {}
+
+void MemoryPressureNotifier::RegisterListener(Listener* listener) {
+ observers_->AddObserver(listener);
+}
+
+void MemoryPressureNotifier::UnregisterListener(Listener* listener) {
+ observers_->RemoveObserver(listener);
+}
+
+void MemoryPressureNotifier::HandleMemoryPressure(
+ MemoryPressureType memory_pressure_type) {
+ observers_->Notify(
+ &MemoryPressureNotifier::Listener::Notify, memory_pressure_type);
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698