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 |
| 31 // Starts monitoring memory usage. After calling this method, memory |
| 32 // coordinator will start dispatching state changes. |
| 33 virtual void Start() = 0; |
| 34 |
32 // Creates a handle to the provided child process. | 35 // Creates a handle to the provided child process. |
33 void CreateHandle(int render_process_id, | 36 void CreateHandle(int render_process_id, |
34 mojom::MemoryCoordinatorHandleRequest request); | 37 mojom::MemoryCoordinatorHandleRequest request); |
35 | 38 |
36 // Returns number of children. Only used for testing. | 39 // Returns number of children. Only used for testing. |
37 size_t NumChildrenForTesting(); | 40 size_t NumChildrenForTesting(); |
38 | 41 |
39 // Dispatches a memory state change to the provided process. Returns true if | 42 // Dispatches a memory state change to the provided process. Returns true if |
40 // the process is tracked by this coordinator and successfully dispatches, | 43 // the process is tracked by this coordinator and successfully dispatches, |
41 // returns false otherwise. | 44 // returns false otherwise. |
42 bool SetMemoryState( | 45 bool SetMemoryState( |
43 int render_process_id, mojom::MemoryState memory_state); | 46 int render_process_id, mojom::MemoryState memory_state); |
44 | 47 |
45 // Returns the memory state of the specified render process. Returns UNKNOWN | 48 // Returns the memory state of the specified render process. Returns UNKNOWN |
46 // if the process is not tracked by this coordinator. | 49 // if the process is not tracked by this coordinator. |
47 mojom::MemoryState GetMemoryState(int render_process_id) const; | 50 mojom::MemoryState GetMemoryState(int render_process_id) const; |
48 | 51 |
| 52 // Called when ChildMemoryCoordinator calls AddChild(). |
| 53 virtual void OnChildAdded(int render_process_id) {} |
| 54 |
49 protected: | 55 protected: |
50 // Constructor. Protected as this is a singleton, but accessible for | 56 // Constructor. Protected as this is a singleton, but accessible for |
51 // unittests. | 57 // unittests. |
52 MemoryCoordinator(); | 58 MemoryCoordinator(); |
53 | 59 |
54 // Adds the given ChildMemoryCoordinator as a child of this coordinator. | 60 // Adds the given ChildMemoryCoordinator as a child of this coordinator. |
55 void AddChildForTesting(int dummy_render_process_id, | 61 void AddChildForTesting(int dummy_render_process_id, |
56 mojom::ChildMemoryCoordinatorPtr child); | 62 mojom::ChildMemoryCoordinatorPtr child); |
57 | 63 |
58 // Callback invoked by mojo when the child connection goes down. Exposed | 64 // Callback invoked by mojo when the child connection goes down. Exposed |
59 // for testing. | 65 // for testing. |
60 void OnConnectionError(int render_process_id); | 66 void OnConnectionError(int render_process_id); |
61 | 67 |
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. | 68 // Stores information about any known child processes. |
71 struct ChildInfo { | 69 struct ChildInfo { |
72 // This object must be compatible with STL containers. | 70 // This object must be compatible with STL containers. |
73 ChildInfo(); | 71 ChildInfo(); |
74 ChildInfo(const ChildInfo& rhs); | 72 ChildInfo(const ChildInfo& rhs); |
75 ~ChildInfo(); | 73 ~ChildInfo(); |
76 | 74 |
77 mojom::MemoryState memory_state; | 75 mojom::MemoryState memory_state; |
78 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 76 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; |
79 }; | 77 }; |
80 | 78 |
81 // A map from process ID (RenderProcessHost::GetID()) to child process info. | 79 // A map from process ID (RenderProcessHost::GetID()) to child process info. |
82 using ChildInfoMap = std::map<int, ChildInfo>; | 80 using ChildInfoMap = std::map<int, ChildInfo>; |
83 | 81 |
| 82 ChildInfoMap& children() { return children_; } |
| 83 |
| 84 private: |
| 85 // Helper function of CreateHandle and AddChildForTesting. |
| 86 void CreateChildInfoMapEntry( |
| 87 int render_process_id, |
| 88 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |
| 89 |
84 // Tracks child processes. An entry is added when a renderer connects to | 90 // Tracks child processes. An entry is added when a renderer connects to |
85 // MemoryCoordinator and removed automatically when an underlying binding is | 91 // MemoryCoordinator and removed automatically when an underlying binding is |
86 // disconnected. | 92 // disconnected. |
87 ChildInfoMap children_; | 93 ChildInfoMap children_; |
88 | 94 |
89 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | 95 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); |
90 }; | 96 }; |
91 | 97 |
92 } // namespace content | 98 } // namespace content |
93 | 99 |
94 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 100 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ |
OLD | NEW |