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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 periodically while the memory condition is WARNING. |
| 209 void OnWarningCondition(); |
| 210 |
| 211 // Called periodically while the memory condition is CRITICAL. |
| 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 TryToPurgeMemoryFromChildren(PurgeTarget target); |
| 222 |
| 223 // Tries to purge memory from the browser process. |
| 224 bool TryToPurgeMemoryFromBrowser(); |
| 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 |
229 // Tracks child processes. An entry is added when a renderer connects to | 254 // Tracks child processes. An entry is added when a renderer connects to |
230 // MemoryCoordinator and removed automatically when an underlying binding is | 255 // MemoryCoordinator and removed automatically when an underlying binding is |
231 // disconnected. | 256 // disconnected. |
232 ChildInfoMap children_; | 257 ChildInfoMap children_; |
233 | 258 |
234 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); | 259 DISALLOW_COPY_AND_ASSIGN(MemoryCoordinatorImpl); |
235 }; | 260 }; |
236 | 261 |
237 } // namespace content | 262 } // namespace content |
238 | 263 |
239 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ | 264 #endif // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_IMPL_H_ |
OLD | NEW |