| 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/common/content_features.h" | |
| 9 | 8 |
| 10 namespace content { | 9 namespace content { |
| 11 | 10 |
| 12 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom | |
| 13 // for the role of this class. | |
| 14 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | |
| 15 public: | |
| 16 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) | |
| 17 : binding_(this, std::move(request)) { | |
| 18 } | |
| 19 | |
| 20 // mojom::MemoryCoordinatorHandle: | |
| 21 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { | |
| 22 DCHECK(!child_.is_bound()); | |
| 23 child_ = std::move(child); | |
| 24 } | |
| 25 | |
| 26 mojom::ChildMemoryCoordinatorPtr& child() { return child_; } | |
| 27 mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } | |
| 28 | |
| 29 private: | |
| 30 mojom::ChildMemoryCoordinatorPtr child_; | |
| 31 mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; | |
| 32 | |
| 33 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); | |
| 34 }; | |
| 35 | |
| 36 // static | |
| 37 MemoryCoordinator* MemoryCoordinator::GetInstance() { | |
| 38 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | |
| 39 return nullptr; | |
| 40 return base::Singleton<MemoryCoordinator, | |
| 41 base::LeakySingletonTraits<MemoryCoordinator>>::get(); | |
| 42 } | |
| 43 | |
| 44 MemoryCoordinator::MemoryCoordinator() {} | 11 MemoryCoordinator::MemoryCoordinator() {} |
| 45 | 12 |
| 46 MemoryCoordinator::~MemoryCoordinator() {} | 13 MemoryCoordinator::~MemoryCoordinator() {} |
| 47 | 14 |
| 48 void MemoryCoordinator::CreateHandle( | 15 void MemoryCoordinator::CreateHandle( |
| 49 int render_process_id, | 16 int render_process_id, |
| 50 mojom::MemoryCoordinatorHandleRequest request) { | 17 mojom::MemoryCoordinatorHandleRequest request) { |
| 51 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 18 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
| 52 new MemoryCoordinatorHandleImpl(std::move(request))); | 19 new MemoryCoordinatorHandleImpl(std::move(request))); |
| 53 handle->binding().set_connection_error_handler( | 20 handle->binding().set_connection_error_handler( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 mojom::MemoryCoordinatorHandlePtr mch; | 61 mojom::MemoryCoordinatorHandlePtr mch; |
| 95 auto request = mojo::GetProxy(&mch); | 62 auto request = mojo::GetProxy(&mch); |
| 96 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 63 std::unique_ptr<MemoryCoordinatorHandleImpl> handle( |
| 97 new MemoryCoordinatorHandleImpl(std::move(request))); | 64 new MemoryCoordinatorHandleImpl(std::move(request))); |
| 98 handle->AddChild(std::move(child)); | 65 handle->AddChild(std::move(child)); |
| 99 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 66 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); |
| 100 } | 67 } |
| 101 | 68 |
| 102 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 69 void MemoryCoordinator::OnConnectionError(int render_process_id) { |
| 103 children_.erase(render_process_id); | 70 children_.erase(render_process_id); |
| 71 OnChildRemoved(); |
| 104 } | 72 } |
| 105 | 73 |
| 106 void MemoryCoordinator::CreateChildInfoMapEntry( | 74 void MemoryCoordinator::CreateChildInfoMapEntry( |
| 107 int render_process_id, | 75 int render_process_id, |
| 108 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { | 76 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { |
| 109 auto& child_info = children_[render_process_id]; | 77 auto& child_info = children_[render_process_id]; |
| 110 // Process always start with normal memory state. | 78 // Process always start with normal memory state. |
| 111 // TODO(chrisha): Consider having memory state be a startup parameter of | 79 // TODO(chrisha): Consider having memory state be a startup parameter of |
| 112 // child processes, allowing them to be launched in a restricted state. | 80 // child processes, allowing them to be launched in a restricted state. |
| 113 child_info.memory_state = mojom::MemoryState::NORMAL; | 81 child_info.memory_state = mojom::MemoryState::NORMAL; |
| 114 child_info.handle = std::move(handle); | 82 child_info.handle = std::move(handle); |
| 83 OnChildAdded(); |
| 115 } | 84 } |
| 116 | 85 |
| 117 MemoryCoordinator::ChildInfo::ChildInfo() {} | 86 MemoryCoordinator::ChildInfo::ChildInfo() {} |
| 118 | 87 |
| 119 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 88 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
| 120 // This is a nop, but exists for compatibility with STL containers. | 89 // This is a nop, but exists for compatibility with STL containers. |
| 121 } | 90 } |
| 122 | 91 |
| 123 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 92 MemoryCoordinator::ChildInfo::~ChildInfo() {} |
| 124 | 93 |
| 94 MemoryCoordinatorHandleImpl::MemoryCoordinatorHandleImpl( |
| 95 mojom::MemoryCoordinatorHandleRequest request) |
| 96 : binding_(this, std::move(request)) {} |
| 97 |
| 98 MemoryCoordinatorHandleImpl::~MemoryCoordinatorHandleImpl() {} |
| 99 |
| 100 void MemoryCoordinatorHandleImpl::AddChild( |
| 101 mojom::ChildMemoryCoordinatorPtr child) { |
| 102 DCHECK(!child_.is_bound()); |
| 103 child_ = std::move(child); |
| 104 } |
| 105 |
| 125 } // namespace content | 106 } // namespace content |
| OLD | NEW |