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: s/RUNNING/NOT_PURGED/g Created 3 years, 10 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 152
153 // Returns true if |first| is considered less desirable to be killed than 153 // Returns true if |first| is considered less desirable to be killed than
154 // |second|. 154 // |second|.
155 static bool CompareTabStats(const TabStats& first, const TabStats& second); 155 static bool CompareTabStats(const TabStats& first, const TabStats& second);
156 156
157 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as 157 // 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. 158 // the WebContents could be deleted if the user closed the tab.
159 static int64_t IdFromWebContents(content::WebContents* web_contents); 159 static int64_t IdFromWebContents(content::WebContents* web_contents);
160 160
161 private: 161 private:
162 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, 162 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ActivateTabResetPurgeState);
163 ActivateTabResetPurgeAndSuspendState); 163 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, NextPurgeState);
164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, NextPurgeAndSuspendState);
165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable); 164 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, AutoDiscardable);
166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce); 165 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, CanOnlyDiscardOnce);
167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications); 166 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ChildProcessNotifications);
168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); 167 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator);
169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); 168 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime);
170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); 169 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt);
171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL); 170 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, InvalidOrEmptyURL);
172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); 171 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage);
173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener); 172 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, OomPressureListener);
174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages); 173 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectPDFPages);
175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs); 174 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectRecentlyUsedTabs);
176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs); 175 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ProtectVideoTabs);
177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); 176 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu);
178 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); 177 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics);
179 FRIEND_TEST_ALL_PREFIXES(TabManagerWebContentsDataTest, PurgeAndSuspendState); 178 FRIEND_TEST_ALL_PREFIXES(TabManagerWebContentsDataTest, PurgeState);
180 179
181 // The time of the first purging after a renderer is backgrounded. 180 // The time of the first purging after a renderer is backgrounded.
182 // The initial value was chosen because most of users activate backgrounded 181 // The initial value was chosen because most of users activate backgrounded
183 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active) 182 // tabs within 30 minutes. (c.f. Tabs.StateTransfer.Time_Inactive_Active)
184 static constexpr base::TimeDelta kDefaultTimeToFirstPurge = 183 static constexpr base::TimeDelta kDefaultTimeToFirstPurge =
185 base::TimeDelta::FromMinutes(30); 184 base::TimeDelta::FromMinutes(30);
186 185
187 // This is needed so WebContentsData can call OnDiscardedStateChange, and 186 // This is needed so WebContentsData can call OnDiscardedStateChange, and
188 // can use PurgeAndSuspendState. 187 // can use PurgeState.
189 friend class WebContentsData; 188 friend class WebContentsData;
190 189
191 // Called by WebContentsData whenever the discard state of a WebContents 190 // Called by WebContentsData whenever the discard state of a WebContents
192 // changes, so that observers can be informed. 191 // changes, so that observers can be informed.
193 void OnDiscardedStateChange(content::WebContents* contents, 192 void OnDiscardedStateChange(content::WebContents* contents,
194 bool is_discarded); 193 bool is_discarded);
195 194
196 // Called by WebContentsData whenever the auto-discardable state of a 195 // Called by WebContentsData whenever the auto-discardable state of a
197 // WebContents changes, so that observers can be informed. 196 // WebContents changes, so that observers can be informed.
198 void OnAutoDiscardableStateChange(content::WebContents* contents, 197 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. 250 // If |active_model| is true, consider its first tab as being active.
252 void AddTabStats(const TabStripModel* model, 251 void AddTabStats(const TabStripModel* model,
253 bool is_app, 252 bool is_app,
254 bool active_model, 253 bool active_model,
255 TabStatsList* stats_list) const; 254 TabStatsList* stats_list) const;
256 255
257 // Callback for when |update_timer_| fires. Takes care of executing the tasks 256 // Callback for when |update_timer_| fires. Takes care of executing the tasks
258 // that need to be run periodically (see comment in implementation). 257 // that need to be run periodically (see comment in implementation).
259 void UpdateTimerCallback(); 258 void UpdateTimerCallback();
260 259
261 // Initially PurgeAndSuspendState is RUNNING. 260 // Initially PurgeState is NOT_PURGED.
262 // RUNNING => SUSPENDED 261 // NOT_PURGED => PURGED
263 // - A tab has been backgrounded for more than purge-and-suspend-time 262 // - A tab has been backgrounded for more than purge-and-suspend-time
264 // seconds. 263 // seconds.
265 // SUSPENDED => RESUMED 264 // PURGED => NOT_PURGED
266 // - A suspended tab is still suspended (i.e. last active time < last 265 // - When ActiveTabChaged, the newly activated tab's state will be NOT_PURGED.
Wez 2017/02/27 22:20:56 typo: ActiveTabChanged The naming here implies th
tasak 2017/02/28 09:45:37 The new code doesn't purge every 20 minutes, becau
Wez 2017/03/02 02:28:32 I thought the idea was to re-purge each background
tasak 2017/03/02 09:22:47 As far as I understand, we have a strong concern t
267 // purge-and-suspend modified time), and 266 enum PurgeState {
Wez 2017/02/27 22:20:56 Note that as per style-guide, this and the other e
tasak 2017/02/28 09:45:37 Removed.
268 // - kMaxTimeRendererAllowedToBeSuspendedBeforeResume time passes since 267 NOT_PURGED,
269 // since the tab was suspended. 268 PURGED,
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 }; 269 };
282 // Returns WebContents whose contents id matches the given tab_contents_id. 270 // Returns WebContents whose contents id matches the given tab_contents_id.
283 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const; 271 content::WebContents* GetWebContentsById(int64_t tab_contents_id) const;
Wez 2017/02/27 22:20:55 Why is this getter placed randomly in-between the
tasak 2017/02/28 09:45:37 Removed.
284 272
285 // Returns the next state of the purge and suspend. 273 // Returns the next state of the purge and suspend.
Wez 2017/02/27 22:20:55 The function is called GetNextPurgeState() so the
tasak 2017/02/28 09:45:37 Replaced the method with ShouldPurgeAtTime. On 20
Wez 2017/03/02 02:28:32 The class has a set_test_tick_clock() method, thou
tasak 2017/03/02 09:22:46 I see. Done.
286 PurgeAndSuspendState GetNextPurgeAndSuspendState( 274 PurgeState GetNextPurgeState(content::WebContents* content,
287 content::WebContents* content, 275 base::TimeTicks current_time) const;
288 base::TimeTicks current_time,
289 const base::TimeDelta& time_to_first_suspension) const;
290 276
291 // Purges and suspends renderers in backgrounded tabs. 277 // Purges renderers in backgrounded tabs.
Wez 2017/02/27 22:20:56 Again, the reader can tell that from the name; wha
tasak 2017/02/28 09:45:37 Done.
292 void PurgeAndSuspendBackgroundedTabs(); 278 void PurgeBackgroundedTabs();
293 279
294 // Does the actual discard by destroying the WebContents in |model| at |index| 280 // 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 281 // and replacing it by an empty one. Returns the new WebContents or NULL if
296 // the operation fails (return value used only in testing). 282 // the operation fails (return value used only in testing).
297 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model); 283 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model);
298 284
299 // Called by the memory pressure listener when the memory pressure rises. 285 // Called by the memory pressure listener when the memory pressure rises.
300 void OnMemoryPressure( 286 void OnMemoryPressure(
301 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); 287 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
302 288
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // used for statistics normalized by usage. 351 // used for statistics normalized by usage.
366 bool recent_tab_discard_; 352 bool recent_tab_discard_;
367 353
368 // Whether a tab can only ever discarded once. 354 // Whether a tab can only ever discarded once.
369 bool discard_once_; 355 bool discard_once_;
370 356
371 // This allows protecting tabs for a certain amount of time after being 357 // This allows protecting tabs for a certain amount of time after being
372 // backgrounded. 358 // backgrounded.
373 base::TimeDelta minimum_protection_time_; 359 base::TimeDelta minimum_protection_time_;
374 360
375 // A backgrounded renderer will be suspended when this time passes. 361 // A backgrounded renderer will be purged when this time passes.
376 base::TimeDelta time_to_first_suspension_; 362 base::TimeDelta time_to_first_purge_;
Wez 2017/02/27 22:20:56 This doesn't change over the lifetime of the TabMa
tasak 2017/02/28 09:45:39 So is it possible to initialize the value by using
377 363
378 #if defined(OS_CHROMEOS) 364 #if defined(OS_CHROMEOS)
379 std::unique_ptr<TabManagerDelegate> delegate_; 365 std::unique_ptr<TabManagerDelegate> delegate_;
380 #endif 366 #endif
381 367
382 // Responsible for automatically registering this class as an observer of all 368 // Responsible for automatically registering this class as an observer of all
383 // TabStripModels. Automatically tracks browsers as they come and go. 369 // TabStripModels. Automatically tracks browsers as they come and go.
384 BrowserTabStripTracker browser_tab_strip_tracker_; 370 BrowserTabStripTracker browser_tab_strip_tracker_;
385 371
386 // Pointer to a test clock. If this is set, NowTicks() returns the value of 372 // 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 412
427 // Weak pointer factory used for posting delayed tasks to task_runner_. 413 // Weak pointer factory used for posting delayed tasks to task_runner_.
428 base::WeakPtrFactory<TabManager> weak_ptr_factory_; 414 base::WeakPtrFactory<TabManager> weak_ptr_factory_;
429 415
430 DISALLOW_COPY_AND_ASSIGN(TabManager); 416 DISALLOW_COPY_AND_ASSIGN(TabManager);
431 }; 417 };
432 418
433 } // namespace memory 419 } // namespace memory
434 420
435 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ 421 #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.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698