| 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 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | |
| 15 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 16 #include "base/memory/memory_pressure_monitor.h" | 15 #include "base/memory/memory_pressure_monitor.h" |
| 17 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
| 18 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 20 #include "base/process/process.h" | 19 #include "base/process/process.h" |
| 21 #include "base/rand_util.h" | 20 #include "base/rand_util.h" |
| 22 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 23 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 #include "chrome/browser/resource_coordinator/tab_manager_web_contents_data.h" | 34 #include "chrome/browser/resource_coordinator/tab_manager_web_contents_data.h" |
| 36 #include "chrome/browser/sessions/session_restore.h" | 35 #include "chrome/browser/sessions/session_restore.h" |
| 37 #include "chrome/browser/ui/browser.h" | 36 #include "chrome/browser/ui/browser.h" |
| 38 #include "chrome/browser/ui/browser_list.h" | 37 #include "chrome/browser/ui/browser_list.h" |
| 39 #include "chrome/browser/ui/browser_window.h" | 38 #include "chrome/browser/ui/browser_window.h" |
| 40 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 39 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 41 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 40 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 42 #include "chrome/browser/ui/tabs/tab_utils.h" | 41 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 43 #include "chrome/common/chrome_constants.h" | 42 #include "chrome/common/chrome_constants.h" |
| 44 #include "chrome/common/chrome_features.h" | 43 #include "chrome/common/chrome_features.h" |
| 45 #include "chrome/common/chrome_switches.h" | |
| 46 #include "chrome/common/url_constants.h" | 44 #include "chrome/common/url_constants.h" |
| 47 #include "components/metrics/system_memory_stats_recorder.h" | 45 #include "components/metrics/system_memory_stats_recorder.h" |
| 48 #include "components/variations/variations_associated_data.h" | 46 #include "components/variations/variations_associated_data.h" |
| 49 #include "content/public/browser/browser_thread.h" | 47 #include "content/public/browser/browser_thread.h" |
| 50 #include "content/public/browser/navigation_controller.h" | 48 #include "content/public/browser/navigation_controller.h" |
| 49 #include "content/public/browser/navigation_handle.h" |
| 51 #include "content/public/browser/render_process_host.h" | 50 #include "content/public/browser/render_process_host.h" |
| 52 #include "content/public/browser/web_contents.h" | 51 #include "content/public/browser/web_contents.h" |
| 53 #include "content/public/common/content_features.h" | 52 #include "content/public/common/content_features.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.h" | 57 #include "ash/shell.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" |
| (...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 } | 833 } |
| 835 | 834 |
| 836 // An active tab is not purged. | 835 // An active tab is not purged. |
| 837 GetWebContentsData(new_contents)->set_is_purged(false); | 836 GetWebContentsData(new_contents)->set_is_purged(false); |
| 838 | 837 |
| 839 // Reload |web_contents| if it is in an active browser and discarded. | 838 // Reload |web_contents| if it is in an active browser and discarded. |
| 840 if (IsActiveWebContentsInActiveBrowser(new_contents)) { | 839 if (IsActiveWebContentsInActiveBrowser(new_contents)) { |
| 841 ReloadWebContentsIfDiscarded(new_contents, | 840 ReloadWebContentsIfDiscarded(new_contents, |
| 842 GetWebContentsData(new_contents)); | 841 GetWebContentsData(new_contents)); |
| 843 } | 842 } |
| 843 |
| 844 ResumeTabNavigationIfNeeded(new_contents); |
| 844 } | 845 } |
| 845 | 846 |
| 846 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, | 847 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, |
| 847 content::WebContents* contents, | 848 content::WebContents* contents, |
| 848 int index, | 849 int index, |
| 849 bool foreground) { | 850 bool foreground) { |
| 850 // Only interested in background tabs, as foreground tabs get taken care of by | 851 // Only interested in background tabs, as foreground tabs get taken care of by |
| 851 // ActiveTabChanged. | 852 // ActiveTabChanged. |
| 852 if (foreground) | 853 if (foreground) |
| 853 return; | 854 return; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 } | 970 } |
| 970 | 971 |
| 971 void TabManager::RecordSwitchToTab(content::WebContents* contents) const { | 972 void TabManager::RecordSwitchToTab(content::WebContents* contents) const { |
| 972 if (is_session_restore_loading_tabs_) { | 973 if (is_session_restore_loading_tabs_) { |
| 973 UMA_HISTOGRAM_ENUMERATION("TabManager.SessionRestore.SwitchToTab", | 974 UMA_HISTOGRAM_ENUMERATION("TabManager.SessionRestore.SwitchToTab", |
| 974 GetWebContentsData(contents)->tab_loading_state(), | 975 GetWebContentsData(contents)->tab_loading_state(), |
| 975 TAB_LOADING_STATE_MAX); | 976 TAB_LOADING_STATE_MAX); |
| 976 } | 977 } |
| 977 } | 978 } |
| 978 | 979 |
| 980 content::NavigationThrottle::ThrottleCheckResult |
| 981 TabManager::MaybeThrottleNavigation( |
| 982 content::NavigationHandle* navigation_handle) { |
| 983 if (!ShouldDelayNavigation(navigation_handle)) { |
| 984 loading_contents_.insert(navigation_handle->GetWebContents()); |
| 985 return content::NavigationThrottle::PROCEED; |
| 986 } |
| 987 |
| 988 // TODO(zhenw): Try to set the favicon and title from history service if this |
| 989 // navigation will be delayed. |
| 990 GetWebContentsData(navigation_handle->GetWebContents()) |
| 991 ->SetTabLoadingState(TAB_IS_NOT_LOADING); |
| 992 pending_navigations_.push_back(navigation_handle); |
| 993 |
| 994 return content::NavigationThrottle::DEFER; |
| 995 } |
| 996 |
| 997 bool TabManager::ShouldDelayNavigation( |
| 998 content::NavigationHandle* navigation_handle) const { |
| 999 // Do not delay the navigation if no tab is currently loading. |
| 1000 if (loading_contents_.empty()) |
| 1001 return false; |
| 1002 |
| 1003 return true; |
| 1004 } |
| 1005 |
| 1006 void TabManager::OnDidFinishNavigation( |
| 1007 content::NavigationHandle* navigation_handle) { |
| 1008 auto it = pending_navigations_.begin(); |
| 1009 while (it != pending_navigations_.end()) { |
| 1010 content::NavigationHandle* pending_handle = *it; |
| 1011 if (pending_handle == navigation_handle) { |
| 1012 pending_navigations_.erase(it); |
| 1013 break; |
| 1014 } |
| 1015 it++; |
| 1016 } |
| 1017 } |
| 1018 |
| 1019 void TabManager::OnDidStopLoading(content::WebContents* contents) { |
| 1020 DCHECK_EQ(TAB_IS_LOADED, GetWebContentsData(contents)->tab_loading_state()); |
| 1021 loading_contents_.erase(contents); |
| 1022 LoadNextBackgroundTabIfNeeded(); |
| 1023 } |
| 1024 |
| 1025 void TabManager::OnWebContentsDestroyed(content::WebContents* contents) { |
| 1026 RemovePendingNavigationIfNeeded(contents); |
| 1027 loading_contents_.erase(contents); |
| 1028 LoadNextBackgroundTabIfNeeded(); |
| 1029 } |
| 1030 |
| 1031 void TabManager::LoadNextBackgroundTabIfNeeded() { |
| 1032 // Do not load more background tabs until all currently loading tabs have |
| 1033 // finished. |
| 1034 if (!loading_contents_.empty()) |
| 1035 return; |
| 1036 |
| 1037 if (pending_navigations_.empty()) |
| 1038 return; |
| 1039 |
| 1040 content::NavigationHandle* navigation_handle = pending_navigations_.front(); |
| 1041 pending_navigations_.erase(pending_navigations_.begin()); |
| 1042 ResumeNavigation(navigation_handle); |
| 1043 } |
| 1044 |
| 1045 void TabManager::ResumeTabNavigationIfNeeded(content::WebContents* contents) { |
| 1046 content::NavigationHandle* navigation_handle = |
| 1047 RemovePendingNavigationIfNeeded(contents); |
| 1048 if (navigation_handle) |
| 1049 ResumeNavigation(navigation_handle); |
| 1050 } |
| 1051 |
| 1052 void TabManager::ResumeNavigation( |
| 1053 content::NavigationHandle* navigation_handle) { |
| 1054 GetWebContentsData(navigation_handle->GetWebContents()) |
| 1055 ->SetTabLoadingState(TAB_IS_LOADING); |
| 1056 loading_contents_.insert(navigation_handle->GetWebContents()); |
| 1057 |
| 1058 navigation_handle->Resume(); |
| 1059 } |
| 1060 |
| 1061 content::NavigationHandle* TabManager::RemovePendingNavigationIfNeeded( |
| 1062 content::WebContents* contents) { |
| 1063 content::NavigationHandle* navigation_handle = nullptr; |
| 1064 auto it = pending_navigations_.begin(); |
| 1065 while (it != pending_navigations_.end()) { |
| 1066 navigation_handle = *it; |
| 1067 if ((*it)->GetWebContents() == contents) { |
| 1068 navigation_handle = *it; |
| 1069 pending_navigations_.erase(it); |
| 1070 break; |
| 1071 } |
| 1072 it++; |
| 1073 } |
| 1074 return navigation_handle; |
| 1075 } |
| 1076 |
| 1077 bool TabManager::IsTabLoadingForTest(content::WebContents* contents) const { |
| 1078 if (loading_contents_.count(contents) == 1) { |
| 1079 DCHECK_EQ(TAB_IS_LOADING, |
| 1080 GetWebContentsData(contents)->tab_loading_state()); |
| 1081 return true; |
| 1082 } |
| 1083 |
| 1084 DCHECK_NE(TAB_IS_LOADING, GetWebContentsData(contents)->tab_loading_state()); |
| 1085 return false; |
| 1086 } |
| 1087 |
| 1088 bool TabManager::IsNavigationDelayedForTest( |
| 1089 const content::NavigationHandle* navigation_handle) const { |
| 1090 for (const auto* nav : pending_navigations_) { |
| 1091 if (nav == navigation_handle) |
| 1092 return true; |
| 1093 } |
| 1094 return false; |
| 1095 } |
| 1096 |
| 979 } // namespace resource_coordinator | 1097 } // namespace resource_coordinator |
| OLD | NEW |