blob: cf4535e9724ab303f73fc35e6f3cea4c9986adc6 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2015 The Chromium Authors
georgesaka3ae61c72015-04-02 01:04:262// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Arthur Sonzogni406947a02024-08-01 22:02:265#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
7#pragma allow_unsafe_buffers
8#endif
9
georgesaka3ae61c72015-04-02 01:04:2610#include "chrome/browser/sessions/session_restore_delegate.h"
11
avib896c712015-12-26 02:10:4312#include <stddef.h>
Daniel Cheng7d9e3d52022-02-26 09:03:2413
dchenge73d8520c2015-12-27 01:19:0914#include <utility>
avib896c712015-12-26 02:10:4315
georgesakfbcd45d2015-04-07 15:08:2416#include "base/metrics/field_trial.h"
georgesak98702bbb2015-04-03 17:59:2217#include "chrome/browser/sessions/session_restore_stats_collector.h"
georgesaka3ae61c72015-04-02 01:04:2618#include "chrome/browser/sessions/tab_loader.h"
georgesak189ed372015-05-08 03:53:3019#include "chrome/common/url_constants.h"
georgesak1912f4a2015-05-11 21:59:5520#include "components/favicon/content/content_favicon_driver.h"
Sebastien Marchand6ef23c42021-04-23 21:38:2221#include "components/performance_manager/public/features.h"
Hailey Wang9d1cc292020-03-26 17:02:5022#include "components/performance_manager/public/graph/policies/background_tab_loading_policy.h"
Connie Wan866179b2019-12-18 21:39:3323#include "components/tab_groups/tab_group_id.h"
24#include "components/tab_groups/tab_group_visual_data.h"
georgesak383026382015-05-08 22:58:5325#include "content/public/browser/web_contents.h"
georgesaka3ae61c72015-04-02 01:04:2626
georgesak189ed372015-05-08 03:53:3027namespace {
28
29bool IsInternalPage(const GURL& url) {
30 // There are many chrome:// UI URLs, but only look for the ones that users
31 // are likely to have open. Most of the benefit is from the NTP URL.
32 const char* const kReloadableUrlPrefixes[] = {
33 chrome::kChromeUIDownloadsURL,
34 chrome::kChromeUIHistoryURL,
35 chrome::kChromeUINewTabURL,
36 chrome::kChromeUISettingsURL,
37 };
38 // Prefix-match against the table above. Use strncmp to avoid allocating
39 // memory to convert the URL prefix constants into std::strings.
Daniel Cheng7d9e3d52022-02-26 09:03:2440 for (size_t i = 0; i < std::size(kReloadableUrlPrefixes); ++i) {
georgesak189ed372015-05-08 03:53:3041 if (!strncmp(url.spec().c_str(), kReloadableUrlPrefixes[i],
42 strlen(kReloadableUrlPrefixes[i])))
43 return true;
44 }
45 return false;
46}
47
48} // namespace
49
Collin Baker81999fd2019-06-05 23:12:2750SessionRestoreDelegate::RestoredTab::RestoredTab(
51 content::WebContents* contents,
52 bool is_active,
53 bool is_app,
54 bool is_pinned,
Arthur Sonzognife132ee2024-01-15 11:01:0455 const std::optional<tab_groups::TabGroupId>& group)
Scott Violet2981ee02024-04-23 20:12:1056 : contents_(contents->GetWeakPtr()),
georgesak189ed372015-05-08 03:53:3057 is_active_(is_active),
58 is_app_(is_app),
59 is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())),
Collin Baker81999fd2019-06-05 23:12:2760 is_pinned_(is_pinned),
61 group_(group) {}
62
Peter Kasting8ab04a42021-07-28 15:06:1963SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default;
64
65SessionRestoreDelegate::RestoredTab&
66SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default;
georgesak189ed372015-05-08 03:53:3067
Keishi Hattoricbd9cfa2021-11-23 17:38:1468SessionRestoreDelegate::RestoredTab::~RestoredTab() = default;
69
georgesak189ed372015-05-08 03:53:3070bool SessionRestoreDelegate::RestoredTab::operator<(
71 const RestoredTab& right) const {
72 // Tab with internal web UI like NTP or Settings are good choices to
73 // defer loading.
74 if (is_internal_page_ != right.is_internal_page_)
75 return !is_internal_page_;
76 // Pinned tabs should be loaded first.
77 if (is_pinned_ != right.is_pinned_)
78 return is_pinned_;
79 // Apps should be loaded before normal tabs.
80 if (is_app_ != right.is_app_)
81 return is_app_;
chrisha0598fc312016-01-07 20:40:4182 // Finally, older tabs should be deferred first.
Olivier ROBIN4b58dd932024-08-06 13:30:3283 return contents_->GetLastActiveTimeTicks() >
84 right.contents_->GetLastActiveTimeTicks();
georgesak189ed372015-05-08 03:53:3085}
86
georgesaka3ae61c72015-04-02 01:04:2687// static
georgesakb1881ce2015-05-06 20:22:1188void SessionRestoreDelegate::RestoreTabs(
89 const std::vector<RestoredTab>& tabs,
90 const base::TimeTicks& restore_started) {
Francois Doray1c7bd682020-11-24 22:46:3591 if (tabs.empty())
92 return;
93
chrishaf3c87f32015-06-20 01:21:5894 // Restore the favicon for all tabs. Any tab may end up being deferred due
95 // to memory pressure so it's best to have some visual indication of its
96 // contents.
97 for (const auto& restored_tab : tabs) {
Scott Violet2981ee02024-04-23 20:12:1098 CHECK(restored_tab.contents());
chrishaf3c87f32015-06-20 01:21:5899 // Restore the favicon for deferred tabs.
100 favicon::ContentFaviconDriver* favicon_driver =
101 favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
Scott Violet651b0772023-07-26 23:33:34102 if (favicon_driver) {
103 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
104 /*is_same_document=*/false);
105 }
chrishaf3c87f32015-06-20 01:21:58106 }
107
Francois Doray1c7bd682020-11-24 22:46:35108 SessionRestoreStatsCollector::GetOrCreateInstance(
109 restore_started,
110 std::make_unique<
111 SessionRestoreStatsCollector::UmaStatsReportingDelegate>())
112 ->TrackTabs(tabs);
113
Hailey Wang9d1cc292020-03-26 17:02:50114 // Don't start a TabLoader here if background tab loading is done by
115 // PerformanceManager.
116 if (!base::FeatureList::IsEnabled(
117 performance_manager::features::
118 kBackgroundTabLoadingFromPerformanceManager)) {
119 TabLoader::RestoreTabs(tabs, restore_started);
120 } else {
121 std::vector<content::WebContents*> web_contents_vector;
122 web_contents_vector.reserve(tabs.size());
Scott Violet2981ee02024-04-23 20:12:10123 for (auto tab : tabs) {
124 CHECK(tab.contents());
Hailey Wang9d1cc292020-03-26 17:02:50125 web_contents_vector.push_back(tab.contents());
Scott Violet2981ee02024-04-23 20:12:10126 }
Hailey Wang9d1cc292020-03-26 17:02:50127 performance_manager::policies::ScheduleLoadForRestoredTabs(
128 std::move(web_contents_vector));
129 }
georgesaka3ae61c72015-04-02 01:04:26130}