File: tab_loader_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 (166 lines) | stat: -rw-r--r-- 6,213 bytes parent folder | download | duplicates (7)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2014 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/tab_loader_delegate.h"

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/resource_coordinator/session_restore_policy.h"
#include "chrome/browser/resource_coordinator/tab_manager_features.h"
#include "chrome/browser/sessions/session_restore_observer.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/network_service_instance.h"
#include "services/network/public/cpp/network_connection_tracker.h"

namespace {

// The timeout time after which the next tab gets loaded if the previous tab did
// not finish loading yet. The used value is half of the median value of all
// ChromeOS devices loading the 25 most common web pages. Half is chosen since
// the loading time is a mix of server response and data bandwidth.
static const int kInitialDelayTimerMS = 1500;

// Similar to the above constant, but the timeout that is afforded to the
// visible tab only. Having this be a longer value ensures the visible time has
// more time during which it is the only one loading, decreasing the time to
// first paint and interactivity of the foreground tab.
static const int kFirstTabLoadTimeoutMS = 60000;

resource_coordinator::SessionRestorePolicy* g_testing_policy = nullptr;

class TabLoaderDelegateImpl
    : public TabLoaderDelegate,
      public network::NetworkConnectionTracker::NetworkConnectionObserver {
 public:
  explicit TabLoaderDelegateImpl(TabLoaderCallback* callback);

  TabLoaderDelegateImpl(const TabLoaderDelegateImpl&) = delete;
  TabLoaderDelegateImpl& operator=(const TabLoaderDelegateImpl&) = delete;

  ~TabLoaderDelegateImpl() override;

  // TabLoaderDelegate:
  base::TimeDelta GetFirstTabLoadingTimeout() const override {
    return resource_coordinator::GetTabLoadTimeout(first_timeout_);
  }

  // TabLoaderDelegate:
  base::TimeDelta GetTimeoutBeforeLoadingNextTab() const override {
    return resource_coordinator::GetTabLoadTimeout(timeout_);
  }

  // TabLoaderDelegate:
  size_t GetMaxSimultaneousTabLoads() const override {
    return policy_->simultaneous_tab_loads();
  }

  // TabLoaderDelegate:
  float AddTabForScoring(content::WebContents* contents) const override {
    return policy_->AddTabForScoring(contents);
  }

  // TabLoaderDelegate:
  void RemoveTabForScoring(content::WebContents* contents) const override {
    policy_->RemoveTabForScoring(contents);
  }

  // TabLoaderDelegate:
  bool ShouldLoad(content::WebContents* contents) const override {
    return policy_->ShouldLoad(contents);
  }

  // TabLoaderDelegate:
  void NotifyTabLoadStarted() override { policy_->NotifyTabLoadStarted(); }

  // TabLoaderDelegate:
  resource_coordinator::SessionRestorePolicy* GetPolicyForTesting() override {
    return policy_;
  }

  // network::NetworkConnectionTracker::NetworkConnectionObserver:
  void OnConnectionChanged(network::mojom::ConnectionType type) override;

  // Callback that is invoked by the policy engine, and forwarded over to the
  // TabLoader.
  void NotifyTabScoreChanged(content::WebContents* content, float score);

 private:
  // The default policy engine used to implement ShouldLoad.
  resource_coordinator::SessionRestorePolicy default_policy_;

  // The policy engine used to implement ShouldLoad. By default this is simply
  // a pointer to |default_policy_|, but it can also point to externally
  // injected policy engine for testing.
  raw_ptr<resource_coordinator::SessionRestorePolicy> policy_;

  // The function to call when the connection type changes.
  raw_ptr<TabLoaderCallback> callback_;

  // The timeouts to use in tab loading.
  base::TimeDelta first_timeout_;
  base::TimeDelta timeout_;

  base::WeakPtrFactory<TabLoaderDelegateImpl> weak_factory_{this};
};

TabLoaderDelegateImpl::TabLoaderDelegateImpl(TabLoaderCallback* callback)
    : policy_(&default_policy_), callback_(callback) {
  content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this);
  auto type = network::mojom::ConnectionType::CONNECTION_UNKNOWN;
  content::GetNetworkConnectionTracker()->GetConnectionType(
      &type, base::BindOnce(&TabLoaderDelegateImpl::OnConnectionChanged,
                            weak_factory_.GetWeakPtr()));
  if (type == network::mojom::ConnectionType::CONNECTION_NONE) {
    // When we are off-line we do not allow loading of tabs, since each of
    // these tabs would start loading simultaneously when going online.
    // TODO(skuhne): Once we get a higher level resource control logic which
    // distributes network access, we can remove this.
    callback->SetTabLoadingEnabled(false);
  }

  first_timeout_ = base::Milliseconds(kFirstTabLoadTimeoutMS);
  timeout_ = base::Milliseconds(kInitialDelayTimerMS);

  // Override |policy_| if a testing policy has been set.
  if (g_testing_policy) {
    policy_ = g_testing_policy;
  }

  // Register the policy callback.
  policy_->SetTabScoreChangedCallback(
      base::BindRepeating(&TabLoaderDelegateImpl::NotifyTabScoreChanged,
                          weak_factory_.GetWeakPtr()));
}

TabLoaderDelegateImpl::~TabLoaderDelegateImpl() {
  content::GetNetworkConnectionTracker()->RemoveNetworkConnectionObserver(this);
}

void TabLoaderDelegateImpl::OnConnectionChanged(
    network::mojom::ConnectionType type) {
  callback_->SetTabLoadingEnabled(
      type != network::mojom::ConnectionType::CONNECTION_NONE);
}

void TabLoaderDelegateImpl::NotifyTabScoreChanged(content::WebContents* content,
                                                  float score) {
  callback_->NotifyTabScoreChanged(content, score);
}

}  // namespace

// static
std::unique_ptr<TabLoaderDelegate> TabLoaderDelegate::Create(
    TabLoaderCallback* callback) {
  return std::unique_ptr<TabLoaderDelegate>(
      new TabLoaderDelegateImpl(callback));
}

// static
void TabLoaderDelegate::SetSessionRestorePolicyForTesting(
    resource_coordinator::SessionRestorePolicy* policy) {
  g_testing_policy = policy;
}