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

Unified Diff: chrome/browser/resource_coordinator/tab_manager.cc

Issue 2931023002: [TooManyTabs] Add TabNavigationThrottle (Closed)
Patch Set: review fix 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 af8a1774b67285945b79dcfa1788dbfec3a56c75..4dead62ea9fe9cbff7b1959a9579d05415c924a8 100644
--- a/chrome/browser/resource_coordinator/tab_manager.cc
+++ b/chrome/browser/resource_coordinator/tab_manager.cc
@@ -12,7 +12,6 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
-#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_monitor.h"
@@ -43,12 +42,12 @@
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
-#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "components/metrics/system_memory_stats_recorder.h"
#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"
@@ -792,6 +791,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,
@@ -881,4 +882,104 @@ bool TabManager::CanOnlyDiscardOnce() const {
#endif
}
+content::NavigationThrottle::ThrottleCheckResult
+TabManager::MaybeThrottleNavigation(
+ content::NavigationHandle* navigation_handle) {
+ if (!ShouldDelayNavigation(navigation_handle)) {
+ loading_contents_.insert(navigation_handle->GetWebContents());
+ return content::NavigationThrottle::PROCEED;
+ }
+
+ GetWebContentsData(navigation_handle->GetWebContents())
+ ->SetTabLoadingState(TAB_IS_NOT_LOADING);
+ pending_navigations_.push_back(navigation_handle);
+
+ return content::NavigationThrottle::DEFER;
+}
+
+bool TabManager::ShouldDelayNavigation(
+ content::NavigationHandle* navigation_handle) const {
+ if (!navigation_handle)
+ return false;
+
+ // Do not throttle the navigation if no tab is currently loading.
+ if (loading_contents_.empty()) {
+ return false;
+ }
+
+ return true;
+}
+
+void TabManager::OnNavigationDone(
+ content::NavigationHandle* navigation_handle) {
+ auto it = pending_navigations_.begin();
+ while (it != pending_navigations_.end()) {
+ content::NavigationHandle* pending_handle = *it;
+ if (pending_handle == navigation_handle) {
+ pending_navigations_.erase(it);
+ break;
+ }
+ it++;
+ }
+}
+
+void TabManager::OnLoadingDone(content::WebContents* contents) {
+ loading_contents_.erase(contents);
+
chrisha 2017/06/20 18:26:54 Remove blank line.
Zhen Wang 2017/07/06 18:26:12 Done.
+ LoadNextBackgroundTabIfNeeded();
+}
+
+void TabManager::OnWebContentsDestroyed(content::WebContents* contents) {
+ RemovePendingNavigationIfNeeded(contents);
+ loading_contents_.erase(contents);
+
chrisha 2017/06/20 18:26:54 Remove blank line.
Zhen Wang 2017/07/06 18:26:12 Done.
+ LoadNextBackgroundTabIfNeeded();
+}
+
+void TabManager::LoadNextBackgroundTabIfNeeded() {
+ // Do not load more background tabs until all currently loading tabs have
+ // finished.
+ if (!loading_contents_.empty())
+ return;
+
+ if (pending_navigations_.empty())
+ return;
+
+ content::NavigationHandle* navigation_handle = pending_navigations_.front();
+ pending_navigations_.erase(pending_navigations_.begin());
+ ResumeNavigation(navigation_handle);
+}
+
+void TabManager::ResumeTabNavigationIfNeeded(content::WebContents* contents) {
+ content::NavigationHandle* navigation_handle =
+ RemovePendingNavigationIfNeeded(contents);
+ if (navigation_handle)
+ ResumeNavigation(navigation_handle);
+}
+
+void TabManager::ResumeNavigation(
+ content::NavigationHandle* navigation_handle) {
+ GetWebContentsData(navigation_handle->GetWebContents())
+ ->SetTabLoadingState(TAB_IS_LOADING);
+ loading_contents_.insert(navigation_handle->GetWebContents());
+
+ navigation_handle->Resume();
+}
+
+content::NavigationHandle* TabManager::RemovePendingNavigationIfNeeded(
+ content::WebContents* contents) {
+ content::NavigationHandle* navigation_handle = nullptr;
+ auto it = pending_navigations_.begin();
+ while (it != pending_navigations_.end()) {
+ content::NavigationHandle* navigation_handle = *it;
+ if ((*it)->GetWebContents() == contents) {
+ navigation_handle = *it;
+ pending_navigations_.erase(it);
+ break;
+ }
+ it++;
+ }
+ return navigation_handle;
+}
+
} // namespace resource_coordinator

Powered by Google App Engine
This is Rietveld 408576698