blob: b2c04616b3056120687633c5babfe8e4fe524d8c [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
5#include "chrome/browser/sessions/session_restore_delegate.h"
6
avib896c712015-12-26 02:10:437#include <stddef.h>
Daniel Cheng7d9e3d52022-02-26 09:03:248
Arthur Sonzogni6718b702025-01-09 10:49:109#include <array>
dchenge73d8520c2015-12-27 01:19:0910#include <utility>
avib896c712015-12-26 02:10:4311
georgesakfbcd45d2015-04-07 15:08:2412#include "base/metrics/field_trial.h"
Youngsoo Choi482e98d2024-11-06 23:42:3913#include "chrome/browser/performance_manager/public/background_tab_loading_policy.h"
georgesak98702bbb2015-04-03 17:59:2214#include "chrome/browser/sessions/session_restore_stats_collector.h"
georgesaka3ae61c72015-04-02 01:04:2615#include "chrome/browser/sessions/tab_loader.h"
georgesak189ed372015-05-08 03:53:3016#include "chrome/common/url_constants.h"
georgesak1912f4a2015-05-11 21:59:5517#include "components/favicon/content/content_favicon_driver.h"
Sebastien Marchand6ef23c42021-04-23 21:38:2218#include "components/performance_manager/public/features.h"
Connie Wan866179b2019-12-18 21:39:3319#include "components/tab_groups/tab_group_id.h"
20#include "components/tab_groups/tab_group_visual_data.h"
georgesak383026382015-05-08 22:58:5321#include "content/public/browser/web_contents.h"
georgesaka3ae61c72015-04-02 01:04:2622
georgesak189ed372015-05-08 03:53:3023namespace {
24
25bool IsInternalPage(const GURL& url) {
26 // There are many chrome:// UI URLs, but only look for the ones that users
27 // are likely to have open. Most of the benefit is from the NTP URL.
Arthur Sonzogni6718b702025-01-09 10:49:1028 const auto kReloadableUrlPrefixes = std::to_array<const char*>({
georgesak189ed372015-05-08 03:53:3029 chrome::kChromeUIDownloadsURL,
30 chrome::kChromeUIHistoryURL,
31 chrome::kChromeUINewTabURL,
32 chrome::kChromeUISettingsURL,
Arthur Sonzogni6718b702025-01-09 10:49:1033 });
georgesak189ed372015-05-08 03:53:3034 // Prefix-match against the table above. Use strncmp to avoid allocating
35 // memory to convert the URL prefix constants into std::strings.
Daniel Cheng7d9e3d52022-02-26 09:03:2436 for (size_t i = 0; i < std::size(kReloadableUrlPrefixes); ++i) {
georgesak189ed372015-05-08 03:53:3037 if (!strncmp(url.spec().c_str(), kReloadableUrlPrefixes[i],
38 strlen(kReloadableUrlPrefixes[i])))
39 return true;
40 }
41 return false;
42}
43
44} // namespace
45
Collin Baker81999fd2019-06-05 23:12:2746SessionRestoreDelegate::RestoredTab::RestoredTab(
47 content::WebContents* contents,
48 bool is_active,
49 bool is_app,
50 bool is_pinned,
Arthur Sonzognife132ee2024-01-15 11:01:0451 const std::optional<tab_groups::TabGroupId>& group)
Scott Violet2981ee02024-04-23 20:12:1052 : contents_(contents->GetWeakPtr()),
georgesak189ed372015-05-08 03:53:3053 is_active_(is_active),
54 is_app_(is_app),
55 is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())),
Collin Baker81999fd2019-06-05 23:12:2756 is_pinned_(is_pinned),
57 group_(group) {}
58
Peter Kasting8ab04a42021-07-28 15:06:1959SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default;
60
61SessionRestoreDelegate::RestoredTab&
62SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default;
georgesak189ed372015-05-08 03:53:3063
Keishi Hattoricbd9cfa2021-11-23 17:38:1464SessionRestoreDelegate::RestoredTab::~RestoredTab() = default;
65
georgesak189ed372015-05-08 03:53:3066bool SessionRestoreDelegate::RestoredTab::operator<(
67 const RestoredTab& right) const {
68 // Tab with internal web UI like NTP or Settings are good choices to
69 // defer loading.
70 if (is_internal_page_ != right.is_internal_page_)
71 return !is_internal_page_;
72 // Pinned tabs should be loaded first.
73 if (is_pinned_ != right.is_pinned_)
74 return is_pinned_;
75 // Apps should be loaded before normal tabs.
76 if (is_app_ != right.is_app_)
77 return is_app_;
chrisha0598fc312016-01-07 20:40:4178 // Finally, older tabs should be deferred first.
Olivier ROBIN4b58dd932024-08-06 13:30:3279 return contents_->GetLastActiveTimeTicks() >
80 right.contents_->GetLastActiveTimeTicks();
georgesak189ed372015-05-08 03:53:3081}
82
georgesaka3ae61c72015-04-02 01:04:2683// static
georgesakb1881ce2015-05-06 20:22:1184void SessionRestoreDelegate::RestoreTabs(
85 const std::vector<RestoredTab>& tabs,
86 const base::TimeTicks& restore_started) {
Francois Doray1c7bd682020-11-24 22:46:3587 if (tabs.empty())
88 return;
89
chrishaf3c87f32015-06-20 01:21:5890 // Restore the favicon for all tabs. Any tab may end up being deferred due
91 // to memory pressure so it's best to have some visual indication of its
92 // contents.
93 for (const auto& restored_tab : tabs) {
Scott Violet2981ee02024-04-23 20:12:1094 CHECK(restored_tab.contents());
chrishaf3c87f32015-06-20 01:21:5895 // Restore the favicon for deferred tabs.
96 favicon::ContentFaviconDriver* favicon_driver =
97 favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
Scott Violet651b0772023-07-26 23:33:3498 if (favicon_driver) {
99 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
100 /*is_same_document=*/false);
101 }
chrishaf3c87f32015-06-20 01:21:58102 }
103
Francois Doray1c7bd682020-11-24 22:46:35104 SessionRestoreStatsCollector::GetOrCreateInstance(
105 restore_started,
106 std::make_unique<
107 SessionRestoreStatsCollector::UmaStatsReportingDelegate>())
108 ->TrackTabs(tabs);
109
Hailey Wang9d1cc292020-03-26 17:02:50110 // Don't start a TabLoader here if background tab loading is done by
111 // PerformanceManager.
112 if (!base::FeatureList::IsEnabled(
113 performance_manager::features::
114 kBackgroundTabLoadingFromPerformanceManager)) {
115 TabLoader::RestoreTabs(tabs, restore_started);
116 } else {
117 std::vector<content::WebContents*> web_contents_vector;
118 web_contents_vector.reserve(tabs.size());
Scott Violet2981ee02024-04-23 20:12:10119 for (auto tab : tabs) {
120 CHECK(tab.contents());
Hailey Wang9d1cc292020-03-26 17:02:50121 web_contents_vector.push_back(tab.contents());
Scott Violet2981ee02024-04-23 20:12:10122 }
Hailey Wang9d1cc292020-03-26 17:02:50123 performance_manager::policies::ScheduleLoadForRestoredTabs(
124 std::move(web_contents_vector));
125 }
georgesaka3ae61c72015-04-02 01:04:26126}