Chromium Code Reviews| 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..c0f1742f6266b06783534d1bf0d1e634031c438b |
| --- /dev/null |
| +++ b/content/browser/memory/memory_pressure_controller.cc |
| @@ -0,0 +1,67 @@ |
| +// 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)); |
| + DCHECK(memory_message_filters_.find(filter) == memory_message_filters_.end()); |
| + |
| + 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()); |
| + |
| + 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)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&MemoryPressureController:: |
| + SetPressureNotificationsSuppressedInAllProcesses, |
| + base::Unretained(this), suppressed)); |
|
nasko
2015/09/16 21:18:37
Please add a comment why base::Unretained(this) is
petrcermak
2015/09/17 15:24:53
Done.
|
| + 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 |