Index: content/browser/memory/memory_pressure_controller.cc |
diff --git a/content/browser/memory/memory_pressure_controller.cc b/content/browser/memory/memory_pressure_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..837459fd3203024506e70fee9ce9e2ae22452595 |
--- /dev/null |
+++ b/content/browser/memory/memory_pressure_controller.cc |
@@ -0,0 +1,69 @@ |
+// Copyright 2015 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 "content/browser/memory/memory_pressure_controller.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/memory_pressure_listener.h" |
+#include "content/browser/memory/memory_message_filter.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace content { |
+ |
+MemoryPressureController::MemoryPressureController() {} |
+ |
+MemoryPressureController::~MemoryPressureController() {} |
+ |
+void MemoryPressureController::OnMemoryMessageFilterAdded( |
+ MemoryMessageFilter* filter) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
You can use just DCHECK_CURRENTLY_ON macro
petrcermak
2015/09/18 10:19:12
Done.
|
+ DCHECK(memory_message_filters_.find(filter) == memory_message_filters_.end()); |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
You could make this a bit faster on debug builds b
petrcermak
2015/09/18 10:19:12
Done.
|
+ |
+ memory_message_filters_.insert(filter); |
+ |
+ // There's no need to send a message to the child process if memory pressure |
+ // notifications are not suppressed. |
+ if (base::MemoryPressureListener::AreNotificationsSuppressed()) |
+ filter->SendSetPressureNotificationsSuppressed(true); |
+} |
+ |
+void MemoryPressureController::OnMemoryMessageFilterRemoved( |
+ MemoryMessageFilter* filter) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(memory_message_filters_.find(filter) != memory_message_filters_.end()); |
Primiano Tucci (use gerrit)
2015/09/17 18:23:49
just use count() here, should be slighly faster
petrcermak
2015/09/18 10:19:12
I changed this to use the return value of std::set
|
+ |
+ memory_message_filters_.erase(filter); |
+} |
+ |
+// static |
+MemoryPressureController* MemoryPressureController::GetInstance() { |
+ return base::Singleton< |
+ MemoryPressureController, |
+ base::LeakySingletonTraits<MemoryPressureController>>::get(); |
+} |
+ |
+void MemoryPressureController::SetPressureNotificationsSuppressedInAllProcesses( |
+ bool suppressed) { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ // Note that passing base::Unretained(this) is safe here because the |
+ // controller is a leaky singleton (i.e. it is never deleted). |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&MemoryPressureController:: |
+ SetPressureNotificationsSuppressedInAllProcesses, |
+ base::Unretained(this), suppressed)); |
+ return; |
+ } |
+ |
+ // Enable/disable suppressing memory notifications in the browser process. |
+ base::MemoryPressureListener::SetNotificationsSuppressed(suppressed); |
+ |
+ // Enable/disable suppressing memory notifications in all child processes. |
+ for (MemoryMessageFilterSet::iterator it = memory_message_filters_.begin(); |
+ it != memory_message_filters_.end(); ++it) { |
+ it->get()->SendSetPressureNotificationsSuppressed(suppressed); |
+ } |
+} |
+ |
+} // namespace content |