blob: a008f64ba928c4c1032a96026ce35c0526dd890a [file] [log] [blame]
georgesaka3ae61c72015-04-02 01:04:261// Copyright 2015 The Chromium Authors. All rights reserved.
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
avib896c712015-12-26 02:10:437#include <stddef.h>
dchenge73d8520c2015-12-27 01:19:098#include <utility>
avib896c712015-12-26 02:10:439
georgesakfbcd45d2015-04-07 15:08:2410#include "base/metrics/field_trial.h"
Avi Drissman22f82872018-12-25 23:09:0711#include "base/stl_util.h"
Hailey Wang9d1cc292020-03-26 17:02:5012#include "chrome/browser/performance_manager/graph/policies/policy_features.h"
georgesak98702bbb2015-04-03 17:59:2213#include "chrome/browser/sessions/session_restore_stats_collector.h"
georgesaka3ae61c72015-04-02 01:04:2614#include "chrome/browser/sessions/tab_loader.h"
georgesak189ed372015-05-08 03:53:3015#include "chrome/common/url_constants.h"
georgesak1912f4a2015-05-11 21:59:5516#include "components/favicon/content/content_favicon_driver.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.
Avi Drissman22f82872018-12-25 23:09:0735 for (size_t i = 0; i < base::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,
Connie Wan866179b2019-12-18 21:39:3350 const base::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
58SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab& other) =
59 default;
georgesak189ed372015-05-08 03:53:3060
61bool SessionRestoreDelegate::RestoredTab::operator<(
62 const RestoredTab& right) const {
63 // Tab with internal web UI like NTP or Settings are good choices to
64 // defer loading.
65 if (is_internal_page_ != right.is_internal_page_)
66 return !is_internal_page_;
67 // Pinned tabs should be loaded first.
68 if (is_pinned_ != right.is_pinned_)
69 return is_pinned_;
70 // Apps should be loaded before normal tabs.
71 if (is_app_ != right.is_app_)
72 return is_app_;
chrisha0598fc312016-01-07 20:40:4173 // Finally, older tabs should be deferred first.
74 return contents_->GetLastActiveTime() > right.contents_->GetLastActiveTime();
georgesak189ed372015-05-08 03:53:3075}
76
georgesaka3ae61c72015-04-02 01:04:2677// static
georgesakb1881ce2015-05-06 20:22:1178void SessionRestoreDelegate::RestoreTabs(
79 const std::vector<RestoredTab>& tabs,
80 const base::TimeTicks& restore_started) {
chrishaf3c87f32015-06-20 01:21:5881 // Restore the favicon for all tabs. Any tab may end up being deferred due
82 // to memory pressure so it's best to have some visual indication of its
83 // contents.
84 for (const auto& restored_tab : tabs) {
85 // Restore the favicon for deferred tabs.
86 favicon::ContentFaviconDriver* favicon_driver =
87 favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
Mikel Astiz047511ef2017-08-15 22:23:3288 favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
89 /*is_same_document=*/false);
chrishaf3c87f32015-06-20 01:21:5890 }
91
Hailey Wang9d1cc292020-03-26 17:02:5092 // Don't start a TabLoader here if background tab loading is done by
93 // PerformanceManager.
94 if (!base::FeatureList::IsEnabled(
95 performance_manager::features::
96 kBackgroundTabLoadingFromPerformanceManager)) {
97 TabLoader::RestoreTabs(tabs, restore_started);
98 } else {
99 std::vector<content::WebContents*> web_contents_vector;
100 web_contents_vector.reserve(tabs.size());
101 for (auto tab : tabs)
102 web_contents_vector.push_back(tab.contents());
103 performance_manager::policies::ScheduleLoadForRestoredTabs(
104 std::move(web_contents_vector));
105 }
georgesaka3ae61c72015-04-02 01:04:26106}