Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: content/browser/memory/memory_coordinator.cc

Issue 2552603002: Add tests for MemoryCoordinator::Can{Throttle,Suspend}Renderer() (Closed)
Patch Set: Fix unittests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 mojom::MemoryCoordinatorHandleRequest request) { 66 mojom::MemoryCoordinatorHandleRequest request) {
67 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( 67 std::unique_ptr<MemoryCoordinatorHandleImpl> handle(
68 new MemoryCoordinatorHandleImpl(std::move(request), this, 68 new MemoryCoordinatorHandleImpl(std::move(request), this,
69 render_process_id)); 69 render_process_id));
70 handle->binding().set_connection_error_handler( 70 handle->binding().set_connection_error_handler(
71 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), 71 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this),
72 render_process_id)); 72 render_process_id));
73 CreateChildInfoMapEntry(render_process_id, std::move(handle)); 73 CreateChildInfoMapEntry(render_process_id, std::move(handle));
74 } 74 }
75 75
76 size_t MemoryCoordinator::NumChildrenForTesting() {
77 return children_.size();
78 }
79
80 bool MemoryCoordinator::SetChildMemoryState(int render_process_id, 76 bool MemoryCoordinator::SetChildMemoryState(int render_process_id,
81 mojom::MemoryState memory_state) { 77 mojom::MemoryState memory_state) {
82 // Can't set an invalid memory state. 78 // Can't set an invalid memory state.
83 if (memory_state == mojom::MemoryState::UNKNOWN) 79 if (memory_state == mojom::MemoryState::UNKNOWN)
84 return false; 80 return false;
85 81
86 // 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.
87 auto iter = children_.find(render_process_id); 83 auto iter = children_.find(render_process_id);
88 if (iter == children_.end()) 84 if (iter == children_.end())
89 return false; 85 return false;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 dummy_render_process_id)); 157 dummy_render_process_id));
162 handle->AddChild(std::move(child)); 158 handle->AddChild(std::move(child));
163 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); 159 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle));
164 } 160 }
165 161
166 void MemoryCoordinator::OnConnectionError(int render_process_id) { 162 void MemoryCoordinator::OnConnectionError(int render_process_id) {
167 children_.erase(render_process_id); 163 children_.erase(render_process_id);
168 } 164 }
169 165
170 bool MemoryCoordinator::CanThrottleRenderer(int render_process_id) { 166 bool MemoryCoordinator::CanThrottleRenderer(int render_process_id) {
171 // If there is no delegate (i.e. tests), renderers are always throttleable. 167 // If there is no delegate (i.e. unittests), renderers are always
168 // throttleable.
172 // TODO(bashi): We check |delegate_| to avoid calling FromID() on a 169 // TODO(bashi): We check |delegate_| to avoid calling FromID() on a
173 // wrong thread in tests. Figure out a better way to handle tests. 170 // wrong thread in tests. Figure out a better way to handle tests.
174 if (!delegate_) 171 if (!delegate_)
175 return true; 172 return true;
176 auto* render_process_host = RenderProcessHost::FromID(render_process_id); 173 auto* render_process_host = RenderProcessHost::FromID(render_process_id);
177 return render_process_host && render_process_host->IsProcessBackgrounded(); 174 return render_process_host && render_process_host->IsProcessBackgrounded();
178 } 175 }
179 176
180 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { 177 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) {
181 // If there is no delegate (i.e. tests), renderers are always suspendable. 178 // If there is no delegate (i.e. unittests), renderers are always suspendable.
182 if (!delegate_) 179 if (!delegate_)
183 return true; 180 return true;
184 auto* render_process_host = RenderProcessHost::FromID(render_process_id); 181 auto* render_process_host = RenderProcessHost::FromID(render_process_id);
185 if (!render_process_host || !render_process_host->IsProcessBackgrounded()) 182 if (!render_process_host || !render_process_host->IsProcessBackgrounded())
186 return false; 183 return false;
187 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); 184 return delegate_->CanSuspendBackgroundedRenderer(render_process_id);
188 } 185 }
189 186
187 void MemoryCoordinator::SetDelegateForTesting(
188 std::unique_ptr<MemoryCoordinatorDelegate> delegate) {
189 CHECK(!delegate_);
190 delegate_ = std::move(delegate);
191 }
192
190 void MemoryCoordinator::CreateChildInfoMapEntry( 193 void MemoryCoordinator::CreateChildInfoMapEntry(
191 int render_process_id, 194 int render_process_id,
192 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { 195 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) {
193 auto& child_info = children_[render_process_id]; 196 auto& child_info = children_[render_process_id];
194 // Process always start with normal memory state. 197 // Process always start with normal memory state.
195 // We'll set renderer's memory state to the current global state when the 198 // We'll set renderer's memory state to the current global state when the
196 // corresponding renderer process is ready to communicate. Renderer processes 199 // corresponding renderer process is ready to communicate. Renderer processes
197 // call AddChild() when they are ready. 200 // call AddChild() when they are ready.
198 child_info.memory_state = mojom::MemoryState::NORMAL; 201 child_info.memory_state = mojom::MemoryState::NORMAL;
199 child_info.handle = std::move(handle); 202 child_info.handle = std::move(handle);
200 } 203 }
201 204
202 MemoryCoordinator::ChildInfo::ChildInfo() {} 205 MemoryCoordinator::ChildInfo::ChildInfo() {}
203 206
204 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { 207 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) {
205 // This is a nop, but exists for compatibility with STL containers. 208 // This is a nop, but exists for compatibility with STL containers.
206 } 209 }
207 210
208 MemoryCoordinator::ChildInfo::~ChildInfo() {} 211 MemoryCoordinator::ChildInfo::~ChildInfo() {}
209 212
210 } // namespace content 213 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator.h ('k') | content/browser/memory/memory_coordinator_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698