| 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 | 11 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom | 
| 13 // for the role of this class. | 12 // for the role of this class. | 
| 14 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | 13 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | 
| 15  public: | 14  public: | 
| 16   MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request) | 15   MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request, | 
| 17       : binding_(this, std::move(request)) { | 16                               MemoryCoordinator* coordinator, | 
| 18   } | 17                               int render_process_id); | 
|  | 18   ~MemoryCoordinatorHandleImpl() override; | 
| 19 | 19 | 
| 20   // mojom::MemoryCoordinatorHandle: | 20   // mojom::MemoryCoordinatorHandle: | 
| 21   void AddChild(mojom::ChildMemoryCoordinatorPtr child) override { | 21   void AddChild(mojom::ChildMemoryCoordinatorPtr child) override; | 
| 22     DCHECK(!child_.is_bound()); |  | 
| 23     child_ = std::move(child); |  | 
| 24   } |  | 
| 25 | 22 | 
| 26   mojom::ChildMemoryCoordinatorPtr& child() { return child_; } | 23   mojom::ChildMemoryCoordinatorPtr& child() { return child_; } | 
| 27   mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } | 24   mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } | 
| 28 | 25 | 
| 29  private: | 26  private: | 
|  | 27   MemoryCoordinator* coordinator_; | 
|  | 28   int render_process_id_; | 
| 30   mojom::ChildMemoryCoordinatorPtr child_; | 29   mojom::ChildMemoryCoordinatorPtr child_; | 
| 31   mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; | 30   mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; | 
| 32 | 31 | 
| 33   DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); | 32   DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); | 
| 34 }; | 33 }; | 
| 35 | 34 | 
| 36 // static | 35 MemoryCoordinatorHandleImpl::MemoryCoordinatorHandleImpl( | 
| 37 MemoryCoordinator* MemoryCoordinator::GetInstance() { | 36     mojom::MemoryCoordinatorHandleRequest request, | 
| 38   if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | 37     MemoryCoordinator* coordinator, | 
| 39     return nullptr; | 38     int render_process_id) | 
| 40   return base::Singleton<MemoryCoordinator, | 39     : coordinator_(coordinator), | 
| 41                          base::LeakySingletonTraits<MemoryCoordinator>>::get(); | 40       render_process_id_(render_process_id), | 
|  | 41       binding_(this, std::move(request)) { | 
|  | 42   DCHECK(coordinator_); | 
|  | 43 } | 
|  | 44 | 
|  | 45 MemoryCoordinatorHandleImpl::~MemoryCoordinatorHandleImpl() {} | 
|  | 46 | 
|  | 47 void MemoryCoordinatorHandleImpl::AddChild( | 
|  | 48     mojom::ChildMemoryCoordinatorPtr child) { | 
|  | 49   DCHECK(!child_.is_bound()); | 
|  | 50   child_ = std::move(child); | 
|  | 51   coordinator_->OnChildAdded(render_process_id_); | 
| 42 } | 52 } | 
| 43 | 53 | 
| 44 MemoryCoordinator::MemoryCoordinator() {} | 54 MemoryCoordinator::MemoryCoordinator() {} | 
| 45 | 55 | 
| 46 MemoryCoordinator::~MemoryCoordinator() {} | 56 MemoryCoordinator::~MemoryCoordinator() {} | 
| 47 | 57 | 
| 48 void MemoryCoordinator::CreateHandle( | 58 void MemoryCoordinator::CreateHandle( | 
| 49     int render_process_id, | 59     int render_process_id, | 
| 50     mojom::MemoryCoordinatorHandleRequest request) { | 60     mojom::MemoryCoordinatorHandleRequest request) { | 
| 51   std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 61   std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 
| 52       new MemoryCoordinatorHandleImpl(std::move(request))); | 62       new MemoryCoordinatorHandleImpl(std::move(request), this, | 
|  | 63                                       render_process_id)); | 
| 53   handle->binding().set_connection_error_handler( | 64   handle->binding().set_connection_error_handler( | 
| 54       base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), | 65       base::Bind(&MemoryCoordinator::OnConnectionError, base::Unretained(this), | 
| 55                  render_process_id)); | 66                  render_process_id)); | 
| 56   CreateChildInfoMapEntry(render_process_id, std::move(handle)); | 67   CreateChildInfoMapEntry(render_process_id, std::move(handle)); | 
| 57 } | 68 } | 
| 58 | 69 | 
| 59 size_t MemoryCoordinator::NumChildrenForTesting() { | 70 size_t MemoryCoordinator::NumChildrenForTesting() { | 
| 60   return children_.size(); | 71   return children_.size(); | 
| 61 } | 72 } | 
| 62 | 73 | 
| 63 bool MemoryCoordinator::SetMemoryState(int render_process_id, | 74 bool MemoryCoordinator::SetMemoryState(int render_process_id, | 
| 64                                        mojom::MemoryState memory_state) { | 75                                        mojom::MemoryState memory_state) { | 
| 65   // Can't set an invalid memory state. | 76   // Can't set an invalid memory state. | 
| 66   if (memory_state == mojom::MemoryState::UNKNOWN) | 77   if (memory_state == mojom::MemoryState::UNKNOWN) | 
| 67     return false; | 78     return false; | 
| 68 | 79 | 
| 69   // Can't send a message to a child that doesn't exist. | 80   // Can't send a message to a child that doesn't exist. | 
| 70   auto iter = children_.find(render_process_id); | 81   auto iter = children_.find(render_process_id); | 
| 71   if (iter == children_.end()) | 82   if (iter == children_.end()) | 
| 72     return false; | 83     return false; | 
| 73 | 84 | 
|  | 85   // Can't send a message to a child that isn't bound. | 
|  | 86   if (!iter->second.handle->child().is_bound()) | 
|  | 87     return false; | 
|  | 88 | 
| 74   // A nop doesn't need to be sent, but is considered successful. | 89   // A nop doesn't need to be sent, but is considered successful. | 
| 75   if (iter->second.memory_state == memory_state) | 90   if (iter->second.memory_state == memory_state) | 
| 76     return true; | 91     return true; | 
| 77 | 92 | 
| 78   // Update the internal state and send the message. | 93   // Update the internal state and send the message. | 
| 79   iter->second.memory_state = memory_state; | 94   iter->second.memory_state = memory_state; | 
| 80   iter->second.handle->child()->OnStateChange(memory_state); | 95   iter->second.handle->child()->OnStateChange(memory_state); | 
| 81   return true; | 96   return true; | 
| 82 } | 97 } | 
| 83 | 98 | 
| 84 mojom::MemoryState MemoryCoordinator::GetMemoryState( | 99 mojom::MemoryState MemoryCoordinator::GetMemoryState( | 
| 85     int render_process_id) const { | 100     int render_process_id) const { | 
| 86   auto iter = children_.find(render_process_id); | 101   auto iter = children_.find(render_process_id); | 
| 87   if (iter == children_.end()) | 102   if (iter == children_.end()) | 
| 88     return mojom::MemoryState::UNKNOWN; | 103     return mojom::MemoryState::UNKNOWN; | 
| 89   return iter->second.memory_state; | 104   return iter->second.memory_state; | 
| 90 } | 105 } | 
| 91 | 106 | 
| 92 void MemoryCoordinator::AddChildForTesting( | 107 void MemoryCoordinator::AddChildForTesting( | 
| 93     int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { | 108     int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { | 
| 94   mojom::MemoryCoordinatorHandlePtr mch; | 109   mojom::MemoryCoordinatorHandlePtr mch; | 
| 95   auto request = mojo::GetProxy(&mch); | 110   auto request = mojo::GetProxy(&mch); | 
| 96   std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 111   std::unique_ptr<MemoryCoordinatorHandleImpl> handle( | 
| 97       new MemoryCoordinatorHandleImpl(std::move(request))); | 112       new MemoryCoordinatorHandleImpl(std::move(request), this, | 
|  | 113                                       dummy_render_process_id)); | 
| 98   handle->AddChild(std::move(child)); | 114   handle->AddChild(std::move(child)); | 
| 99   CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 115   CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); | 
| 100 } | 116 } | 
| 101 | 117 | 
| 102 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 118 void MemoryCoordinator::OnConnectionError(int render_process_id) { | 
| 103   children_.erase(render_process_id); | 119   children_.erase(render_process_id); | 
| 104 } | 120 } | 
| 105 | 121 | 
| 106 void MemoryCoordinator::CreateChildInfoMapEntry( | 122 void MemoryCoordinator::CreateChildInfoMapEntry( | 
| 107     int render_process_id, | 123     int render_process_id, | 
| 108     std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { | 124     std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { | 
| 109   auto& child_info = children_[render_process_id]; | 125   auto& child_info = children_[render_process_id]; | 
| 110   // Process always start with normal memory state. | 126   // Process always start with normal memory state. | 
| 111   // TODO(chrisha): Consider having memory state be a startup parameter of | 127   // TODO(chrisha): Consider having memory state be a startup parameter of | 
| 112   // child processes, allowing them to be launched in a restricted state. | 128   // child processes, allowing them to be launched in a restricted state. | 
| 113   child_info.memory_state = mojom::MemoryState::NORMAL; | 129   child_info.memory_state = mojom::MemoryState::NORMAL; | 
| 114   child_info.handle = std::move(handle); | 130   child_info.handle = std::move(handle); | 
| 115 } | 131 } | 
| 116 | 132 | 
| 117 MemoryCoordinator::ChildInfo::ChildInfo() {} | 133 MemoryCoordinator::ChildInfo::ChildInfo() {} | 
| 118 | 134 | 
| 119 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 135 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 
| 120   // This is a nop, but exists for compatibility with STL containers. | 136   // This is a nop, but exists for compatibility with STL containers. | 
| 121 } | 137 } | 
| 122 | 138 | 
| 123 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 139 MemoryCoordinator::ChildInfo::~ChildInfo() {} | 
| 124 | 140 | 
| 125 }  // namespace content | 141 }  // namespace content | 
| OLD | NEW | 
|---|