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_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/cancelable_callback.h" | 9 #include "base/cancelable_callback.h" |
10 #include "base/memory/memory_coordinator_client.h" | 10 #include "base/memory/memory_coordinator_client.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 // the memory condition remains the same until |duration| is passed or | 105 // the memory condition remains the same until |duration| is passed or |
106 // another call of this method. | 106 // another call of this method. |
107 void ForceSetMemoryCondition(MemoryCondition condition, | 107 void ForceSetMemoryCondition(MemoryCondition condition, |
108 base::TimeDelta duration); | 108 base::TimeDelta duration); |
109 | 109 |
110 // Changes current memory condition if needed. This may trigger some actions | 110 // Changes current memory condition if needed. This may trigger some actions |
111 // like purging memory and memory state changes. | 111 // like purging memory and memory state changes. |
112 void UpdateConditionIfNeeded(MemoryCondition condition); | 112 void UpdateConditionIfNeeded(MemoryCondition condition); |
113 | 113 |
114 // Asks the delegate to discard a tab. | 114 // Asks the delegate to discard a tab. |
115 void DiscardTab(); | 115 bool DiscardTab(); |
116 | 116 |
117 protected: | 117 protected: |
118 // Returns the RenderProcessHost which is correspond to the given id. | 118 // Returns the RenderProcessHost which is correspond to the given id. |
119 // Returns nullptr if there is no corresponding RenderProcessHost. | 119 // Returns nullptr if there is no corresponding RenderProcessHost. |
120 // This is a virtual method so that we can write tests without having | 120 // This is a virtual method so that we can write tests without having |
121 // actual RenderProcessHost. | 121 // actual RenderProcessHost. |
122 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id); | 122 virtual RenderProcessHost* GetRenderProcessHost(int render_process_id); |
123 | 123 |
124 // Sets a delegate for testing. | 124 // Sets a delegate for testing. |
125 void SetDelegateForTesting( | 125 void SetDelegateForTesting( |
(...skipping 17 matching lines...) Expand all Loading... | |
143 | 143 |
144 // Stores information about any known child processes. | 144 // Stores information about any known child processes. |
145 struct ChildInfo { | 145 struct ChildInfo { |
146 // This object must be compatible with STL containers. | 146 // This object must be compatible with STL containers. |
147 ChildInfo(); | 147 ChildInfo(); |
148 ChildInfo(const ChildInfo& rhs); | 148 ChildInfo(const ChildInfo& rhs); |
149 ~ChildInfo(); | 149 ~ChildInfo(); |
150 | 150 |
151 MemoryState memory_state; | 151 MemoryState memory_state; |
152 bool is_visible = false; | 152 bool is_visible = false; |
153 base::TimeTicks can_purge_after; | |
153 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 154 std::unique_ptr<MemoryCoordinatorHandleImpl> handle; |
154 }; | 155 }; |
155 | 156 |
156 // A map from process ID (RenderProcessHost::GetID()) to child process info. | 157 // A map from process ID (RenderProcessHost::GetID()) to child process info. |
157 using ChildInfoMap = std::map<int, ChildInfo>; | 158 using ChildInfoMap = std::map<int, ChildInfo>; |
158 | 159 |
159 ChildInfoMap& children() { return children_; } | 160 ChildInfoMap& children() { return children_; } |
160 | 161 |
161 base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } | 162 base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } |
162 | 163 |
163 private: | 164 private: |
164 #if !defined(OS_MACOSX) | 165 #if !defined(OS_MACOSX) |
165 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); | 166 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, HandleAdded); |
166 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, | 167 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplBrowserTest, |
167 CanSuspendRenderer); | 168 CanSuspendRenderer); |
168 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, | 169 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorWithServiceWorkerTest, |
169 CannotSuspendRendererWithServiceWorker); | 170 CannotSuspendRendererWithServiceWorker); |
170 #endif | 171 #endif |
171 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, OnChildVisibilityChanged); | 172 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, OnChildVisibilityChanged); |
172 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextCondition); | 173 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextCondition); |
173 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateCondition); | 174 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateCondition); |
174 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); | 175 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, SetMemoryStateForTesting); |
175 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetMemoryCondition); | 176 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetMemoryCondition); |
176 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, DiscardTabUnderCritical); | 177 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, DiscardTabUnderCritical); |
178 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, OnWarningCondition); | |
179 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, OnCriticalCondition); | |
177 | 180 |
178 friend struct MemoryCoordinatorSingletonTraits; | 181 friend struct MemoryCoordinatorSingletonTraits; |
179 friend class MemoryCoordinatorHandleImpl; | 182 friend class MemoryCoordinatorHandleImpl; |
180 | 183 |
181 // Called when ChildMemoryCoordinator calls AddChild(). | 184 // Called when ChildMemoryCoordinator calls AddChild(). |
182 void OnChildAdded(int render_process_id); | 185 void OnChildAdded(int render_process_id); |
183 | 186 |
184 // Called when visibility of a child process is changed. | 187 // Called when visibility of a child process is changed. |
185 void OnChildVisibilityChanged(int render_process_id, bool is_visible); | 188 void OnChildVisibilityChanged(int render_process_id, bool is_visible); |
186 | 189 |
187 // Called by SetChildMemoryState() to determine a child memory state based on | 190 // Called by SetChildMemoryState() to determine a child memory state based on |
188 // the current status of the child process. | 191 // the current status of the child process. |
189 MemoryState OverrideState(MemoryState memroy_state, const ChildInfo& child); | 192 MemoryState OverrideState(MemoryState memroy_state, const ChildInfo& child); |
190 | 193 |
191 // Helper function of CreateHandle and AddChildForTesting. | 194 // Helper function of CreateHandle and AddChildForTesting. |
192 void CreateChildInfoMapEntry( | 195 void CreateChildInfoMapEntry( |
193 int render_process_id, | 196 int render_process_id, |
194 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | 197 std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |
195 | 198 |
196 // Updates the browser's memory state and notifies it to in-process clients. | 199 // Updates the browser's memory state and notifies it to in-process clients. |
197 void UpdateBrowserStateAndNotifyStateToClients(MemoryState state); | 200 void UpdateBrowserStateAndNotifyStateToClients(MemoryState state); |
198 | 201 |
199 // Notifies a state change to in-process clients. | 202 // Notifies a state change to in-process clients. |
200 void NotifyStateToClients(MemoryState state); | 203 void NotifyStateToClients(MemoryState state); |
201 | 204 |
202 // Notifies a state change to child processes. | 205 // Notifies a state change to child processes. |
203 void NotifyStateToChildren(MemoryState state); | 206 void NotifyStateToChildren(MemoryState state); |
204 | 207 |
208 // Called regularly while the memory condition is WARNING. | |
haraken
2017/03/24 11:55:39
periodically
bashi
2017/03/28 03:15:31
Done.
| |
209 void OnWarningCondition(); | |
210 | |
211 // Called regularly while the memory condition is CRITICAL. | |
haraken
2017/03/24 11:55:39
periodically
bashi
2017/03/28 03:15:31
Done.
| |
212 void OnCriticalCondition(); | |
213 | |
214 enum class PurgeTarget { | |
215 BACKGROUNDED, | |
216 ALL, | |
217 }; | |
218 | |
219 // Tries to find a candidate child process for purging memory and asks the | |
220 // child to purge memory. | |
221 bool TryPurgeMemoryFromChildren(PurgeTarget target); | |
haraken
2017/03/24 11:55:39
TryTo
bashi
2017/03/28 03:15:31
Done.
| |
222 | |
223 // Tries to purge memory from the browser process. | |
224 bool TryPurgeMemoryFromBrowser(); | |
haraken
2017/03/24 11:55:39
TryTo
bashi
2017/03/28 03:15:31
Done.
| |
225 | |
205 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 226 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
206 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | 227 std::unique_ptr<MemoryCoordinatorDelegate> delegate_; |
207 std::unique_ptr<MemoryMonitor> memory_monitor_; | 228 std::unique_ptr<MemoryMonitor> memory_monitor_; |
208 std::unique_ptr<MemoryConditionObserver> condition_observer_; | 229 std::unique_ptr<MemoryConditionObserver> condition_observer_; |
209 std::unique_ptr<base::TickClock> tick_clock_; | 230 std::unique_ptr<base::TickClock> tick_clock_; |
210 NotificationRegistrar notification_registrar_; | 231 NotificationRegistrar notification_registrar_; |
211 | 232 |
212 // The current memory condition. | 233 // The current memory condition. |
213 MemoryCondition memory_condition_ = MemoryCondition::NORMAL; | 234 MemoryCondition memory_condition_ = MemoryCondition::NORMAL; |
214 | 235 |
215 // The memory state of the browser process. | 236 // The memory state of the browser process. |
216 MemoryState browser_memory_state_ = MemoryState::NORMAL; | 237 MemoryState browser_memory_state_ = MemoryState::NORMAL; |
217 | 238 |
218 // The time tick of last state change of the browser process. | 239 // The time tick of last state change of the browser process. |
219 base::TimeTicks last_state_change_; | 240 base::TimeTicks last_state_change_; |
220 | 241 |
221 // Memory state for a process will remain unchanged until this period of time | 242 // Memory state for a process will remain unchanged until this period of time |
222 // passes. | 243 // passes. |
223 base::TimeDelta minimum_state_transition_period_; | 244 base::TimeDelta minimum_state_transition_period_; |
224 | 245 |
225 // Used to delay setting browser's memory state. Cancelable to avoid executing | 246 // Used to delay setting browser's memory state. Cancelable to avoid executing |
226 // multiple tasks in the same time frame. | 247 // multiple tasks in the same time frame. |
227 base::CancelableClosure delayed_browser_memory_state_setter_; | 248 base::CancelableClosure delayed_browser_memory_state_setter_; |
228 | 249 |
250 // If this isn't null, purging memory from the browser process is suppressed | |
251 // until this ticks is passed. | |
252 base::TimeTicks can_purge_after_; | |
253 | |
254 // When a child process remains backgrounded for this TimeDelta, the process | |
255 // becomes a candidate for purging memory on WARNING condition. | |
256 base::TimeDelta background_child_purge_candidate_period_; | |
257 | |
229 // Tracks child processes. An entry is added when a renderer connects to | 258 // Tracks child processes. An entry is added when a renderer connects to |
230 // MemoryCoordinator and removed automatically when an underlying binding is | 259 // MemoryCoordinator and removed automatically when an underlying binding is |
231 // disconnected. | 260 // disconnected. |
232 ChildInfoMap children_; | 261 ChildInfoMap children_; |
233 | 262 |
234 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | 263 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); |
235 }; | 264 }; |
236 | 265 |
237 } // namespace content | 266 } // namespace content |
238 | 267 |
239 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 268 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
OLD | NEW |