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