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/content_browser_client.h" | 8 #include "content/public/browser/content_browser_client.h" |
9 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
10 #include "content/public/common/content_client.h" | 10 #include "content/public/common/content_client.h" |
11 #include "content/public/common/content_features.h" | 11 #include "content/public/common/content_features.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom | 15 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom |
16 // for the role of this class. | 16 // for the role of this class. |
17 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | 17 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { |
18 public: | 18 public: |
19 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) | 19 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request, |
20 : binding_(this, std::move(request)) { | 20 MemoryCoordinator* coordinator, |
21 } | 21 int render_process_id); |
| 22 ~MemoryCoordinatorHandleImpl() override; |
22 | 23 |
23 // mojom::MemoryCoordinatorHandle: | 24 // mojom::MemoryCoordinatorHandle: |
24 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { | 25 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override; |
25 DCHECK(!child_.is_bound()); | |
26 child_ = std::move(child); | |
27 } | |
28 | 26 |
29 mojom::ChildMemoryCoordinatorPtr& child() { return child_; } | 27 mojom::ChildMemoryCoordinatorPtr& child() { return child_; } |
30 mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } | 28 mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } |
31 | 29 |
32 private: | 30 private: |
| 31 MemoryCoordinator* coordinator_; |
| 32 int render_process_id_; |
33 mojom::ChildMemoryCoordinatorPtr child_; | 33 mojom::ChildMemoryCoordinatorPtr child_; |
34 mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; | 34 mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; |
35 | 35 |
36 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); | 36 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); |
37 }; | 37 }; |
38 | 38 |
39 // static | 39 MemoryCoordinatorHandleImpl::MemoryCoordinatorHandleImpl( |
40 MemoryCoordinator* MemoryCoordinator::GetInstance() { | 40 mojom::MemoryCoordinatorHandleRequest request, |
41 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | 41 MemoryCoordinator* coordinator, |
42 return nullptr; | 42 int render_process_id) |
43 return base::Singleton<MemoryCoordinator, | 43 : coordinator_(coordinator), |
44 base::LeakySingletonTraits<MemoryCoordinator>>::get(); | 44 render_process_id_(render_process_id), |
| 45 binding_(this, std::move(request)) { |
| 46 DCHECK(coordinator_); |
| 47 } |
| 48 |
| 49 MemoryCoordinatorHandleImpl::~MemoryCoordinatorHandleImpl() {} |
| 50 |
| 51 void MemoryCoordinatorHandleImpl::AddChild( |
| 52 mojom::ChildMemoryCoordinatorPtr child) { |
| 53 DCHECK(!child_.is_bound()); |
| 54 child_ = std::move(child); |
| 55 coordinator_->OnChildAdded(render_process_id_); |
45 } | 56 } |
46 | 57 |
47 MemoryCoordinator::MemoryCoordinator() | 58 MemoryCoordinator::MemoryCoordinator() |
48 : delegate_(GetContentClient()->browser()->GetMemoryCoordinatorDelegate()) { | 59 : delegate_(GetContentClient()->browser()->GetMemoryCoordinatorDelegate()) { |
49 } | 60 } |
50 | 61 |
51 MemoryCoordinator::~MemoryCoordinator() {} | 62 MemoryCoordinator::~MemoryCoordinator() {} |
52 | 63 |
53 void MemoryCoordinator::CreateHandle( | 64 void MemoryCoordinator::CreateHandle( |
54 int render_process_id, | 65 int render_process_id, |
55 mojom::MemoryCoordinatorHandleRequest request) { | 66 mojom::MemoryCoordinatorHandleRequest request) { |
56 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 67 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
57 new MemoryCoordinatorHandleImpl(std::move(request))); | 68 new MemoryCoordinatorHandleImpl(std::move(request), this, |
| 69 render_process_id)); |
58 handle->binding().set_connection_error_handler( | 70 handle->binding().set_connection_error_handler( |
59 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), | 71 base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), |
60 render_process_id)); | 72 render_process_id)); |
61 CreateChildInfoMapEntry(render_process_id, std::move(handle)); | 73 CreateChildInfoMapEntry(render_process_id, std::move(handle)); |
62 } | 74 } |
63 | 75 |
64 size_t MemoryCoordinator::NumChildrenForTesting() { | 76 size_t MemoryCoordinator::NumChildrenForTesting() { |
65 return children_.size(); | 77 return children_.size(); |
66 } | 78 } |
67 | 79 |
68 bool MemoryCoordinator::SetMemoryState(int render_process_id, | 80 bool MemoryCoordinator::SetMemoryState(int render_process_id, |
69 mojom::MemoryState memory_state) { | 81 mojom::MemoryState memory_state) { |
70 // Can't set an invalid memory state. | 82 // Can't set an invalid memory state. |
71 if (memory_state == mojom::MemoryState::UNKNOWN) | 83 if (memory_state == mojom::MemoryState::UNKNOWN) |
72 return false; | 84 return false; |
73 | 85 |
74 // Can't send a message to a child that doesn't exist. | 86 // Can't send a message to a child that doesn't exist. |
75 auto iter = children_.find(render_process_id); | 87 auto iter = children_.find(render_process_id); |
76 if (iter == children_.end()) | 88 if (iter == children_.end()) |
77 return false; | 89 return false; |
78 | 90 |
| 91 // Can't send a message to a child that isn't bound. |
| 92 if (!iter->second.handle->child().is_bound()) |
| 93 return false; |
| 94 |
79 // A nop doesn't need to be sent, but is considered successful. | 95 // A nop doesn't need to be sent, but is considered successful. |
80 if (iter->second.memory_state == memory_state) | 96 if (iter->second.memory_state == memory_state) |
81 return true; | 97 return true; |
82 | 98 |
| 99 // Can't throttle the given renderer. |
| 100 if (memory_state == mojom::MemoryState::THROTTLED && |
| 101 !CanThrottleRenderer(render_process_id)) |
| 102 return false; |
| 103 |
83 // Can't suspend the given renderer. | 104 // Can't suspend the given renderer. |
84 if (memory_state == mojom::MemoryState::SUSPENDED && | 105 if (memory_state == mojom::MemoryState::SUSPENDED && |
85 !CanSuspendRenderer(render_process_id)) | 106 !CanSuspendRenderer(render_process_id)) |
86 return false; | 107 return false; |
87 | 108 |
88 // Update the internal state and send the message. | 109 // Update the internal state and send the message. |
89 iter->second.memory_state = memory_state; | 110 iter->second.memory_state = memory_state; |
90 iter->second.handle->child()->OnStateChange(memory_state); | 111 iter->second.handle->child()->OnStateChange(memory_state); |
91 return true; | 112 return true; |
92 } | 113 } |
93 | 114 |
94 mojom::MemoryState MemoryCoordinator::GetMemoryState( | 115 mojom::MemoryState MemoryCoordinator::GetMemoryState( |
95 int render_process_id) const { | 116 int render_process_id) const { |
96 auto iter = children_.find(render_process_id); | 117 auto iter = children_.find(render_process_id); |
97 if (iter == children_.end()) | 118 if (iter == children_.end()) |
98 return mojom::MemoryState::UNKNOWN; | 119 return mojom::MemoryState::UNKNOWN; |
99 return iter->second.memory_state; | 120 return iter->second.memory_state; |
100 } | 121 } |
101 | 122 |
102 void MemoryCoordinator::AddChildForTesting( | 123 void MemoryCoordinator::AddChildForTesting( |
103 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { | 124 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { |
104 mojom::MemoryCoordinatorHandlePtr mch; | 125 mojom::MemoryCoordinatorHandlePtr mch; |
105 auto request = mojo::GetProxy(&mch); | 126 auto request = mojo::GetProxy(&mch); |
106 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 127 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
107 new MemoryCoordinatorHandleImpl(std::move(request))); | 128 new MemoryCoordinatorHandleImpl(std::move(request), this, |
| 129 dummy_render_process_id)); |
108 handle->AddChild(std::move(child)); | 130 handle->AddChild(std::move(child)); |
109 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 131 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); |
110 } | 132 } |
111 | 133 |
112 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 134 void MemoryCoordinator::OnConnectionError(int render_process_id) { |
113 children_.erase(render_process_id); | 135 children_.erase(render_process_id); |
114 } | 136 } |
115 | 137 |
| 138 bool MemoryCoordinator::CanThrottleRenderer(int render_process_id) { |
| 139 // If there is no delegate (i.e. tests), renderers are always throttleable. |
| 140 // TODO(bashi): We check |delegate_| to avoid calling FromID() on a |
| 141 // wrong thread in tests. Figure out a better way to handle tests. |
| 142 if (!delegate_) |
| 143 return true; |
| 144 auto* render_process_host = RenderProcessHost::FromID(render_process_id); |
| 145 return render_process_host->IsProcessBackgrounded(); |
| 146 } |
| 147 |
116 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { | 148 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { |
117 // If there is no delegate (i.e. tests), renderers are always suspendable. | 149 // If there is no delegate (i.e. tests), renderers are always suspendable. |
118 if (!delegate_) | 150 if (!delegate_) |
119 return true; | 151 return true; |
120 auto* render_process_host = RenderProcessHost::FromID(render_process_id); | 152 auto* render_process_host = RenderProcessHost::FromID(render_process_id); |
121 if (!render_process_host->IsProcessBackgrounded()) | 153 if (!render_process_host->IsProcessBackgrounded()) |
122 return false; | 154 return false; |
123 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); | 155 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); |
124 } | 156 } |
125 | 157 |
(...skipping 10 matching lines...) Expand all Loading... |
136 | 168 |
137 MemoryCoordinator::ChildInfo::ChildInfo() {} | 169 MemoryCoordinator::ChildInfo::ChildInfo() {} |
138 | 170 |
139 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 171 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
140 // This is a nop, but exists for compatibility with STL containers. | 172 // This is a nop, but exists for compatibility with STL containers. |
141 } | 173 } |
142 | 174 |
143 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 175 MemoryCoordinator::ChildInfo::~ChildInfo() {} |
144 | 176 |
145 } // namespace content | 177 } // namespace content |
OLD | NEW |