Chromium Code Reviews| 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 "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "content/public/browser/content_browser_client.h" | 9 #include "content/public/browser/content_browser_client.h" |
| 10 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 81 |
| 82 // Can't send a message to a child that doesn't exist. | 82 // Can't send a message to a child that doesn't exist. |
| 83 auto iter = children_.find(render_process_id); | 83 auto iter = children_.find(render_process_id); |
| 84 if (iter == children_.end()) | 84 if (iter == children_.end()) |
| 85 return false; | 85 return false; |
| 86 | 86 |
| 87 // Can't send a message to a child that isn't bound. | 87 // Can't send a message to a child that isn't bound. |
| 88 if (!iter->second.handle->child().is_bound()) | 88 if (!iter->second.handle->child().is_bound()) |
| 89 return false; | 89 return false; |
| 90 | 90 |
| 91 // We don't suspend foreground renderers. Throttle them instead. | 91 memory_state = OverrideChildState(memory_state, iter->second); |
|
haraken
2016/12/09 03:48:50
OverrideGlobalState ?
I might want to unify terms
bashi
2016/12/09 04:18:45
Done. Maybe "Override" isn't a good verb for this
| |
| 92 if (memory_state == mojom::MemoryState::SUSPENDED && | |
| 93 iter->second.is_visible) | |
| 94 memory_state = mojom::MemoryState::THROTTLED; | |
| 95 | 92 |
| 96 // A nop doesn't need to be sent, but is considered successful. | 93 // A nop doesn't need to be sent, but is considered successful. |
| 97 if (iter->second.memory_state == memory_state) | 94 if (iter->second.memory_state == memory_state) |
| 98 return true; | 95 return true; |
| 99 | 96 |
| 100 // Can't suspend the given renderer. | 97 // Can't suspend the given renderer. |
| 101 if (memory_state == mojom::MemoryState::SUSPENDED && | 98 if (memory_state == mojom::MemoryState::SUSPENDED && |
| 102 !CanSuspendRenderer(render_process_id)) | 99 !CanSuspendRenderer(render_process_id)) |
| 103 return false; | 100 return false; |
| 104 | 101 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { | 163 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { |
| 167 // If there is no delegate (i.e. unittests), renderers are always suspendable. | 164 // If there is no delegate (i.e. unittests), renderers are always suspendable. |
| 168 if (!delegate_) | 165 if (!delegate_) |
| 169 return true; | 166 return true; |
| 170 auto* render_process_host = RenderProcessHost::FromID(render_process_id); | 167 auto* render_process_host = RenderProcessHost::FromID(render_process_id); |
| 171 if (!render_process_host || !render_process_host->IsProcessBackgrounded()) | 168 if (!render_process_host || !render_process_host->IsProcessBackgrounded()) |
| 172 return false; | 169 return false; |
| 173 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); | 170 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); |
| 174 } | 171 } |
| 175 | 172 |
| 173 mojom::MemoryState MemoryCoordinator::OverrideChildState( | |
| 174 mojom::MemoryState memory_state, | |
| 175 const ChildInfo& child) { | |
| 176 // We don't suspend foreground renderers. Throttle them instead. | |
| 177 if (child.is_visible && memory_state == mojom::MemoryState::SUSPENDED) | |
| 178 return mojom::MemoryState::THROTTLED; | |
| 179 #if defined(OS_ANDROID) | |
| 180 // On Android, we throttle background renderers immediately. | |
| 181 // TODO(bashi): Create a specialized class of MemoryCoordinator for Android | |
| 182 // and move this ifdef to the class. | |
| 183 if (!child.is_visible && memory_state == mojom::MemoryState::NORMAL) | |
| 184 return mojom::MemoryState::THROTTLED; | |
| 185 // TODO(bashi): Suspend background renderers after a certain period of time. | |
| 186 #endif // defined(OS_ANDROID) | |
| 187 return memory_state; | |
| 188 } | |
| 189 | |
| 176 void MemoryCoordinator::SetDelegateForTesting( | 190 void MemoryCoordinator::SetDelegateForTesting( |
| 177 std::unique_ptr<MemoryCoordinatorDelegate> delegate) { | 191 std::unique_ptr<MemoryCoordinatorDelegate> delegate) { |
| 178 CHECK(!delegate_); | 192 CHECK(!delegate_); |
| 179 delegate_ = std::move(delegate); | 193 delegate_ = std::move(delegate); |
| 180 } | 194 } |
| 181 | 195 |
| 182 void MemoryCoordinator::CreateChildInfoMapEntry( | 196 void MemoryCoordinator::CreateChildInfoMapEntry( |
| 183 int render_process_id, | 197 int render_process_id, |
| 184 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { | 198 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { |
| 185 auto& child_info = children_[render_process_id]; | 199 auto& child_info = children_[render_process_id]; |
| 186 // Process always start with normal memory state. | 200 // Process always start with normal memory state. |
| 187 // We'll set renderer's memory state to the current global state when the | 201 // We'll set renderer's memory state to the current global state when the |
| 188 // corresponding renderer process is ready to communicate. Renderer processes | 202 // corresponding renderer process is ready to communicate. Renderer processes |
| 189 // call AddChild() when they are ready. | 203 // call AddChild() when they are ready. |
| 190 child_info.memory_state = mojom::MemoryState::NORMAL; | 204 child_info.memory_state = mojom::MemoryState::NORMAL; |
| 191 child_info.is_visible = true; | 205 child_info.is_visible = true; |
| 192 child_info.handle = std::move(handle); | 206 child_info.handle = std::move(handle); |
| 193 } | 207 } |
| 194 | 208 |
| 195 MemoryCoordinator::ChildInfo::ChildInfo() {} | 209 MemoryCoordinator::ChildInfo::ChildInfo() {} |
| 196 | 210 |
| 197 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 211 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
| 198 // This is a nop, but exists for compatibility with STL containers. | 212 // This is a nop, but exists for compatibility with STL containers. |
| 199 } | 213 } |
| 200 | 214 |
| 201 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 215 MemoryCoordinator::ChildInfo::~ChildInfo() {} |
| 202 | 216 |
| 203 } // namespace content | 217 } // namespace content |
| OLD | NEW |