| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | |
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | |
| 7 | |
| 8 #include "base/memory/memory_coordinator_client_registry.h" | |
| 9 #include "base/memory/memory_pressure_monitor.h" | |
| 10 #include "base/process/process_handle.h" | |
| 11 #include "content/common/content_export.h" | |
| 12 #include "content/common/memory_coordinator.mojom.h" | |
| 13 #include "content/public/browser/memory_coordinator_delegate.h" | |
| 14 #include "mojo/public/cpp/bindings/binding.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 // NOTE: Memory coordinator is under development and not fully working. | |
| 19 // TODO(bashi): Add more explanations when we implement memory coordinator V0. | |
| 20 | |
| 21 class MemoryCoordinatorHandleImpl; | |
| 22 | |
| 23 // MemoryCoordinator is responsible for the whole memory management accross the | |
| 24 // browser and child proceeses. It dispatches memory events to its clients and | |
| 25 // child processes based on its best knowledge of the memory usage. | |
| 26 class CONTENT_EXPORT MemoryCoordinator { | |
| 27 public: | |
| 28 virtual ~MemoryCoordinator(); | |
| 29 | |
| 30 // Singleton factory/accessor. | |
| 31 static MemoryCoordinator* GetInstance(); | |
| 32 | |
| 33 // Starts monitoring memory usage. After calling this method, memory | |
| 34 // coordinator will start dispatching state changes. | |
| 35 virtual void Start() {} | |
| 36 | |
| 37 // Creates a handle to the provided child process. | |
| 38 void CreateHandle(int render_process_id, | |
| 39 mojom::MemoryCoordinatorHandleRequest request); | |
| 40 | |
| 41 // Dispatches a memory state change to the provided process. Returns true if | |
| 42 // the process is tracked by this coordinator and successfully dispatches, | |
| 43 // returns false otherwise. | |
| 44 bool SetChildMemoryState( | |
| 45 int render_process_id, mojom::MemoryState memory_state); | |
| 46 | |
| 47 // Returns the memory state of the specified render process. Returns UNKNOWN | |
| 48 // if the process is not tracked by this coordinator. | |
| 49 mojom::MemoryState GetChildMemoryState(int render_process_id) const; | |
| 50 | |
| 51 // Records memory pressure notifications. Called by MemoryPressureMonitor. | |
| 52 // TODO(bashi): Remove this when MemoryPressureMonitor is retired. | |
| 53 void RecordMemoryPressure( | |
| 54 base::MemoryPressureMonitor::MemoryPressureLevel level); | |
| 55 | |
| 56 // Called when ChildMemoryCoordinator calls AddChild(). | |
| 57 virtual void OnChildAdded(int render_process_id) {} | |
| 58 | |
| 59 // Returns the global memory state. | |
| 60 virtual base::MemoryState GetGlobalMemoryState() const; | |
| 61 | |
| 62 // Returns the browser's current memory state. Note that the current state | |
| 63 // could be different from the global memory state as the browser won't be | |
| 64 // suspended. | |
| 65 virtual base::MemoryState GetCurrentMemoryState() const; | |
| 66 | |
| 67 // Sets the global memory state for testing. | |
| 68 virtual void SetCurrentMemoryStateForTesting(base::MemoryState memory_state); | |
| 69 | |
| 70 protected: | |
| 71 // Constructor. Protected as this is a singleton, but accessible for | |
| 72 // unittests. | |
| 73 MemoryCoordinator(); | |
| 74 | |
| 75 // Adds the given ChildMemoryCoordinator as a child of this coordinator. | |
| 76 void AddChildForTesting(int dummy_render_process_id, | |
| 77 mojom::ChildMemoryCoordinatorPtr child); | |
| 78 | |
| 79 // Callback invoked by mojo when the child connection goes down. Exposed | |
| 80 // for testing. | |
| 81 void OnConnectionError(int render_process_id); | |
| 82 | |
| 83 // Returns true when a given renderer can be suspended. | |
| 84 bool CanSuspendRenderer(int render_process_id); | |
| 85 | |
| 86 // Stores information about any known child processes. | |
| 87 struct ChildInfo { | |
| 88 // This object must be compatible with STL containers. | |
| 89 ChildInfo(); | |
| 90 ChildInfo(const ChildInfo& rhs); | |
| 91 ~ChildInfo(); | |
| 92 | |
| 93 mojom::MemoryState memory_state; | |
| 94 bool is_visible = false; | |
| 95 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | |
| 96 }; | |
| 97 | |
| 98 // A map from process ID (RenderProcessHost::GetID()) to child process info. | |
| 99 using ChildInfoMap = std::map<int, ChildInfo>; | |
| 100 | |
| 101 ChildInfoMap& children() { return children_; } | |
| 102 | |
| 103 private: | |
| 104 #if !defined(OS_MACOSX) | |
| 105 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorTest, HandleAdded); | |
| 106 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorTest, CanSuspendRenderer); | |
| 107 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorTest, CanThrottleRenderer); | |
| 108 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, | |
| 109 CannotSuspendRendererWithServiceWorker); | |
| 110 #endif | |
| 111 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorTest, | |
| 112 ChildRemovedOnConnectionError); | |
| 113 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorTest, SetChildMemoryState); | |
| 114 | |
| 115 // Called by SetChildMemoryState() to determine a child memory state based on | |
| 116 // the current status of the child process. | |
| 117 mojom::MemoryState OverrideGlobalState(mojom::MemoryState memroy_state, | |
| 118 const ChildInfo& child); | |
| 119 | |
| 120 void SetDelegateForTesting( | |
| 121 std::unique_ptr<MemoryCoordinatorDelegate> delegate); | |
| 122 | |
| 123 // Helper function of CreateHandle and AddChildForTesting. | |
| 124 void CreateChildInfoMapEntry( | |
| 125 int render_process_id, | |
| 126 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | |
| 127 | |
| 128 // Tracks child processes. An entry is added when a renderer connects to | |
| 129 // MemoryCoordinator and removed automatically when an underlying binding is | |
| 130 // disconnected. | |
| 131 ChildInfoMap children_; | |
| 132 | |
| 133 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | |
| 136 }; | |
| 137 | |
| 138 } // namespace content | |
| 139 | |
| 140 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | |
| OLD | NEW |