Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 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 "content/browser/renderer_host/renderer_cancellation_throttle.h" |
| 6 | |
| 7 | #include "content/browser/renderer_host/frame_tree_node.h" |
| 8 | #include "content/browser/renderer_host/navigation_request.h" |
| 9 | |
| 10 | namespace content { |
| 11 | |
| 12 | namespace { |
| 13 | |
Rakina Zata Amni | dd1c9cd | 2022-10-21 08:35:54 | [diff] [blame] | 14 | // Default timeout for the cancellation window. From gathering data through the |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 15 | // "Navigation.RendererInitiatedCancellation.DeferStartToCancellationWindowEnd" |
Rakina Zata Amni | dd1c9cd | 2022-10-21 08:35:54 | [diff] [blame] | 16 | // UMA, 99% of navigations' cancellation window ends in under 2000ms, and all |
| 17 | // cancellation windows end in under 10000ms, so setting this to 11000ms. |
| 18 | constexpr base::TimeDelta kDefaultCancellationTimeout = base::Seconds(11); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 19 | base::TimeDelta g_cancellation_timeout = kDefaultCancellationTimeout; |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | // static |
Takashi Toyoshima | a6f3af181 | 2025-05-29 04:45:59 | [diff] [blame] | 24 | void RendererCancellationThrottle::MaybeCreateAndAdd( |
| 25 | NavigationThrottleRegistry& registry) { |
| 26 | NavigationRequest* request = |
| 27 | NavigationRequest::From(®istry.GetNavigationHandle()); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 28 | if (request->ShouldWaitForRendererCancellationWindowToEnd()) { |
Takashi Toyoshima | a6f3af181 | 2025-05-29 04:45:59 | [diff] [blame] | 29 | registry.AddThrottle( |
| 30 | std::make_unique<RendererCancellationThrottle>(registry)); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 31 | } |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // static |
| 35 | void RendererCancellationThrottle::SetCancellationTimeoutForTesting( |
| 36 | base::TimeDelta timeout) { |
| 37 | if (timeout.is_zero()) { |
| 38 | g_cancellation_timeout = kDefaultCancellationTimeout; |
| 39 | } else { |
| 40 | g_cancellation_timeout = timeout; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | RendererCancellationThrottle::RendererCancellationThrottle( |
Takashi Toyoshima | a6f3af181 | 2025-05-29 04:45:59 | [diff] [blame] | 45 | NavigationThrottleRegistry& registry) |
| 46 | : NavigationThrottle(registry) {} |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 47 | |
| 48 | RendererCancellationThrottle::~RendererCancellationThrottle() = default; |
| 49 | |
| 50 | NavigationThrottle::ThrottleCheckResult |
| 51 | RendererCancellationThrottle::WillProcessResponse() { |
Rakina Zata Amni | df8d1b4 | 2024-07-26 09:21:25 | [diff] [blame] | 52 | return WaitForRendererCancellationIfNeeded(); |
| 53 | } |
| 54 | |
| 55 | NavigationThrottle::ThrottleCheckResult |
| 56 | RendererCancellationThrottle::WillCommitWithoutUrlLoader() { |
| 57 | return WaitForRendererCancellationIfNeeded(); |
| 58 | } |
| 59 | |
| 60 | NavigationThrottle::ThrottleCheckResult |
| 61 | RendererCancellationThrottle::WaitForRendererCancellationIfNeeded() { |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 62 | NavigationRequest* request = NavigationRequest::From(navigation_handle()); |
| 63 | DCHECK(request); |
| 64 | if (request->renderer_cancellation_window_ended()) { |
| 65 | // The cancellation window had already ended, so the navigation doesn't need |
| 66 | // deferring. |
| 67 | return NavigationThrottle::PROCEED; |
| 68 | } |
| 69 | |
| 70 | if (!request->GetRenderFrameHost() || |
Sharon Yang | a890684 | 2024-10-10 16:37:12 | [diff] [blame] | 71 | request->GetRenderFrameHost()->GetSiteInstance()->group() != |
| 72 | request->frame_tree_node() |
| 73 | ->current_frame_host() |
| 74 | ->GetSiteInstance() |
| 75 | ->group()) { |
| 76 | // Only defer same-SiteInstanceGroup navigations, as only those navigations |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 77 | // were previously guaranteed to be cancelable from the same JS task it |
Sharon Yang | a890684 | 2024-10-10 16:37:12 | [diff] [blame] | 78 | // started on (see class level comment for more details). |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 79 | return NavigationThrottle::PROCEED; |
| 80 | } |
| 81 | |
| 82 | // Start the cancellation timeout, to warn users of an unresponsive renderer |
| 83 | // if the cancellation window is longer than the set time limit. |
| 84 | RestartTimeout(); |
| 85 | // Wait for the navigation cancellation window to end before continuing. |
| 86 | request->set_renderer_cancellation_window_ended_callback(base::BindOnce( |
| 87 | &RendererCancellationThrottle::NavigationCancellationWindowEnded, |
| 88 | base::Unretained(this))); |
| 89 | |
| 90 | return NavigationThrottle::DEFER; |
| 91 | } |
| 92 | |
| 93 | void RendererCancellationThrottle::NavigationCancellationWindowEnded() { |
| 94 | CHECK(NavigationRequest::From(navigation_handle()) |
| 95 | ->renderer_cancellation_window_ended()); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 96 | // Stop the timeout and notify that renderer is responsive if necessary. |
| 97 | renderer_cancellation_timeout_timer_.Stop(); |
| 98 | NavigationRequest* request = NavigationRequest::From(navigation_handle()); |
| 99 | request->GetRenderFrameHost()->GetRenderWidgetHost()->RendererIsResponsive(); |
| 100 | |
| 101 | Resume(); |
| 102 | } |
| 103 | |
| 104 | void RendererCancellationThrottle::OnTimeout() { |
| 105 | // Warn that the renderer is unresponsive. |
| 106 | NavigationRequest* request = NavigationRequest::From(navigation_handle()); |
| 107 | DCHECK(request); |
Khushal Sagar | d7bdc38 | 2023-10-24 19:10:43 | [diff] [blame] | 108 | |
| 109 | auto* previous_rfh = |
| 110 | RenderFrameHostImpl::FromID(request->GetPreviousRenderFrameHostId()); |
| 111 | if (!previous_rfh) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | previous_rfh->GetRenderWidgetHost()->RendererIsUnresponsive( |
Patrick Monette | 617db89 | 2024-09-24 17:29:19 | [diff] [blame] | 116 | RenderWidgetHostImpl::RendererIsUnresponsiveReason:: |
| 117 | kRendererCancellationThrottleTimeout, |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 118 | base::BindRepeating(&RendererCancellationThrottle::RestartTimeout, |
| 119 | weak_factory_.GetWeakPtr())); |
| 120 | } |
| 121 | |
| 122 | void RendererCancellationThrottle::RestartTimeout() { |
| 123 | renderer_cancellation_timeout_timer_.Start( |
| 124 | FROM_HERE, g_cancellation_timeout, |
| 125 | base::BindOnce(&RendererCancellationThrottle::OnTimeout, |
| 126 | base::Unretained(this))); |
| 127 | } |
| 128 | |
| 129 | const char* RendererCancellationThrottle::GetNameForLogging() { |
| 130 | return "RendererCancellationThrottle"; |
| 131 | } |
| 132 | |
Khushal Sagar | d7bdc38 | 2023-10-24 19:10:43 | [diff] [blame] | 133 | } // namespace content |