Commit efbf745f authored by bashi's avatar bashi Committed by Commit bot

memory coordinator: Observe renderer visibility to update state

Memory coordinator should update renderers' memory state when their
visibility have changed. For a visible (foreground) renderer,
the state is set to NORMAL. For background renderers, the states
are set to the global state.

BUG=617492

Review-Url: https://codereview.chromium.org/2443093002
Cr-Commit-Position: refs/heads/master@{#427185}
parent 07aa6dca
......@@ -6,6 +6,10 @@
#include "base/threading/thread_task_runner_handle.h"
#include "content/browser/memory/memory_monitor.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/common/content_features.h"
namespace content {
......@@ -94,6 +98,10 @@ void MemoryCoordinatorImpl::Start() {
DCHECK(CalledOnValidThread());
DCHECK(last_state_change_.is_null());
DCHECK(ValidateParameters());
notification_registrar_.Add(
this, NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
NotificationService::AllBrowserContextsAndSources());
ScheduleUpdateState(base::TimeDelta());
}
......@@ -106,6 +114,24 @@ base::MemoryState MemoryCoordinatorImpl::GetCurrentMemoryState() const {
return current_state_;
}
void MemoryCoordinatorImpl::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED);
RenderWidgetHost* render_widget_host = Source<RenderWidgetHost>(source).ptr();
RenderProcessHost* process = render_widget_host->GetProcess();
if (!process)
return;
auto iter = children().find(process->GetID());
if (iter == children().end())
return;
bool is_visible = *Details<bool>(details).ptr();
// We don't throttle/suspend a visible renderer for now.
auto new_state = is_visible ? mojom::MemoryState::NORMAL
: ToMojomMemoryState(current_state_);
SetMemoryState(iter->first, new_state);
}
base::MemoryState MemoryCoordinatorImpl::CalculateNextState() {
using MemoryState = base::MemoryState;
......@@ -174,9 +200,6 @@ void MemoryCoordinatorImpl::NotifyStateToChildren() {
auto mojo_state = ToMojomMemoryState(current_state_);
// It's OK to call SetMemoryState() unconditionally because it checks whether
// this state transition is valid.
// TODO(bashi): In SUSPENDED state, we should update children's state
// accordingly when the foreground tab is changed (e.g. resume a renderer
// which becomes foreground and suspend a renderer which goes to background).
for (auto& iter : children())
SetMemoryState(iter.first, mojo_state);
}
......
......@@ -12,6 +12,8 @@
#include "base/threading/non_thread_safe.h"
#include "base/time/time.h"
#include "content/browser/memory/memory_coordinator.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
namespace content {
......@@ -35,6 +37,7 @@ struct MemoryCoordinatorSingletonTraits;
// back to a relaxed state. (e.g. THROTTLED -> NORMAL)
// * Once a state is changed, it remains the same for a certain period of time.
class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator,
public NotificationObserver,
public base::NonThreadSafe {
public:
MemoryCoordinatorImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
......@@ -49,6 +52,11 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator,
base::MemoryState GetCurrentMemoryState() const override;
// NotificationObserver implementation:
void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) override;
private:
FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextState);
FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateState);
......@@ -76,6 +84,7 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public MemoryCoordinator,
void ScheduleUpdateState(base::TimeDelta delay);
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
NotificationRegistrar notification_registrar_;
std::unique_ptr<MemoryMonitor> memory_monitor_;
base::Closure update_state_callback_;
base::MemoryState current_state_ = MemoryState::NORMAL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment