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
content / browser / renderer_host / renderer_cancellation_throttle.cc [blame]
// 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 "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.
constexpr base::TimeDelta kDefaultCancellationTimeout = base::Seconds(11);
base::TimeDelta g_cancellation_timeout = kDefaultCancellationTimeout;
} // namespace
// static
std::unique_ptr<RendererCancellationThrottle>
RendererCancellationThrottle::MaybeCreateThrottleFor(NavigationHandle* handle) {
NavigationRequest* request = NavigationRequest::From(handle);
if (request->ShouldWaitForRendererCancellationWindowToEnd()) {
return std::make_unique<RendererCancellationThrottle>(handle);
}
return nullptr;
}
// static
void RendererCancellationThrottle::SetCancellationTimeoutForTesting(
base::TimeDelta timeout) {
if (timeout.is_zero()) {
g_cancellation_timeout = kDefaultCancellationTimeout;
} else {
g_cancellation_timeout = timeout;
}
}
RendererCancellationThrottle::RendererCancellationThrottle(
NavigationHandle* navigation_handle)
: NavigationThrottle(navigation_handle) {}
RendererCancellationThrottle::~RendererCancellationThrottle() = default;
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.
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() {
CHECK(NavigationRequest::From(navigation_handle())
->renderer_cancellation_window_ended());
// Stop the timeout and notify that renderer is responsive if necessary.
renderer_cancellation_timeout_timer_.Stop();
NavigationRequest* request = NavigationRequest::From(navigation_handle());
request->GetRenderFrameHost()->GetRenderWidgetHost()->RendererIsResponsive();
Resume();
}
void RendererCancellationThrottle::OnTimeout() {
// 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, g_cancellation_timeout,
base::BindOnce(&RendererCancellationThrottle::OnTimeout,
base::Unretained(this)));
}
const char* RendererCancellationThrottle::GetNameForLogging() {
return "RendererCancellationThrottle";
}
} // namespace content