blob: fd279139f99d0c1c0dd0162d193d668007e0f4ff [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
Tom Sepezd3ac02b2025-04-17 22:58:0612#include "base/strings/string_util.h"
13#include "base/metrics/field_trial.h"
14#include "base/compiler_specific.h"
georgesakfbcd45d2015-04-07 15:08:2415#include "base/metrics/field_trial.h"
Youngsoo Choi482e98d2024-11-06 23:42:3916#include "chrome/browser/performance_manager/public/background_tab_loading_policy.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"
Connie Wan866179b2019-12-18 21:39:3322#include "components/tab_groups/tab_group_id.h"
23#include "components/tab_groups/tab_group_visual_data.h"
georgesak383026382015-05-08 22:58:5324#include "content/public/browser/web_contents.h"
georgesaka3ae61c72015-04-02 01:04:2625
georgesak189ed372015-05-08 03:53:3026namespace {
27
28bool IsInternalPage(const GURL& url) {
29 // There are many chrome:// UI URLs, but only look for the ones that users
30 // are likely to have open. Most of the benefit is from the NTP URL.
Arthur Sonzogni6718b702025-01-09 10:49:1031 const auto kReloadableUrlPrefixes = std::to_array<const char*>({
georgesak189ed372015-05-08 03:53:3032 chrome::kChromeUIDownloadsURL,
33 chrome::kChromeUIHistoryURL,
34 chrome::kChromeUINewTabURL,
35 chrome::kChromeUISettingsURL,
Arthur Sonzogni6718b702025-01-09 10:49:1036 });
Tom Sepezd3ac02b2025-04-17 22:58:0637 // Prefix-match against the table above.
38 for (const char* prefix : kReloadableUrlPrefixes) {
39 if (base::StartsWith(url.spec(), prefix)) {
georgesak189ed372015-05-08 03:53:3040 return true;
Tom Sepezd3ac02b2025-04-17 22:58:0641 }
georgesak189ed372015-05-08 03:53:3042 }
43 return false;
44}
45
46} // namespace
47
Collin Baker81999fd2019-06-05 23:12:2748SessionRestoreDelegate::RestoredTab::RestoredTab(
49 content::WebContents* contents,
50 bool is_active,
51 bool is_app,
52 bool is_pinned,
Arthur Sonzognife132ee2024-01-15 11:01:0453 const std::optional<tab_groups::TabGroupId>& group)
Scott Violet2981ee02024-04-23 20:12:1054 : contents_(contents->GetWeakPtr()),
georgesak189ed372015-05-08 03:53:3055 is_active_(is_active),
56 is_app_(is_app),
57 is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())),
Collin Baker81999fd2019-06-05 23:12:2758 is_pinned_(is_pinned),
59 group_(group) {}
60
Peter Kasting8ab04a42021-07-28 15:06:1961SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default;
62
63SessionRestoreDelegate::RestoredTab&
64SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default;
georgesak189ed372015-05-08 03:53:3065
Keishi Hattoricbd9cfa2021-11-23 17:38:1466SessionRestoreDelegate::RestoredTab::~RestoredTab() = default;
67
georgesak189ed372015-05-08 03:53:3068bool SessionRestoreDelegate::RestoredTab::operator<(
69 const RestoredTab& right) const {
70 // Tab with internal web UI like NTP or Settings are good choices to
71 // defer loading.
72 if (is_internal_page_ != right.is_internal_page_)
73 return !is_internal_page_;
74 // Pinned tabs should be loaded first.
75 if (is_pinned_ != right.is_pinned_)
76 return is_pinned_;
77 // Apps should be loaded before normal tabs.
78 if (is_app_ != right.is_app_)
79 return is_app_;
chrisha0598fc312016-01-07 20:40:4180 // Finally, older tabs should be deferred first.
Olivier ROBIN4b58dd932024-08-06 13:30:3281 return contents_->GetLastActiveTimeTicks() >
82 right.contents_->GetLastActiveTimeTicks();
georgesak189ed372015-05-08 03:53:3083}
84
georgesaka3ae61c72015-04-02 01:04:2685// static
georgesakb1881ce2015-05-06 20:22:1186void SessionRestoreDelegate::RestoreTabs(
87 const std::vector<RestoredTab>& tabs,
88 const base::TimeTicks& restore_started) {
Francois Doray1c7bd682020-11-24 22:46:3589 if (tabs.empty())
90 return;
91
chrishaf3c87f32015-06-20 01:21:5892 // Restore the favicon for all tabs. Any tab may end up being deferred due
93 // to memory pressure so it's best to have some visual indication of its
94 // contents.
95 for (const auto& restored_tab : tabs) {
Scott Violet2981ee02024-04-23 20:12:1096 CHECK(restored_tab.contents());
chrishaf3c87f32015-06-20 01:21:5897 // Restore the favicon for deferred tabs.
98 favicon::ContentFaviconDriver* favicon_driver =
99 favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
Scott Violet651b0772023-07-26 23:33:34100 if (favicon_driver) {
101 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
102 /*is_same_document=*/false);
103 }
chrishaf3c87f32015-06-20 01:21:58104 }
105
Francois Doray1c7bd682020-11-24 22:46:35106 SessionRestoreStatsCollector::GetOrCreateInstance(
107 restore_started,
108 std::make_unique<
109 SessionRestoreStatsCollector::UmaStatsReportingDelegate>())
110 ->TrackTabs(tabs);
111
Hailey Wang9d1cc292020-03-26 17:02:50112 // Don't start a TabLoader here if background tab loading is done by
113 // PerformanceManager.
114 if (!base::FeatureList::IsEnabled(
115 performance_manager::features::
116 kBackgroundTabLoadingFromPerformanceManager)) {
117 TabLoader::RestoreTabs(tabs, restore_started);
118 } else {
119 std::vector<content::WebContents*> web_contents_vector;
120 web_contents_vector.reserve(tabs.size());
Tom Sepez009245442025-05-08 23:18:39121 for (const auto& tab : tabs) {
Scott Violet2981ee02024-04-23 20:12:10122 CHECK(tab.contents());
Hailey Wang9d1cc292020-03-26 17:02:50123 web_contents_vector.push_back(tab.contents());
Scott Violet2981ee02024-04-23 20:12:10124 }
Hailey Wang9d1cc292020-03-26 17:02:50125 performance_manager::policies::ScheduleLoadForRestoredTabs(
126 std::move(web_contents_vector));
127 }
georgesaka3ae61c72015-04-02 01:04:26128}