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

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

Issue 2399293002: Add MemoryCoordinatorDelegate (Closed)
Patch Set: Add a separate delegate class Created 4 years, 2 months 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
« no previous file with comments | « content/browser/memory/memory_coordinator.h ('k') | content/public/browser/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/common/content_client.h"
8 #include "content/public/common/content_features.h" 11 #include "content/public/common/content_features.h"
9 12
10 namespace content { 13 namespace content {
11 14
12 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom 15 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom
13 // for the role of this class. 16 // for the role of this class.
14 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { 17 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle {
15 public: 18 public:
16 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) 19 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request)
17 : binding_(this, std::move(request)) { 20 : binding_(this, std::move(request)) {
(...skipping 16 matching lines...) Expand all
34 }; 37 };
35 38
36 // static 39 // static
37 MemoryCoordinator* MemoryCoordinator::GetInstance() { 40 MemoryCoordinator* MemoryCoordinator::GetInstance() {
38 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) 41 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator))
39 return nullptr; 42 return nullptr;
40 return base::Singleton<MemoryCoordinator, 43 return base::Singleton<MemoryCoordinator,
41 base::LeakySingletonTraits<MemoryCoordinator>>::get(); 44 base::LeakySingletonTraits<MemoryCoordinator>>::get();
42 } 45 }
43 46
44 MemoryCoordinator::MemoryCoordinator() {} 47 MemoryCoordinator::MemoryCoordinator()
48 : delegate_(GetContentClient()->browser()->GetMemoryCoordinatorDelegate()) {
49 }
45 50
46 MemoryCoordinator::~MemoryCoordinator() {} 51 MemoryCoordinator::~MemoryCoordinator() {}
47 52
48 void MemoryCoordinator::CreateHandle( 53 void MemoryCoordinator::CreateHandle(
49 int render_process_id, 54 int render_process_id,
50 mojom::MemoryCoordinatorHandleRequest request) { 55 mojom::MemoryCoordinatorHandleRequest request) {
51 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( 56 std::unique_ptr<MemoryCoordinatorHandleImpl> handle(
52 new MemoryCoordinatorHandleImpl(std::move(request))); 57 new MemoryCoordinatorHandleImpl(std::move(request)));
53 handle->binding().set_connection_error_handler( 58 handle->binding().set_connection_error_handler(
54 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), 59 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this),
(...skipping 13 matching lines...) Expand all
68 73
69 // Can't send a message to a child that doesn't exist. 74 // Can't send a message to a child that doesn't exist.
70 auto iter = children_.find(render_process_id); 75 auto iter = children_.find(render_process_id);
71 if (iter == children_.end()) 76 if (iter == children_.end())
72 return false; 77 return false;
73 78
74 // A nop doesn't need to be sent, but is considered successful. 79 // A nop doesn't need to be sent, but is considered successful.
75 if (iter->second.memory_state == memory_state) 80 if (iter->second.memory_state == memory_state)
76 return true; 81 return true;
77 82
83 // Can't suspend the given renderer.
84 if (memory_state == mojom::MemoryState::SUSPENDED &&
85 !CanSuspendRenderer(render_process_id))
86 return false;
87
78 // Update the internal state and send the message. 88 // Update the internal state and send the message.
79 iter->second.memory_state = memory_state; 89 iter->second.memory_state = memory_state;
80 iter->second.handle->child()->OnStateChange(memory_state); 90 iter->second.handle->child()->OnStateChange(memory_state);
81 return true; 91 return true;
82 } 92 }
83 93
84 mojom::MemoryState MemoryCoordinator::GetMemoryState( 94 mojom::MemoryState MemoryCoordinator::GetMemoryState(
85 int render_process_id) const { 95 int render_process_id) const {
86 auto iter = children_.find(render_process_id); 96 auto iter = children_.find(render_process_id);
87 if (iter == children_.end()) 97 if (iter == children_.end())
88 return mojom::MemoryState::UNKNOWN; 98 return mojom::MemoryState::UNKNOWN;
89 return iter->second.memory_state; 99 return iter->second.memory_state;
90 } 100 }
91 101
92 void MemoryCoordinator::AddChildForTesting( 102 void MemoryCoordinator::AddChildForTesting(
93 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { 103 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) {
94 mojom::MemoryCoordinatorHandlePtr mch; 104 mojom::MemoryCoordinatorHandlePtr mch;
95 auto request = mojo::GetProxy(&mch); 105 auto request = mojo::GetProxy(&mch);
96 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( 106 std::unique_ptr<MemoryCoordinatorHandleImpl> handle(
97 new MemoryCoordinatorHandleImpl(std::move(request))); 107 new MemoryCoordinatorHandleImpl(std::move(request)));
98 handle->AddChild(std::move(child)); 108 handle->AddChild(std::move(child));
99 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); 109 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle));
100 } 110 }
101 111
102 void MemoryCoordinator::OnConnectionError(int render_process_id) { 112 void MemoryCoordinator::OnConnectionError(int render_process_id) {
103 children_.erase(render_process_id); 113 children_.erase(render_process_id);
104 } 114 }
105 115
116 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) {
117 // If there is no delegate (i.e. tests), renderers are always suspendable.
118 if (!delegate_)
119 return true;
120 auto* render_process_host = RenderProcessHost::FromID(render_process_id);
121 if (!render_process_host->IsProcessBackgrounded())
122 return false;
123 return delegate_->CanSuspendBackgroundedRenderer(render_process_id);
124 }
125
106 void MemoryCoordinator::CreateChildInfoMapEntry( 126 void MemoryCoordinator::CreateChildInfoMapEntry(
107 int render_process_id, 127 int render_process_id,
108 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { 128 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) {
109 auto& child_info = children_[render_process_id]; 129 auto& child_info = children_[render_process_id];
110 // Process always start with normal memory state. 130 // Process always start with normal memory state.
111 // TODO(chrisha): Consider having memory state be a startup parameter of 131 // TODO(chrisha): Consider having memory state be a startup parameter of
112 // child processes, allowing them to be launched in a restricted state. 132 // child processes, allowing them to be launched in a restricted state.
113 child_info.memory_state = mojom::MemoryState::NORMAL; 133 child_info.memory_state = mojom::MemoryState::NORMAL;
114 child_info.handle = std::move(handle); 134 child_info.handle = std::move(handle);
115 } 135 }
116 136
117 MemoryCoordinator::ChildInfo::ChildInfo() {} 137 MemoryCoordinator::ChildInfo::ChildInfo() {}
118 138
119 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { 139 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) {
120 // This is a nop, but exists for compatibility with STL containers. 140 // This is a nop, but exists for compatibility with STL containers.
121 } 141 }
122 142
123 MemoryCoordinator::ChildInfo::~ChildInfo() {} 143 MemoryCoordinator::ChildInfo::~ChildInfo() {}
124 144
125 } // namespace content 145 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator.h ('k') | content/public/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698