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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resource_coordinator/tab_manager.cc
diff --git a/chrome/browser/resource_coordinator/tab_manager.cc b/chrome/browser/resource_coordinator/tab_manager.cc
index c2b5150b795c23e21d1e97cbe43d51ba2ba9316e..1612e2f3cb7c89535d0d317feb3cf3dc60109a1d 100644
--- a/chrome/browser/resource_coordinator/tab_manager.cc
+++ b/chrome/browser/resource_coordinator/tab_manager.cc
@@ -49,6 +49,7 @@
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
+#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_importance_signals.h"
@@ -111,6 +112,7 @@ TabManager::TabManager()
#endif
browser_tab_strip_tracker_(this, nullptr, nullptr),
test_tick_clock_(nullptr),
+ loading_contents_(nullptr),
weak_ptr_factory_(this) {
#if defined(OS_CHROMEOS)
delegate_.reset(new TabManagerDelegate(weak_ptr_factory_.GetWeakPtr()));
@@ -786,6 +788,8 @@ void TabManager::ActiveTabChanged(content::WebContents* old_contents,
GetWebContentsData(old_contents)
->set_time_to_purge(GetTimeToPurge(min_time_to_purge_));
}
+
+ ResumeTabNavigationIfNeeded(new_contents);
}
void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
@@ -875,4 +879,67 @@ bool TabManager::CanOnlyDiscardOnce() const {
#endif
}
+content::NavigationThrottle::ThrottleCheckResult
+TabManager::MaybeThrottleNavigation(
+ content::NavigationHandle* navigation_handle) {
+ if (!ShouldThrottleNavigation(navigation_handle)) {
+ loading_contents_ = navigation_handle->GetWebContents();
+ return content::NavigationThrottle::PROCEED;
+ }
+
+ pending_navigations_.push_back(navigation_handle);
+ return content::NavigationThrottle::DEFER;
+}
+
+bool TabManager::ShouldThrottleNavigation(
+ content::NavigationHandle* navigation_handle) const {
+ if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
+ 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.
+ return false;
+ }
+
+ 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.
+ return false;
+
+ // It only throttle background tabs. Never throttle foreground tabs.
+ 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.
+ return false;
+
+ // Do not throttle the navigation if no tab is currently loading.
+ 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
+ return false;
+ }
+
+ return true;
+}
+
+void TabManager::HasFinishedLoadingTab(content::WebContents* contents) {
+ loading_contents_ = nullptr;
+
+ while (!pending_navigations_.empty()) {
+ content::NavigationHandle* navigation_handle = pending_navigations_.front();
+ pending_navigations_.erase(pending_navigations_.begin());
+
+ if (navigation_handle) {
+ loading_contents_ = navigation_handle->GetWebContents();
+ navigation_handle->Resume();
+ break;
+ }
+ }
+}
+
+void TabManager::ResumeTabNavigationIfNeeded(content::WebContents* contents) {
+ auto it = pending_navigations_.begin();
+ while (it != pending_navigations_.end()) {
+ content::NavigationHandle* navigation_handle = *it;
+ 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.
+ 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
+ navigation_handle->Resume();
+ pending_navigations_.erase(it);
+ break;
+ }
+ it++;
+ }
+}
+
} // namespace resource_coordinator

Powered by Google App Engine
This is Rietveld 408576698