Chromium Code Reviews| 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/resource_coordinator/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" | |
| 16 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
| 17 #include "base/macros.h" | 16 #include "base/macros.h" |
| 18 #include "base/memory/memory_pressure_monitor.h" | 17 #include "base/memory/memory_pressure_monitor.h" |
| 19 #include "base/metrics/field_trial.h" | 18 #include "base/metrics/field_trial.h" |
| 20 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 21 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
| 22 #include "base/process/process.h" | 21 #include "base/process/process.h" |
| 23 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 24 #include "base/strings/string16.h" | 23 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 36 #include "chrome/browser/resource_coordinator/tab_manager_observer.h" | 35 #include "chrome/browser/resource_coordinator/tab_manager_observer.h" |
| 37 #include "chrome/browser/resource_coordinator/tab_manager_web_contents_data.h" | 36 #include "chrome/browser/resource_coordinator/tab_manager_web_contents_data.h" |
| 38 #include "chrome/browser/ui/browser.h" | 37 #include "chrome/browser/ui/browser.h" |
| 39 #include "chrome/browser/ui/browser_list.h" | 38 #include "chrome/browser/ui/browser_list.h" |
| 40 #include "chrome/browser/ui/browser_window.h" | 39 #include "chrome/browser/ui/browser_window.h" |
| 41 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 40 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 42 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 41 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 43 #include "chrome/browser/ui/tabs/tab_utils.h" | 42 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 44 #include "chrome/common/chrome_constants.h" | 43 #include "chrome/common/chrome_constants.h" |
| 45 #include "chrome/common/chrome_features.h" | 44 #include "chrome/common/chrome_features.h" |
| 46 #include "chrome/common/chrome_switches.h" | |
| 47 #include "chrome/common/url_constants.h" | 45 #include "chrome/common/url_constants.h" |
| 48 #include "components/metrics/system_memory_stats_recorder.h" | 46 #include "components/metrics/system_memory_stats_recorder.h" |
| 49 #include "components/variations/variations_associated_data.h" | 47 #include "components/variations/variations_associated_data.h" |
| 50 #include "content/public/browser/browser_thread.h" | 48 #include "content/public/browser/browser_thread.h" |
| 51 #include "content/public/browser/navigation_controller.h" | 49 #include "content/public/browser/navigation_controller.h" |
| 50 #include "content/public/browser/navigation_handle.h" | |
| 52 #include "content/public/browser/render_process_host.h" | 51 #include "content/public/browser/render_process_host.h" |
| 53 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
| 54 #include "content/public/common/page_importance_signals.h" | 53 #include "content/public/common/page_importance_signals.h" |
| 55 | 54 |
| 56 #if defined(OS_CHROMEOS) | 55 #if defined(OS_CHROMEOS) |
| 57 #include "ash/multi_profile_uma.h" | 56 #include "ash/multi_profile_uma.h" |
| 58 #include "ash/shell_port.h" | 57 #include "ash/shell_port.h" |
| 59 #include "chrome/browser/resource_coordinator/tab_manager_delegate_chromeos.h" | 58 #include "chrome/browser/resource_coordinator/tab_manager_delegate_chromeos.h" |
| 60 #include "components/user_manager/user_manager.h" | 59 #include "components/user_manager/user_manager.h" |
| 61 #endif | 60 #endif |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 785 // When ActiveTabChanged, |new_contents| purged state changes to be false. | 784 // When ActiveTabChanged, |new_contents| purged state changes to be false. |
| 786 GetWebContentsData(new_contents)->set_is_purged(false); | 785 GetWebContentsData(new_contents)->set_is_purged(false); |
| 787 // If |old_contents| is set, that tab has switched from being active to | 786 // If |old_contents| is set, that tab has switched from being active to |
| 788 // inactive, so record the time of that transition. | 787 // inactive, so record the time of that transition. |
| 789 if (old_contents) { | 788 if (old_contents) { |
| 790 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); | 789 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); |
| 791 // Re-setting time-to-purge every time a tab becomes inactive. | 790 // Re-setting time-to-purge every time a tab becomes inactive. |
| 792 GetWebContentsData(old_contents) | 791 GetWebContentsData(old_contents) |
| 793 ->set_time_to_purge(GetTimeToPurge(min_time_to_purge_)); | 792 ->set_time_to_purge(GetTimeToPurge(min_time_to_purge_)); |
| 794 } | 793 } |
| 794 | |
| 795 ResumeTabNavigationIfNeeded(new_contents); | |
| 795 } | 796 } |
| 796 | 797 |
| 797 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, | 798 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, |
| 798 content::WebContents* contents, | 799 content::WebContents* contents, |
| 799 int index, | 800 int index, |
| 800 bool foreground) { | 801 bool foreground) { |
| 801 // Only interested in background tabs, as foreground tabs get taken care of by | 802 // Only interested in background tabs, as foreground tabs get taken care of by |
| 802 // ActiveTabChanged. | 803 // ActiveTabChanged. |
| 803 if (foreground) | 804 if (foreground) |
| 804 return; | 805 return; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 874 // TODO(georgesak): Add Linux when automatic discarding is enabled for that | 875 // TODO(georgesak): Add Linux when automatic discarding is enabled for that |
| 875 // platform. | 876 // platform. |
| 876 std::string allow_multiple_discards = variations::GetVariationParamValue( | 877 std::string allow_multiple_discards = variations::GetVariationParamValue( |
| 877 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); | 878 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); |
| 878 return (allow_multiple_discards != "true"); | 879 return (allow_multiple_discards != "true"); |
| 879 #else | 880 #else |
| 880 return false; | 881 return false; |
| 881 #endif | 882 #endif |
| 882 } | 883 } |
| 883 | 884 |
| 885 content::NavigationThrottle::ThrottleCheckResult | |
| 886 TabManager::MaybeThrottleNavigation( | |
| 887 content::NavigationHandle* navigation_handle) { | |
| 888 if (!ShouldDelayNavigation(navigation_handle)) { | |
| 889 loading_contents_.insert(navigation_handle->GetWebContents()); | |
| 890 return content::NavigationThrottle::PROCEED; | |
| 891 } | |
| 892 | |
| 893 GetWebContentsData(navigation_handle->GetWebContents()) | |
| 894 ->SetTabLoadingState(TAB_IS_NOT_LOADING); | |
| 895 pending_navigations_.push_back(navigation_handle); | |
| 896 | |
| 897 return content::NavigationThrottle::DEFER; | |
| 898 } | |
| 899 | |
| 900 bool TabManager::ShouldDelayNavigation( | |
| 901 content::NavigationHandle* navigation_handle) const { | |
| 902 if (!navigation_handle) | |
| 903 return false; | |
| 904 | |
| 905 // Do not throttle the navigation if no tab is currently loading. | |
| 906 if (loading_contents_.empty()) { | |
| 907 return false; | |
| 908 } | |
| 909 | |
| 910 return true; | |
| 911 } | |
| 912 | |
| 913 void TabManager::OnNavigationDone( | |
| 914 content::NavigationHandle* navigation_handle) { | |
| 915 auto it = pending_navigations_.begin(); | |
| 916 while (it != pending_navigations_.end()) { | |
| 917 content::NavigationHandle* pending_handle = *it; | |
| 918 if (pending_handle == navigation_handle) { | |
| 919 pending_navigations_.erase(it); | |
| 920 break; | |
| 921 } | |
| 922 it++; | |
| 923 } | |
| 924 } | |
| 925 | |
| 926 void TabManager::OnLoadingDone(content::WebContents* contents) { | |
| 927 loading_contents_.erase(contents); | |
| 928 | |
|
chrisha
2017/06/20 18:26:54
Remove blank line.
Zhen Wang
2017/07/06 18:26:12
Done.
| |
| 929 LoadNextBackgroundTabIfNeeded(); | |
| 930 } | |
| 931 | |
| 932 void TabManager::OnWebContentsDestroyed(content::WebContents* contents) { | |
| 933 RemovePendingNavigationIfNeeded(contents); | |
| 934 loading_contents_.erase(contents); | |
| 935 | |
|
chrisha
2017/06/20 18:26:54
Remove blank line.
Zhen Wang
2017/07/06 18:26:12
Done.
| |
| 936 LoadNextBackgroundTabIfNeeded(); | |
| 937 } | |
| 938 | |
| 939 void TabManager::LoadNextBackgroundTabIfNeeded() { | |
| 940 // Do not load more background tabs until all currently loading tabs have | |
| 941 // finished. | |
| 942 if (!loading_contents_.empty()) | |
| 943 return; | |
| 944 | |
| 945 if (pending_navigations_.empty()) | |
| 946 return; | |
| 947 | |
| 948 content::NavigationHandle* navigation_handle = pending_navigations_.front(); | |
| 949 pending_navigations_.erase(pending_navigations_.begin()); | |
| 950 ResumeNavigation(navigation_handle); | |
| 951 } | |
| 952 | |
| 953 void TabManager::ResumeTabNavigationIfNeeded(content::WebContents* contents) { | |
| 954 content::NavigationHandle* navigation_handle = | |
| 955 RemovePendingNavigationIfNeeded(contents); | |
| 956 if (navigation_handle) | |
| 957 ResumeNavigation(navigation_handle); | |
| 958 } | |
| 959 | |
| 960 void TabManager::ResumeNavigation( | |
| 961 content::NavigationHandle* navigation_handle) { | |
| 962 GetWebContentsData(navigation_handle->GetWebContents()) | |
| 963 ->SetTabLoadingState(TAB_IS_LOADING); | |
| 964 loading_contents_.insert(navigation_handle->GetWebContents()); | |
| 965 | |
| 966 navigation_handle->Resume(); | |
| 967 } | |
| 968 | |
| 969 content::NavigationHandle* TabManager::RemovePendingNavigationIfNeeded( | |
| 970 content::WebContents* contents) { | |
| 971 content::NavigationHandle* navigation_handle = nullptr; | |
| 972 auto it = pending_navigations_.begin(); | |
| 973 while (it != pending_navigations_.end()) { | |
| 974 content::NavigationHandle* navigation_handle = *it; | |
| 975 if ((*it)->GetWebContents() == contents) { | |
| 976 navigation_handle = *it; | |
| 977 pending_navigations_.erase(it); | |
| 978 break; | |
| 979 } | |
| 980 it++; | |
| 981 } | |
| 982 return navigation_handle; | |
| 983 } | |
| 984 | |
| 884 } // namespace resource_coordinator | 985 } // namespace resource_coordinator |
| OLD | NEW |