File: session_restore_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 4,460 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sessions/session_restore_delegate.h"

#include <stddef.h>

#include <array>
#include <utility>

#include "base/strings/string_util.h"
#include "base/metrics/field_trial.h"
#include "base/compiler_specific.h"
#include "base/metrics/field_trial.h"
#include "chrome/browser/performance_manager/public/background_tab_loading_policy.h"
#include "chrome/browser/sessions/session_restore_stats_collector.h"
#include "chrome/browser/sessions/tab_loader.h"
#include "chrome/common/url_constants.h"
#include "components/favicon/content/content_favicon_driver.h"
#include "components/performance_manager/public/features.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "content/public/browser/web_contents.h"

namespace {

bool IsInternalPage(const GURL& url) {
  // There are many chrome:// UI URLs, but only look for the ones that users
  // are likely to have open. Most of the benefit is from the NTP URL.
  const auto kReloadableUrlPrefixes = std::to_array<const char*>({
      chrome::kChromeUIDownloadsURL,
      chrome::kChromeUIHistoryURL,
      chrome::kChromeUINewTabURL,
      chrome::kChromeUISettingsURL,
  });
  // Prefix-match against the table above.
  for (const char* prefix : kReloadableUrlPrefixes) {
    if (base::StartsWith(url.spec(), prefix)) {
      return true;
    }
  }
  return false;
}

}  // namespace

SessionRestoreDelegate::RestoredTab::RestoredTab(
    content::WebContents* contents,
    bool is_active,
    bool is_app,
    bool is_pinned,
    const std::optional<tab_groups::TabGroupId>& group)
    : contents_(contents->GetWeakPtr()),
      is_active_(is_active),
      is_app_(is_app),
      is_internal_page_(IsInternalPage(contents->GetLastCommittedURL())),
      is_pinned_(is_pinned),
      group_(group) {}

SessionRestoreDelegate::RestoredTab::RestoredTab(const RestoredTab&) = default;

SessionRestoreDelegate::RestoredTab&
SessionRestoreDelegate::RestoredTab::operator=(const RestoredTab&) = default;

SessionRestoreDelegate::RestoredTab::~RestoredTab() = default;

bool SessionRestoreDelegate::RestoredTab::operator<(
    const RestoredTab& right) const {
  // Tab with internal web UI like NTP or Settings are good choices to
  // defer loading.
  if (is_internal_page_ != right.is_internal_page_)
    return !is_internal_page_;
  // Pinned tabs should be loaded first.
  if (is_pinned_ != right.is_pinned_)
    return is_pinned_;
  // Apps should be loaded before normal tabs.
  if (is_app_ != right.is_app_)
    return is_app_;
  // Finally, older tabs should be deferred first.
  return contents_->GetLastActiveTimeTicks() >
         right.contents_->GetLastActiveTimeTicks();
}

// static
void SessionRestoreDelegate::RestoreTabs(
    const std::vector<RestoredTab>& tabs,
    const base::TimeTicks& restore_started) {
  if (tabs.empty())
    return;

  // Restore the favicon for all tabs. Any tab may end up being deferred due
  // to memory pressure so it's best to have some visual indication of its
  // contents.
  for (const auto& restored_tab : tabs) {
    CHECK(restored_tab.contents());
    // Restore the favicon for deferred tabs.
    favicon::ContentFaviconDriver* favicon_driver =
        favicon::ContentFaviconDriver::FromWebContents(restored_tab.contents());
    if (favicon_driver) {
      favicon_driver->FetchFavicon(favicon_driver->GetActiveURL(),
                                   /*is_same_document=*/false);
    }
  }

  SessionRestoreStatsCollector::GetOrCreateInstance(
      restore_started,
      std::make_unique<
          SessionRestoreStatsCollector::UmaStatsReportingDelegate>())
      ->TrackTabs(tabs);

  // Don't start a TabLoader here if background tab loading is done by
  // PerformanceManager.
  if (!base::FeatureList::IsEnabled(
          performance_manager::features::
              kBackgroundTabLoadingFromPerformanceManager)) {
    TabLoader::RestoreTabs(tabs, restore_started);
  } else {
    std::vector<content::WebContents*> web_contents_vector;
    web_contents_vector.reserve(tabs.size());
    for (const auto& tab : tabs) {
      CHECK(tab.contents());
      web_contents_vector.push_back(tab.contents());
    }
    performance_manager::policies::ScheduleLoadForRestoredTabs(
        std::move(web_contents_vector));
  }
}