OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/sessions/tab_loader.h" | 5 #include "chrome/browser/sessions/tab_loader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/memory_coordinator_client_registry.h" | 10 #include "base/memory/memory_coordinator_client_registry.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
25 #include "content/public/browser/render_widget_host.h" | 25 #include "content/public/browser/render_widget_host.h" |
26 #include "content/public/browser/render_widget_host_view.h" | 26 #include "content/public/browser/render_widget_host_view.h" |
27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "content/public/common/content_features.h" | 28 #include "content/public/common/content_features.h" |
29 | 29 |
30 using content::NavigationController; | 30 using content::NavigationController; |
31 using content::RenderWidgetHost; | 31 using content::RenderWidgetHost; |
32 using content::WebContents; | 32 using content::WebContents; |
33 | 33 |
| 34 namespace { |
| 35 |
| 36 size_t g_max_loaded_tab_count_for_testing = 0; |
| 37 |
| 38 } // namespace |
| 39 |
34 void TabLoader::Observe(int type, | 40 void TabLoader::Observe(int type, |
35 const content::NotificationSource& source, | 41 const content::NotificationSource& source, |
36 const content::NotificationDetails& details) { | 42 const content::NotificationDetails& details) { |
37 switch (type) { | 43 switch (type) { |
38 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | 44 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { |
39 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 45 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
40 HandleTabClosedOrLoaded(&web_contents->GetController()); | 46 HandleTabClosedOrLoaded(&web_contents->GetController()); |
41 break; | 47 break; |
42 } | 48 } |
43 case content::NOTIFICATION_LOAD_STOP: { | 49 case content::NOTIFICATION_LOAD_STOP: { |
(...skipping 27 matching lines...) Expand all Loading... |
71 // static | 77 // static |
72 void TabLoader::RestoreTabs(const std::vector<RestoredTab>& tabs, | 78 void TabLoader::RestoreTabs(const std::vector<RestoredTab>& tabs, |
73 const base::TimeTicks& restore_started) { | 79 const base::TimeTicks& restore_started) { |
74 if (!shared_tab_loader_) | 80 if (!shared_tab_loader_) |
75 shared_tab_loader_ = new TabLoader(restore_started); | 81 shared_tab_loader_ = new TabLoader(restore_started); |
76 | 82 |
77 shared_tab_loader_->stats_collector_->TrackTabs(tabs); | 83 shared_tab_loader_->stats_collector_->TrackTabs(tabs); |
78 shared_tab_loader_->StartLoading(tabs); | 84 shared_tab_loader_->StartLoading(tabs); |
79 } | 85 } |
80 | 86 |
| 87 // static |
| 88 void TabLoader::SetMaxLoadedTabCountForTest(size_t value) { |
| 89 g_max_loaded_tab_count_for_testing = value; |
| 90 } |
| 91 |
81 TabLoader::TabLoader(base::TimeTicks restore_started) | 92 TabLoader::TabLoader(base::TimeTicks restore_started) |
82 : memory_pressure_listener_( | 93 : memory_pressure_listener_( |
83 base::Bind(&TabLoader::OnMemoryPressure, base::Unretained(this))), | 94 base::Bind(&TabLoader::OnMemoryPressure, base::Unretained(this))), |
84 force_load_delay_multiplier_(1), | 95 force_load_delay_multiplier_(1), |
85 loading_enabled_(true), | 96 loading_enabled_(true), |
| 97 started_to_load_count_(0), |
86 restore_started_(restore_started) { | 98 restore_started_(restore_started) { |
87 stats_collector_ = new SessionRestoreStatsCollector( | 99 stats_collector_ = new SessionRestoreStatsCollector( |
88 restore_started, | 100 restore_started, |
89 base::MakeUnique< | 101 base::MakeUnique< |
90 SessionRestoreStatsCollector::UmaStatsReportingDelegate>()); | 102 SessionRestoreStatsCollector::UmaStatsReportingDelegate>()); |
91 shared_tab_loader_ = this; | 103 shared_tab_loader_ = this; |
92 this_retainer_ = this; | 104 this_retainer_ = this; |
93 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 105 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
94 } | 106 } |
95 | 107 |
(...skipping 15 matching lines...) Expand all Loading... |
111 for (auto& restored_tab : tabs) { | 123 for (auto& restored_tab : tabs) { |
112 if (!restored_tab.is_active()) { | 124 if (!restored_tab.is_active()) { |
113 tabs_to_load_.push_back(&restored_tab.contents()->GetController()); | 125 tabs_to_load_.push_back(&restored_tab.contents()->GetController()); |
114 favicon::ContentFaviconDriver* favicon_driver = | 126 favicon::ContentFaviconDriver* favicon_driver = |
115 favicon::ContentFaviconDriver::FromWebContents( | 127 favicon::ContentFaviconDriver::FromWebContents( |
116 restored_tab.contents()); | 128 restored_tab.contents()); |
117 // |favicon_driver| might be null when testing. | 129 // |favicon_driver| might be null when testing. |
118 if (favicon_driver) | 130 if (favicon_driver) |
119 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL()); | 131 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL()); |
120 } else { | 132 } else { |
| 133 ++started_to_load_count_; |
121 tabs_loading_.insert(&restored_tab.contents()->GetController()); | 134 tabs_loading_.insert(&restored_tab.contents()->GetController()); |
122 } | 135 } |
123 RegisterForNotifications(&restored_tab.contents()->GetController()); | 136 RegisterForNotifications(&restored_tab.contents()->GetController()); |
124 } | 137 } |
125 | 138 |
126 // When multiple profiles are using the same TabLoader, another profile might | 139 // When multiple profiles are using the same TabLoader, another profile might |
127 // already have started loading. In that case, the tabs scheduled for loading | 140 // already have started loading. In that case, the tabs scheduled for loading |
128 // by this profile are already in the loading queue, and they will get loaded | 141 // by this profile are already in the loading queue, and they will get loaded |
129 // eventually. | 142 // eventually. |
130 if (delegate_) | 143 if (delegate_) |
(...skipping 30 matching lines...) Expand all Loading... |
161 // large delay between a memory pressure event and receiving a notification | 174 // large delay between a memory pressure event and receiving a notification |
162 // of that event (in that case tab restore can trigger memory pressure but | 175 // of that event (in that case tab restore can trigger memory pressure but |
163 // will complete before the notification arrives). | 176 // will complete before the notification arrives). |
164 if (ShouldStopLoadingTabs()) { | 177 if (ShouldStopLoadingTabs()) { |
165 StopLoadingTabs(); | 178 StopLoadingTabs(); |
166 return; | 179 return; |
167 } | 180 } |
168 | 181 |
169 NavigationController* controller = tabs_to_load_.front(); | 182 NavigationController* controller = tabs_to_load_.front(); |
170 DCHECK(controller); | 183 DCHECK(controller); |
| 184 ++started_to_load_count_; |
171 tabs_loading_.insert(controller); | 185 tabs_loading_.insert(controller); |
172 tabs_to_load_.pop_front(); | 186 tabs_to_load_.pop_front(); |
173 controller->LoadIfNecessary(); | 187 controller->LoadIfNecessary(); |
174 content::WebContents* contents = controller->GetWebContents(); | 188 content::WebContents* contents = controller->GetWebContents(); |
175 if (contents) { | 189 if (contents) { |
176 Browser* browser = chrome::FindBrowserWithWebContents(contents); | 190 Browser* browser = chrome::FindBrowserWithWebContents(contents); |
177 if (browser && | 191 if (browser && |
178 browser->tab_strip_model()->GetActiveWebContents() != contents) { | 192 browser->tab_strip_model()->GetActiveWebContents() != contents) { |
179 // By default tabs are marked as visible. As only the active tab is | 193 // By default tabs are marked as visible. As only the active tab is |
180 // visible we need to explicitly tell non-active tabs they are hidden. | 194 // visible we need to explicitly tell non-active tabs they are hidden. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 content::Source<NavigationController>(controller)); | 249 content::Source<NavigationController>(controller)); |
236 } | 250 } |
237 | 251 |
238 void TabLoader::HandleTabClosedOrLoaded(NavigationController* controller) { | 252 void TabLoader::HandleTabClosedOrLoaded(NavigationController* controller) { |
239 RemoveTab(controller); | 253 RemoveTab(controller); |
240 if (delegate_) | 254 if (delegate_) |
241 LoadNextTab(); | 255 LoadNextTab(); |
242 } | 256 } |
243 | 257 |
244 bool TabLoader::ShouldStopLoadingTabs() const { | 258 bool TabLoader::ShouldStopLoadingTabs() const { |
| 259 if (g_max_loaded_tab_count_for_testing != 0 && |
| 260 started_to_load_count_ >= g_max_loaded_tab_count_for_testing) |
| 261 return true; |
245 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | 262 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) |
246 return base::MemoryCoordinatorProxy::GetInstance()->GetCurrentMemoryState() | 263 return base::MemoryCoordinatorProxy::GetInstance()->GetCurrentMemoryState() |
247 != base::MemoryState::NORMAL; | 264 != base::MemoryState::NORMAL; |
248 if (base::MemoryPressureMonitor::Get()) { | 265 if (base::MemoryPressureMonitor::Get()) { |
249 return base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() != | 266 return base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() != |
250 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 267 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
251 } | 268 } |
252 return false; | 269 return false; |
253 } | 270 } |
254 | 271 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 // memory pressure. | 308 // memory pressure. |
292 stats_collector_->DeferTab(tab); | 309 stats_collector_->DeferTab(tab); |
293 } | 310 } |
294 // By calling |LoadNextTab| explicitly, we make sure that the | 311 // By calling |LoadNextTab| explicitly, we make sure that the |
295 // |NOTIFICATION_SESSION_RESTORE_DONE| event gets sent. | 312 // |NOTIFICATION_SESSION_RESTORE_DONE| event gets sent. |
296 LoadNextTab(); | 313 LoadNextTab(); |
297 } | 314 } |
298 | 315 |
299 // static | 316 // static |
300 TabLoader* TabLoader::shared_tab_loader_ = nullptr; | 317 TabLoader* TabLoader::shared_tab_loader_ = nullptr; |
OLD | NEW |