blob: 19b7e841b0e5c43ee47030b23115eceb23faf659 [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
dchenge73d8520c2015-12-27 01:19:099#include <utility>
avib896c712015-12-26 02:10:4310
georgesakfbcd45d2015-04-07 15:08:2411#include "base/metrics/field_trial.h"
georgesak98702bbb2015-04-03 17:59:2212#include "chrome/browser/sessions/session_restore_stats_collector.h"
georgesaka3ae61c72015-04-02 01:04:2613#include "chrome/browser/sessions/tab_loader.h"
georgesak189ed372015-05-08 03:53:3014#include "chrome/common/url_constants.h"
georgesak1912f4a2015-05-11 21:59:5515#include "components/favicon/content/content_favicon_driver.h"
Sebastien Marchand6ef23c42021-04-23 21:38:2216#include "components/performance_manager/public/features.h"
Hailey Wang9d1cc292020-03-26 17:02:5017#include "components/performance_manager/public/graph/policies/background_tab_loading_policy.h"
Connie Wan866179b2019-12-18 21:39:3318#include "components/tab_groups/tab_group_id.h"
19#include "components/tab_groups/tab_group_visual_data.h"
georgesak383026382015-05-08 22:58:5320#include "content/public/browser/web_contents.h"
georgesaka3ae61c72015-04-02 01:04:2621
georgesak189ed372015-05-08 03:53:3022namespace {
23
24bool IsInternalPage(const GURL& url) {
25 // There are many chrome:// UI URLs, but only look for the ones that users
26 // are likely to have open. Most of the benefit is from the NTP URL.
27 const char* const kReloadableUrlPrefixes[] = {
28 chrome::kChromeUIDownloadsURL,
29 chrome::kChromeUIHistoryURL,
30 chrome::kChromeUINewTabURL,
31 chrome::kChromeUISettingsURL,
32 };
33 // Prefix-match against the table above. Use strncmp to avoid allocating
34 // memory to convert the URL prefix constants into std::strings.
Daniel Cheng7d9e3d52022-02-26 09:03:2435 for (size_t i = 0; i < std::size(kReloadableUrlPrefixes); ++i) {
georgesak189ed372015-05-08 03:53:3036 if (!strncmp(url.spec().c_str(), kReloadableUrlPrefixes[i],
37 strlen(kReloadableUrlPrefixes[i])))
38 return true;
39 }
40 return false;
41}
42
43} // namespace
44
Collin Baker81999fd2019-06-05 23:12:2745SessionRestoreDelegate::RestoredTab::RestoredTab(
46 content::WebContents* contents,
47 bool is_active,
48 bool is_app,
49 bool is_pinned,
Arthur Sonzognife132ee2024-01-15 11:01:0450 const std::optional<tab_groups::TabGroupId>& group)
georgesak189ed372015-05-08 03:53:3051 : contents_(contents),
52 is_active_(is_active),
53 is_app_(is_app),
54 is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())),
Collin Baker81999fd2019-06-05 23:12:2755 is_pinned_(is_pinned),
56 group_(group) {}
57
Peter Kasting8ab04a42021-07-28 15:06:1958SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default;
59
60SessionRestoreDelegate::RestoredTab&
61SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default;
georgesak189ed372015-05-08 03:53:3062
Keishi Hattoricbd9cfa2021-11-23 17:38:1463SessionRestoreDelegate::RestoredTab::~RestoredTab() = default;
64
georgesak189ed372015-05-08 03:53:3065bool SessionRestoreDelegate::RestoredTab::operator<(
66 const RestoredTab& right) const {
67 // Tab with internal web UI like NTP or Settings are good choices to
68 // defer loading.
69 if (is_internal_page_ != right.is_internal_page_)
70 return !is_internal_page_;
71 // Pinned tabs should be loaded first.
72 if (is_pinned_ != right.is_pinned_)
73 return is_pinned_;
74 // Apps should be loaded before normal tabs.
75 if (is_app_ != right.is_app_)
76 return is_app_;
chrisha0598fc312016-01-07 20:40:4177 // Finally, older tabs should be deferred first.
78 return contents_->GetLastActiveTime() > right.contents_->GetLastActiveTime();
georgesak189ed372015-05-08 03:53:3079}
80
Scott Violeta1efaa4d2023-09-18 21:00:5281void SessionRestoreDelegate::RestoredTab::StartTrackingWebContentsLifetime() {
82 if (tracker_) {
83 return;
84 }
85 tracker_ = base::MakeRefCounted<WebContentsTracker>(contents_);
86}
87
88void SessionRestoreDelegate::RestoredTab::StopTrackingWebContentsLifetime() {
89 tracker_ = nullptr;
90}
91
georgesaka3ae61c72015-04-02 01:04:2692// static
georgesakb1881ce2015-05-06 20:22:1193void SessionRestoreDelegate::RestoreTabs(
94 const std::vector<RestoredTab>& tabs,
95 const base::TimeTicks& restore_started) {
Francois Doray1c7bd682020-11-24 22:46:3596 if (tabs.empty())
97 return;
98
chrishaf3c87f32015-06-20 01:21:5899 // Restore the favicon for all tabs. Any tab may end up being deferred due
100 // to memory pressure so it's best to have some visual indication of its
101 // contents.
102 for (const auto& restored_tab : tabs) {
103 // Restore the favicon for deferred tabs.
104 favicon::ContentFaviconDriver* favicon_driver =
105 favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
Scott Violet651b0772023-07-26 23:33:34106 if (favicon_driver) {
107 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
108 /*is_same_document=*/false);
109 }
chrishaf3c87f32015-06-20 01:21:58110 }
111
Francois Doray1c7bd682020-11-24 22:46:35112 SessionRestoreStatsCollector::GetOrCreateInstance(
113 restore_started,
114 std::make_unique<
115 SessionRestoreStatsCollector::UmaStatsReportingDelegate>())
116 ->TrackTabs(tabs);
117
Hailey Wang9d1cc292020-03-26 17:02:50118 // Don't start a TabLoader here if background tab loading is done by
119 // PerformanceManager.
120 if (!base::FeatureList::IsEnabled(
121 performance_manager::features::
122 kBackgroundTabLoadingFromPerformanceManager)) {
123 TabLoader::RestoreTabs(tabs, restore_started);
124 } else {
125 std::vector<content::WebContents*> web_contents_vector;
126 web_contents_vector.reserve(tabs.size());
127 for (auto tab : tabs)
128 web_contents_vector.push_back(tab.contents());
129 performance_manager::policies::ScheduleLoadForRestoredTabs(
130 std::move(web_contents_vector));
131 }
georgesaka3ae61c72015-04-02 01:04:26132}
Scott Violeta1efaa4d2023-09-18 21:00:52133
134SessionRestoreDelegate::WebContentsTracker::WebContentsTracker(
135 content::WebContents* web_contents)
136 : WebContentsObserver(web_contents) {}
137
138void SessionRestoreDelegate::WebContentsTracker::WebContentsDestroyed() {
139 // TODO(https://crbug.com/1482502): remove once crash understood.
140 DUMP_WILL_BE_CHECK(false);
141}