| 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 |
| 83 // Can't suspend the given renderer. | 99 // Can't suspend the given renderer. |
| 84 if (memory_state == mojom::MemoryState::SUSPENDED && | 100 if (memory_state == mojom::MemoryState::SUSPENDED && |
| 85 !CanSuspendRenderer(render_process_id)) | 101 !CanSuspendRenderer(render_process_id)) |
| 86 return false; | 102 return false; |
| 87 | 103 |
| 88 // Update the internal state and send the message. | 104 // Update the internal state and send the message. |
| 89 iter->second.memory_state = memory_state; | 105 iter->second.memory_state = memory_state; |
| 90 iter->second.handle->child()->OnStateChange(memory_state); | 106 iter->second.handle->child()->OnStateChange(memory_state); |
| 91 return true; | 107 return true; |
| 92 } | 108 } |
| 93 | 109 |
| 94 mojom::MemoryState MemoryCoordinator::GetMemoryState( | 110 mojom::MemoryState MemoryCoordinator::GetMemoryState( |
| 95 int render_process_id) const { | 111 int render_process_id) const { |
| 96 auto iter = children_.find(render_process_id); | 112 auto iter = children_.find(render_process_id); |
| 97 if (iter == children_.end()) | 113 if (iter == children_.end()) |
| 98 return mojom::MemoryState::UNKNOWN; | 114 return mojom::MemoryState::UNKNOWN; |
| 99 return iter->second.memory_state; | 115 return iter->second.memory_state; |
| 100 } | 116 } |
| 101 | 117 |
| 102 void MemoryCoordinator::AddChildForTesting( | 118 void MemoryCoordinator::AddChildForTesting( |
| 103 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { | 119 int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { |
| 104 mojom::MemoryCoordinatorHandlePtr mch; | 120 mojom::MemoryCoordinatorHandlePtr mch; |
| 105 auto request = mojo::GetProxy(&mch); | 121 auto request = mojo::GetProxy(&mch); |
| 106 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 122 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
| 107 new MemoryCoordinatorHandleImpl(std::move(request))); | 123 new MemoryCoordinatorHandleImpl(std::move(request), this, |
| 124 dummy_render_process_id)); |
| 108 handle->AddChild(std::move(child)); | 125 handle->AddChild(std::move(child)); |
| 109 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 126 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); |
| 110 } | 127 } |
| 111 | 128 |
| 112 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 129 void MemoryCoordinator::OnConnectionError(int render_process_id) { |
| 113 children_.erase(render_process_id); | 130 children_.erase(render_process_id); |
| 114 } | 131 } |
| 115 | 132 |
| 116 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { | 133 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { |
| 117 // If there is no delegate (i.e. tests), renderers are always suspendable. | 134 // If there is no delegate (i.e. tests), renderers are always suspendable. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 | 153 |
| 137 MemoryCoordinator::ChildInfo::ChildInfo() {} | 154 MemoryCoordinator::ChildInfo::ChildInfo() {} |
| 138 | 155 |
| 139 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 156 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
| 140 // This is a nop, but exists for compatibility with STL containers. | 157 // This is a nop, but exists for compatibility with STL containers. |
| 141 } | 158 } |
| 142 | 159 |
| 143 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 160 MemoryCoordinator::ChildInfo::~ChildInfo() {} |
| 144 | 161 |
| 145 } // namespace content | 162 } // namespace content |
| OLD | NEW |