georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 1 | // 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 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 7 | #include <stddef.h> |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 8 | #include <utility> |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 9 | |
georgesak | fbcd45d | 2015-04-07 15:08:24 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
Avi Drissman | 22f8287 | 2018-12-25 23:09:07 | [diff] [blame] | 11 | #include "base/stl_util.h" |
Hailey Wang | 9d1cc29 | 2020-03-26 17:02:50 | [diff] [blame^] | 12 | #include "chrome/browser/performance_manager/graph/policies/policy_features.h" |
georgesak | 98702bbb | 2015-04-03 17:59:22 | [diff] [blame] | 13 | #include "chrome/browser/sessions/session_restore_stats_collector.h" |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 14 | #include "chrome/browser/sessions/tab_loader.h" |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 15 | #include "chrome/common/url_constants.h" |
georgesak | 1912f4a | 2015-05-11 21:59:55 | [diff] [blame] | 16 | #include "components/favicon/content/content_favicon_driver.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. |
Avi Drissman | 22f8287 | 2018-12-25 23:09:07 | [diff] [blame] | 35 | for (size_t i = 0; i < base::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, |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 50 | const base::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 | |
| 58 | SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab& other) = |
| 59 | default; |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 60 | |
| 61 | bool 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_; |
chrisha | 0598fc31 | 2016-01-07 20:40:41 | [diff] [blame] | 73 | // Finally, older tabs should be deferred first. |
| 74 | return contents_->GetLastActiveTime() > right.contents_->GetLastActiveTime(); |
georgesak | 189ed37 | 2015-05-08 03:53:30 | [diff] [blame] | 75 | } |
| 76 | |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 77 | // static |
georgesak | b1881ce | 2015-05-06 20:22:11 | [diff] [blame] | 78 | void SessionRestoreDelegate::RestoreTabs( |
| 79 | const std::vector<RestoredTab>& tabs, |
| 80 | const base::TimeTicks& restore_started) { |
chrisha | f3c87f3 | 2015-06-20 01:21:58 | [diff] [blame] | 81 | // 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 Astiz | 047511ef | 2017-08-15 22:23:32 | [diff] [blame] | 88 | favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(), |
| 89 | /*is_same_document=*/false); |
chrisha | f3c87f3 | 2015-06-20 01:21:58 | [diff] [blame] | 90 | } |
| 91 | |
Hailey Wang | 9d1cc29 | 2020-03-26 17:02:50 | [diff] [blame^] | 92 | // 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 | } |
georgesak | a3ae61c7 | 2015-04-02 01:04:26 | [diff] [blame] | 106 | } |