| 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/memory/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> |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 int index = FindTabStripModelById(target_web_contents_id, &model); | 287 int index = FindTabStripModelById(target_web_contents_id, &model); |
| 288 | 288 |
| 289 if (index == -1) | 289 if (index == -1) |
| 290 return nullptr; | 290 return nullptr; |
| 291 | 291 |
| 292 VLOG(1) << "Discarding tab " << index << " id " << target_web_contents_id; | 292 VLOG(1) << "Discarding tab " << index << " id " << target_web_contents_id; |
| 293 | 293 |
| 294 return DiscardWebContentsAt(index, model); | 294 return DiscardWebContentsAt(index, model); |
| 295 } | 295 } |
| 296 | 296 |
| 297 WebContents* TabManager::DiscardTabByExtension(content::WebContents* contents) { |
| 298 if (contents) |
| 299 return DiscardTabById(reinterpret_cast<int64_t>(contents)); |
| 300 |
| 301 return DiscardTabImpl(); |
| 302 } |
| 303 |
| 297 void TabManager::LogMemoryAndDiscardTab() { | 304 void TabManager::LogMemoryAndDiscardTab() { |
| 298 LogMemory("Tab Discards Memory details", | 305 LogMemory("Tab Discards Memory details", |
| 299 base::Bind(&TabManager::PurgeMemoryAndDiscardTab)); | 306 base::Bind(&TabManager::PurgeMemoryAndDiscardTab)); |
| 300 } | 307 } |
| 301 | 308 |
| 302 void TabManager::LogMemory(const std::string& title, | 309 void TabManager::LogMemory(const std::string& title, |
| 303 const base::Closure& callback) { | 310 const base::Closure& callback) { |
| 304 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 311 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 305 OomMemoryDetails::Log(title, callback); | 312 OomMemoryDetails::Log(title, callback); |
| 306 } | 313 } |
| (...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 task_runner_->PostDelayedTask( | 832 task_runner_->PostDelayedTask( |
| 826 FROM_HERE, | 833 FROM_HERE, |
| 827 base::Bind(&TabManager::DoChildProcessDispatch, | 834 base::Bind(&TabManager::DoChildProcessDispatch, |
| 828 weak_ptr_factory_.GetWeakPtr()), | 835 weak_ptr_factory_.GetWeakPtr()), |
| 829 base::TimeDelta::FromSeconds(kRendererNotificationDelayInSeconds)); | 836 base::TimeDelta::FromSeconds(kRendererNotificationDelayInSeconds)); |
| 830 } | 837 } |
| 831 | 838 |
| 832 // TODO(jamescook): This should consider tabs with references to other tabs, | 839 // TODO(jamescook): This should consider tabs with references to other tabs, |
| 833 // such as tabs created with JavaScript window.open(). Potentially consider | 840 // such as tabs created with JavaScript window.open(). Potentially consider |
| 834 // discarding the entire set together, or use that in the priority computation. | 841 // discarding the entire set together, or use that in the priority computation. |
| 835 bool TabManager::DiscardTabImpl() { | 842 content::WebContents* TabManager::DiscardTabImpl() { |
| 836 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 843 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 837 TabStatsList stats = GetTabStats(); | 844 TabStatsList stats = GetTabStats(); |
| 838 | 845 |
| 839 if (stats.empty()) | 846 if (stats.empty()) |
| 840 return false; | 847 return nullptr; |
| 841 // Loop until a non-discarded tab to kill is found. | 848 // Loop until a non-discarded tab to kill is found. |
| 842 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin(); | 849 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin(); |
| 843 stats_rit != stats.rend(); ++stats_rit) { | 850 stats_rit != stats.rend(); ++stats_rit) { |
| 844 int64_t least_important_tab_id = stats_rit->tab_contents_id; | 851 int64_t least_important_tab_id = stats_rit->tab_contents_id; |
| 845 if (CanDiscardTab(least_important_tab_id) && | 852 if (CanDiscardTab(least_important_tab_id)) { |
| 846 DiscardTabById(least_important_tab_id)) | 853 WebContents* new_contents = DiscardTabById(least_important_tab_id); |
| 847 return true; | 854 if (new_contents) |
| 855 return new_contents; |
| 856 } |
| 848 } | 857 } |
| 849 return false; | 858 return nullptr; |
| 850 } | 859 } |
| 851 | 860 |
| 852 // Check the variation parameter to see if a tab can be discarded only once or | 861 // Check the variation parameter to see if a tab can be discarded only once or |
| 853 // multiple times. | 862 // multiple times. |
| 854 // Default is to only discard once per tab. | 863 // Default is to only discard once per tab. |
| 855 bool TabManager::CanOnlyDiscardOnce() { | 864 bool TabManager::CanOnlyDiscardOnce() { |
| 856 #if defined(OS_WIN) || defined(OS_MACOSX) | 865 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 857 // On Windows and MacOS, default to discarding only once unless otherwise | 866 // On Windows and MacOS, default to discarding only once unless otherwise |
| 858 // specified by the variation parameter. | 867 // specified by the variation parameter. |
| 859 // TODO(georgesak): Add Linux when automatic discarding is enabled for that | 868 // TODO(georgesak): Add Linux when automatic discarding is enabled for that |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 void TabManager::RemoveObserver(TabManagerObserver* observer) { | 908 void TabManager::RemoveObserver(TabManagerObserver* observer) { |
| 900 observers_.RemoveObserver(observer); | 909 observers_.RemoveObserver(observer); |
| 901 } | 910 } |
| 902 | 911 |
| 903 void TabManager::OnDiscardedStateChange(content::WebContents* contents, | 912 void TabManager::OnDiscardedStateChange(content::WebContents* contents, |
| 904 bool is_discarded) { | 913 bool is_discarded) { |
| 905 FOR_EACH_OBSERVER(TabManagerObserver, observers_, | 914 FOR_EACH_OBSERVER(TabManagerObserver, observers_, |
| 906 OnDiscardedStateChange(contents, is_discarded)); | 915 OnDiscardedStateChange(contents, is_discarded)); |
| 907 } | 916 } |
| 908 | 917 |
| 918 void TabManager::set_minimum_protection_time_for_tests( |
| 919 unsigned int time_seconds) { |
| 920 minimum_protection_time_ = base::TimeDelta::FromSeconds(time_seconds); |
| 921 } |
| 922 |
| 909 } // namespace memory | 923 } // namespace memory |
| OLD | NEW |