Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: chrome/browser/resource_coordinator/tab_manager.cc

Issue 2931023002: [TooManyTabs] Add TabNavigationThrottle (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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/navigation_handle.h"
52 #include "content/public/browser/render_process_host.h" 53 #include "content/public/browser/render_process_host.h"
53 #include "content/public/browser/web_contents.h" 54 #include "content/public/browser/web_contents.h"
54 #include "content/public/common/page_importance_signals.h" 55 #include "content/public/common/page_importance_signals.h"
55 56
56 #if defined(OS_CHROMEOS) 57 #if defined(OS_CHROMEOS)
57 #include "ash/multi_profile_uma.h" 58 #include "ash/multi_profile_uma.h"
58 #include "ash/shell_port.h" 59 #include "ash/shell_port.h"
59 #include "chrome/browser/resource_coordinator/tab_manager_delegate_chromeos.h" 60 #include "chrome/browser/resource_coordinator/tab_manager_delegate_chromeos.h"
60 #include "components/user_manager/user_manager.h" 61 #include "components/user_manager/user_manager.h"
61 #endif 62 #endif
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 105
105 TabManager::TabManager() 106 TabManager::TabManager()
106 : discard_count_(0), 107 : discard_count_(0),
107 recent_tab_discard_(false), 108 recent_tab_discard_(false),
108 discard_once_(false), 109 discard_once_(false),
109 #if !defined(OS_CHROMEOS) 110 #if !defined(OS_CHROMEOS)
110 minimum_protection_time_(base::TimeDelta::FromMinutes(10)), 111 minimum_protection_time_(base::TimeDelta::FromMinutes(10)),
111 #endif 112 #endif
112 browser_tab_strip_tracker_(this, nullptr, nullptr), 113 browser_tab_strip_tracker_(this, nullptr, nullptr),
113 test_tick_clock_(nullptr), 114 test_tick_clock_(nullptr),
115 loading_contents_(nullptr),
114 weak_ptr_factory_(this) { 116 weak_ptr_factory_(this) {
115 #if defined(OS_CHROMEOS) 117 #if defined(OS_CHROMEOS)
116 delegate_.reset(new TabManagerDelegate(weak_ptr_factory_.GetWeakPtr())); 118 delegate_.reset(new TabManagerDelegate(weak_ptr_factory_.GetWeakPtr()));
117 #endif 119 #endif
118 browser_tab_strip_tracker_.Init(); 120 browser_tab_strip_tracker_.Init();
119 } 121 }
120 122
121 TabManager::~TabManager() { 123 TabManager::~TabManager() {
122 Stop(); 124 Stop();
123 } 125 }
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 // When ActiveTabChanged, |new_contents| purged state changes to be false. 781 // When ActiveTabChanged, |new_contents| purged state changes to be false.
780 GetWebContentsData(new_contents)->set_is_purged(false); 782 GetWebContentsData(new_contents)->set_is_purged(false);
781 // If |old_contents| is set, that tab has switched from being active to 783 // If |old_contents| is set, that tab has switched from being active to
782 // inactive, so record the time of that transition. 784 // inactive, so record the time of that transition.
783 if (old_contents) { 785 if (old_contents) {
784 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks()); 786 GetWebContentsData(old_contents)->SetLastInactiveTime(NowTicks());
785 // Re-setting time-to-purge every time a tab becomes inactive. 787 // Re-setting time-to-purge every time a tab becomes inactive.
786 GetWebContentsData(old_contents) 788 GetWebContentsData(old_contents)
787 ->set_time_to_purge(GetTimeToPurge(min_time_to_purge_)); 789 ->set_time_to_purge(GetTimeToPurge(min_time_to_purge_));
788 } 790 }
791
792 ResumeTabNavigationIfNeeded(new_contents);
789 } 793 }
790 794
791 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, 795 void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
792 content::WebContents* contents, 796 content::WebContents* contents,
793 int index, 797 int index,
794 bool foreground) { 798 bool foreground) {
795 // Only interested in background tabs, as foreground tabs get taken care of by 799 // Only interested in background tabs, as foreground tabs get taken care of by
796 // ActiveTabChanged. 800 // ActiveTabChanged.
797 if (foreground) 801 if (foreground)
798 return; 802 return;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 // TODO(georgesak): Add Linux when automatic discarding is enabled for that 872 // TODO(georgesak): Add Linux when automatic discarding is enabled for that
869 // platform. 873 // platform.
870 std::string allow_multiple_discards = variations::GetVariationParamValue( 874 std::string allow_multiple_discards = variations::GetVariationParamValue(
871 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 875 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
872 return (allow_multiple_discards != "true"); 876 return (allow_multiple_discards != "true");
873 #else 877 #else
874 return false; 878 return false;
875 #endif 879 #endif
876 } 880 }
877 881
882 content::NavigationThrottle::ThrottleCheckResult
883 TabManager::MaybeThrottleNavigation(
884 content::NavigationHandle* navigation_handle) {
885 if (!ShouldThrottleNavigation(navigation_handle)) {
886 loading_contents_ = navigation_handle->GetWebContents();
887 return content::NavigationThrottle::PROCEED;
888 }
889
890 pending_navigations_.push_back(navigation_handle);
891 return content::NavigationThrottle::DEFER;
892 }
893
894 bool TabManager::ShouldThrottleNavigation(
895 content::NavigationHandle* navigation_handle) const {
896 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
897 switches::kStaggeredBackgroundTabOpen)) {
Charlie Reis 2017/06/10 00:53:36 We shouldn't be creating the throttle if this flag
Zhen Wang 2017/06/13 23:33:20 Done.
898 return false;
899 }
900
901 if (!navigation_handle)
Charlie Reis 2017/06/10 00:53:36 Is this possible?
Zhen Wang 2017/06/13 23:33:20 I am not sure if this is ever possible. That's why
Zhen Wang 2017/06/19 23:00:12 Ping
Charlie Reis 2017/07/06 23:50:30 Let's remove it if we don't know that it's needed.
Zhen Wang 2017/07/07 18:06:38 Done.
902 return false;
903
904 // It only throttle background tabs. Never throttle foreground tabs.
905 if (navigation_handle->GetWebContents()->IsVisible())
Charlie Reis 2017/06/10 00:53:36 This also seems like it could be checked before cr
Zhen Wang 2017/06/13 23:33:20 Done.
906 return false;
907
908 // Do not throttle the navigation if no tab is currently loading.
909 if (!loading_contents_) {
nasko 2017/06/12 23:22:02 Wouldn't it be simpler to just check the pending_n
Zhen Wang 2017/06/13 23:33:20 |pending_navigations_| tracks which tabs has not s
910 return false;
911 }
912
913 return true;
914 }
915
916 void TabManager::HasFinishedLoadingTab(content::WebContents* contents) {
917 loading_contents_ = nullptr;
918
919 while (!pending_navigations_.empty()) {
920 content::NavigationHandle* navigation_handle = pending_navigations_.front();
921 pending_navigations_.erase(pending_navigations_.begin());
922
923 if (navigation_handle) {
924 loading_contents_ = navigation_handle->GetWebContents();
925 navigation_handle->Resume();
926 break;
927 }
928 }
929 }
930
931 void TabManager::ResumeTabNavigationIfNeeded(content::WebContents* contents) {
932 auto it = pending_navigations_.begin();
933 while (it != pending_navigations_.end()) {
934 content::NavigationHandle* navigation_handle = *it;
935 if (navigation_handle && navigation_handle->GetWebContents() == contents) {
nasko 2017/06/12 23:22:02 We better ensure we don't put nullptr into the pen
Zhen Wang 2017/06/13 23:33:20 When a navigation gets cancelled, will the corresp
nasko 2017/06/15 20:01:58 The WebContentsObserver::DidFinishNavigation will
Zhen Wang 2017/06/19 23:00:12 Thanks! Now observing DidFinishNavigation.
936 loading_contents_ = navigation_handle->GetWebContents();
Charlie Reis 2017/06/10 00:53:36 Keep in mind that there may have been an existing
Zhen Wang 2017/06/13 23:33:20 You are right. I think the best way is to know how
937 navigation_handle->Resume();
938 pending_navigations_.erase(it);
939 break;
940 }
941 it++;
942 }
943 }
944
878 } // namespace resource_coordinator 945 } // namespace resource_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698