Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 2 | // 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 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 7 | #include <stddef.h> |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 8 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 9 | #include <utility> |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 10 | |
georgesak | fbcd45d | 2015-04-07 15:08:24 | [diff] [blame] | 11 | #include "base/metrics/field_trial.h" |
georgesak | 98702bbb | 2015-04-03 17:59:22 | [diff] [blame] | 12 | #include "chrome/browser/sessions/session_restore_stats_collector.h" |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 13 | #include "chrome/browser/sessions/tab_loader.h" |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 14 | #include "chrome/common/url_constants.h" |
georgesak | 1912f4a | 2015-05-11 21:59:55 | [diff] [blame] | 15 | #include "components/favicon/content/content_favicon_driver.h" |
Sebastien Marchand | 6ef23c4 | 2021-04-23 21:38:22 | [diff] [blame] | 16 | #include "components/performance_manager/public/features.h" |
Hailey Wang | 9d1cc29 | 2020-03-26 17:02:50 | [diff] [blame] | 17 | #include "components/performance_manager/public/graph/policies/background_tab_loading_policy.h" |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 18 | #include "components/tab_groups/tab_group_id.h" |
| 19 | #include "components/tab_groups/tab_group_visual_data.h" |
georgesak | 38302638 | 2015-05-08 22:58:53 | [diff] [blame] | 20 | #include "content/public/browser/web_contents.h" |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 21 | |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | bool 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 Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 35 | for (size_t i = 0; i < std::size(kReloadableUrlPrefixes); ++i) { |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 36 | 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 Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 45 | SessionRestoreDelegate::RestoredTab::RestoredTab( |
| 46 | content::WebContents* contents, |
| 47 | bool is_active, |
| 48 | bool is_app, |
| 49 | bool is_pinned, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame^] | 50 | const std::optional<tab_groups::TabGroupId>& group) |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 51 | : contents_(contents), |
| 52 | is_active_(is_active), |
| 53 | is_app_(is_app), |
| 54 | is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())), |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 55 | is_pinned_(is_pinned), |
| 56 | group_(group) {} |
| 57 | |
Peter Kasting | 8ab04a4 | 2021-07-28 15:06:19 | [diff] [blame] | 58 | SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default; |
| 59 | |
| 60 | SessionRestoreDelegate::RestoredTab& |
| 61 | SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default; |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 62 | |
Keishi Hattori | cbd9cfa | 2021-11-23 17:38:14 | [diff] [blame] | 63 | SessionRestoreDelegate::RestoredTab::~RestoredTab() = default; |
| 64 | |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 65 | bool 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_; |
chrisha | 0598fc31 | 2016-01-07 20:40:41 | [diff] [blame] | 77 | // Finally, older tabs should be deferred first. |
| 78 | return contents_->GetLastActiveTime() > right.contents_->GetLastActiveTime(); |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 79 | } |
| 80 | |
Scott Violet | a1efaa4d | 2023-09-18 21:00:52 | [diff] [blame] | 81 | void SessionRestoreDelegate::RestoredTab::StartTrackingWebContentsLifetime() { |
| 82 | if (tracker_) { |
| 83 | return; |
| 84 | } |
| 85 | tracker_ = base::MakeRefCounted<WebContentsTracker>(contents_); |
| 86 | } |
| 87 | |
| 88 | void SessionRestoreDelegate::RestoredTab::StopTrackingWebContentsLifetime() { |
| 89 | tracker_ = nullptr; |
| 90 | } |
| 91 | |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 92 | // static |
georgesak | b1881ce | 2015-05-06 20:22:11 | [diff] [blame] | 93 | void SessionRestoreDelegate::RestoreTabs( |
| 94 | const std::vector<RestoredTab>& tabs, |
| 95 | const base::TimeTicks& restore_started) { |
Francois Doray | 1c7bd68 | 2020-11-24 22:46:35 | [diff] [blame] | 96 | if (tabs.empty()) |
| 97 | return; |
| 98 | |
chrisha | f3c87f3 | 2015-06-20 01:21:58 | [diff] [blame] | 99 | // 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 Violet | 651b077 | 2023-07-26 23:33:34 | [diff] [blame] | 106 | if (favicon_driver) { |
| 107 | favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(), |
| 108 | /*is_same_document=*/false); |
| 109 | } |
chrisha | f3c87f3 | 2015-06-20 01:21:58 | [diff] [blame] | 110 | } |
| 111 | |
Francois Doray | 1c7bd68 | 2020-11-24 22:46:35 | [diff] [blame] | 112 | SessionRestoreStatsCollector::GetOrCreateInstance( |
| 113 | restore_started, |
| 114 | std::make_unique< |
| 115 | SessionRestoreStatsCollector::UmaStatsReportingDelegate>()) |
| 116 | ->TrackTabs(tabs); |
| 117 | |
Hailey Wang | 9d1cc29 | 2020-03-26 17:02:50 | [diff] [blame] | 118 | // 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 | } |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 132 | } |
Scott Violet | a1efaa4d | 2023-09-18 21:00:52 | [diff] [blame] | 133 | |
| 134 | SessionRestoreDelegate::WebContentsTracker::WebContentsTracker( |
| 135 | content::WebContents* web_contents) |
| 136 | : WebContentsObserver(web_contents) {} |
| 137 | |
| 138 | void SessionRestoreDelegate::WebContentsTracker::WebContentsDestroyed() { |
| 139 | // TODO(https://crbug.com/1482502): remove once crash understood. |
| 140 | DUMP_WILL_BE_CHECK(false); |
| 141 | } |