Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: chrome/browser/memory/tab_manager.h

Issue 2711093002: Purge once random minutes(between 30min and 60min) after backgrounded. (Closed)
Patch Set: Fixed. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 // Returns the auto-discardable state of the tab. When true, the tab is 140 // Returns the auto-discardable state of the tab. When true, the tab is
141 // eligible to be automatically discarded when critical memory pressure hits, 141 // eligible to be automatically discarded when critical memory pressure hits,
142 // otherwise the tab is ignored and will never be automatically discarded. 142 // otherwise the tab is ignored and will never be automatically discarded.
143 // Note that this property doesn't block the discarding of the tab via other 143 // Note that this property doesn't block the discarding of the tab via other
144 // methods (about:discards for instance). 144 // methods (about:discards for instance).
145 bool IsTabAutoDiscardable(content::WebContents* contents) const; 145 bool IsTabAutoDiscardable(content::WebContents* contents) const;
146 146
147 // Sets/clears the auto-discardable state of the tab. 147 // Sets/clears the auto-discardable state of the tab.
148 void SetTabAutoDiscardableState(content::WebContents* contents, bool state); 148 void SetTabAutoDiscardableState(content::WebContents* contents, bool state);
149 149
150 // Returns true when a given renderer can suspend when it is backgrounded. 150 // Returns true when a given renderer can be purged when it is backgrounded.
Wez 2017/03/02 02:28:32 nit: Suggest ".. if the specified rendered is elig
tasak 2017/03/02 09:22:47 Done.
151 // TODO(tasak): rename this to CanPurgeBackgroundedRenderer.
151 bool CanSuspendBackgroundedRenderer(int render_process_id) const; 152 bool CanSuspendBackgroundedRenderer(int render_process_id) const;
152 153
153 // Returns true if |first| is considered less desirable to be killed than 154 // Returns true if |first| is considered less desirable to be killed than
154 // |second|. 155 // |second|.
155 static bool CompareTabStats(const TabStats& first, const TabStats& second); 156 static bool CompareTabStats(const TabStats& first, const TabStats& second);
156 157
157 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as 158 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as
158 // the WebContents could be deleted if the user closed the tab. 159 // the WebContents could be deleted if the user closed the tab.
159 static int64_t IdFromWebContents(content::WebContents* web_contents); 160 static int64_t IdFromWebContents(content::WebContents* web_contents);
160 161
161 private: 162 private:
162 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, 163 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ActivateTabResetPurgeState);
163 ActivateTabResetPurgeAndSuspendState); 164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, NextPurgeState);
164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, NextPurgeAndSuspendState);
165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable); 165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable);
166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce); 166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce);
167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications); 167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications);
168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); 168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator);
169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); 169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime);
170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); 170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt);
171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL); 171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL);
172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); 172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage);
173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener); 173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener);
174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages); 174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages);
175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs); 175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs);
176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs); 176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs);
177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); 177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu);
178 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); 178 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics);
179 FRIEND_TEST_ALL_PREFIXES(TabManagerWebContentsDataTest, PurgeAndSuspendState); 179 FRIEND_TEST_ALL_PREFIXES(TabManagerWebContentsDataTest, PurgeState);
180 180
181 // The time of the first purging after a renderer is backgrounded. 181 // The time of the first purging after a renderer is backgrounded.
182 // The initial value was chosen because most of users activate backgrounded 182 // The initial value was chosen because most of users activate backgrounded
183 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active) 183 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active)
184 static constexpr base::TimeDelta kDefaultTimeToFirstPurge = 184 static constexpr base::TimeDelta kDefaultMinTimeToPurge =
185 base::TimeDelta::FromMinutes(30); 185 base::TimeDelta::FromMinutes(30);
186 186
187 // This is needed so WebContentsData can call OnDiscardedStateChange, and 187 // This is needed so WebContentsData can call OnDiscardedStateChange, and
188 // can use PurgeAndSuspendState. 188 // can use PurgeState.
189 friend class WebContentsData; 189 friend class WebContentsData;
190 190
191 // Called by WebContentsData whenever the discard state of a WebContents 191 // Called by WebContentsData whenever the discard state of a WebContents
192 // changes, so that observers can be informed. 192 // changes, so that observers can be informed.
193 void OnDiscardedStateChange(content::WebContents* contents, 193 void OnDiscardedStateChange(content::WebContents* contents,
194 bool is_discarded); 194 bool is_discarded);
195 195
196 // Called by WebContentsData whenever the auto-discardable state of a 196 // Called by WebContentsData whenever the auto-discardable state of a
197 // WebContents changes, so that observers can be informed. 197 // WebContents changes, so that observers can be informed.
198 void OnAutoDiscardableStateChange(content::WebContents* contents, 198 void OnAutoDiscardableStateChange(content::WebContents* contents,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 // If |active_model| is true, consider its first tab as being active. 251 // If |active_model| is true, consider its first tab as being active.
252 void AddTabStats(const TabStripModel* model, 252 void AddTabStats(const TabStripModel* model,
253 bool is_app, 253 bool is_app,
254 bool active_model, 254 bool active_model,
255 TabStatsList* stats_list) const; 255 TabStatsList* stats_list) const;
256 256
257 // Callback for when |update_timer_| fires. Takes care of executing the tasks 257 // Callback for when |update_timer_| fires. Takes care of executing the tasks
258 // that need to be run periodically (see comment in implementation). 258 // that need to be run periodically (see comment in implementation).
259 void UpdateTimerCallback(); 259 void UpdateTimerCallback();
260 260
261 // Initially PurgeAndSuspendState is RUNNING.
262 // RUNNING => SUSPENDED
263 // - A tab has been backgrounded for more than purge-and-suspend-time
264 // seconds.
265 // SUSPENDED => RESUMED
266 // - A suspended tab is still suspended (i.e. last active time < last
267 // purge-and-suspend modified time), and
268 // - kMaxTimeRendererAllowedToBeSuspendedBeforeResume time passes since
269 // since the tab was suspended.
270 // RESUMED => SUSPENDED
271 // - A resumed tab is still backgrounded (i.e. last active time < last
272 // purge-and-suspend modified time), and
273 // - kSuspendedRendererLengthOfResumption time passes since the tab was
274 // resumed.
275 // SUSPENDED, RESUMED, RUNNING => RUNNING
276 // - When ActiveTabChaged, the newly activated tab's state will be RUNNING.
277 enum PurgeAndSuspendState {
278 RUNNING,
279 RESUMED,
280 SUSPENDED,
281 };
282 // Returns WebContents whose contents id matches the given tab_contents_id. 261 // Returns WebContents whose contents id matches the given tab_contents_id.
283 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const; 262 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const;
284 263
285 // Returns the next state of the purge and suspend. 264 // Returns a random time-to-purge whose min value is min_time_to_purge and max
286 PurgeAndSuspendState GetNextPurgeAndSuspendState( 265 // value is min_time_to_purge + kRangeBetweenMinAndMaxTimeToPurge.
Wez 2017/03/02 02:28:32 kRangeBetween... isn't defined anywhere in this he
tasak 2017/03/02 09:22:47 I would like to confirm your suggestion. So: - Th
Wez 2017/03/03 21:06:51 Sorry; I meant why does it need to be a member fun
tasak 2017/03/06 10:30:19 On 2017/03/03 21:06:51, Wez wrote: range at the fu
287 content::WebContents* content, 266 base::TimeDelta GetTimeToPurge(base::TimeDelta min_time_to_purge) const;
288 base::TimeTicks current_time,
289 const base::TimeDelta& time_to_first_suspension) const;
290 267
291 // Purges and suspends renderers in backgrounded tabs. 268 // Returns whether to purge or not at the given time.
292 void PurgeAndSuspendBackgroundedTabs(); 269 bool ShouldPurgeAtTime(content::WebContents* content,
270 base::TimeTicks time) const;
271
272 // Purges renderers in backgrounded tabs if the following conditions are
273 // satisfied:
274 // - the renderers are not purged yet,
275 // - the renderers are not playing media,
276 // (CanPurgeBackgroundedRenderer returns true)
277 // - the renderers are left inactive and background for time-to-purge.
278 // If renderers are purged, their internal states become 'purged'.
279 // The state is reset to be 'not purged' only when they are activated
280 // (=ActiveTabChanged is invoked).
281 void PurgeBackgroundedTabsIfNeeded();
293 282
294 // Does the actual discard by destroying the WebContents in |model| at |index| 283 // Does the actual discard by destroying the WebContents in |model| at |index|
295 // and replacing it by an empty one. Returns the new WebContents or NULL if 284 // and replacing it by an empty one. Returns the new WebContents or NULL if
296 // the operation fails (return value used only in testing). 285 // the operation fails (return value used only in testing).
297 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model); 286 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model);
298 287
299 // Called by the memory pressure listener when the memory pressure rises. 288 // Called by the memory pressure listener when the memory pressure rises.
300 void OnMemoryPressure( 289 void OnMemoryPressure(
301 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); 290 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
302 291
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // used for statistics normalized by usage. 354 // used for statistics normalized by usage.
366 bool recent_tab_discard_; 355 bool recent_tab_discard_;
367 356
368 // Whether a tab can only ever discarded once. 357 // Whether a tab can only ever discarded once.
369 bool discard_once_; 358 bool discard_once_;
370 359
371 // This allows protecting tabs for a certain amount of time after being 360 // This allows protecting tabs for a certain amount of time after being
372 // backgrounded. 361 // backgrounded.
373 base::TimeDelta minimum_protection_time_; 362 base::TimeDelta minimum_protection_time_;
374 363
375 // A backgrounded renderer will be suspended when this time passes. 364 // A backgrounded renderer will be purged when this time passes.
376 base::TimeDelta time_to_first_suspension_; 365 base::TimeDelta min_time_to_purge_;
377 366
378 #if defined(OS_CHROMEOS) 367 #if defined(OS_CHROMEOS)
379 std::unique_ptr<TabManagerDelegate> delegate_; 368 std::unique_ptr<TabManagerDelegate> delegate_;
380 #endif 369 #endif
381 370
382 // Responsible for automatically registering this class as an observer of all 371 // Responsible for automatically registering this class as an observer of all
383 // TabStripModels. Automatically tracks browsers as they come and go. 372 // TabStripModels. Automatically tracks browsers as they come and go.
384 BrowserTabStripTracker browser_tab_strip_tracker_; 373 BrowserTabStripTracker browser_tab_strip_tracker_;
385 374
386 // Pointer to a test clock. If this is set, NowTicks() returns the value of 375 // Pointer to a test clock. If this is set, NowTicks() returns the value of
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 415
427 // Weak pointer factory used for posting delayed tasks to task_runner_. 416 // Weak pointer factory used for posting delayed tasks to task_runner_.
428 base::WeakPtrFactory<TabManager> weak_ptr_factory_; 417 base::WeakPtrFactory<TabManager> weak_ptr_factory_;
429 418
430 DISALLOW_COPY_AND_ASSIGN(TabManager); 419 DISALLOW_COPY_AND_ASSIGN(TabManager);
431 }; 420 };
432 421
433 } // namespace memory 422 } // namespace memory
434 423
435 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 424 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/memory/tab_manager.cc » ('j') | chrome/browser/memory/tab_manager_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698