Commit 3453c176 authored by bashi's avatar bashi Committed by Commit bot

Change type of MemoryCoordinatorImpl::ChildInfo::memory_state

This CL changes the type of MemoryCoordinatorImpl::ChildInfo::memory_state
from mojom::MemoryState to base::MemoryState. The reason of this change
is to avoid unnecessary type conversion. We are planning to add public
interface of memory coordinator so that chrome/ can access memory state
of each process. Since mojom::MemoryState is only accessible from
content/ we have to convert mojom::MemoryState to base::MemoryState when
we expose memory state outside content/. By storing states as
base::MemoryState we can eliminate type conversion.

BUG=673216

Review-Url: https://codereview.chromium.org/2586873002
Cr-Commit-Position: refs/heads/master@{#439734}
parent d16a6bc2
...@@ -195,9 +195,9 @@ void MemoryCoordinatorImpl::CreateHandle( ...@@ -195,9 +195,9 @@ void MemoryCoordinatorImpl::CreateHandle(
} }
bool MemoryCoordinatorImpl::SetChildMemoryState(int render_process_id, bool MemoryCoordinatorImpl::SetChildMemoryState(int render_process_id,
mojom::MemoryState memory_state) { MemoryState memory_state) {
// Can't set an invalid memory state. // Can't set an invalid memory state.
if (memory_state == mojom::MemoryState::UNKNOWN) if (memory_state == MemoryState::UNKNOWN)
return false; return false;
// Can't send a message to a child that doesn't exist. // Can't send a message to a child that doesn't exist.
...@@ -216,21 +216,21 @@ bool MemoryCoordinatorImpl::SetChildMemoryState(int render_process_id, ...@@ -216,21 +216,21 @@ bool MemoryCoordinatorImpl::SetChildMemoryState(int render_process_id,
return true; return true;
// Can't suspend the given renderer. // Can't suspend the given renderer.
if (memory_state == mojom::MemoryState::SUSPENDED && if (memory_state == MemoryState::SUSPENDED &&
!CanSuspendRenderer(render_process_id)) !CanSuspendRenderer(render_process_id))
return false; return false;
// Update the internal state and send the message. // Update the internal state and send the message.
iter->second.memory_state = memory_state; iter->second.memory_state = memory_state;
iter->second.handle->child()->OnStateChange(memory_state); iter->second.handle->child()->OnStateChange(ToMojomMemoryState(memory_state));
return true; return true;
} }
mojom::MemoryState MemoryCoordinatorImpl::GetChildMemoryState( base::MemoryState MemoryCoordinatorImpl::GetChildMemoryState(
int render_process_id) const { int render_process_id) const {
auto iter = children_.find(render_process_id); auto iter = children_.find(render_process_id);
if (iter == children_.end()) if (iter == children_.end())
return mojom::MemoryState::UNKNOWN; return base::MemoryState::UNKNOWN;
return iter->second.memory_state; return iter->second.memory_state;
} }
...@@ -292,7 +292,7 @@ void MemoryCoordinatorImpl::Observe(int type, ...@@ -292,7 +292,7 @@ void MemoryCoordinatorImpl::Observe(int type,
if (iter == children().end()) if (iter == children().end())
return; return;
iter->second.is_visible = *Details<bool>(details).ptr(); iter->second.is_visible = *Details<bool>(details).ptr();
auto new_state = ToMojomMemoryState(GetGlobalMemoryState()); auto new_state = GetGlobalMemoryState();
SetChildMemoryState(iter->first, new_state); SetChildMemoryState(iter->first, new_state);
} }
...@@ -345,22 +345,22 @@ bool MemoryCoordinatorImpl::CanSuspendRenderer(int render_process_id) { ...@@ -345,22 +345,22 @@ bool MemoryCoordinatorImpl::CanSuspendRenderer(int render_process_id) {
void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) {
// Populate the global state as an initial state of a newly created process. // Populate the global state as an initial state of a newly created process.
auto new_state = ToMojomMemoryState(GetGlobalMemoryState()); auto new_state = GetGlobalMemoryState();
SetChildMemoryState(render_process_id, new_state); SetChildMemoryState(render_process_id, new_state);
} }
mojom::MemoryState MemoryCoordinatorImpl::OverrideGlobalState( base::MemoryState MemoryCoordinatorImpl::OverrideGlobalState(
mojom::MemoryState memory_state, MemoryState memory_state,
const ChildInfo& child) { const ChildInfo& child) {
// We don't suspend foreground renderers. Throttle them instead. // We don't suspend foreground renderers. Throttle them instead.
if (child.is_visible && memory_state == mojom::MemoryState::SUSPENDED) if (child.is_visible && memory_state == MemoryState::SUSPENDED)
return mojom::MemoryState::THROTTLED; return MemoryState::THROTTLED;
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// On Android, we throttle background renderers immediately. // On Android, we throttle background renderers immediately.
// TODO(bashi): Create a specialized class of MemoryCoordinator for Android // TODO(bashi): Create a specialized class of MemoryCoordinator for Android
// and move this ifdef to the class. // and move this ifdef to the class.
if (!child.is_visible && memory_state == mojom::MemoryState::NORMAL) if (!child.is_visible && memory_state == MemoryState::NORMAL)
return mojom::MemoryState::THROTTLED; return MemoryState::THROTTLED;
// TODO(bashi): Suspend background renderers after a certain period of time. // TODO(bashi): Suspend background renderers after a certain period of time.
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
return memory_state; return memory_state;
...@@ -380,7 +380,7 @@ void MemoryCoordinatorImpl::CreateChildInfoMapEntry( ...@@ -380,7 +380,7 @@ void MemoryCoordinatorImpl::CreateChildInfoMapEntry(
// We'll set renderer's memory state to the current global state when the // We'll set renderer's memory state to the current global state when the
// corresponding renderer process is ready to communicate. Renderer processes // corresponding renderer process is ready to communicate. Renderer processes
// call AddChild() when they are ready. // call AddChild() when they are ready.
child_info.memory_state = mojom::MemoryState::NORMAL; child_info.memory_state = MemoryState::NORMAL;
child_info.is_visible = true; child_info.is_visible = true;
child_info.handle = std::move(handle); child_info.handle = std::move(handle);
} }
...@@ -391,11 +391,10 @@ void MemoryCoordinatorImpl::NotifyStateToClients() { ...@@ -391,11 +391,10 @@ void MemoryCoordinatorImpl::NotifyStateToClients() {
} }
void MemoryCoordinatorImpl::NotifyStateToChildren() { void MemoryCoordinatorImpl::NotifyStateToChildren() {
auto mojo_state = ToMojomMemoryState(current_state_);
// It's OK to call SetChildMemoryState() unconditionally because it checks // It's OK to call SetChildMemoryState() unconditionally because it checks
// whether this state transition is valid. // whether this state transition is valid.
for (auto& iter : children()) for (auto& iter : children())
SetChildMemoryState(iter.first, mojo_state); SetChildMemoryState(iter.first, current_state_);
} }
void MemoryCoordinatorImpl::RecordStateChange(MemoryState prev_state, void MemoryCoordinatorImpl::RecordStateChange(MemoryState prev_state,
......
...@@ -57,12 +57,11 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver, ...@@ -57,12 +57,11 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
// Dispatches a memory state change to the provided process. Returns true if // Dispatches a memory state change to the provided process. Returns true if
// the process is tracked by this coordinator and successfully dispatches, // the process is tracked by this coordinator and successfully dispatches,
// returns false otherwise. // returns false otherwise.
bool SetChildMemoryState( bool SetChildMemoryState(int render_process_id, MemoryState memory_state);
int render_process_id, mojom::MemoryState memory_state);
// Returns the memory state of the specified render process. Returns UNKNOWN // Returns the memory state of the specified render process. Returns UNKNOWN
// if the process is not tracked by this coordinator. // if the process is not tracked by this coordinator.
mojom::MemoryState GetChildMemoryState(int render_process_id) const; MemoryState GetChildMemoryState(int render_process_id) const;
// Records memory pressure notifications. Called by MemoryPressureMonitor. // Records memory pressure notifications. Called by MemoryPressureMonitor.
// TODO(bashi): Remove this when MemoryPressureMonitor is retired. // TODO(bashi): Remove this when MemoryPressureMonitor is retired.
...@@ -115,7 +114,7 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver, ...@@ -115,7 +114,7 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
ChildInfo(const ChildInfo& rhs); ChildInfo(const ChildInfo& rhs);
~ChildInfo(); ~ChildInfo();
mojom::MemoryState memory_state; MemoryState memory_state;
bool is_visible = false; bool is_visible = false;
std::unique_ptr<MemoryCoordinatorHandleImpl> handle; std::unique_ptr<MemoryCoordinatorHandleImpl> handle;
}; };
...@@ -146,8 +145,8 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver, ...@@ -146,8 +145,8 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
// Called by SetChildMemoryState() to determine a child memory state based on // Called by SetChildMemoryState() to determine a child memory state based on
// the current status of the child process. // the current status of the child process.
mojom::MemoryState OverrideGlobalState(mojom::MemoryState memroy_state, MemoryState OverrideGlobalState(MemoryState memroy_state,
const ChildInfo& child); const ChildInfo& child);
void SetDelegateForTesting( void SetDelegateForTesting(
std::unique_ptr<MemoryCoordinatorDelegate> delegate); std::unique_ptr<MemoryCoordinatorDelegate> delegate);
......
...@@ -116,7 +116,7 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { ...@@ -116,7 +116,7 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
// Wrapper of MemoryCoordinator::SetMemoryState that also calls RunUntilIdle. // Wrapper of MemoryCoordinator::SetMemoryState that also calls RunUntilIdle.
bool SetChildMemoryState( bool SetChildMemoryState(
int render_process_id, mojom::MemoryState memory_state) { int render_process_id, MemoryState memory_state) {
bool result = MemoryCoordinatorImpl::SetChildMemoryState( bool result = MemoryCoordinatorImpl::SetChildMemoryState(
render_process_id, memory_state); render_process_id, memory_state);
RunUntilIdle(); RunUntilIdle();
...@@ -130,6 +130,8 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { ...@@ -130,6 +130,8 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
class MemoryCoordinatorImplTest : public testing::Test { class MemoryCoordinatorImplTest : public testing::Test {
public: public:
using MemoryState = base::MemoryState;
void SetUp() override { void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator); scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator);
...@@ -168,7 +170,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateFailsInvalidState) { ...@@ -168,7 +170,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateFailsInvalidState) {
auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1); auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1);
EXPECT_FALSE( EXPECT_FALSE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::UNKNOWN)); coordinator_->SetChildMemoryState(1, MemoryState::UNKNOWN));
EXPECT_EQ(0, cmc1->on_state_change_calls()); EXPECT_EQ(0, cmc1->on_state_change_calls());
} }
...@@ -176,7 +178,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateFailsInvalidRenderer) { ...@@ -176,7 +178,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateFailsInvalidRenderer) {
auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1); auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1);
EXPECT_FALSE( EXPECT_FALSE(
coordinator_->SetChildMemoryState(2, mojom::MemoryState::THROTTLED)); coordinator_->SetChildMemoryState(2, MemoryState::THROTTLED));
EXPECT_EQ(0, cmc1->on_state_change_calls()); EXPECT_EQ(0, cmc1->on_state_change_calls());
} }
...@@ -184,7 +186,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateNotDeliveredNop) { ...@@ -184,7 +186,7 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateNotDeliveredNop) {
auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1); auto cmc1 = coordinator_->CreateChildMemoryCoordinator(1);
EXPECT_FALSE( EXPECT_FALSE(
coordinator_->SetChildMemoryState(2, mojom::MemoryState::NORMAL)); coordinator_->SetChildMemoryState(2, MemoryState::NORMAL));
EXPECT_EQ(0, cmc1->on_state_change_calls()); EXPECT_EQ(0, cmc1->on_state_change_calls());
} }
...@@ -193,12 +195,12 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateDelivered) { ...@@ -193,12 +195,12 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateDelivered) {
auto cmc2 = coordinator_->CreateChildMemoryCoordinator(2); auto cmc2 = coordinator_->CreateChildMemoryCoordinator(2);
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::THROTTLED)); coordinator_->SetChildMemoryState(1, MemoryState::THROTTLED));
EXPECT_EQ(1, cmc1->on_state_change_calls()); EXPECT_EQ(1, cmc1->on_state_change_calls());
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc1->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc1->state());
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(2, mojom::MemoryState::SUSPENDED)); coordinator_->SetChildMemoryState(2, MemoryState::SUSPENDED));
EXPECT_EQ(1, cmc2->on_state_change_calls()); EXPECT_EQ(1, cmc2->on_state_change_calls());
// Child processes are considered as visible (foreground) by default, // Child processes are considered as visible (foreground) by default,
// and visible ones won't be suspended but throttled. // and visible ones won't be suspended but throttled.
...@@ -212,28 +214,28 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) { ...@@ -212,28 +214,28 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) {
// Foreground // Foreground
iter->second.is_visible = true; iter->second.is_visible = true;
EXPECT_TRUE(coordinator_->SetChildMemoryState(1, mojom::MemoryState::NORMAL)); EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL));
EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state()); EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state());
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::THROTTLED)); coordinator_->SetChildMemoryState(1, MemoryState::THROTTLED));
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::SUSPENDED)); coordinator_->SetChildMemoryState(1, MemoryState::SUSPENDED));
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
// Background // Background
iter->second.is_visible = false; iter->second.is_visible = false;
EXPECT_TRUE(coordinator_->SetChildMemoryState(1, mojom::MemoryState::NORMAL)); EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL));
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
#else #else
EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state()); EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state());
#endif #endif
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::THROTTLED)); coordinator_->SetChildMemoryState(1, MemoryState::THROTTLED));
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, mojom::MemoryState::SUSPENDED)); coordinator_->SetChildMemoryState(1, MemoryState::SUSPENDED));
EXPECT_EQ(mojom::MemoryState::SUSPENDED, cmc->state()); EXPECT_EQ(mojom::MemoryState::SUSPENDED, cmc->state());
} }
......
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