Chromium Code Reviews| 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 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ |
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ |
| 7 | 7 |
| 8 #include "base/memory/memory_coordinator_client_registry.h" | 8 #include "base/memory/memory_coordinator_client_registry.h" |
| 9 #include "base/memory/singleton.h" | |
| 10 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
| 11 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| 12 #include "content/common/memory_coordinator.mojom.h" | 11 #include "content/common/memory_coordinator.mojom.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 14 | 13 |
| 15 namespace content { | 14 namespace content { |
| 16 | 15 |
| 17 // NOTE: Memory coordinator is under development and not fully working. | 16 // NOTE: Memory coordinator is under development and not fully working. |
| 18 // TODO(bashi): Add more explanations when we implement memory coordinator V0. | 17 // TODO(bashi): Add more explanations when we implement memory coordinator V0. |
| 19 | 18 |
| 20 class MemoryCoordinatorHandleImpl; | 19 class MemoryCoordinatorHandleImpl; |
| 21 | 20 |
| 22 // MemoryCoordinator is responsible for the whole memory management accross the | 21 // MemoryCoordinator is responsible for the whole memory management accross the |
| 23 // browser and child proceeses. It dispatches memory events to its clients and | 22 // browser and child proceeses. It dispatches memory events to its clients and |
| 24 // child processes based on its best knowledge of the memory usage. | 23 // child processes based on its best knowledge of the memory usage. |
| 25 class CONTENT_EXPORT MemoryCoordinator { | 24 class CONTENT_EXPORT MemoryCoordinator { |
| 26 public: | 25 public: |
| 27 ~MemoryCoordinator(); | 26 virtual ~MemoryCoordinator(); |
| 28 | 27 |
| 29 // Singleton factory/accessor. | 28 // Singleton factory/accessor. |
| 30 static MemoryCoordinator* GetInstance(); | 29 static MemoryCoordinator* GetInstance(); |
| 31 | 30 |
| 32 // Creates a handle to the provided child process. | 31 // Creates a handle to the provided child process. |
| 33 void CreateHandle(int render_process_id, | 32 void CreateHandle(int render_process_id, |
| 34 mojom::MemoryCoordinatorHandleRequest request); | 33 mojom::MemoryCoordinatorHandleRequest request); |
| 35 | 34 |
| 36 // Returns number of children. Only used for testing. | 35 // Returns number of children. Only used for testing. |
| 37 size_t NumChildrenForTesting(); | 36 size_t NumChildrenForTesting(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 52 MemoryCoordinator(); | 51 MemoryCoordinator(); |
| 53 | 52 |
| 54 // Adds the given ChildMemoryCoordinator as a child of this coordinator. | 53 // Adds the given ChildMemoryCoordinator as a child of this coordinator. |
| 55 void AddChildForTesting(int dummy_render_process_id, | 54 void AddChildForTesting(int dummy_render_process_id, |
| 56 mojom::ChildMemoryCoordinatorPtr child); | 55 mojom::ChildMemoryCoordinatorPtr child); |
| 57 | 56 |
| 58 // Callback invoked by mojo when the child connection goes down. Exposed | 57 // Callback invoked by mojo when the child connection goes down. Exposed |
| 59 // for testing. | 58 // for testing. |
| 60 void OnConnectionError(int render_process_id); | 59 void OnConnectionError(int render_process_id); |
| 61 | 60 |
| 62 private: | |
| 63 friend struct base::DefaultSingletonTraits<MemoryCoordinator>; | |
| 64 | |
| 65 // Helper function of CreateHandle and AddChildForTesting. | |
| 66 void CreateChildInfoMapEntry( | |
| 67 int render_process_id, | |
| 68 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | |
| 69 | |
| 70 // Stores information about any known child processes. | 61 // Stores information about any known child processes. |
| 71 struct ChildInfo { | 62 struct ChildInfo { |
| 72 // This object must be compatible with STL containers. | 63 // This object must be compatible with STL containers. |
| 73 ChildInfo(); | 64 ChildInfo(); |
| 74 ChildInfo(const ChildInfo& rhs); | 65 ChildInfo(const ChildInfo& rhs); |
| 75 ~ChildInfo(); | 66 ~ChildInfo(); |
| 76 | 67 |
| 77 mojom::MemoryState memory_state; | 68 mojom::MemoryState memory_state; |
| 78 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 69 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; |
| 79 }; | 70 }; |
| 80 | 71 |
| 81 // A map from process ID (RenderProcessHost::GetID()) to child process info. | 72 // A map from process ID (RenderProcessHost::GetID()) to child process info. |
| 82 using ChildInfoMap = std::map<int, ChildInfo>; | 73 using ChildInfoMap = std::map<int, ChildInfo>; |
| 83 | 74 |
| 75 ChildInfoMap& children() { return children_; } | |
| 76 | |
| 77 virtual void OnChildAdded() {} | |
| 78 virtual void OnChildRemoved() {} | |
|
chrisha
2016/09/30 02:57:20
Why are these needed? The Impl should be able to i
| |
| 79 | |
| 80 private: | |
| 81 // Helper function of CreateHandle and AddChildForTesting. | |
| 82 void CreateChildInfoMapEntry( | |
| 83 int render_process_id, | |
| 84 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | |
| 85 | |
| 84 // Tracks child processes. An entry is added when a renderer connects to | 86 // Tracks child processes. An entry is added when a renderer connects to |
| 85 // MemoryCoordinator and removed automatically when an underlying binding is | 87 // MemoryCoordinator and removed automatically when an underlying binding is |
| 86 // disconnected. | 88 // disconnected. |
| 87 ChildInfoMap children_; | 89 ChildInfoMap children_; |
| 88 | 90 |
| 89 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | 91 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); |
| 90 }; | 92 }; |
| 91 | 93 |
| 94 // The implementation of MemoryCoordinatorHandle. See memory_coordinator.mojom | |
| 95 // for the role of this class. | |
| 96 class MemoryCoordinatorHandleImpl : public mojom::MemoryCoordinatorHandle { | |
|
chrisha
2016/09/30 02:57:20
Why do we need to expose this impl detail?
| |
| 97 public: | |
| 98 MemoryCoordinatorHandleImpl(mojom::MemoryCoordinatorHandleRequest request); | |
| 99 ~MemoryCoordinatorHandleImpl() override; | |
| 100 | |
| 101 // mojom::MemoryCoordinatorHandle: | |
| 102 void AddChild(mojom::ChildMemoryCoordinatorPtr child) override; | |
| 103 | |
| 104 mojom::ChildMemoryCoordinatorPtr& child() { return child_; } | |
| 105 mojo::Binding<mojom::MemoryCoordinatorHandle>& binding() { return binding_; } | |
| 106 | |
| 107 private: | |
| 108 mojom::ChildMemoryCoordinatorPtr child_; | |
| 109 mojo::Binding<mojom::MemoryCoordinatorHandle> binding_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorHandleImpl); | |
| 112 }; | |
| 113 | |
| 92 } // namespace content | 114 } // namespace content |
| 93 | 115 |
| 94 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 116 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ |
| OLD | NEW |