Index: content/browser/memory/memory_message_filter.cc |
diff --git a/content/browser/memory/memory_message_filter.cc b/content/browser/memory/memory_message_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..278e85392767c3183fbf7d3f525e9a2869b289ee |
--- /dev/null |
+++ b/content/browser/memory/memory_message_filter.cc |
@@ -0,0 +1,79 @@ |
+// 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_message_filter.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/memory_pressure_listener.h" |
+#include "content/common/memory_messages.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace content { |
+namespace memory { |
+ |
+MemoryMessageFilter::MemoryMessageFilter() |
+ : BrowserMessageFilter(MemoryMsgStart) {} |
+ |
+MemoryMessageFilter::~MemoryMessageFilter() {} |
+ |
+void MemoryMessageFilter::OnFilterAdded(IPC::Sender* sender) { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(&MemoryMessageFilter::OnFilterAdded, |
+ base::Unretained(this), sender)); |
+ return; |
+ } |
+ memory_message_filters_.Get().insert(this); |
+ if (base::MemoryPressureListener::AreNotificationsSuppressed()) |
+ SendSetNotificationsSuppressed(true); |
+} |
+ |
+void MemoryMessageFilter::OnFilterRemoved() { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(&MemoryMessageFilter::OnFilterRemoved, |
+ base::Unretained(this))); |
+ return; |
+ } |
+ memory_message_filters_.Get().erase(this); |
+} |
+ |
+bool MemoryMessageFilter::OnMessageReceived(const IPC::Message& message) { |
+ return false; |
+} |
+ |
+// static |
+base::LazyInstance<MemoryMessageFilter::MemoryMessageFilterSet>::Leaky |
+ MemoryMessageFilter::memory_message_filters_ = LAZY_INSTANCE_INITIALIZER; |
+ |
+// static |
+void MemoryMessageFilter::SendSetNotificationsSuppressedToAllProcesses( |
+ bool suppressed) { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind( |
+ &MemoryMessageFilter::SendSetNotificationsSuppressedToAllProcesses, |
+ suppressed)); |
+ return; |
+ } |
+ |
+ // Enable/disable suppressing memory notifications in the browser process. |
+ base::MemoryPressureListener::SetNotificationsSuppressed(suppressed); |
+ |
+ // Enable/disable suppressing memory notifications in all child processes. |
+ MemoryMessageFilterSet& filters = memory_message_filters_.Get(); |
+ for (MemoryMessageFilterSet::iterator it = filters.begin(); |
+ it != filters.end(); ++it) { |
+ it->get()->SendSetNotificationsSuppressed(suppressed); |
+ } |
+} |
+ |
+void MemoryMessageFilter::SendSetNotificationsSuppressed(bool suppressed) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Send(new MemoryMsg_SetNotificationsSuppressed(suppressed)); |
+} |
+ |
+} // namespace memory |
+} // namespace content |