OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/memory/memory_coordinator.h" | 5 #include "content/browser/memory/memory_coordinator.h" |
6 | 6 |
7 #include "base/memory/memory_coordinator_client_registry.h" | 7 #include "base/memory/memory_coordinator_client_registry.h" |
| 8 #include "content/public/browser/render_process_host.h" |
8 #include "content/public/common/content_features.h" | 9 #include "content/public/common/content_features.h" |
9 | 10 |
10 namespace content { | 11 namespace content { |
11 | 12 |
| 13 // static |
| 14 void MemoryCoordinatorDelegate::Set( |
| 15 const base::WeakPtr<MemoryCoordinatorDelegate>& delegate) { |
| 16 auto* coordinator = MemoryCoordinator::GetInstance(); |
| 17 if (coordinator) |
| 18 coordinator->SetDelegate(delegate); |
| 19 } |
| 20 |
12 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom | 21 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom |
13 // for the role of this class. | 22 // for the role of this class. |
14 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | 23 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { |
15 public: | 24 public: |
16 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) | 25 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) |
17 : binding_(this, std::move(request)) { | 26 : binding_(this, std::move(request)) { |
18 } | 27 } |
19 | 28 |
20 // mojom::MemoryCoordinatorHandle: | 29 // mojom::MemoryCoordinatorHandle: |
21 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { | 30 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 77 |
69 // Can't send a message to a child that doesn't exist. | 78 // Can't send a message to a child that doesn't exist. |
70 auto iter = children_.find(render_process_id); | 79 auto iter = children_.find(render_process_id); |
71 if (iter == children_.end()) | 80 if (iter == children_.end()) |
72 return false; | 81 return false; |
73 | 82 |
74 // A nop doesn't need to be sent, but is considered successful. | 83 // A nop doesn't need to be sent, but is considered successful. |
75 if (iter->second.memory_state == memory_state) | 84 if (iter->second.memory_state == memory_state) |
76 return true; | 85 return true; |
77 | 86 |
| 87 // Can't suspend the given renderer. |
| 88 if (memory_state == mojom::MemoryState::SUSPENDED && |
| 89 !CanSuspendRenderer(render_process_id)) |
| 90 return false; |
| 91 |
78 // Update the internal state and send the message. | 92 // Update the internal state and send the message. |
79 iter->second.memory_state = memory_state; | 93 iter->second.memory_state = memory_state; |
80 iter->second.handle->child()->OnStateChange(memory_state); | 94 iter->second.handle->child()->OnStateChange(memory_state); |
81 return true; | 95 return true; |
82 } | 96 } |
83 | 97 |
84 mojom::MemoryState MemoryCoordinator::GetMemoryState( | 98 mojom::MemoryState MemoryCoordinator::GetMemoryState( |
85 int render_process_id) const { | 99 int render_process_id) const { |
86 auto iter = children_.find(render_process_id); | 100 auto iter = children_.find(render_process_id); |
87 if (iter == children_.end()) | 101 if (iter == children_.end()) |
88 return mojom::MemoryState::UNKNOWN; | 102 return mojom::MemoryState::UNKNOWN; |
89 return iter->second.memory_state; | 103 return iter->second.memory_state; |
90 } | 104 } |
91 | 105 |
92 void MemoryCoordinator::AddChildForTesting( | 106 void MemoryCoordinator::AddChildForTesting( |
93 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { | 107 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { |
94 mojom::MemoryCoordinatorHandlePtr mch; | 108 mojom::MemoryCoordinatorHandlePtr mch; |
95 auto request = mojo::GetProxy(&mch); | 109 auto request = mojo::GetProxy(&mch); |
96 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 110 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
97 new MemoryCoordinatorHandleImpl(std::move(request))); | 111 new MemoryCoordinatorHandleImpl(std::move(request))); |
98 handle->AddChild(std::move(child)); | 112 handle->AddChild(std::move(child)); |
99 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 113 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); |
100 } | 114 } |
101 | 115 |
102 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 116 void MemoryCoordinator::OnConnectionError(int render_process_id) { |
103 children_.erase(render_process_id); | 117 children_.erase(render_process_id); |
104 } | 118 } |
105 | 119 |
| 120 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { |
| 121 // If there is no delegate (i.e. tests), renderers are always suspendable. |
| 122 if (!delegate_) |
| 123 return true; |
| 124 auto* render_process_host = RenderProcessHost::FromID(render_process_id); |
| 125 if (!render_process_host->IsProcessBackgrounded()) |
| 126 return false; |
| 127 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); |
| 128 } |
| 129 |
106 void MemoryCoordinator::CreateChildInfoMapEntry( | 130 void MemoryCoordinator::CreateChildInfoMapEntry( |
107 int render_process_id, | 131 int render_process_id, |
108 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { | 132 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { |
109 auto& child_info = children_[render_process_id]; | 133 auto& child_info = children_[render_process_id]; |
110 // Process always start with normal memory state. | 134 // Process always start with normal memory state. |
111 // TODO(chrisha): Consider having memory state be a startup parameter of | 135 // TODO(chrisha): Consider having memory state be a startup parameter of |
112 // child processes, allowing them to be launched in a restricted state. | 136 // child processes, allowing them to be launched in a restricted state. |
113 child_info.memory_state = mojom::MemoryState::NORMAL; | 137 child_info.memory_state = mojom::MemoryState::NORMAL; |
114 child_info.handle = std::move(handle); | 138 child_info.handle = std::move(handle); |
115 } | 139 } |
116 | 140 |
117 MemoryCoordinator::ChildInfo::ChildInfo() {} | 141 MemoryCoordinator::ChildInfo::ChildInfo() {} |
118 | 142 |
119 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 143 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
120 // This is a nop, but exists for compatibility with STL containers. | 144 // This is a nop, but exists for compatibility with STL containers. |
121 } | 145 } |
122 | 146 |
123 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 147 MemoryCoordinator::ChildInfo::~ChildInfo() {} |
124 | 148 |
125 } // namespace content | 149 } // namespace content |
OLD | NEW |