| OLD | NEW |
| 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> |
| 11 #include <set> | |
| 12 #include <string> | 11 #include <string> |
| 13 #include <utility> | 12 #include <utility> |
| 14 #include <vector> | 13 #include <vector> |
| 15 | 14 |
| 16 #include "base/callback.h" | 15 #include "base/callback.h" |
| 17 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 18 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 19 #include "base/macros.h" | 18 #include "base/macros.h" |
| 20 #include "base/memory/memory_pressure_listener.h" | 19 #include "base/memory/memory_pressure_listener.h" |
| 21 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 22 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 23 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
| 24 #include "base/task_runner.h" | |
| 25 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 26 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 27 #include "chrome/browser/memory/tab_manager_observer.h" | 25 #include "chrome/browser/memory/tab_manager_observer.h" |
| 28 #include "chrome/browser/memory/tab_stats.h" | 26 #include "chrome/browser/memory/tab_stats.h" |
| 29 #include "chrome/browser/ui/browser_tab_strip_tracker.h" | 27 #include "chrome/browser/ui/browser_tab_strip_tracker.h" |
| 30 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
| 31 | 29 |
| 32 class BrowserList; | 30 class BrowserList; |
| 33 class GURL; | 31 class GURL; |
| 34 class TabStripModel; | 32 class TabStripModel; |
| 35 | 33 |
| 36 namespace base { | 34 namespace base { |
| 37 class TickClock; | 35 class TickClock; |
| 38 } | 36 } |
| 39 | 37 |
| 40 namespace content { | 38 namespace content { |
| 41 class RenderProcessHost; | |
| 42 class WebContents; | 39 class WebContents; |
| 43 } | 40 } |
| 44 | 41 |
| 45 namespace memory { | 42 namespace memory { |
| 46 | 43 |
| 47 #if defined(OS_CHROMEOS) | 44 #if defined(OS_CHROMEOS) |
| 48 class TabManagerDelegate; | 45 class TabManagerDelegate; |
| 49 #endif | 46 #endif |
| 50 | 47 |
| 51 // The TabManager periodically updates (see | 48 // The TabManager periodically updates (see |
| (...skipping 28 matching lines...) Expand all Loading... |
| 80 bool recent_tab_discard() const { return recent_tab_discard_; } | 77 bool recent_tab_discard() const { return recent_tab_discard_; } |
| 81 | 78 |
| 82 // Start/Stop the Tab Manager. | 79 // Start/Stop the Tab Manager. |
| 83 void Start(); | 80 void Start(); |
| 84 void Stop(); | 81 void Stop(); |
| 85 | 82 |
| 86 // Returns the list of the stats for all renderers. Must be called on the UI | 83 // Returns the list of the stats for all renderers. Must be called on the UI |
| 87 // thread. The returned list is sorted by reversed importance. | 84 // thread. The returned list is sorted by reversed importance. |
| 88 TabStatsList GetTabStats() const; | 85 TabStatsList GetTabStats() const; |
| 89 | 86 |
| 90 // Returns a sorted list of renderers, from most important to least important. | |
| 91 std::vector<content::RenderProcessHost*> GetOrderedRenderers() const; | |
| 92 | |
| 93 // Returns true if |contents| is currently discarded. | 87 // Returns true if |contents| is currently discarded. |
| 94 bool IsTabDiscarded(content::WebContents* contents) const; | 88 bool IsTabDiscarded(content::WebContents* contents) const; |
| 95 | 89 |
| 96 // Goes through a list of checks to see if a tab is allowed to be discarded by | 90 // Goes through a list of checks to see if a tab is allowed to be discarded by |
| 97 // the automatic tab discarding mechanism. Note that this is not used when | 91 // the automatic tab discarding mechanism. Note that this is not used when |
| 98 // discarding a particular tab from about:discards. | 92 // discarding a particular tab from about:discards. |
| 99 bool CanDiscardTab(int64_t target_web_contents_id) const; | 93 bool CanDiscardTab(int64_t target_web_contents_id) const; |
| 100 | 94 |
| 101 // Discards a tab to free the memory occupied by its renderer. The tab still | 95 // Discards a tab to free the memory occupied by its renderer. The tab still |
| 102 // exists in the tab-strip; clicking on it will reload it. | 96 // exists in the tab-strip; clicking on it will reload it. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // Called by WebContentsData whenever the discard state of a WebContents | 196 // Called by WebContentsData whenever the discard state of a WebContents |
| 203 // changes, so that observers can be informed. | 197 // changes, so that observers can be informed. |
| 204 void OnDiscardedStateChange(content::WebContents* contents, | 198 void OnDiscardedStateChange(content::WebContents* contents, |
| 205 bool is_discarded); | 199 bool is_discarded); |
| 206 | 200 |
| 207 // Called by WebContentsData whenever the auto-discardable state of a | 201 // Called by WebContentsData whenever the auto-discardable state of a |
| 208 // WebContents changes, so that observers can be informed. | 202 // WebContents changes, so that observers can be informed. |
| 209 void OnAutoDiscardableStateChange(content::WebContents* contents, | 203 void OnAutoDiscardableStateChange(content::WebContents* contents, |
| 210 bool is_auto_discardable); | 204 bool is_auto_discardable); |
| 211 | 205 |
| 212 // The time that a renderer is given to react to a memory pressure | |
| 213 // notification before another renderer is also notified. This prevents all | |
| 214 // renderers from receiving and acting upon notifications simultaneously, | |
| 215 // which can quickly overload a system. Exposed for unittesting. | |
| 216 // NOTE: This value needs to be big enough to allow a process to get over the | |
| 217 // hump in responding to memory pressure, so there aren't multiple processes | |
| 218 // fighting for CPU and worse, temporary memory, while trying to free things | |
| 219 // up. Similarly, it shouldn't be too large otherwise it will take too long | |
| 220 // for the entire system to respond. Ideally, there would be a callback from a | |
| 221 // child process indicating that the message has been handled. In the meantime | |
| 222 // this is chosen to be sufficient to allow a worst-case V8+Oilpan GC to run, | |
| 223 // with a little slop. | |
| 224 enum : int { kRendererNotificationDelayInSeconds = 2 }; | |
| 225 | |
| 226 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | |
| 227 | |
| 228 // A callback that returns the current memory pressure level. | |
| 229 using MemoryPressureLevelCallback = base::Callback<MemoryPressureLevel()>; | |
| 230 | |
| 231 // Callback that notifies a |renderer| of the memory pressure at a given | |
| 232 // |level|. Provides a testing seam. | |
| 233 using RendererNotificationCallback = base::Callback< | |
| 234 void(const content::RenderProcessHost* /* renderer */, | |
| 235 MemoryPressureLevel /* level */)>; | |
| 236 | |
| 237 static void PurgeMemoryAndDiscardTab(); | 206 static void PurgeMemoryAndDiscardTab(); |
| 238 | 207 |
| 239 // Returns true if the |url| represents an internal Chrome web UI page that | 208 // Returns true if the |url| represents an internal Chrome web UI page that |
| 240 // can be easily reloaded and hence makes a good choice to discard. | 209 // can be easily reloaded and hence makes a good choice to discard. |
| 241 static bool IsInternalPage(const GURL& url); | 210 static bool IsInternalPage(const GURL& url); |
| 242 | 211 |
| 243 // Records UMA histogram statistics for a tab discard. Record statistics for | 212 // Records UMA histogram statistics for a tab discard. Record statistics for |
| 244 // user triggered discards via chrome://discards/ because that allows to | 213 // user triggered discards via chrome://discards/ because that allows to |
| 245 // manually test the system. | 214 // manually test the system. |
| 246 void RecordDiscardStatistics(); | 215 void RecordDiscardStatistics(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 bool IsMediaTab(content::WebContents* contents) const; | 288 bool IsMediaTab(content::WebContents* contents) const; |
| 320 | 289 |
| 321 // Returns the WebContentsData associated with |contents|. Also takes care of | 290 // Returns the WebContentsData associated with |contents|. Also takes care of |
| 322 // creating one if needed. | 291 // creating one if needed. |
| 323 WebContentsData* GetWebContentsData(content::WebContents* contents) const; | 292 WebContentsData* GetWebContentsData(content::WebContents* contents) const; |
| 324 | 293 |
| 325 // Returns either the system's clock or the test clock. See |test_tick_clock_| | 294 // Returns either the system's clock or the test clock. See |test_tick_clock_| |
| 326 // for more details. | 295 // for more details. |
| 327 base::TimeTicks NowTicks() const; | 296 base::TimeTicks NowTicks() const; |
| 328 | 297 |
| 329 // Dispatches a memory pressure message to a single child process, and | |
| 330 // schedules another call to itself as long as memory pressure continues. | |
| 331 void DoChildProcessDispatch(); | |
| 332 | |
| 333 // Implementation of DiscardTab. Returns null if no tab was discarded. | 298 // Implementation of DiscardTab. Returns null if no tab was discarded. |
| 334 // Otherwise returns the new web_contents of the discarded tab. | 299 // Otherwise returns the new web_contents of the discarded tab. |
| 335 content::WebContents* DiscardTabImpl(); | 300 content::WebContents* DiscardTabImpl(); |
| 336 | 301 |
| 337 // Returns true if tabs can be discarded only once. | 302 // Returns true if tabs can be discarded only once. |
| 338 bool CanOnlyDiscardOnce() const; | 303 bool CanOnlyDiscardOnce() const; |
| 339 | 304 |
| 340 // Timer to periodically update the stats of the renderers. | 305 // Timer to periodically update the stats of the renderers. |
| 341 base::RepeatingTimer update_timer_; | 306 base::RepeatingTimer update_timer_; |
| 342 | 307 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 #endif | 345 #endif |
| 381 | 346 |
| 382 // Responsible for automatically registering this class as an observer of all | 347 // Responsible for automatically registering this class as an observer of all |
| 383 // TabStripModels. Automatically tracks browsers as they come and go. | 348 // TabStripModels. Automatically tracks browsers as they come and go. |
| 384 BrowserTabStripTracker browser_tab_strip_tracker_; | 349 BrowserTabStripTracker browser_tab_strip_tracker_; |
| 385 | 350 |
| 386 // Pointer to a test clock. If this is set, NowTicks() returns the value of | 351 // Pointer to a test clock. If this is set, NowTicks() returns the value of |
| 387 // this test clock. Otherwise it returns the system clock's value. | 352 // this test clock. Otherwise it returns the system clock's value. |
| 388 base::TickClock* test_tick_clock_; | 353 base::TickClock* test_tick_clock_; |
| 389 | 354 |
| 390 // The task runner used for child process notifications. Defaults to the | |
| 391 // thread task runner handle that is used by the memory pressure subsystem, | |
| 392 // but may be explicitly set for unittesting. | |
| 393 scoped_refptr<base::TaskRunner> task_runner_; | |
| 394 | |
| 395 // Indicates that the system is currently experiencing memory pressure. Used | |
| 396 // to determine whether a new round of child-process memory pressure | |
| 397 // dispatches is starting, or whether an existing one is continuing. | |
| 398 bool under_memory_pressure_; | |
| 399 | |
| 400 // The set of child renderers that have received memory pressure notifications | |
| 401 // during the current bout of memory pressure. This is emptied when all | |
| 402 // children have been notified (restarting another round of notification) and | |
| 403 // when a bout of memory pressure ends. | |
| 404 std::set<const content::RenderProcessHost*> notified_renderers_; | |
| 405 | |
| 406 // The callback that returns the current memory pressure level. Defaults to | |
| 407 // querying base::MemoryPressureMonitor, but can be overridden for testing. | |
| 408 MemoryPressureLevelCallback get_current_pressure_level_; | |
| 409 | |
| 410 // The callback to be invoked to notify renderers. Defaults to calling | |
| 411 // content::SendPressureNotification, but can be overridden for testing. | |
| 412 RendererNotificationCallback notify_renderer_process_; | |
| 413 | |
| 414 // Injected tab strip models. Allows this to be tested end-to-end without | 355 // Injected tab strip models. Allows this to be tested end-to-end without |
| 415 // requiring a full browser environment. If specified these tab strips will be | 356 // requiring a full browser environment. If specified these tab strips will be |
| 416 // crawled as the authoritative source of tabs, otherwise the BrowserList and | 357 // crawled as the authoritative source of tabs, otherwise the BrowserList and |
| 417 // associated Browser objects are crawled. The first of these is considered to | 358 // associated Browser objects are crawled. The first of these is considered to |
| 418 // be the 'active' tab strip model. | 359 // be the 'active' tab strip model. |
| 419 // TODO(chrisha): Factor out tab-strip model enumeration to a helper class, | 360 // TODO(chrisha): Factor out tab-strip model enumeration to a helper class, |
| 420 // and make a delegate that centralizes all testing seams. | 361 // and make a delegate that centralizes all testing seams. |
| 421 using TestTabStripModel = std::pair<const TabStripModel*, bool>; | 362 using TestTabStripModel = std::pair<const TabStripModel*, bool>; |
| 422 std::vector<TestTabStripModel> test_tab_strip_models_; | 363 std::vector<TestTabStripModel> test_tab_strip_models_; |
| 423 | 364 |
| 424 // List of observers that will receive notifications on state changes. | 365 // List of observers that will receive notifications on state changes. |
| 425 base::ObserverList<TabManagerObserver> observers_; | 366 base::ObserverList<TabManagerObserver> observers_; |
| 426 | 367 |
| 427 // Weak pointer factory used for posting delayed tasks to task_runner_. | 368 // Weak pointer factory used for posting delayed tasks. |
| 428 base::WeakPtrFactory<TabManager> weak_ptr_factory_; | 369 base::WeakPtrFactory<TabManager> weak_ptr_factory_; |
| 429 | 370 |
| 430 DISALLOW_COPY_AND_ASSIGN(TabManager); | 371 DISALLOW_COPY_AND_ASSIGN(TabManager); |
| 431 }; | 372 }; |
| 432 | 373 |
| 433 } // namespace memory | 374 } // namespace memory |
| 434 | 375 |
| 435 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ | 376 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ |
| OLD | NEW |