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" | 9 #include "base/memory/singleton.h" |
| 10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| 11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
| 12 #include "content/common/memory_coordinator.mojom.h" | 12 #include "content/common/memory_coordinator.mojom.h" |
| 13 #include "content/public/browser/memory_coordinator_delegate.h" | |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "mojo/public/cpp/bindings/binding.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 // NOTE: Memory coordinator is under development and not fully working. | 18 // NOTE: Memory coordinator is under development and not fully working. |
| 18 // TODO(bashi): Add more explanations when we implement memory coordinator V0. | 19 // TODO(bashi): Add more explanations when we implement memory coordinator V0. |
| 19 | 20 |
| 20 class MemoryCoordinatorHandleImpl; | 21 class MemoryCoordinatorHandleImpl; |
| 21 | 22 |
| 22 // MemoryCoordinator is responsible for the whole memory management accross the | 23 // MemoryCoordinator is responsible for the whole memory management accross the |
| 23 // browser and child proceeses. It dispatches memory events to its clients and | 24 // browser and child proceeses. It dispatches memory events to its clients and |
| 24 // child processes based on its best knowledge of the memory usage. | 25 // child processes based on its best knowledge of the memory usage. |
| 25 class CONTENT_EXPORT MemoryCoordinator { | 26 class CONTENT_EXPORT MemoryCoordinator { |
| 26 public: | 27 public: |
| 27 ~MemoryCoordinator(); | 28 ~MemoryCoordinator(); |
| 28 | 29 |
| 29 // Singleton factory/accessor. | 30 // Singleton factory/accessor. |
| 30 static MemoryCoordinator* GetInstance(); | 31 static MemoryCoordinator* GetInstance(); |
| 31 | 32 |
| 33 void SetDelegate(const base::WeakPtr<MemoryCoordinatorDelegate>& delegate) { | |
| 34 delegate_ = delegate; | |
| 35 } | |
| 36 | |
| 32 // Creates a handle to the provided child process. | 37 // Creates a handle to the provided child process. |
| 33 void CreateHandle(int render_process_id, | 38 void CreateHandle(int render_process_id, |
| 34 mojom::MemoryCoordinatorHandleRequest request); | 39 mojom::MemoryCoordinatorHandleRequest request); |
| 35 | 40 |
| 36 // Returns number of children. Only used for testing. | 41 // Returns number of children. Only used for testing. |
| 37 size_t NumChildrenForTesting(); | 42 size_t NumChildrenForTesting(); |
| 38 | 43 |
| 39 // Dispatches a memory state change to the provided process. Returns true if | 44 // Dispatches a memory state change to the provided process. Returns true if |
| 40 // the process is tracked by this coordinator and successfully dispatches, | 45 // the process is tracked by this coordinator and successfully dispatches, |
| 41 // returns false otherwise. | 46 // returns false otherwise. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 52 MemoryCoordinator(); | 57 MemoryCoordinator(); |
| 53 | 58 |
| 54 // Adds the given ChildMemoryCoordinator as a child of this coordinator. | 59 // Adds the given ChildMemoryCoordinator as a child of this coordinator. |
| 55 void AddChildForTesting(int dummy_render_process_id, | 60 void AddChildForTesting(int dummy_render_process_id, |
| 56 mojom::ChildMemoryCoordinatorPtr child); | 61 mojom::ChildMemoryCoordinatorPtr child); |
| 57 | 62 |
| 58 // Callback invoked by mojo when the child connection goes down. Exposed | 63 // Callback invoked by mojo when the child connection goes down. Exposed |
| 59 // for testing. | 64 // for testing. |
| 60 void OnConnectionError(int render_process_id); | 65 void OnConnectionError(int render_process_id); |
| 61 | 66 |
| 67 // Returns true when a given renderer can be suspended. | |
| 68 bool CanSuspendRenderer(int render_process_id); | |
| 69 | |
| 62 private: | 70 private: |
| 63 friend struct base::DefaultSingletonTraits<MemoryCoordinator>; | 71 friend struct base::DefaultSingletonTraits<MemoryCoordinator>; |
| 64 | 72 |
| 65 // Helper function of CreateHandle and AddChildForTesting. | 73 // Helper function of CreateHandle and AddChildForTesting. |
| 66 void CreateChildInfoMapEntry( | 74 void CreateChildInfoMapEntry( |
| 67 int render_process_id, | 75 int render_process_id, |
| 68 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | 76 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |
| 69 | 77 |
| 70 // Stores information about any known child processes. | 78 // Stores information about any known child processes. |
| 71 struct ChildInfo { | 79 struct ChildInfo { |
| 72 // This object must be compatible with STL containers. | 80 // This object must be compatible with STL containers. |
| 73 ChildInfo(); | 81 ChildInfo(); |
| 74 ChildInfo(const ChildInfo& rhs); | 82 ChildInfo(const ChildInfo& rhs); |
| 75 ~ChildInfo(); | 83 ~ChildInfo(); |
| 76 | 84 |
| 77 mojom::MemoryState memory_state; | 85 mojom::MemoryState memory_state; |
| 78 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 86 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; |
| 79 }; | 87 }; |
| 80 | 88 |
| 81 // A map from process ID (RenderProcessHost::GetID()) to child process info. | 89 // A map from process ID (RenderProcessHost::GetID()) to child process info. |
| 82 using ChildInfoMap = std::map<int, ChildInfo>; | 90 using ChildInfoMap = std::map<int, ChildInfo>; |
| 83 | 91 |
| 84 // Tracks child processes. An entry is added when a renderer connects to | 92 // Tracks child processes. An entry is added when a renderer connects to |
| 85 // MemoryCoordinator and removed automatically when an underlying binding is | 93 // MemoryCoordinator and removed automatically when an underlying binding is |
| 86 // disconnected. | 94 // disconnected. |
| 87 ChildInfoMap children_; | 95 ChildInfoMap children_; |
| 88 | 96 |
| 97 base::WeakPtr<MemoryCoordinatorDelegate> delegate_; | |
|
haraken
2016/10/07 06:38:19
Why does this need to be a WeakPtr?
bashi
2016/10/07 07:13:37
TabManager (which is effectively the only implemen
haraken
2016/10/07 07:31:07
I'll defer the decision to Chris -- how the lifeti
bashi
2016/10/07 07:34:01
Just to clarify: This is a tentative workaround. W
| |
| 98 | |
| 89 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | 99 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); |
| 90 }; | 100 }; |
| 91 | 101 |
| 92 } // namespace content | 102 } // namespace content |
| 93 | 103 |
| 94 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 104 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ |
| OLD | NEW |