| 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 #include "chrome/browser/memory/tab_manager.h" | 5 #include "chrome/browser/resource_coordinator/tab_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/feature_list.h" | 16 #include "base/feature_list.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/memory_pressure_monitor.h" | 18 #include "base/memory/memory_pressure_monitor.h" |
| 19 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
| 20 #include "base/metrics/histogram_macros.h" | 20 #include "base/metrics/histogram_macros.h" |
| 21 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 22 #include "base/process/process.h" | 22 #include "base/process/process.h" |
| 23 #include "base/rand_util.h" | 23 #include "base/rand_util.h" |
| 24 #include "base/strings/string16.h" | 24 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| 26 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 27 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 28 #include "base/threading/thread.h" | 28 #include "base/threading/thread.h" |
| 29 #include "base/time/tick_clock.h" | 29 #include "base/time/tick_clock.h" |
| 30 #include "build/build_config.h" | 30 #include "build/build_config.h" |
| 31 #include "chrome/browser/browser_process.h" | 31 #include "chrome/browser/browser_process.h" |
| 32 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h" | 32 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h" |
| 33 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h" | 33 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h" |
| 34 #include "chrome/browser/memory/oom_memory_details.h" | 34 #include "chrome/browser/memory/oom_memory_details.h" |
| 35 #include "chrome/browser/memory/tab_manager_observer.h" | |
| 36 #include "chrome/browser/memory/tab_manager_web_contents_data.h" | |
| 37 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 36 #include "chrome/browser/resource_coordinator/tab_manager_observer.h" |
| 37 #include "chrome/browser/resource_coordinator/tab_manager_web_contents_data.h" |
| 38 #include "chrome/browser/ui/browser.h" | 38 #include "chrome/browser/ui/browser.h" |
| 39 #include "chrome/browser/ui/browser_list.h" | 39 #include "chrome/browser/ui/browser_list.h" |
| 40 #include "chrome/browser/ui/browser_window.h" | 40 #include "chrome/browser/ui/browser_window.h" |
| 41 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 41 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 42 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 42 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 43 #include "chrome/browser/ui/tabs/tab_utils.h" | 43 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 44 #include "chrome/common/chrome_constants.h" | 44 #include "chrome/common/chrome_constants.h" |
| 45 #include "chrome/common/chrome_features.h" | 45 #include "chrome/common/chrome_features.h" |
| 46 #include "chrome/common/chrome_switches.h" | 46 #include "chrome/common/chrome_switches.h" |
| 47 #include "chrome/common/url_constants.h" | 47 #include "chrome/common/url_constants.h" |
| 48 #include "components/metrics/system_memory_stats_recorder.h" | 48 #include "components/metrics/system_memory_stats_recorder.h" |
| 49 #include "components/variations/variations_associated_data.h" | 49 #include "components/variations/variations_associated_data.h" |
| 50 #include "content/public/browser/browser_thread.h" | 50 #include "content/public/browser/browser_thread.h" |
| 51 #include "content/public/browser/navigation_controller.h" | 51 #include "content/public/browser/navigation_controller.h" |
| 52 #include "content/public/browser/render_process_host.h" | 52 #include "content/public/browser/render_process_host.h" |
| 53 #include "content/public/browser/web_contents.h" | 53 #include "content/public/browser/web_contents.h" |
| 54 #include "content/public/common/page_importance_signals.h" | 54 #include "content/public/common/page_importance_signals.h" |
| 55 | 55 |
| 56 #if defined(OS_CHROMEOS) | 56 #if defined(OS_CHROMEOS) |
| 57 #include "ash/multi_profile_uma.h" | 57 #include "ash/multi_profile_uma.h" |
| 58 #include "ash/shell_port.h" | 58 #include "ash/shell_port.h" |
| 59 #include "chrome/browser/memory/tab_manager_delegate_chromeos.h" | 59 #include "chrome/browser/resource_coordinator/tab_manager_delegate_chromeos.h" |
| 60 #include "components/user_manager/user_manager.h" | 60 #include "components/user_manager/user_manager.h" |
| 61 #endif | 61 #endif |
| 62 | 62 |
| 63 using base::TimeDelta; | 63 using base::TimeDelta; |
| 64 using base::TimeTicks; | 64 using base::TimeTicks; |
| 65 using content::BrowserThread; | 65 using content::BrowserThread; |
| 66 using content::WebContents; | 66 using content::WebContents; |
| 67 | 67 |
| 68 namespace memory { | 68 namespace resource_coordinator { |
| 69 namespace { | 69 namespace { |
| 70 | 70 |
| 71 // The default interval in seconds after which to adjust the oom_score_adj | 71 // The default interval in seconds after which to adjust the oom_score_adj |
| 72 // value. | 72 // value. |
| 73 const int kAdjustmentIntervalSeconds = 10; | 73 const int kAdjustmentIntervalSeconds = 10; |
| 74 | 74 |
| 75 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 75 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 76 // For each period of this length record a statistic to indicate whether or not | 76 // For each period of this length record a statistic to indicate whether or not |
| 77 // the user experienced a low memory event. If this interval is changed, | 77 // the user experienced a low memory event. If this interval is changed, |
| 78 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic. | 78 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 149 |
| 150 // Check if only one discard is allowed. | 150 // Check if only one discard is allowed. |
| 151 discard_once_ = CanOnlyDiscardOnce(); | 151 discard_once_ = CanOnlyDiscardOnce(); |
| 152 | 152 |
| 153 if (!update_timer_.IsRunning()) { | 153 if (!update_timer_.IsRunning()) { |
| 154 update_timer_.Start(FROM_HERE, | 154 update_timer_.Start(FROM_HERE, |
| 155 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), | 155 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), |
| 156 this, &TabManager::UpdateTimerCallback); | 156 this, &TabManager::UpdateTimerCallback); |
| 157 } | 157 } |
| 158 | 158 |
| 159 // MemoryPressureMonitor is not implemented on Linux so far and tabs are never | 159 // MemoryPressureMonitor is not implemented on Linux so far and tabs are never |
| 160 // discarded. | 160 // discarded. |
| 161 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 161 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 162 if (!recent_tab_discard_timer_.IsRunning()) { | 162 if (!recent_tab_discard_timer_.IsRunning()) { |
| 163 recent_tab_discard_timer_.Start( | 163 recent_tab_discard_timer_.Start( |
| 164 FROM_HERE, TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), | 164 FROM_HERE, TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), |
| 165 this, &TabManager::RecordRecentTabDiscard); | 165 this, &TabManager::RecordRecentTabDiscard); |
| 166 } | 166 } |
| 167 start_time_ = NowTicks(); | 167 start_time_ = NowTicks(); |
| 168 // Create a |MemoryPressureListener| to listen for memory events. | 168 // Create a |MemoryPressureListener| to listen for memory events. |
| 169 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); | 169 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); |
| 170 if (monitor) { | 170 if (monitor) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 327 } |
| 328 | 328 |
| 329 void TabManager::LogMemoryAndDiscardTab() { | 329 void TabManager::LogMemoryAndDiscardTab() { |
| 330 LogMemory("Tab Discards Memory details", | 330 LogMemory("Tab Discards Memory details", |
| 331 base::Bind(&TabManager::PurgeMemoryAndDiscardTab)); | 331 base::Bind(&TabManager::PurgeMemoryAndDiscardTab)); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void TabManager::LogMemory(const std::string& title, | 334 void TabManager::LogMemory(const std::string& title, |
| 335 const base::Closure& callback) { | 335 const base::Closure& callback) { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 337 OomMemoryDetails::Log(title, callback); | 337 memory::OomMemoryDetails::Log(title, callback); |
| 338 } | 338 } |
| 339 | 339 |
| 340 void TabManager::set_test_tick_clock(base::TickClock* test_tick_clock) { | 340 void TabManager::set_test_tick_clock(base::TickClock* test_tick_clock) { |
| 341 test_tick_clock_ = test_tick_clock; | 341 test_tick_clock_ = test_tick_clock; |
| 342 } | 342 } |
| 343 | 343 |
| 344 // Things to collect on the browser thread (because TabStripModel isn't thread | 344 // Things to collect on the browser thread (because TabStripModel isn't thread |
| 345 // safe): | 345 // safe): |
| 346 // 1) whether or not a tab is pinned | 346 // 1) whether or not a tab is pinned |
| 347 // 2) last time a tab was selected | 347 // 2) last time a tab was selected |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 // TODO(georgesak): Add Linux when automatic discarding is enabled for that | 868 // TODO(georgesak): Add Linux when automatic discarding is enabled for that |
| 869 // platform. | 869 // platform. |
| 870 std::string allow_multiple_discards = variations::GetVariationParamValue( | 870 std::string allow_multiple_discards = variations::GetVariationParamValue( |
| 871 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); | 871 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); |
| 872 return (allow_multiple_discards != "true"); | 872 return (allow_multiple_discards != "true"); |
| 873 #else | 873 #else |
| 874 return false; | 874 return false; |
| 875 #endif | 875 #endif |
| 876 } | 876 } |
| 877 | 877 |
| 878 } // namespace memory | 878 } // namespace resource_coordinator |
| OLD | NEW |