File: renderer_cancellation_throttle.cc

package info (click to toggle)
chromium 142.0.7444.175-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,303,984 kB
  • sloc: cpp: 35,488,370; ansic: 7,479,680; javascript: 4,259,373; python: 1,466,844; xml: 757,444; asm: 710,716; pascal: 187,980; sh: 89,247; perl: 88,690; objc: 79,984; sql: 56,984; cs: 42,192; fortran: 24,137; makefile: 22,919; tcl: 15,277; php: 14,018; yacc: 9,005; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (175 lines) | stat: -rw-r--r-- 6,242 bytes parent folder | download | duplicates (4)
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
167
168
169
170
171
172
173
174
175
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/renderer_cancellation_throttle.h"

#include "base/metrics/histogram_functions.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/navigation_request.h"

namespace content {

namespace {

// Default timeout for the cancellation window. From gathering data through the
// "Navigation.RendererInitiatedCancellation.DeferStartToCancellationWindowEnd"
// UMA, 99% of navigations' cancellation window ends in under 2000ms, and all
// cancellation windows end in under 10000ms, so setting this to 11000ms.
const base::FeatureParam<base::TimeDelta> throttle_timeout{
    &features::kRendererCancellationThrottleImprovements, "timeout",
    base::Milliseconds(11000)};

}  // namespace

// static
void RendererCancellationThrottle::MaybeCreateAndAdd(
    NavigationThrottleRegistry& registry) {
  NavigationRequest* request =
      NavigationRequest::From(&registry.GetNavigationHandle());
  if (request->ShouldWaitForRendererCancellationWindowToEnd()) {
    registry.AddThrottle(
        std::make_unique<RendererCancellationThrottle>(registry));
  }
}

RendererCancellationThrottle::RendererCancellationThrottle(
    NavigationThrottleRegistry& registry)
    : NavigationThrottle(registry) {}

RendererCancellationThrottle::~RendererCancellationThrottle() {
  if (defer_start_time_ == base::TimeTicks()) {
    return;
  }
  base::UmaHistogramBoolean(
      "Navigation.RendererCancellationThrottle.NavigationCancelled",
      !did_resume_navigation_);
  if (!did_resume_navigation_) {
    base::UmaHistogramTimes(
        "Navigation.RendererCancellationThrottle.NavigationCancelled."
        "TimeUntilCancel",
        base::TimeTicks::Now() - defer_start_time_);
  }
}

NavigationThrottle::ThrottleCheckResult
RendererCancellationThrottle::WillProcessResponse() {
  return WaitForRendererCancellationIfNeeded();
}

NavigationThrottle::ThrottleCheckResult
RendererCancellationThrottle::WillCommitWithoutUrlLoader() {
  return WaitForRendererCancellationIfNeeded();
}

NavigationThrottle::ThrottleCheckResult
RendererCancellationThrottle::WaitForRendererCancellationIfNeeded() {
  NavigationRequest* request = NavigationRequest::From(navigation_handle());
  DCHECK(request);
  if (request->renderer_cancellation_window_ended()) {
    // The cancellation window had already ended, so the navigation doesn't need
    // deferring.
    return NavigationThrottle::PROCEED;
  }

  if (!request->GetRenderFrameHost() ||
      request->GetRenderFrameHost()->GetSiteInstance()->group() !=
          request->frame_tree_node()
              ->current_frame_host()
              ->GetSiteInstance()
              ->group()) {
    // Only defer same-SiteInstanceGroup navigations, as only those navigations
    // were previously guaranteed to be cancelable from the same JS task it
    // started on (see class level comment for more details).
    return NavigationThrottle::PROCEED;
  }

  // Start the cancellation timeout, to warn users of an unresponsive renderer
  // if the cancellation window is longer than the set time limit.
  defer_start_time_ = base::TimeTicks::Now();
  RestartTimeout();
  // Wait for the navigation cancellation window to end before continuing.
  request->set_renderer_cancellation_window_ended_callback(base::BindOnce(
      &RendererCancellationThrottle::NavigationCancellationWindowEnded,
      base::Unretained(this)));

  return NavigationThrottle::DEFER;
}

void RendererCancellationThrottle::NavigationCancellationWindowEnded() {
  if (did_resume_navigation_) {
    // The timeout handler already resumed the navigation.
    return;
  }

  base::UmaHistogramBoolean(
      "Navigation.RendererCancellationThrottle.NotCancelled.TimeoutIsHit",
      false);
  base::UmaHistogramTimes(
      "Navigation.RendererCancellationThrottle.TimeUntilWindowEnd",
      base::TimeTicks::Now() - defer_start_time_);
  NavigationRequest* request = NavigationRequest::From(navigation_handle());
  CHECK(request->renderer_cancellation_window_ended());

  // Stop the timeout and notify that renderer is responsive if necessary.
  renderer_cancellation_timeout_timer_.Stop();

  if (!base::FeatureList::IsEnabled(
          features::kRendererCancellationThrottleImprovements)) {
    request->GetRenderFrameHost()
        ->GetRenderWidgetHost()
        ->RendererIsResponsive();
  }
  did_resume_navigation_ = true;
  Resume();
}

void RendererCancellationThrottle::SetOnTimeoutCallbackForTesting(
    base::OnceClosure callback) {
  on_timeout_callback_for_testing_ = std::move(callback);
}

void RendererCancellationThrottle::OnTimeout() {
  if (on_timeout_callback_for_testing_) {
    std::move(on_timeout_callback_for_testing_).Run();
  }
  base::UmaHistogramBoolean(
      "Navigation.RendererCancellationThrottle.NotCancelled.TimeoutIsHit",
      true);
  if (base::FeatureList::IsEnabled(
          features::kRendererCancellationThrottleImprovements)) {
    // Resume the navigation once it hits the timeout, without marking the
    // renderer as unresponsive.
    did_resume_navigation_ = true;
    Resume();
    return;
  }
  // Warn that the renderer is unresponsive.
  NavigationRequest* request = NavigationRequest::From(navigation_handle());
  DCHECK(request);

  auto* previous_rfh =
      RenderFrameHostImpl::FromID(request->GetPreviousRenderFrameHostId());
  if (!previous_rfh) {
    return;
  }

  previous_rfh->GetRenderWidgetHost()->RendererIsUnresponsive(
      RenderWidgetHostImpl::RendererIsUnresponsiveReason::
          kRendererCancellationThrottleTimeout,
      base::BindRepeating(&RendererCancellationThrottle::RestartTimeout,
                          weak_factory_.GetWeakPtr()));
}

void RendererCancellationThrottle::RestartTimeout() {
  renderer_cancellation_timeout_timer_.Start(
      FROM_HERE, throttle_timeout.Get(),
      base::BindOnce(&RendererCancellationThrottle::OnTimeout,
                     base::Unretained(this)));
}

const char* RendererCancellationThrottle::GetNameForLogging() {
  return "RendererCancellationThrottle";
}

}  // namespace content