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

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: Added a browser test. 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 if the specified
151 // rendered is eligible for purging.
152 // TODO(tasak): rename this to CanPurgeBackgroundedRenderer.
151 bool CanSuspendBackgroundedRenderer(int render_process_id) const; 153 bool CanSuspendBackgroundedRenderer(int render_process_id) const;
152 154
153 // Returns true if |first| is considered less desirable to be killed than 155 // Returns true if |first| is considered less desirable to be killed than
154 // |second|. 156 // |second|.
155 static bool CompareTabStats(const TabStats& first, const TabStats& second); 157 static bool CompareTabStats(const TabStats& first, const TabStats& second);
156 158
157 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as 159 // 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. 160 // the WebContents could be deleted if the user closed the tab.
159 static int64_t IdFromWebContents(content::WebContents* web_contents); 161 static int64_t IdFromWebContents(content::WebContents* web_contents);
160 162
161 private: 163 private:
162 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, 164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, PurgeBackgroundRenderer);
163 ActivateTabResetPurgeAndSuspendState); 165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ActivateTabResetPurgeState);
164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, NextPurgeAndSuspendState); 166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ShouldPurgeAtDefaultTime);
167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DefaultTimeToPurgeInCorrectRange);
165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable); 168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable);
166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce); 169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce);
167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications); 170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications);
168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); 171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator);
169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); 172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime);
170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); 173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt);
171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL); 174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL);
172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); 175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage);
173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener); 176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener);
174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages); 177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages);
175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs); 178 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs);
176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs); 179 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs);
177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); 180 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu);
178 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); 181 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics);
179 FRIEND_TEST_ALL_PREFIXES(TabManagerWebContentsDataTest, PurgeAndSuspendState);
180 182
181 // The time of the first purging after a renderer is backgrounded. 183 // The time of the first purging after a renderer is backgrounded.
182 // The initial value was chosen because most of users activate backgrounded 184 // The initial value was chosen because most of users activate backgrounded
183 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active) 185 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active)
184 static constexpr base::TimeDelta kDefaultTimeToFirstPurge = 186 static constexpr base::TimeDelta kDefaultMinTimeToPurge =
185 base::TimeDelta::FromMinutes(30); 187 base::TimeDelta::FromMinutes(30);
186 188
187 // This is needed so WebContentsData can call OnDiscardedStateChange, and 189 // This is needed so WebContentsData can call OnDiscardedStateChange, and
188 // can use PurgeAndSuspendState. 190 // can use PurgeState.
189 friend class WebContentsData; 191 friend class WebContentsData;
190 192
191 // Called by WebContentsData whenever the discard state of a WebContents 193 // Called by WebContentsData whenever the discard state of a WebContents
192 // changes, so that observers can be informed. 194 // changes, so that observers can be informed.
193 void OnDiscardedStateChange(content::WebContents* contents, 195 void OnDiscardedStateChange(content::WebContents* contents,
194 bool is_discarded); 196 bool is_discarded);
195 197
196 // Called by WebContentsData whenever the auto-discardable state of a 198 // Called by WebContentsData whenever the auto-discardable state of a
197 // WebContents changes, so that observers can be informed. 199 // WebContents changes, so that observers can be informed.
198 void OnAutoDiscardableStateChange(content::WebContents* contents, 200 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. 253 // If |active_model| is true, consider its first tab as being active.
252 void AddTabStats(const TabStripModel* model, 254 void AddTabStats(const TabStripModel* model,
253 bool is_app, 255 bool is_app,
254 bool active_model, 256 bool active_model,
255 TabStatsList* stats_list) const; 257 TabStatsList* stats_list) const;
256 258
257 // Callback for when |update_timer_| fires. Takes care of executing the tasks 259 // Callback for when |update_timer_| fires. Takes care of executing the tasks
258 // that need to be run periodically (see comment in implementation). 260 // that need to be run periodically (see comment in implementation).
259 void UpdateTimerCallback(); 261 void UpdateTimerCallback();
260 262
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. 263 // Returns WebContents whose contents id matches the given tab_contents_id.
283 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const; 264 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const;
284 265
285 // Returns the next state of the purge and suspend. 266 // Returns a random time-to-purge whose min value is min_time_to_purge and max
286 PurgeAndSuspendState GetNextPurgeAndSuspendState( 267 // value is min_time_to_purge + kRangeBetweenMinAndMaxTimeToPurge.
287 content::WebContents* content, 268 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 269
291 // Purges and suspends renderers in backgrounded tabs. 270 // Returns whether to purge or not at the given time.
292 void PurgeAndSuspendBackgroundedTabs(); 271 bool ShouldPurgeAtTime(content::WebContents* content) const;
Wez 2017/03/03 21:06:51 ShouldPurgeNow()? Suggest: Returns true if the ta
tasak 2017/03/06 10:30:19 Done.
272
273 // Purges renderers in backgrounded tabs if the following conditions are
274 // satisfied:
275 // - the renderers are not purged yet,
276 // - the renderers are not playing media,
277 // (CanPurgeBackgroundedRenderer returns true)
278 // - the renderers are left inactive and background for time-to-purge.
279 // If renderers are purged, their internal states become 'purged'.
280 // The state is reset to be 'not purged' only when they are activated
281 // (=ActiveTabChanged is invoked).
282 void PurgeBackgroundedTabsIfNeeded();
293 283
294 // Does the actual discard by destroying the WebContents in |model| at |index| 284 // 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 285 // and replacing it by an empty one. Returns the new WebContents or NULL if
296 // the operation fails (return value used only in testing). 286 // the operation fails (return value used only in testing).
297 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model); 287 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model);
298 288
299 // Called by the memory pressure listener when the memory pressure rises. 289 // Called by the memory pressure listener when the memory pressure rises.
300 void OnMemoryPressure( 290 void OnMemoryPressure(
301 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); 291 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
302 292
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // used for statistics normalized by usage. 355 // used for statistics normalized by usage.
366 bool recent_tab_discard_; 356 bool recent_tab_discard_;
367 357
368 // Whether a tab can only ever discarded once. 358 // Whether a tab can only ever discarded once.
369 bool discard_once_; 359 bool discard_once_;
370 360
371 // This allows protecting tabs for a certain amount of time after being 361 // This allows protecting tabs for a certain amount of time after being
372 // backgrounded. 362 // backgrounded.
373 base::TimeDelta minimum_protection_time_; 363 base::TimeDelta minimum_protection_time_;
374 364
375 // A backgrounded renderer will be suspended when this time passes. 365 // A backgrounded renderer will be purged when this time passes.
376 base::TimeDelta time_to_first_suspension_; 366 base::TimeDelta min_time_to_purge_;
377 367
378 #if defined(OS_CHROMEOS) 368 #if defined(OS_CHROMEOS)
379 std::unique_ptr<TabManagerDelegate> delegate_; 369 std::unique_ptr<TabManagerDelegate> delegate_;
380 #endif 370 #endif
381 371
382 // Responsible for automatically registering this class as an observer of all 372 // Responsible for automatically registering this class as an observer of all
383 // TabStripModels. Automatically tracks browsers as they come and go. 373 // TabStripModels. Automatically tracks browsers as they come and go.
384 BrowserTabStripTracker browser_tab_strip_tracker_; 374 BrowserTabStripTracker browser_tab_strip_tracker_;
385 375
386 // Pointer to a test clock. If this is set, NowTicks() returns the value of 376 // 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 416
427 // Weak pointer factory used for posting delayed tasks to task_runner_. 417 // Weak pointer factory used for posting delayed tasks to task_runner_.
428 base::WeakPtrFactory<TabManager> weak_ptr_factory_; 418 base::WeakPtrFactory<TabManager> weak_ptr_factory_;
429 419
430 DISALLOW_COPY_AND_ASSIGN(TabManager); 420 DISALLOW_COPY_AND_ASSIGN(TabManager);
431 }; 421 };
432 422
433 } // namespace memory 423 } // namespace memory
434 424
435 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 425 #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_browsertest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698